Skip to content

Instantly share code, notes, and snippets.

@zah
Created December 3, 2011 21:27
Show Gist options
  • Select an option

  • Save zah/1428204 to your computer and use it in GitHub Desktop.

Select an option

Save zah/1428204 to your computer and use it in GitHub Desktop.
proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
00D84CA0 push ebx
00D84CA1 push esi
sysAssert(roundup(65, 8) == 72, "rawAlloc 1")
sysAssert requestedSize >= sizeof(TFreeCell), "rawAlloc 2"
var size = roundup(requestedSize, MemAlign)
00D84CA2 lea esi,[edx+7]
00D84CA5 and esi,0FFFFFFF8h
00D84CA8 mov ebx,ecx
#c_fprintf(c_stdout, "alloc; size: %ld; %ld\n", requestedSize, size)
if size <= SmallChunkSize-smallChunkOverhead():
00D84CAA cmp esi,0FE0h
00D84CB0 jle LA2+0Bh (0D84CC9h)
else:
size = roundup(requestedSize+bigChunkOverhead(), PageSize)
00D84CB2 add edx,1017h
00D84CB8 and edx,0FFFFF000h
# allocate a large block
var c = getBigChunk(a, size)
00D84CBE call @ILT+2620(@Getbigchunk_11045@8) (0CE1A41h)
00D84CC3 pop esi
sysAssert c.prev == nil, "rawAlloc 10"
sysAssert c.next == nil, "rawAlloc 11"
sysAssert c.size == size, "rawAlloc 12"
result = addr(c.data)
00D84CC4 add eax,18h
00D84CC7 pop ebx
00D84CC8 ret
# allocate a small block: for small chunks, we use only its next pointer
var s = size div MemAlign
00D84CC9 mov eax,esi
00D84CCB cdq
00D84CCC push ebp
00D84CCD and edx,7
00D84CD0 push edi
00D84CD1 lea edi,[edx+eax]
00D84CD4 sar edi,3
var c = a.freeSmallChunks[s]
00D84CD7 mov eax,dword ptr [ebx+edi*4+14h]
if c == nil:
00D84CDB xor ebp,ebp
00D84CDD cmp eax,ebp
00D84CDF je LA7+2 (0D84D05h)
sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4")
else:
sysAssert c.next != c, "rawAlloc 5"
#if c.size != size:
# c_fprintf(c_stdout, "csize: %lld; size %lld\n", c.size, size)
sysAssert c.size == size, "rawAlloc 6"
if c.freeList == nil:
00D84CE1 mov ecx,dword ptr [eax+14h]
LA5:
00D84CE4 cmp ecx,ebp
00D84CE6 je LA8+5 (0D84CF4h)
else:
result = c.freeList
00D84CE8 mov edx,ecx
sysAssert(c.freeList.zeroField == 0, "rawAlloc 8")
c.freeList = c.freeList.next
00D84CEA mov ecx,dword ptr [ecx] ### <---------------------------------------------- Access violation here!
dec(c.free, size)
00D84CEC sub dword ptr [eax+18h],esi
LA8:
00D84CEF mov dword ptr [eax+14h],ecx
00D84CF2 jmp LA4 (0D84D41h)
sysAssert(c.acc + smallChunkOverhead() + size <= SmallChunkSize,
"rawAlloc 7")
result = cast[pointer](cast[TAddress](addr(c.data)) +% c.acc)
00D84CF4 mov ecx,dword ptr [eax+1Ch]
00D84CF7 lea edx,[ecx+eax+20h]
inc(c.acc, size)
00D84CFB add ecx,esi
dec(c.free, size)
00D84CFD sub dword ptr [eax+18h],esi
00D84D00 mov dword ptr [eax+1Ch],ecx
LA7:
00D84D03 jmp LA4 (0D84D41h)
c = getSmallChunk(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment