Dynamic memory heap management and allocation
More...
|
file | malloc.h |
| Standard C Malloc functionality.
|
|
|
void * | malloc (size_t size) |
| Allocate a block of memory.
|
|
void * | calloc (size_t nmemb, size_t size) |
| Allocate a block of memory and initialize it to 0.
|
|
void | free (void *ptr) |
| Release a previously allocated block of memory.
|
|
void * | realloc (void *ptr, size_t size) |
| Change the size of a previously allocated block of memory.
|
|
void * | memalign (size_t alignment, size_t size) |
| Allocate a block of memory aligned to a specified block size.
|
|
void * | valloc (size_t size) |
| Allocate a block of memory aligned to the system page size.
|
|
void * | aligned_alloc (size_t alignment, size_t size) |
| Allocate memory aligned to a specified block size.
|
|
struct mallinfo | mallinfo () |
| Sets tunable parameters for malloc related options.
|
|
int | mallopt (int, int) |
|
void | malloc_stats (void) |
| Debug function.
|
|
int | malloc_irq_safe (void) |
| Determine if it is safe to call malloc() in an IRQ context.
|
|
int | mem_check_block (void *p) |
| Only available with KM_DBG.
|
|
int | mem_check_all (void) |
| Only available with KM_DBG.
|
|
Dynamic memory heap management and allocation
◆ DEFAULT_MMAP_MAX
#define DEFAULT_MMAP_MAX 65536 |
◆ DEFAULT_MMAP_THRESHOLD
#define DEFAULT_MMAP_THRESHOLD (256*1024) |
◆ DEFAULT_MXFAST
#define DEFAULT_MXFAST 64 |
◆ DEFAULT_TOP_PAD
#define DEFAULT_TOP_PAD 0 |
◆ DEFAULT_TRIM_THRESHOLD
#define DEFAULT_TRIM_THRESHOLD (256*1024) |
◆ M_MMAP_MAX
◆ M_MMAP_THRESHOLD
#define M_MMAP_THRESHOLD -3 |
◆ M_MXFAST
◆ M_TOP_PAD
◆ M_TRIM_THRESHOLD
#define M_TRIM_THRESHOLD -1 |
◆ aligned_alloc()
void * aligned_alloc |
( |
size_t | alignment, |
|
|
size_t | size ) |
Allocate memory aligned to a specified block size.
This function is the standard-compliant C11 method for allocating aligned blocks of memory. It works mostly the same as memalign(), although the size must be a multiple of the alignment.
- Parameters
-
alignment | Required alignment of the memory block. |
size | Number of bytes of memory to allocate. |
- Returns
- A pointer to the newly allocated memory block.
◆ calloc()
void * calloc |
( |
size_t | nmemb, |
|
|
size_t | size ) |
Allocate a block of memory and initialize it to 0.
This function allocates a block of memory of size * nmemb bytes, initializing it to all zero bytes in the process. In other words, this function allocates an array of nmemb elements that are each size bytes in length. Just as with malloc(), you are responsible for calling free() on the block of memory when you are done with it.
- Parameters
-
nmemb | Number of elements to allocate space for. |
size | Size of each element in the array. |
- Returns
- A pointer to the newly allocated block of memory, or NULL on failure.
- See also
- free
-
malloc
◆ free()
Release a previously allocated block of memory.
This function frees memory that has been previously allocated by way of any of the allocation functions in this file (malloc(), calloc(), memalign(), valloc(), or aligned_alloc()), releasing the memory to be potentially used for any future allocations.
- Parameters
-
ptr | A pointer to the block of memory to be freed. |
- Note
- Passing a NULL pointer to this function has no effect.
-
Attempting to free the same block of memory twice exhibits undefined behavior (i.e, it may crash, it may do something else evil, etc).
-
Attempting to free a block of memory that was not allocated by way of the normal allocation functions exhibits undefined behavior.
◆ mallinfo()
struct mallinfo mallinfo |
( |
| ) |
|
Sets tunable parameters for malloc related options.
◆ malloc()
void * malloc |
( |
size_t | size | ) |
|
Allocate a block of memory.
This function allocates a block of memory of the specified number of bytes on the heap. This memory must later be freed by way of the free() function. The memory is not freed simply because a pointer to it goes out of scope.
- Parameters
-
size | Number of bytes of memory to allocate. |
- Returns
- A pointer to the newly allocated block of memory, or NULL on failure.
- See also
- free
-
calloc
- Note
- The block of memory returned is completely uninitialized and may contain random data.
◆ malloc_irq_safe()
int malloc_irq_safe |
( |
void | | ) |
|
Determine if it is safe to call malloc() in an IRQ context.
This function checks the value of the internal spinlock that is used for malloc() to ensure that a call to it will not freeze the running process. This is only really useful in an IRQ context to ensure that a call to malloc() (or some other memory allocation function) won't cause a deadlock.
- Return values
-
1 | If it is safe to call malloc() in the current IRQ. |
0 | Otherwise. |
◆ malloc_stats()
void malloc_stats |
( |
void | | ) |
|
◆ mallopt()
int mallopt |
( |
int | , |
|
|
int | ) |
◆ mem_check_all()
int mem_check_all |
( |
void | | ) |
|
Only available with KM_DBG.
◆ mem_check_block()
int mem_check_block |
( |
void * | p | ) |
|
Only available with KM_DBG.
◆ memalign()
void * memalign |
( |
size_t | alignment, |
|
|
size_t | size ) |
Allocate a block of memory aligned to a specified block size.
This function allocates a block of memory such that the lowest address in the block is aligned to a multiple of alignment bytes. This is useful, for instance, for things like DMA that require aligned blocks of memory to work.
Over-reliance on memalign will most certainly cause memory fragmentation, so you should only use it when it is actually necessary.
- Parameters
-
alignment | The alignment requested for the block of memory. This must be a power-of-two. |
size | The number of bytes of memory to allocate. |
- Returns
- A pointer to the newly allocated block of memory on success, or NULL on failure.
- Note
- All memory allocation functions will have their blocks aligned on 8-byte boundaries. There is no reason to call this function if you need less than 16-byte alignment.
◆ realloc()
void * realloc |
( |
void * | ptr, |
|
|
size_t | size ) |
Change the size of a previously allocated block of memory.
This function changes the size of the previously allocated block of memory from its current size to the specified size. This may involve reallocating and copying the data from the original pointer to a new location in memory.
If this function returns non-NULL, the old pointer is considered invalid and should not be used (unless, of course, the returned pointer is the same as the old pointer). No action is needed to clean up the old pointer, as this function will have freed it if necessary.
If this function returns NULL, the old pointer is still valid and has not had its size changed.
- Parameters
-
ptr | A pointer to the block of memory to resize. It must have been previously allocated by way of one of the memory allocation functions in this file (or must be NULL). |
size | The requested size of the new block of memory. |
- Returns
- A pointer to the newly allocated/resized block of memory on success, or NULL on failure.
- Note
- If ptr is NULL on a call to this function, the function acts the same as malloc(size).
-
If ptr is non-NULL and size is zero, this function does not* free the block of memory – it simply returns a block that is of minimal size. By the standard, this pointer must not be dereferenced.
◆ valloc()
void * valloc |
( |
size_t | size | ) |
|
Allocate a block of memory aligned to the system page size.
This function allocates a block of memory such that the lowest address in the block is aligned to the system page size (typically 4096 bytes). It basically ends up doing return memalign(PAGESIZE, size).
- Parameters
-
size | The number of bytes of memory to allocate. |
- Returns
- A pointer to the newly allocated block of memory on success, or NULL on failure.
- See also
- PAGESIZE
-
<arch/arch.h>