KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
Allocator

Dynamic memory heap management and allocation. More...

Files

file  malloc.h
 Standard C Malloc functionality.
 

Data Structures

struct  mallinfo
 ANSI C functions. More...
 

Macros

#define M_MXFAST   1
 
#define DEFAULT_MXFAST   64
 
#define M_TRIM_THRESHOLD   -1
 
#define DEFAULT_TRIM_THRESHOLD   (256*1024)
 
#define M_TOP_PAD   -2
 
#define DEFAULT_TOP_PAD   0
 
#define M_MMAP_THRESHOLD   -3
 
#define DEFAULT_MMAP_THRESHOLD   (256*1024)
 
#define M_MMAP_MAX   -4
 
#define DEFAULT_MMAP_MAX   65536
 

Functions

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.
 

Detailed Description

Dynamic memory heap management and allocation.

Macro Definition Documentation

◆ 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

#define M_MMAP_MAX   -4

◆ M_MMAP_THRESHOLD

#define M_MMAP_THRESHOLD   -3

◆ M_MXFAST

#define M_MXFAST   1

◆ M_TOP_PAD

#define M_TOP_PAD   -2

◆ M_TRIM_THRESHOLD

#define M_TRIM_THRESHOLD   -1

Function Documentation

◆ 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
alignmentRequired alignment of the memory block.
sizeNumber 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
nmembNumber of elements to allocate space for.
sizeSize 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()

void free ( void *  ptr)

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
ptrA 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
sizeNumber 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
1If it is safe to call malloc() in the current IRQ.
0Otherwise.

◆ malloc_stats()

void malloc_stats ( void  )

Debug function.

◆ 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
alignmentThe alignment requested for the block of memory. This must be a power-of-two.
sizeThe 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
ptrA 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).
sizeThe 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
sizeThe 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>