📄 heap.c
字号:
* NULL if an error condition occurred.
*
* Error Codes:
* CSSM_CALLOC_FAILED
*---------------------------------------------------------------------------*/
void * cssm_calloc (uint32 num_elem, uint32 num_bytes, void* allocRef)
{
void *ptr = NULL; /* Return pointer */
uint32 totalmemory = num_elem * num_bytes; /* memory to allocate and initialize */
uint32 *Temp1; /* init a DWORD at a time pointer */
uint8 *Temp2; /* init the last few bytes */
uint32 x; /* index counter */
ptr = cssm_malloc(totalmemory,allocRef); /* allocate the memory */
if (ptr != NULL) /* got the memory? */
{
Temp1 = (uint32 *)ptr; /* Init using 32bits at a time */
for (x = 0; x < (totalmemory >> 2); x++)
{
*Temp1++ = 0;
}
Temp2 = (uint8 *) Temp1; /* finish using 8 bits */
for (x = 0; x < (totalmemory & 0x03); x++)
{
*Temp2++ = 0;
}
}
return(ptr);
}
/*-----------------------------------------------------------------------------
* Name: cssm_free
*
* Description: Frees an allocated memory block
*
* Parameters:
* mem_ptr (input) : Pointer to allocated memory
* allocRef (input) : Reference to the heap where the memory was allocated
*
* Returns: void
*
* Notes : if _DEBUG is defined the malloc routine initializes memory
* with CD (a big odd number) in the data area and pads a guard zone
* with FD (a big odd number) both before and after the data area.
* cssm_free checks the guard zone to see if is has been destroyed
* in which case an INT 3 is executed to break in the debugger.
* if _DEBUG is not defined there is NO guard zone. Only the
* heaps intrinsic granularity buffers memory area. That means if
* you hit a guard zone in debug mode you may destroy your heap in
* non debug mode. Don't do that;
* Error Codes:
* None
*---------------------------------------------------------------------------*/
void cssm_free (void *mem_ptr, void* allocRef)
{
_HEAP_REF *HeapRef = (_HEAP_REF *)NULL;
_HEAPHEADER *hdr = HEADERBLOCK(mem_ptr);
HeapRef = (allocRef == NULL) ? gpHeap : (_HEAP_REF *)allocRef;
if (HeapRef != NULL && /* Heap init was done */
!(mem_ptr == 0 || (uint32)mem_ptr & 1) && /* likely to be legal */
(hdr->Flags & HEAP_ALLOCATED)) /* was allocated */
{
# if defined (_DEBUG)
{
_HEAPHEADER *SearchBlock = (_HEAPHEADER *)NULL; /* Define the remaining blk */
uint8 *postpad;
uint8 *pad;
uint32 x;
uint32 AlignFirstBlock;
/* Walk the real heap to find the pointer before validation */
/* This prevents someone from screwing up the heap using free */
/* but only during debug mode */
AlignFirstBlock = (uint32)((uint8 *)HeapRef + sizeof(_HEAP_REF));
AlignFirstBlock = (AlignFirstBlock % HEAP_GRANULARITY) ?
HEAP_GRANULARITY - AlignFirstBlock % HEAP_GRANULARITY :
0;
SearchBlock = (_HEAPHEADER *) ((uint8 *)HeapRef + sizeof(_HEAP_REF) +
AlignFirstBlock);
while (SearchBlock != hdr &&
SearchBlock != NULL &&
SearchBlock < HeapRef->HeapBottom &&
SearchBlock->Size != 0)
{
SearchBlock = (_HEAPHEADER *)((uint8 *)SearchBlock + SearchBlock->Size * HEAP_GRANULARITY);
}
if (SearchBlock != hdr)
{ /* the memory your are freeing is not a valid block in the heap */
/* if you get here someone is doing something bad */
// There will be no asm code in the efi implementation..
//_asm int 3; /* Debug Break: I couldn't find this in the heap anywhere */
return;
}
else
{
/* Check the guard zone for pointers writing past there area */
postpad = (uint8 *)hdr + (hdr->Size << HEAP_GRANSHIFT) - (hdr->Tailsize);
pad = hdr->pad;
for (x = 0; x < DEBUGPADSIZE; x++)
{
if (*pad != _PadFill || *postpad != _PadFill)
{
// There will be no asm code in the efi implementation
//_asm int 3; /* Debug Break: Someone wrote into the area before the data BAD EVIL SOMEONE */
return;
}
pad++;
postpad++;
}
/* Fill memory with odd garbage */
for (x = 0; x < (hdr->Size << HEAP_GRANSHIFT) - sizeof(_HEAPHEADER); x++)
{
*pad++ = _DeadFill;
}
}
}
# endif
hdr->Flags = HEAP_DELETED | HEAP_AVAILABLE;
#if defined (TEST_ALLOW_ALLOC_FAIL)
HeapRef->CurrentAlloc -= hdr->Size << HEAP_GRANSHIFT;
#endif
_HeapReclaim(hdr,allocRef); /* Defragment if necessary */
}
# if defined (_DEBUG)
/* this causes the system to break when freeing invalid memory */
/* you will hit this point if you free NULL, an odd address or */
/* a non-allocated heap entry. This happens only in debug */
else
{
# if defined (_CATCH_NULL_FREE)
# else
if (mem_ptr != 0) /* this prevents NULL frees from hitting the break */
# endif
// There will be no asm code in the efi implementation
//_asm int 3;
}
#endif
}
/*-----------------------------------------------------------------------------
* Name: cssm_realloc
*
* Description: Re-allocates a memory block of the requested size
*
* Parameters:
* old_ptr (input) : Pointer to old memory block
* num_bytes (input) : Size of memory block to allocate
* allocRef (input) : Reference to heap where memory to be reallocated
* is currently allocated
*
* Returns:
* Pointer to allocated memory.
* NULL if an error condition occurred or memory was freed, see comments.
*
* Error Codes:
* CSSM_REALLOC_FAILED
*
* Comments:
* If the pointer passed in is NULL, cssm_realloc behaves like cssm_malloc.
* The return value is NULL if the size is zero and the buffer argument is
* not NULL, or if there is not enough available memory to expand the block
* to the given size. In the first case, the original block is freed. In the
* second, the original block is unchanged.
*---------------------------------------------------------------------------*/
void * cssm_realloc (void *old_ptr, uint32 num_bytes, void* allocRef)
{
void *new_ptr = NULL;
if (old_ptr == NULL) /* This is just a malloc call */
{
new_ptr = cssm_malloc(num_bytes,allocRef);
}
else
{
_HEAP_REF *HeapRef = (_HEAP_REF *)NULL;
_HEAPHEADER *hdr;
uint32 OldBlockSize;
uint32 NewBlockSize;
NewBlockSize = GranAdjustSize(num_bytes); /* calc new size */
hdr = HEADERBLOCK(old_ptr); /* point to header */
OldBlockSize = (hdr->Size << HEAP_GRANSHIFT);
HeapRef = (allocRef == NULL) ? gpHeap : (_HEAP_REF *)allocRef;
/* Realloc Same */
if (NewBlockSize == OldBlockSize)
{
new_ptr = old_ptr;
# if defined (_DEBUG) /* initialize new data area */
{ /* The memsize may be different even though */
uint8 *PostPadData; /* the block size is the same */
uint32 PostPad;
uint32 x;
PostPadData = (uint8 *)hdr + (hdr->Size << HEAP_GRANSHIFT);
PostPad = (hdr->Size << HEAP_GRANSHIFT)-(num_bytes + sizeof(_HEAPHEADER));
if (hdr->Tailsize <= PostPad) /* Alloc getting smaller or the same*/
{
PostPadData -= PostPad; /* point to padding */
for (x = 0; x < PostPad; x++)
{
*PostPadData++ = _PadFill; /* init postpad area*/
}
}
else
{
PostPadData -= hdr->Tailsize; /* point to New Mem */
for (x = 0; x < hdr->Tailsize - PostPad; x++)
{
*PostPadData++ = _CleanFill; /* init new memory */
}
for (x = 0; x < PostPad; x++)
{
*PostPadData++ = _PadFill; /* init postpad area */
}
}
hdr->Tailsize = PostPad;;
}
# endif
}
/* Realloc Bigger */
else if (NewBlockSize > OldBlockSize)
{
new_ptr = cssm_malloc(num_bytes, allocRef);
if (new_ptr != NULL)
{
cssm_memcpy(new_ptr,old_ptr,OldBlockSize - (sizeof(_HEAPHEADER) + hdr->Tailsize));
cssm_free(old_ptr,allocRef);
}
}
/* Realloc Smaller */
else if (NewBlockSize < OldBlockSize)
{
new_ptr = cssm_malloc(num_bytes, allocRef);
if (new_ptr != NULL)
{
cssm_memcpy(new_ptr,old_ptr,num_bytes);
cssm_free(old_ptr,allocRef);
}
}
}
return (new_ptr);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -