#include "akmalloc/malloc.h"
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <sys/mman.h>
Go to the source code of this file.
Data Structures | |
struct | ak_slab_root |
struct | ak_ca_root |
struct | ak_malloc_state |
Macros | |
#define | AK_MALLOCSTATE_USE_LOCKS |
#define | AK_COALESCE_ALIGN 16 |
Typedefs | |
typedef int(* | ak_seg_cbk )(const void *, size_t) |
Functions | |
static void | ak_slab_init_root (ak_slab_root *s, ak_sz sz, ak_u32 npages, ak_u32 relrate, ak_u32 maxpagefree) |
static void | ak_slab_init_root_default (ak_slab_root *s, ak_sz sz) |
static void * | ak_slab_alloc (ak_slab_root *root) |
static void | ak_slab_free (void *p) |
static void | ak_slab_destroy (ak_slab_root *root) |
static void | ak_ca_init_root (ak_ca_root *root, ak_u32 relrate, ak_u32 maxsegstofree) |
static void | ak_ca_init_root_default (ak_ca_root *root) |
static void * | ak_ca_realloc_in_place (ak_ca_root *root, void *mem, ak_sz newsz) |
static void * | ak_ca_alloc (ak_ca_root *root, ak_sz s) |
static void | ak_ca_free (ak_ca_root *root, void *m) |
static void | ak_ca_destroy (ak_ca_root *root) |
static size_t | ak_malloc_usable_size_in_state (const void *mem) |
static void | ak_malloc_init_state (ak_malloc_state *s) |
static void | ak_malloc_destroy_state (ak_malloc_state *m) |
static void * | ak_malloc_from_state (ak_malloc_state *m, size_t sz) |
static void | ak_free_to_state (ak_malloc_state *m, void *mem) |
static void * | ak_realloc_in_place_from_state (ak_malloc_state *m, void *mem, size_t newsz) |
static void * | ak_realloc_from_state (ak_malloc_state *m, void *mem, size_t newsz) |
static void * | ak_aligned_alloc_from_state (ak_malloc_state *m, size_t aln, size_t sz) |
static int | ak_posix_memalign_from_state (ak_malloc_state *m, void **pmem, size_t aln, size_t sz) |
static void | ak_malloc_for_each_segment_in_state (ak_malloc_state *m, ak_seg_cbk cbk) |
void * | ak_malloc (size_t sz) |
void * | ak_calloc (size_t elsz, size_t numel) |
void | ak_free (void *mem) |
void * | ak_aligned_alloc (size_t sz, size_t aln) |
int | ak_posix_memalign (void **pmem, size_t aln, size_t sz) |
void * | ak_memalign (size_t sz, size_t aln) |
size_t | ak_malloc_usable_size (const void *mem) |
void * | ak_realloc (void *mem, size_t newsz) |
void | ak_malloc_for_each_segment (ak_seg_cbk cbk) |
Variables | |
static const ak_sz | SLAB_SIZES [16] |
static const ak_sz | CA_SIZES [8] |
Definition in file malloc.c.
#define AK_COALESCE_ALIGN 16 |
We choose a minimum alignment of 16. One could increase this, but not decrease.
16 byte alignment buys us a few things:
#define AK_MALLOCSTATE_USE_LOCKS |
typedef int(* ak_seg_cbk)(const void *, size_t) |
void* ak_aligned_alloc | ( | size_t | aln, |
size_t | sz | ||
) |
Attempt to allocate memory containing at least n
bytes at an address which is a multiple of aln
. aln
must be a power of two. sz
must be a multiple of aln
.
aln; | The alignment |
sz; | The size for the allocation |
0
on failure, else pointer to at least n
bytes of memory at an aligned address.
|
static |
Attempt to allocate memory containing at least n
bytes at an address which is a multiple of aln
. aln
must be a power of two. sz
must be a multiple of aln
.
m; | The allocator |
aln; | The alignment |
sz; | The size for the allocation |
0
on failure, else pointer to at least n
bytes of memory at an aligned address.
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
void* ak_calloc | ( | size_t | n, |
size_t | s | ||
) |
void ak_free | ( | void * | p | ) |
|
static |
void* ak_malloc | ( | size_t | n | ) |
|
static |
void ak_malloc_for_each_segment | ( | ak_seg_cbk | cbk | ) |
Iterate over all memory segments allocated.
cbk; | Callback that is given the address of a segment and its size. |
|
static |
Iterate over all memory segments allocated.
m; | The allocator |
cbk; | Callback that is given the address of a segment and its size. |
|
static |
|
static |
size_t ak_malloc_usable_size | ( | const void * | p | ) |
|
static |
void* ak_memalign | ( | size_t | aln, |
size_t | sz | ||
) |
Attempt to allocate memory containing at least n
bytes at an address which is a multiple of aln
. aln
must be a power of two.
aln; | The alignment |
sz; | The size for the allocation |
0
on failure, else pointer to at least n
bytes of memory at an aligned address. int ak_posix_memalign | ( | void ** | pptr, |
size_t | aln, | ||
size_t | sz | ||
) |
Attempt to allocate memory containing at least n
bytes at an address which is a multiple of aln
and assign the address to *pptr
. aln
must be a power of two and a multiple of sizeof(void*)
.
pptr; | The address where the memory address should be writted. |
aln; | The alignment |
sz; | The size for the allocation |
0
on success, 12 if no more memory is available, and 22 if aln
was not a power of two and a multiple of sizeof(void*)
|
static |
Attempt to allocate memory containing at least n
bytes at an address which is a multiple of aln
and assign the address to *pmem
. aln
must be a power of two and a multiple of sizeof(void*)
.
m; | The allocator |
pmem; | The address where the memory address should be writted. |
aln; | The alignment |
sz; | The size for the allocation |
0
on success, 12 if no more memory is available, and 22 if aln
was not a power of two and a multiple of sizeof(void*)
void* ak_realloc | ( | void * | p, |
size_t | newsz | ||
) |
Attempt to grow memory at the region pointed to by p
to a size newsz
.
p; | Memory to grow |
newsz; | New size to grow to |
This function will copy the old bytes to a new memory location if the old memory cannot be grown in place, and will free the old memory. If no more memory is available it will not destroy the old memory.
NULL
if no memory is available, or a pointer to memory with at least newsz
bytes.
|
static |
Attempt to grow memory at the region pointed to by p
to a size newsz
.
m; | The allocator |
mem; | Memory to grow |
newsz; | New size to grow to |
This function will copy the old bytes to a new memory location if the old memory cannot be grown in place, and will free the old memory. If no more memory is available it will not destroy the old memory.
NULL
if no memory is available, or a pointer to memory with at least newsz
bytes.
|
static |
|
static |
|
static |
|
static |
|
static |
Initialize a slab allocator.
|
static |
|
static |