dlmalloc #
Constants #
const n_small_bins = 32
const n_tree_bins = 32
const small_bin_shift = 3
const tree_bin_shift = 8
const max_release_check_rate = 4095
fn calloc #
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 #
fn free(ptr voidptr)
free deallocates a ptr
.
fn get_system_allocator #
fn get_system_allocator() Allocator
fn malloc #
fn malloc(size usize) voidptr
/ malloc allocates size
bytes. / / Returns a null pointer if allocation fails. Returns a valid pointer / otherwise.
fn memalign #
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 #
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 {
pub:
alloc fn (voidptr, usize) (voidptr, usize, u32) = unsafe { nil }
remap fn (voidptr, voidptr, usize, usize, bool) voidptr = unsafe { nil }
free_part fn (voidptr, voidptr, usize, usize) bool = unsafe { nil }
free_ fn (voidptr, voidptr, usize) bool = unsafe { nil }
can_release_part fn (voidptr, u32) bool = unsafe { nil }
allocates_zeros fn (voidptr) bool = unsafe { nil }
page_size fn (voidptr) usize = unsafe { nil } // not a constant field because some platforms might have different page sizes depending on configs
data voidptr
}
In order for dlmalloc to efficiently 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 = unsafe { nil }
top &Chunk = unsafe { nil }
footprint usize
max_footprint usize
seg Segment
trim_check u32
least_addr voidptr
release_checks usize
}
fn (Dlmalloc) calloc_must_clear #
fn (dl &Dlmalloc) calloc_must_clear(ptr voidptr) bool
fn (Dlmalloc) calloc #
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_ #
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 #
fn (mut dl Dlmalloc) realloc(oldmem voidptr, bytes usize) voidptr
realloc behaves as libc realloc, but operates within the given space
fn (Dlmalloc) memalign #
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