Skip to content

dlmalloc #

Constants #

const (
	n_small_bins           = 32
	n_tree_bins            = 32
	small_bin_shift        = 3
	tree_bin_shift         = 8

	max_release_check_rate = 4095
)

fn calloc #

unsafe
fn calloc(size usize) voidptr

Same as malloc, except if the allocation succeeds it's guaranteed to point to size bytes of zeros.

fn free #

unsafe
fn free(ptr voidptr)

free deallocates a ptr.

fn get_system_allocator #

fn get_system_allocator() Allocator

fn malloc #

unsafe
fn malloc(size usize) voidptr

/ malloc allocates size bytes.
/ / Returns a null pointer if allocation fails. Returns a valid pointer / otherwise.

fn memalign #

unsafe
fn memalign(size usize, align usize) voidptr

memalign allocates size bytes with align align.

Returns a null pointer if allocation fails. Returns a valid pointer otherwise.

fn new #

fn new(system_allocator Allocator) Dlmalloc

fn realloc #

unsafe
fn realloc(ptr voidptr, oldsize usize, newsize usize) voidptr

realloc reallocates ptr, a previous allocation with old_size and to have new_size.

Returns a null pointer if the memory couldn't be reallocated, but ptr is still valid. Returns a valid pointer and frees ptr if the request is satisfied.

enum Map_flags #

enum Map_flags {
	map_shared = 0x01
	map_private = 0x02
	map_shared_validate = 0x03
	map_type = 0x0f
	map_fixed = 0x10
	map_file = 0x00
	map_anonymous = 0x20
	map_huge_shift = 26
	map_huge_mask = 0x3f
}

enum Mm_prot #

enum Mm_prot {
	prot_read = 0x1
	prot_write = 0x2
	prot_exec = 0x4
	prot_none = 0x0
	prot_growsdown = 0x01000000
	prot_growsup = 0x02000000
}

struct Allocator #

struct Allocator {
	alloc            fn (voidptr, usize) (voidptr, usize, u32)
	remap            fn (voidptr, voidptr, usize, usize, bool) voidptr
	free_part        fn (voidptr, voidptr, usize, usize) bool
	free_            fn (voidptr, voidptr, usize) bool
	can_release_part fn (voidptr, u32) bool
	allocates_zeros  fn (voidptr) bool
	page_size        fn (voidptr) usize // not a constant field because some platforms might have different page sizes depending on configs
	data             voidptr
}

In order for dlmalloc to efficently manage memory, it needs a way to communicate with the underlying platform.
This Allocator type provides an interface for this communication.

Why not interface? Interfaces require memory allocation so it is simpler to pass a struct.

struct Dlmalloc #

struct Dlmalloc {
	system_allocator Allocator
	max_request      usize = 4294901657
mut:
// bin maps
	smallmap u32 // bin map for small bins
	treemap  u32 // bin map  for tree bins

	smallbins      [66]&Chunk // small bins, it is actually (n_small_bins + 1) * 2
	treebins       [n_tree_bins]&TreeChunk
	dvsize         usize
	topsize        usize
	dv             &Chunk = voidptr(0)
	top            &Chunk = voidptr(0)
	footprint      usize
	max_footprint  usize
	seg            Segment
	trim_check     u32
	least_addr     voidptr
	release_checks usize
}

fn (Dlmalloc) calloc_must_clear #

unsafe
fn (dl &Dlmalloc) calloc_must_clear(ptr voidptr) bool

fn (Dlmalloc) calloc #

unsafe
fn (mut dl Dlmalloc) calloc(size usize) voidptr

calloc is the same as malloc, except if the allocation succeeds it's guaranteed to point to size bytes of zeros.

fn (Dlmalloc) free_ #

unsafe
fn (mut dl Dlmalloc) free_(mem voidptr)

free_ behaves as libc free, but operates within the given space

fn (Dlmalloc) malloc #

fn (mut dl Dlmalloc) malloc(size usize) voidptr

fn (Dlmalloc) realloc #

unsafe
fn (mut dl Dlmalloc) realloc(oldmem voidptr, bytes usize) voidptr

realloc behaves as libc realloc, but operates within the given space

fn (Dlmalloc) memalign #

unsafe
fn (mut dl Dlmalloc) memalign(alignment_ usize, bytes usize) voidptr

memaligns allocates memory aligned to alignment_. Only call this with power-of-two alignment and alignment > dlmalloc.malloc_alignment