akmalloc
 All Data Structures Files Functions Variables Typedefs Macros Pages
Data Structures | Macros | Typedefs | Functions | Variables
malloc.c File Reference
#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]
 

Detailed Description

Date
Apr 08, 2016

Definition in file malloc.c.

Macro Definition Documentation

#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:

  1. The 3 low-bits of an address will be 000. Therefore we can store metadata in them.
  2. On x64, we can exactly store two pointers worth of information in any block which can be used to house an implicit free list.

Definition at line 1761 of file malloc.c.

#define AK_MALLOCSTATE_USE_LOCKS

Decide to use or not use locks

Definition at line 1232 of file malloc.c.

Typedef Documentation

typedef int(* ak_seg_cbk)(const void *, size_t)

Gets a pointer to a memory segment and its size.

Parameters
p;Pointer to segment memory.
sz;Number of bytes in the segment.
Returns
0 to stop iteration, non-zero to continue.

Definition at line 1261 of file malloc.c.

Function Documentation

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.

Parameters
aln;The alignment
sz;The size for the allocation
Returns
0 on failure, else pointer to at least n bytes of memory at an aligned address.

Definition at line 3024 of file malloc.c.

static void* ak_aligned_alloc_from_state ( ak_malloc_state *  m,
size_t  aln,
size_t  sz 
)
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.

Parameters
m;The allocator
aln;The alignment
sz;The size for the allocation
Returns
0 on failure, else pointer to at least n bytes of memory at an aligned address.

Definition at line 2884 of file malloc.c.

static void* ak_ca_alloc ( ak_ca_root *  root,
ak_sz  s 
)
static

Attempt to allocate memory from the coalescing allocator root.

Parameters
root;Pointer to the allocator root
s;The size for the allocation
Returns
0 on failure, else pointer to at least s bytes of memory.

Definition at line 2174 of file malloc.c.

static void ak_ca_destroy ( ak_ca_root *  root)
static

Destroy the coalescing allocator root and return all memory to the OS.

Parameters
root;Pointer to the allocator root

Definition at line 2305 of file malloc.c.

static void ak_ca_free ( ak_ca_root *  root,
void *  m 
)
static

Return memory to the coalescing allocator root.

Parameters
root;Pointer to the allocator root
m;The memory to return.

Definition at line 2199 of file malloc.c.

static void ak_ca_init_root ( ak_ca_root *  root,
ak_u32  relrate,
ak_u32  maxsegstofree 
)
static

Initialize a coalescing allocator.

Parameters
root;Pointer to the allocator root to initialize (non-NULL)
relrate;Release rate, akmalloc
maxsegstofree;Number of segments to free upon release, akmalloc

Definition at line 2090 of file malloc.c.

static void ak_ca_init_root_default ( ak_ca_root *  root)
static

Default initialize a coalescing allocator.

Parameters
root;Pointer to the allocator root to initialize (non-NULL)

Definition at line 2110 of file malloc.c.

static void* ak_ca_realloc_in_place ( ak_ca_root *  root,
void *  mem,
ak_sz  newsz 
)
static

Attempt to grow an existing allocation.

Parameters
root;Pointer to the allocator root
mem;Existing memory to grow
newsz;The new size for the allocation
Returns
0 on failure, and mem on success which can hold at least newsz bytes.

Definition at line 2128 of file malloc.c.

void* ak_calloc ( size_t  n,
size_t  s 
)

Attempt to allocate zeroed memory, containing at least n x s bytes.

Parameters
n;Number of objects to zero.
s;The size for each object.
Returns
0 on failure, else pointer to at least s bytes of memory.

Definition at line 3011 of file malloc.c.

void ak_free ( void *  p)

Return memory to the allocator.

Parameters
p;Pointer to the memory to return.

Definition at line 3018 of file malloc.c.

static void ak_free_to_state ( ak_malloc_state *  m,
void *  mem 
)
static

Return memory to the allocator.

Parameters
m;The allocator
mem;Pointer to the memory to return.

Definition at line 2759 of file malloc.c.

void* ak_malloc ( size_t  n)

Attempt to allocate memory containing at least n bytes.

Parameters
n;The size for the allocation
Returns
0 on failure, else pointer to at least n bytes of memory.

Definition at line 3005 of file malloc.c.

static void ak_malloc_destroy_state ( ak_malloc_state *  m)
static

Destroy the private malloc like allocator and return all memory to the OS.

Parameters
m;Pointer to the allocator

Definition at line 2718 of file malloc.c.

void ak_malloc_for_each_segment ( ak_seg_cbk  cbk)

Iterate over all memory segments allocated.

Parameters
cbk;Callback that is given the address of a segment and its size.
See also
ak_seg_cbk.

Definition at line 3059 of file malloc.c.

static void ak_malloc_for_each_segment_in_state ( ak_malloc_state *  m,
ak_seg_cbk  cbk 
)
static

Iterate over all memory segments allocated.

Parameters
m;The allocator
cbk;Callback that is given the address of a segment and its size.
See also
ak_seg_cbk.

Definition at line 2942 of file malloc.c.

static void* ak_malloc_from_state ( ak_malloc_state *  m,
size_t  sz 
)
static

Attempt to allocate memory containing at least n bytes.

Parameters
m;The allocator
sz;The size for the allocation
Returns
0 on failure, else pointer to at least n bytes of memory.

Definition at line 2743 of file malloc.c.

static void ak_malloc_init_state ( ak_malloc_state *  s)
static

Initialize a private malloc like allocator.

Parameters
s;Pointer to the allocator to initialize (non-NULL)

Definition at line 2696 of file malloc.c.

size_t ak_malloc_usable_size ( const void *  p)

Return the usable size of the memory region pointed to by p.

Parameters
p;Pointer to the memory to determize size of.
Returns
The number of bytes that can be written to in the region.

Definition at line 3042 of file malloc.c.

static size_t ak_malloc_usable_size_in_state ( const void *  mem)
static

Return the usable size of the memory region pointed to by p.

Parameters
mem;Pointer to the memory to determize size of.
Returns
The number of bytes that can be written to in the region.

Definition at line 2854 of file malloc.c.

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.

Parameters
aln;The alignment
sz;The size for the allocation
Returns
0 on failure, else pointer to at least n bytes of memory at an aligned address.

Definition at line 3036 of file malloc.c.

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*).

Parameters
pptr;The address where the memory address should be writted.
aln;The alignment
sz;The size for the allocation
Returns
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*)

Definition at line 3030 of file malloc.c.

static int ak_posix_memalign_from_state ( ak_malloc_state *  m,
void **  pmem,
size_t  aln,
size_t  sz 
)
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*).

Parameters
m;The allocator
pmem;The address where the memory address should be writted.
aln;The alignment
sz;The size for the allocation
Returns
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*)

Definition at line 2914 of file malloc.c.

void* ak_realloc ( void *  p,
size_t  newsz 
)

Attempt to grow memory at the region pointed to by p to a size newsz.

Parameters
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.

Returns
NULL if no memory is available, or a pointer to memory with at least newsz bytes.

Definition at line 3047 of file malloc.c.

static void* ak_realloc_from_state ( ak_malloc_state *  m,
void *  mem,
size_t  newsz 
)
static

Attempt to grow memory at the region pointed to by p to a size newsz.

Parameters
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.

Returns
NULL if no memory is available, or a pointer to memory with at least newsz bytes.

Definition at line 2826 of file malloc.c.

static void* ak_realloc_in_place_from_state ( ak_malloc_state *  m,
void *  mem,
size_t  newsz 
)
static

Attempt to grow memory at the region pointed to by p to a size newsz without relocation.

Parameters
m;The allocator
mem;Memory to grow
newsz;New size to grow to
Returns
NULL if no memory is available, or mem with at least newsz bytes.

Definition at line 2795 of file malloc.c.

static void* ak_slab_alloc ( ak_slab_root *  root)
static

Attempt to allocate memory from the slab allocator root.

Parameters
root;Pointer to the allocator root
Returns
0 on failure, else pointer to at least root->sz bytes of memory.

Definition at line 1619 of file malloc.c.

static void ak_slab_destroy ( ak_slab_root *  root)
static

Destroy the slab allocator root and return all memory to the OS.

Parameters
root;Pointer to the allocator root

Definition at line 1689 of file malloc.c.

static void ak_slab_free ( void *  p)
static

Return memory to the slab allocator root.

Parameters
p;Pointer to the memory to return.

Definition at line 1650 of file malloc.c.

static void ak_slab_init_root ( ak_slab_root *  s,
ak_sz  sz,
ak_u32  npages,
ak_u32  relrate,
ak_u32  maxpagefree 
)
static

Initialize a slab allocator.

Parameters
s;Pointer to the allocator root to initialize (non-NULL)
sz;Size of the slab elements (maximum allowed is 4000)
npages;Number of pages to allocate from the OS at once.
relrate;Release rate, akmalloc
maxpagefree;Number of segments to free upon release, akmalloc

Definition at line 1586 of file malloc.c.

static void ak_slab_init_root_default ( ak_slab_root *  s,
ak_sz  sz 
)
static

Default initialize a slab allocator.

Parameters
s;Pointer to the allocator root to initialize (non-NULL)
sz;Size of the slab elements (maximum allowed is 4000)

Definition at line 1608 of file malloc.c.

Variable Documentation

const ak_sz CA_SIZES[8]
static
Initial value:
= {
768, 1408, 2048, 4096, 8192, 16384, 65536, ( ((ak_sz)1) << 20)
}

Sizes for the coalescing allocators in an ak_malloc_state

Size here denotes maximum size request for each allocator.

Definition at line 2526 of file malloc.c.

const ak_sz SLAB_SIZES[16]
static
Initial value:
= {
16, 32, 48, 64, 80, 96, 112, 128,
144, 160, 176, 192, 208, 224, 240, 256
}

Sizes for the slabs in an ak_malloc_state

Definition at line 2514 of file malloc.c.