Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Last active November 15, 2018 05:04
Show Gist options
  • Save d3x0r/b6fce0b0e0468c6a7e785193aa6ddd78 to your computer and use it in GitHub Desktop.
Save d3x0r/b6fce0b0e0468c6a7e785193aa6ddd78 to your computer and use it in GitHub Desktop.
struct malloc_chunk_tag
{
uint16_t dwOwners; // if 0 - block is free
uintptr_t dwSize; // limited to allocating 4 billion bytes...
uint16_t alignment; // this is additional to subtract to get back to start (aligned allocate)
uint16_t to_chunk_start; // this is additional to subtract to get back to start (aligned allocate)
uint8_t byData[1]; // uint8_t is the smallest valid datatype could be _0
};
typedef struct malloc_chunk_tag MALLOC_CHUNK;
typedef struct malloc_chunk_tag *PMALLOC_CHUNK;
void *Allocate( int dwSize );
PMALLOC_CHUNK pc;
pc = (PMALLOC_CHUNK)malloc( sizeof( MALLOC_CHUNK ) + dwSize );
pc->dwOwners = 1;
pc->dwSize = dwSize;
pc->dwPad = 0;
pc->alignment = 0;
pc->to_chunk_start = 0;
return pc->byData;
}
void Relesae( void* p ) {
POINTER ReleaseEx ( POINTER pData DBG_PASS )
{
if( pData )
{
PMALLOC_CHUNK pc = (PMALLOC_CHUNK)( ((uintptr_t)pData) - offsetof( MALLOC_CHUNK, byData ) );
pc->dwOwners--;
if( !pc->dwOwners )
{
free( pc );
return NULL;
}
return pData;
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment