Skip to content

Commit 6502bcb

Browse files
committed
a slightly different/safer way to allocate arrays from a blob
1 parent 8b1f3a8 commit 6502bcb

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionState.cs

+30-12
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,9 @@ public void ResetActionState(int actionIndex, InputActionPhase toPhase = InputAc
872872
// Wipe state.
873873
actionState->phase = toPhase;
874874
actionState->controlIndex = kInvalidIndex;
875-
actionState->bindingIndex = memory.actionBindingIndices[memory.actionBindingIndicesAndCounts[actionIndex]];
875+
var idx = memory.actionBindingIndicesAndCounts[actionIndex];
876+
var bindingCount = memory.actionBindingIndicesAndCounts[actionIndex] + 1;
877+
actionState->bindingIndex = memory.actionBindingIndices != null ? memory.actionBindingIndices[idx] : 0;
876878
actionState->interactionIndex = kInvalidIndex;
877879
actionState->startTime = 0;
878880
actionState->time = 0;
@@ -2879,6 +2881,9 @@ internal TValue ReadValue<TValue>(int bindingIndex, int controlIndex, bool ignor
28792881
internal TValue ApplyProcessors<TValue>(int bindingIndex, TValue value, InputControl<TValue> controlOfType = null)
28802882
where TValue : struct
28812883
{
2884+
if (totalBindingCount == 0)
2885+
return value;
2886+
28822887
var processorCount = bindingStates[bindingIndex].processorCount;
28832888
if (processorCount > 0)
28842889
{
@@ -4139,6 +4144,19 @@ public struct UnmanagedMemory : IDisposable
41394144

41404145
public ActionMapIndices* mapIndices;
41414146

4147+
// This is Anthony's version of block allocation from a blob
4148+
private byte* AllocFromBlob(ref byte* top, int size)
4149+
{
4150+
if (size == 0)
4151+
return null;
4152+
4153+
var allocation = top;
4154+
4155+
top += size;
4156+
4157+
return allocation;
4158+
}
4159+
41424160
public void Allocate(int mapCount, int actionCount, int bindingCount, int controlCount, int interactionCount, int compositeCount)
41434161
{
41444162
Debug.Assert(basePtr == null, "Memory already allocated! Free first!");
@@ -4164,17 +4182,17 @@ public void Allocate(int mapCount, int actionCount, int bindingCount, int contro
41644182
// NOTE: This depends on the individual structs being sufficiently aligned in order to not
41654183
// cause any misalignment here. TriggerState, InteractionState, and BindingState all
41664184
// contain doubles so put them first in memory to make sure they get proper alignment.
4167-
actionStates = (TriggerState*)ptr; ptr += actionCount * sizeof(TriggerState);
4168-
interactionStates = (InteractionState*)ptr; ptr += interactionCount * sizeof(InteractionState);
4169-
bindingStates = (BindingState*)ptr; ptr += bindingCount * sizeof(BindingState);
4170-
mapIndices = (ActionMapIndices*)ptr; ptr += mapCount * sizeof(ActionMapIndices);
4171-
controlMagnitudes = (float*)ptr; ptr += controlCount * sizeof(float);
4172-
compositeMagnitudes = (float*)ptr; ptr += compositeCount * sizeof(float);
4173-
controlIndexToBindingIndex = (int*)ptr; ptr += controlCount * sizeof(int);
4174-
controlGroupingAndComplexity = (ushort*)ptr; ptr += controlCount * sizeof(ushort) * 2;
4175-
actionBindingIndicesAndCounts = (ushort*)ptr; ptr += actionCount * sizeof(ushort) * 2;
4176-
actionBindingIndices = (ushort*)ptr; ptr += bindingCount * sizeof(ushort);
4177-
enabledControls = (int*)ptr; ptr += (controlCount + 31) / 32 * sizeof(int);
4185+
actionStates = (TriggerState*)AllocFromBlob(ref ptr, actionCount * sizeof(TriggerState));
4186+
interactionStates = (InteractionState*)AllocFromBlob(ref ptr, interactionCount * sizeof(InteractionState));
4187+
bindingStates = (BindingState*)AllocFromBlob(ref ptr, bindingCount * sizeof(BindingState));
4188+
mapIndices = (ActionMapIndices*)AllocFromBlob(ref ptr, mapCount * sizeof(ActionMapIndices));
4189+
controlMagnitudes = (float*)AllocFromBlob(ref ptr, controlCount * sizeof(float));
4190+
compositeMagnitudes = (float*)AllocFromBlob(ref ptr, compositeCount * sizeof(float));
4191+
controlIndexToBindingIndex = (int*)AllocFromBlob(ref ptr, controlCount * sizeof(int));
4192+
controlGroupingAndComplexity = (ushort*)AllocFromBlob(ref ptr, controlCount * sizeof(ushort) * 2);
4193+
actionBindingIndicesAndCounts = (ushort*)AllocFromBlob(ref ptr, actionCount * sizeof(ushort) * 2);
4194+
actionBindingIndices = (ushort*)AllocFromBlob(ref ptr, bindingCount * sizeof(ushort));
4195+
enabledControls = (int*)AllocFromBlob(ref ptr, (controlCount + 31) / 32 * sizeof(int));
41784196
}
41794197

41804198
public void Dispose()

0 commit comments

Comments
 (0)