mirror of
https://github.com/reactos/reactos.git
synced 2025-05-06 18:31:26 +00:00
[FTFD] Fix malloc/realloc/free wrappers
On x64 malloc needs to return a 16 byte aligned buffer, the previous code used an 8 byte header, making the allocations unaligned. This is now fixed with an improved header structure. Also simplify realloc a bit and make it handle Object == NULL.
This commit is contained in:
parent
91fadeb6c3
commit
dca5194279
1 changed files with 30 additions and 19 deletions
|
@ -36,42 +36,53 @@ DbgPrint(IN PCCH Format, IN ...)
|
|||
* buffer (need to copy the old contents to the new buffer). So, allocate
|
||||
* extra space for a size_t, store the allocated size in there and return
|
||||
* the address just past it as the allocated buffer.
|
||||
* On win64 we need to align the allocation to 16 bytes, otherwise 8 bytes.
|
||||
*/
|
||||
typedef struct _MALLOC_HEADER
|
||||
{
|
||||
SIZE_T Size;
|
||||
SIZE_T Alignment;
|
||||
} MALLOC_HEADER, * PMALLOC_HEADER;
|
||||
|
||||
void *
|
||||
malloc(size_t Size)
|
||||
{
|
||||
void *Object;
|
||||
PMALLOC_HEADER Header;
|
||||
|
||||
Object = EngAllocMem(0, sizeof(size_t) + Size, TAG_FREETYPE);
|
||||
if (Object != NULL)
|
||||
Header = EngAllocMem(0, sizeof(MALLOC_HEADER) + Size, TAG_FREETYPE);
|
||||
if (Header == NULL)
|
||||
{
|
||||
*((size_t *)Object) = Size;
|
||||
Object = (void *)((size_t *)Object + 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return Object;
|
||||
Header->Size = Size;
|
||||
Header->Alignment = -1;
|
||||
return (Header + 1);
|
||||
}
|
||||
|
||||
void *
|
||||
realloc(void *Object, size_t Size)
|
||||
{
|
||||
void *NewObject;
|
||||
PVOID NewObject;
|
||||
PMALLOC_HEADER OldHeader;
|
||||
size_t CopySize;
|
||||
|
||||
NewObject = EngAllocMem(0, sizeof(size_t) + Size, TAG_FREETYPE);
|
||||
if (NewObject != NULL)
|
||||
NewObject = malloc(Size);
|
||||
if (NewObject == NULL)
|
||||
{
|
||||
*((size_t *)NewObject) = Size;
|
||||
NewObject = (void *)((size_t *)NewObject + 1);
|
||||
CopySize = *((size_t *)Object - 1);
|
||||
if (Size < CopySize)
|
||||
{
|
||||
CopySize = Size;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (Object == NULL)
|
||||
{
|
||||
return NewObject;
|
||||
}
|
||||
|
||||
OldHeader = (PMALLOC_HEADER)Object - 1;
|
||||
CopySize = min(OldHeader->Size, Size);
|
||||
memcpy(NewObject, Object, CopySize);
|
||||
EngFreeMem((size_t *)Object - 1);
|
||||
}
|
||||
|
||||
free(Object);
|
||||
|
||||
return NewObject;
|
||||
}
|
||||
|
@ -81,7 +92,7 @@ free(void *Object)
|
|||
{
|
||||
if (Object != NULL)
|
||||
{
|
||||
EngFreeMem((size_t *)Object - 1);
|
||||
EngFreeMem((PMALLOC_HEADER)Object - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue