Skip to content

Commit 8c51fb1

Browse files
authored
Account for callback arguments in y_malloc.
1 parent c8694c3 commit 8c51fb1

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

YSI_Coding/y_malloc/y_malloc_heapalloc.inc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Optional plugins:
7070
Me - sscanf2, fixes2, Whirlpool.
7171
*/
7272

73+
// Allocate some space right at the top of the stack for callback parameters.
74+
#if !defined YSI_DEFAULT_MALLOC
75+
// Will actually allocate 271 bytes (rounds up to 16n-1).
76+
#define YSI_DEFAULT_MALLOC (256)
77+
#endif
78+
7379
/*-------------------------------------------------------------------------*//**
7480
* <remarks>
7581
* Finds all "BOUNDS 0" OpCodes in the AMX and rewrites them to "NOP NOP". The
@@ -166,14 +172,14 @@ public OnCodeInit()
166172
// Move the pointer in the header to our stub.
167173
AMX_Write(entry, ctx[AsmContext_buffer] + ctx[AsmContext_buffer_offset] - AMX_HEADER_COD);
168174
// Check if the heap has already been modified in the current stack.
169-
@emit LCTRL 2
170-
@emit CONST.alt AMX_HEADER_HEA
171-
@emit JNEQ addr + offset
172-
// It hasn't, modify it.
173-
@emit HEAP (MALLOC_MEMORY * 4)
174-
@emit JUMP addr + offset
175+
@emit HEAP 0 // Get heap location in `alt`.
176+
@emit CONST.pri (AMX_HEADER_HEA + MALLOC_MEMORY * 4) // Get the end of malloc data.
177+
@emit JSLEQ addr + offset // Already allocated.
178+
@emit SCTRL 2 // Store the new end.
179+
@emit JUMP addr + offset // Continue with code.
175180
}
176181
while ((idx = AMX_GetPublicEntry(idx, entry)));
182+
malloc(YSI_DEFAULT_MALLOC);
177183
P:6("Malloc_OnCodeInit done");
178184
return 1;
179185
}

0 commit comments

Comments
 (0)