123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- heap.c
- Abstract:
- This module implements heap functionality for the C Library.
- Author:
- Evan Green 6-Mar-2013
- Environment:
- User Mode C Library
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define MALLOC_ALLOCATION_TAG 0x6C6C614D // 'llaM'
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- void
- free (
- void *Memory
- )
- /*++
- Routine Description:
- This routine frees previously allocated memory.
- Arguments:
- Memory - Supplies a pointer to memory returned by the allocation function.
- Return Value:
- None.
- --*/
- {
- if (Memory == NULL) {
- return;
- }
- OsHeapFree(Memory);
- return;
- }
- LIBC_API
- void *
- malloc (
- size_t AllocationSize
- )
- /*++
- Routine Description:
- This routine allocates memory from the heap.
- Arguments:
- AllocationSize - Supplies the required allocation size in bytes.
- Return Value:
- Returns a pointer to the allocated memory on success.
- NULL on failure.
- --*/
- {
- if (AllocationSize == 0) {
- AllocationSize = 1;
- }
- return OsHeapAllocate(AllocationSize, MALLOC_ALLOCATION_TAG);
- }
- LIBC_API
- void *
- realloc (
- void *Allocation,
- size_t AllocationSize
- )
- /*++
- Routine Description:
- This routine resizes the given buffer. If the new allocation size is
- greater than the original allocation, the contents of the new bytes are
- unspecified (just like the contents of a malloced buffer).
- Arguments:
- Allocation - Supplies the optional original allocation. If this is not
- supplied, this routine is equivalent to malloc.
- AllocationSize - Supplies the new required allocation size in bytes.
- Return Value:
- Returns a pointer to the new buffer on success.
- NULL on failure or if the supplied size was zero. If the new buffer could
- not be allocated, errno will be set to ENOMEM.
- --*/
- {
- void *NewBuffer;
- if ((Allocation == NULL) && (AllocationSize == 0)) {
- AllocationSize = 1;
- }
- NewBuffer = OsHeapReallocate(Allocation,
- AllocationSize,
- MALLOC_ALLOCATION_TAG);
- if ((NewBuffer == NULL) && (AllocationSize != 0)) {
- errno = ENOMEM;
- }
- return NewBuffer;
- }
- LIBC_API
- void *
- calloc (
- size_t ElementCount,
- size_t ElementSize
- )
- /*++
- Routine Description:
- This routine allocates memory from the heap large enough to store the
- given number of elements of the given size (the product of the two
- parameters). The buffer returned will have all zeros written to it.
- Arguments:
- ElementCount - Supplies the number of elements to allocate.
- ElementSize - Supplies the size of each element in bytes.
- Return Value:
- Returns a pointer to the new zeroed buffer on success.
- NULL on failure or if the either the element count of the element size was
- zero. If the new buffer could not be allocated, errno will be set to ENOMEM.
- --*/
- {
- void *NewBuffer;
- size_t TotalSize;
- TotalSize = ElementCount * ElementSize;
- if ((ElementCount != 0) && ((TotalSize / ElementCount) != ElementSize)) {
- errno = ENOMEM;
- return NULL;
- }
- if (TotalSize == 0) {
- TotalSize = 1;
- }
- NewBuffer = OsHeapAllocate(TotalSize, MALLOC_ALLOCATION_TAG);
- if (NewBuffer == NULL) {
- errno = ENOMEM;
- return NULL;
- }
- memset(NewBuffer, 0, TotalSize);
- return NewBuffer;
- }
- LIBC_API
- int
- posix_memalign (
- void **AllocationPointer,
- size_t AllocationAlignment,
- size_t AllocationSize
- )
- /*++
- Routine Description:
- This routine allocates aligned memory from the heap. The given alignment
- must be a power of 2 and a multiple of the size of a pointer.
- Arguments:
- AllocationPointer - Supplies a pointer that receives a pointer to the
- allocated memory on success.
- AllocationAlignment - Supplies the required allocation alignment in bytes.
- AllocationSize - Supplies the required allocation size in bytes.
- Return Value:
- 0 on success.
- Returns an error number on failure.
- --*/
- {
- KSTATUS Status;
- if ((IS_ALIGNED(AllocationAlignment, sizeof(void *)) == FALSE) ||
- (POWER_OF_2(AllocationAlignment) == FALSE) ||
- (AllocationAlignment == 0)) {
- return EINVAL;
- }
- Status = OsHeapAlignedAllocate(AllocationPointer,
- AllocationAlignment,
- AllocationSize,
- MALLOC_ALLOCATION_TAG);
- return ClConvertKstatusToErrorNumber(Status);
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|