Allocation¶
mm_aligned_alloc¶
#include <mmlib.h>
-
void*
mm_aligned_alloc
(size_t alignment, size_t size)¶ Allocate memory on a specified alignment boundary.
Parameters: - alignment (size_t) – alignment value, must be a power of 2
- size (size_t) – size of the requested memory allocation
Description¶
This allocates a block of size
bytes whose address is a multiple of
alignment
.
Use mm_aligned_free()
to deallocate data returned by mm_aligned_alloc()
.
Return¶
A pointer to the memory block that was allocated in case of success. Otherwise NULL is returned and error state set accordingly
See also¶
mm_aligned_free¶
#include <mmlib.h>
-
void
mm_aligned_free
(void* ptr)¶ Free memory allocated with
mm_aligned_alloc()
Parameters: - ptr (void*) – data to dellocate
Description¶
This function cause the space pointed to by ptr
to be deallocated. If
ptr is a NULL pointer, no action occur (this is not an error). Otherwise
the behavior is undefined if the space has not been allocated with
mm_aligned_alloc()
.
See also¶
mm_aligned_alloca¶
#include <mmlib.h>
-
mm_aligned_alloca
(alignment, size)¶ allocates memory on the stack with alignment
Parameters: - alignment – alignment, must be a power of two
- size – size of memory to be allocated
Description¶
This macro allocates size
bytes from the stack and the returned pointer
is ensured to be aligned on alignment
boundaries (if alignment
is a
power of two). If size is 0, mm_aligned_alloca()
allocates a zero-length
item and returns a unique pointer to that item.
Please note that more than size
byte are consumed from the stack (even in
case of if size
is 0). This is due to the overhead necessary for having
an aligned allocated memory block.
The lifetime of the allocated object ends just before the calling
function returns to its caller. This is so even when mm_aligned_alloca()
is called within a nested block.
WARNING: If is NOT safe to try allocate more than a page size on the
stack. In general it is even recommended to limit allocation under half
of a page. Ignoring this put the program under the thread of more than
simply a stack overflow: there will be a risk that the stack overflow
will not be detected and the execution to continue while corrupting both
heap and stack… For safe stack allocation, use mm_malloca()
/mm_freea().
Return¶
the pointer to the allocated space in case of success. Otherwise,
ie, if alignment
is not a power of two, NULL is returned.
See also¶
mm_malloca¶
#include <mmlib.h>
-
mm_malloca
(size)¶ safely allocates memory on the stack
Parameters: - size – size of memory to be allocated
Description¶
This macro allocates size
bytes from the stack if not too big (lower or
equal to MM_STACK_ALLOC_THRESHOLD) or on the heap. The returned pointer
is ensured to be aligned on a boundary suitable for any data type. If
size
is 0, mm_malloca()
allocates a zero-length item and returns a valid
pointer to that item.
Use this macro as a safer replacement of alloca()
or Variable Length
Array (VLA).
Return¶
pointer to the allocated space in case success, NULL otherwise.
The allocation might fail if the requested size is larger that the system
memory (or the OS do not overcommit and is running out of memory). In
case of successful allocation, the returned pointer must be passed to
mm_freea()
before calling function returns to its caller.
See also¶
alloca()
mm_freea¶
#include <mmlib.h>
-
void
mm_freea
(void* ptr)¶ free memory allocated with
mm_malloca()
Parameters: - ptr (void*) – memory object allocated by
mm_malloca()
- ptr (void*) – memory object allocated by
Description¶
This function free memory allocated by mm_malloca()
. Please note that
when this function returns, the memory might not be reusable yet. If ptr
has been allocated on stack, the memory will be reclaimed (hence
reusable) only when the function that has called mm_malloca()
for
allocating ptr
will return to its caller.