Skip to content

Index

Records

Functions

amd64_syscall

uintptr_t amd64_syscall(int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6)

Defined at amd64/syscall.c#5

malloc_inspect_all

void malloc_inspect_all(void ()(void , void , size_t, void ) handler, void * arg)

malloc_inspect_all(void(handler)(void start, void end, size_t used_bytes, void callback_arg), void* arg); Traverses the heap and calls the given handler for each managed region, skipping all bytes that are (or may be) used for bookkeeping purposes. Traversal does not include include chunks that have been directly memory mapped. Each reported region begins at the start address, and continues up to but not including the end address. The first used_bytes of the region contain allocated data. If used_bytes is zero, the region is unallocated. The handler is invoked with the given callback argument. If locks are defined, they are held during the entire traversal. It is a bad idea to invoke other malloc functions from within the handler.

For example, to count the number of in-use chunks with size greater than 1000, you could write: static int count = 0; void count_chunks(void start, void end, size_t used, void* arg) { if (used >= 1000) ++count; } then: malloc_inspect_all(count_chunks, NULL);

malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.

stall_read_tsc

static uint64_t stall_read_tsc()

Defined at amd64/stall.c#4

_clone_tramp1

void _clone_tramp1()

Defined at amd64/clone_tramp1.c#5

app_main

void app_main()

_clone_tramp

void _clone_tramp()

dlrealloc_in_place

void * dlrealloc_in_place(void * , size_t )

realloc_in_place(void* p, size_t n) Resizes the space allocated for p to size n, only if this can be done without moving p (i.e., only if there is adjacent space available if n is greater than p's current allocated size, or n is less than or equal to p's size). This may be used instead of plain realloc if an alternative allocation strategy is needed upon failure to expand space; for example, reallocation of a buffer that must be memory-aligned or cleared. You can use realloc_in_place to trigger these alternatives only when needed.

Returns p if successful; otherwise null.

fmod

double fmod(double a, double b)

Defined at math.c#3

longjmp

void longjmp(jmp_buf buf, int ret)

str_to_uint64

uint64_t str_to_uint64(const char * s)

Defined at strconv.c#3

fmodf

float fmodf(float a, float b)

Defined at math.c#5

terminal_print

void terminal_print(const char * string, size_t len)

Defined at terminal.c#5

path_validate_char

_Bool path_validate_char(char ch)

Defined at path.c#5

arena_create_chunk

static struct arena_chunk * arena_create_chunk(size_t capacity)

Defined at arena.c#5

malloc_stats

void malloc_stats()

malloc_stats(); Prints on stderr the amount of space obtained from the system (both via sbrk and mmap), the maximum amount (which may be more than current if malloc_trim and/or munmap got called), and the current number of bytes allocated via malloc (or realloc, etc) but not yet freed. Note that this is the number of bytes allocated, not the number requested. It will be larger than the number requested because of alignment and bookkeeping overhead. Because it includes alignment wastage as being in use, this figure may be greater than zero even when no user-level chunks are allocated.

The reported current and maximum system memory can be inaccurate if a program makes other calls to system memory allocation functions (normally sbrk) outside of malloc.

malloc_stats prints only the most commonly interesting statistics. More information can be obtained by calling mallinfo.

malloc_stats(); Prints on stderr the amount of space obtained from the system (both via sbrk and mmap), the maximum amount (which may be more than current if malloc_trim and/or munmap got called), and the current number of bytes allocated via malloc (or realloc, etc) but not yet freed. Note that this is the number of bytes allocated, not the number requested. It will be larger than the number requested because of alignment and bookkeeping overhead. Because it includes alignment wastage as being in use, this figure may be greater than zero even when no user-level chunks are allocated.

The reported current and maximum system memory can be inaccurate if a program makes other calls to system memory allocation functions (normally sbrk) outside of malloc.

malloc_stats prints only the most commonly interesting statistics. More information can be obtained by calling mallinfo.

malloc_stats is not compiled if NO_MALLOC_STATS is defined.

memset

size_t memset(void * dst, uint8_t b, size_t n)

Defined at string.c#6

in_gb_init

void in_gb_init(in_gb_malloc_func_t mallocfn, void * ctx, struct in_gb * gb, size_t capacity)

Defined at in_gb.c#6

kb_read_key

int kb_read_key()

Defined at kb.c#7

terminal_dimensions

void terminal_dimensions(size_t * cols, size_t * rows)

Defined at terminal.c#9

premain_memset

static size_t premain_memset(void * dst, uint8_t b, size_t n)

Defined at __premain.c#10

stall_get_tsc_freq_hz

static uint64_t stall_get_tsc_freq_hz()

Defined at amd64/stall.c#10

filereader_init

int filereader_init(struct filereader * fw, const char * volume, const char * path)

Defined at filereader.c#10

debugconsole_printf_buffer_lock

static void debugconsole_printf_buffer_lock()

Defined at debugconsole.c#11

translate

static void translate(size_t fs_block, size_t fs_block_count, size_t fs_block_size, size_t device_sector_size, size_t * out_phys_sector, size_t * out_sector_count)

Defined at libfat.c#11

filewriter_init

int filewriter_init(struct filewriter * fw, const char * volume, const char * path, uint32_t flags)

Defined at filewriter.c#11

quit

int quit()

Defined at system.c#12

mprintf_buffer_lock

static void mprintf_buffer_lock()

Defined at mprintf.c#13

test

int test(char c)

Defined at system.c#14

memcpy

size_t memcpy(void * dst, const void * src, size_t n)

Defined at string.c#14

in_wmalloc

static void * in_wmalloc(void * ctx, size_t size)

Defined at in_input.c#15

path_validate

_Bool path_validate(const char * path)

Defined at path.c#15

sched

int sched()

Defined at system.c#16

arena_destroy_chunk

static void arena_destroy_chunk(struct arena_chunk * chunk)

Defined at arena.c#18

clear_bss

static void clear_bss()

Defined at __premain.c#18

map

void * map(uintptr_t vaddr, size_t pages, uint32_t flags)

Defined at system.c#18

in_gb_fini

void in_gb_fini(in_gb_free_func_t freefn, void * ctx, struct in_gb * gb)

Defined at in_gb.c#18

debugconsole_printf_buffer_unlock

static void debugconsole_printf_buffer_unlock()

Defined at debugconsole.c#19

in_wfree

static void in_wfree(void * ctx, void * mem)

Defined at in_input.c#20

arena_reset

void arena_reset(struct arena * arena)

Defined at arena.c#20

stall_ms

void stall_ms(uint64_t ms)

Defined at amd64/stall.c#20

mprintf_buffer_unlock

static void mprintf_buffer_unlock()

Defined at mprintf.c#21

process_spawn

struct process_data * process_spawn(process_func_t func, void * argument_ptr)

Defined at process.c#21

unmap

int unmap(uintptr_t vaddr, size_t pages)

Defined at system.c#22

__premain

void __premain()

Defined at __premain.c#22

debugconsole_print

int debugconsole_print(const char * string, size_t len)

Defined at debugconsole.c#23

in_gb_grow

void in_gb_grow(in_gb_realloc_func_t reallocfn, void * ctx, struct in_gb * gb)

Defined at in_gb.c#23

fat_diskio_read

static int fat_diskio_read(struct fatfs_ctx * ctx, uint32_t sector, uint8_t * buffer, uint32_t sector_count)

Defined at libfat.c#23

clone

int clone(uintptr_t vstack_top, void ()(void) entry, void * argument_ptr)*

Defined at system.c#24

strncpy

void strncpy(char * dst, const char * src, size_t n)

Defined at string.c#24

SOURCE: https://stackoverflow.com/a/48967408

in_wrealloc

static void * in_wrealloc(void * ctx, void * mem, size_t old, size_t new)

Defined at in_input.c#25

mprintf

void mprintf(const char * fmt)

Defined at mprintf.c#25

debug_printf

void debug_printf(const char * fmt)

Defined at debugconsole.c#27

mutex_create

int mutex_create()

Defined at system.c#28

arena_destroy

void arena_destroy(struct arena * arena)

Defined at arena.c#28

mutex_delete

int mutex_delete(int mutex_rid)

Defined at system.c#30

strlen

size_t strlen(const char * str)

Defined at string.c#30

mutex_lock

int mutex_lock(int mutex_rid)

Defined at system.c#32

mutex_unlock

int mutex_unlock(int mutex_rid)

Defined at system.c#34

in_stream_read_line

void in_stream_read_line(const char * prompt, char * buffer, size_t max)

Defined at in_input.c#35

argument_ptr

void * argument_ptr()

Defined at system.c#36

filereader_fini

int filereader_fini(struct filereader * fw)

Defined at filereader.c#37

memcmp

int memcmp(const void * s1, const void * s2, size_t n)

Defined at string.c#37

device_do

int device_do(const char * device_key, int cmd, void * a1, void * a2, void * a3, void * a4)

Defined at system.c#38

path_parse

_Bool path_parse(const char * source, char * volume, const char ** path)

Defined at path.c#40

arena_malloc

void * arena_malloc(struct arena * arena, size_t size)

Defined at arena.c#41

exec

int exec(const char * volume, const char * path)

Defined at system.c#42

in_gb_move

void in_gb_move(struct in_gb * gb, size_t pos)

Defined at in_gb.c#43

volume_open

int volume_open(const char * volume)

Defined at system.c#44

_init_lock

static int _init_lock(int * lock)

Defined at _malloc_port.c#44

volume_close

int volume_close()

Defined at system.c#46

filereader_read

int filereader_read(struct filereader * fw, uint8_t * buffer, size_t buffer_size)

Defined at filereader.c#46

read_file

int read_file(const char * path, size_t off, uint8_t * buffer, size_t size)

Defined at system.c#48

_fini_lock

static int _fini_lock(int * lock)

Defined at _malloc_port.c#49

process_data_free

void process_data_free(struct process_data * pdata)

Defined at process.c#50

strtokenize

void strtokenize(const char * str, char delim, void * ctx, strtokenize_cb_func_t cb)

Defined at string.c#50

fat_diskio_write

static int fat_diskio_write(struct fatfs_ctx * ctx, uint32_t sector, uint8_t * buffer, uint32_t sector_count)

Defined at libfat.c#50

_acquire_lock

static int _acquire_lock(int * lock)

Defined at _malloc_port.c#51

describe

int describe(const char * path, struct desc * desc)

Defined at system.c#52

_release_lock

static int _release_lock(int * lock)

Defined at _malloc_port.c#53

process_self_init

void process_self_init()

Defined at process.c#55

filewriter_fini

int filewriter_fini(struct filewriter * fw)

Defined at filewriter.c#55

mail_send

int mail_send(int pgid, void * mesg, size_t mesg_size)

Defined at system.c#56

mail_receive

int mail_receive(void * mesg, size_t mesg_size)

Defined at system.c#60

in_gb_insert

void in_gb_insert(in_gb_realloc_func_t reallocfn, void * ctx, struct in_gb * gb, char c)

Defined at in_gb.c#60

process_get_exec_pid

int process_get_exec_pid()

Defined at process.c#62

process_get_exec_pgid

int process_get_exec_pgid()

Defined at process.c#64

get_procgroup

int get_procgroup(int pid)

Defined at system.c#64

filewriter_write

int filewriter_write(struct filewriter * fw, uint8_t * buffer, size_t buffer_size)

Defined at filewriter.c#64

_open_dummy

int _open_dummy(const char * path, uint16_t modes)

Defined at _malloc_port.c#65

process_get_pid

int process_get_pid()

Defined at process.c#66

get_exec_pid

int get_exec_pid()

Defined at system.c#66

process_get_pgid

int process_get_pgid()

Defined at process.c#68

read_dir_entry

int read_dir_entry(const char * path, struct dir_entry * entry, size_t entry_num)

Defined at system.c#68

in_gb_backspace

void in_gb_backspace(struct in_gb * gb)

Defined at in_gb.c#68

path_basename

const char * path_basename(const char * path)

Defined at path.c#68

mmap

void * mmap(void * addr, size_t size, int prot, int flags, int fd, size_t off)

Defined at _malloc_port.c#70

str_split_lines

void str_split_lines(const char * str, size_t total_len, void * ctx, strtokenize_cb_func_t cb)

Defined at string.c#71

create_file

int create_file(const char * path)

Defined at system.c#72

in_gb_length

size_t in_gb_length(struct in_gb * gb)

Defined at in_gb.c#73

write_file

int write_file(const char * path, size_t off, uint8_t * buffer, size_t size, uint32_t flags)

Defined at system.c#74

in_gb_string_at

char * in_gb_string_at(in_gb_malloc_func_t mallocfn, void * ctx, struct in_gb * gb, size_t pos)

Defined at in_gb.c#75

fat_format_drive

int fat_format_drive(struct fatfs_ctx * ctx, const char * device_key, int fs_type)

Defined at libfat.c#77

wait_for_pid

int wait_for_pid(int pid)

Defined at system.c#78

kill

int kill(int pid)

Defined at system.c#80

arena_realloc

void * arena_realloc(struct arena * arena, void * memory, size_t prev_size, size_t new_size)

Defined at arena.c#80

create_dir

int create_dir(const char * path)

Defined at system.c#82

remove

int remove(const char * path)

Defined at system.c#84

create_volume

int create_volume(const char * key, int fs_type, const char * device_key)

Defined at system.c#86

munmap

int munmap(void * addr, size_t length)

Defined at _malloc_port.c#86

env_set

int env_set(int pgid, const char * key, void * buffer, size_t len)

Defined at system.c#90

env_get

int env_get(int pgid, const char * key, void * buffer, size_t len)

Defined at system.c#94

exec_partial

int exec_partial(const char * volume, const char * path)

Defined at system.c#98

exec_partial_fini

int exec_partial_fini(int pid)

Defined at system.c#102

memchr

void * memchr(const void * s, unsigned char c, size_t n)

Defined at string.c#102

https://svnweb.freebsd.org/base/stable/7/lib/libc/string/memchr.c?view=markup

get_self_pid

int get_self_pid()

Defined at system.c#104

stream_write

int stream_write(int pgid, int rid, void * buffer, size_t size)

Defined at system.c#106

stream_read

int stream_read(int pgid, int rid, void * buffer, size_t size)

Defined at system.c#110

get_proc_info

int get_proc_info(struct proc_info * infos, size_t count)

Defined at system.c#114

in_gb_concat

void in_gb_concat(in_gb_realloc_func_t reallocfn, void * ctx, struct in_gb * dest, struct in_gb * src)

Defined at in_gb.c#114

get_volume_info

int get_volume_info(struct volume_info * infos, size_t count)

Defined at system.c#122

volume_delete

int volume_delete(const char * key)

Defined at system.c#126

date_time

int date_time(struct date_time * dt)

Defined at system.c#128

sbrk

void * sbrk(ptrdiff_t )

strcmp

int strcmp(const char * s1, const char * s2)

Defined at string.c#115

https://stackoverflow.com/a/34873406

get_device_info

int get_device_info(struct device_info * infos, size_t count)

Defined at system.c#118

setjmp

int setjmp(jmp_buf buf)

strcat

char * strcat(char * dest, const char * src)

Defined at string.c#124

https://stackoverflow.com/a/2490637

strncmp

int strncmp(const char * s1, const char * s2, size_t n)

Defined at string.c#136

strstr

char * strstr(const char * str, const char * substring)

Defined at string.c#150

https://stackoverflow.com/questions/49131175/recreate-the-strstr-function

strchr

char * strchr(const char * s, int c)

Defined at string.c#181

https://github.com/gcc-mirror/gcc/blob/master/libiberty/strchr.c

memmove

void * memmove(void * dest, const void * src, unsigned int n)

Defined at string.c#191

SOURCE: https://aticleworld.com/memmove-function-implementation-in-c/

strncat

char * strncat(char * dest, const char * src, size_t n)

Defined at string.c#211

strcpy

char * strcpy(char * strDest, const char * strSrc)

Defined at string.c#226

https://stackoverflow.com/questions/14476627/strcpy-implementation-in-c

isalnum

int isalnum(int c)

Defined at string.c#233

isalpha

int isalpha(int c)

Defined at string.c#235

iscntrl

int iscntrl(int c)

Defined at string.c#237

isdigit

int isdigit(int c)

Defined at string.c#239

isgraph

int isgraph(int c)

Defined at string.c#241

islower

int islower(int c)

Defined at string.c#243

isprint

int isprint(int c)

Defined at string.c#245

ispunct

int ispunct(int c)

Defined at string.c#247

isspace

int isspace(int c)

Defined at string.c#249

isupper

int isupper(int c)

Defined at string.c#253

isxdigit

int isxdigit(int c)

Defined at string.c#255

isascii

int isascii(int c)

Defined at string.c#257

isblank

int isblank(int c)

Defined at string.c#259

tolower

int tolower(int chr)

Defined at string.c#261

toupper

int toupper(int chr)

Defined at string.c#263

get_bit_access

static floating_point_with_bit_access get_bit_access(floating_point_t x)

Defined at printf.c#335

This is unnecessary in C99, since compound initializers can be used, but: 1. Some compilers are finicky about this; 2. Some people may want to convert this to C89; 3. If you try to use it as C++, only C++20 supports compound literals

get_sign_bit

static int get_sign_bit(floating_point_t x)

Defined at printf.c#341

get_exp2

static int get_exp2(floating_point_with_bit_access x)

Defined at printf.c#346

putchar_via_gadget

static void putchar_via_gadget(output_gadget_t * gadget, char c)

Defined at printf.c#392

Note: This function currently assumes it is not passed a '\0' c, or alternatively, that '\0' can be passed to the function in the output gadget. The former assumption holds within the printf library. It also assumes that the output gadget has been properly initialized.

append_termination_with_gadget

static void append_termination_with_gadget(output_gadget_t * gadget)

Defined at printf.c#414

Possibly-write the string-terminating '\0' character

putchar_wrapper

static void putchar_wrapper(char c, void * unused)

Defined at printf.c#430

We can't use putchar_ as is, since our output gadget only takes pointers to functions with an extra argument

discarding_gadget

static output_gadget_t discarding_gadget()

Defined at printf.c#435

buffer_gadget

static output_gadget_t buffer_gadget(char * buffer, size_t buffer_size)

Defined at printf.c#445

function_gadget

static output_gadget_t function_gadget(void ()(char, void ) function, void * extra_arg)

Defined at printf.c#457

extern_putchar_gadget

static output_gadget_t extern_putchar_gadget()

Defined at printf.c#465

strnlen_s_

static printf_size_t strnlen_s_(const char * str, printf_size_t maxsize)

Defined at printf.c#475

internal secure strlen

return The length of the string (excluding the terminating 0) limited by 'maxsize'

note strlen uses size_t, but wes only use this function with printf_size_t variables - hence the signature.

is_digit_

static Bool is_digit(char ch)

Defined at printf.c#486

internal test if char is a digit (0-9)

return true if char is a digit

atou_

static printf_size_t atou_(const char ** str)

Defined at printf.c#489

internal ASCII string to printf_size_t conversion

out_rev_

static void out_rev_(output_gadget_t * output, const char * buf, printf_size_t len, printf_size_t width, printf_flags_t flags)

Defined at printf.c#498

output the specified string in reverse, taking care of any zero-padding

static void print_integer_finalization(output_gadget_t * output, char * buf, printf_size_t len, _Bool negative, numeric_base_t base, printf_size_t precision, printf_size_t width, printf_flags_t flags)

Defined at printf.c#527

Invoked by print_integer after the actual number has been printed, performing necessary work on the number's prefix (as the number is initially printed in reverse order)

static void print_integer(output_gadget_t * output, printf_unsigned_value_t value, _Bool negative, numeric_base_t base, printf_size_t precision, printf_size_t width, printf_flags_t flags)

Defined at printf.c#596

An internal itoa-like function

get_components

static struct floating_point_components get_components(floating_point_t number, printf_size_t precision)

Defined at printf.c#683

Break up a non-negative, finite, floating-point number into two integral parts of its decimal representation: The number up to the decimal point, and the number appearing after that point - whose number of digits corresponds to the precision value. Example: The components of 12.621 are 12 and 621 for precision 3, or 12 and 62 for precision 2.

apply_scaling

static floating_point_t apply_scaling(floating_point_t num, struct scaling_factor normalization)

Defined at printf.c#727

unapply_scaling

static floating_point_t unapply_scaling(floating_point_t normalized, struct scaling_factor normalization)

Defined at printf.c#731

update_normalization

static struct scaling_factor update_normalization(struct scaling_factor sf, floating_point_t extra_multiplicative_factor)

Defined at printf.c#749

get_normalized_components

static struct floating_point_components get_normalized_components(_Bool negative, printf_size_t precision, floating_point_t non_normalized, struct scaling_factor normalization, int floored_exp10)

Defined at printf.c#771

static void print_broken_up_decimal(struct floating_point_components number_, output_gadget_t * output, printf_size_t precision, printf_size_t width, printf_flags_t flags, char * buf, printf_size_t len)

Defined at printf.c#828

static void print_decimal_number(output_gadget_t * output, floating_point_t number, printf_size_t precision, printf_size_t width, printf_flags_t flags, char * buf, printf_size_t len)

Defined at printf.c#911

internal ftoa for fixed decimal floating point

bastardized_floor

static int bastardized_floor(floating_point_t x)

Defined at printf.c#924

A floor function - but one which only works for numbers whose floor value is representable by an int.

log10_of_positive

static floating_point_t log10_of_positive(floating_point_t positive_number)

Defined at printf.c#937

Computes the base-10 logarithm of the input number - which must be an actual positive number (not infinity or NaN, nor a sub-normal)

pow10_of_int

static floating_point_t pow10_of_int(int floored_exp10)

Defined at printf.c#979

static void print_exponential_number(output_gadget_t * output, floating_point_t number, printf_size_t precision, printf_size_t width, printf_flags_t flags, char * buf, printf_size_t len)

Defined at printf.c#1005

static void print_floating_point(output_gadget_t * output, floating_point_t value, printf_size_t precision, printf_size_t width, printf_flags_t flags, _Bool prefer_exponential)

Defined at printf.c#1172

parse_flags

static printf_flags_t parse_flags(const char ** format)

Defined at printf.c#1231

Advances the format pointer past the flags, and returns the parsed flags due to the characters passed

format_string_loop

static void format_string_loop(output_gadget_t * output, const char * format, va_list args)

Defined at printf.c#1261

vsnprintf_impl

static int vsnprintf_impl(output_gadget_t * output, const char * format, va_list args)

Defined at printf.c#1625

internal vsnprintf - used for implementing _all library functions

vprintf

int vprintf(const char * format, va_list arg)

Defined at printf.c#1641

===========================================================================

vsnprintf

int vsnprintf(char * s, size_t n, const char * format, va_list arg)

Defined at printf.c#1646

vsprintf

int vsprintf(char * s, const char * format, va_list arg)

Defined at printf.c#1651

vfctprintf

int vfctprintf(void ()(char, void ) out, void * extra_arg, const char * format, va_list arg)

Defined at printf.c#1655

printf

int printf(const char * format)

Defined at printf.c#1665

sprintf

int sprintf(char * s, const char * format)

Defined at printf.c#1674

snprintf

int snprintf(char * s, size_t n, const char * format)

Defined at printf.c#1683

fctprintf

int fctprintf(void ()(char, void ) out, void * extra_arg, const char * format)

Defined at printf.c#1692

putchar_

void putchar_(char ch)

Defined at printf.c#1701

segment_holding

static msegmentptr segment_holding(mstate m, char * addr)

Defined at malloc.c#2710

Return segment holding given address

static int has_segment_link(mstate m, msegmentptr ss)

Defined at malloc.c#2721

Return true if segment contains a segment link

init_mparams

static int init_mparams()

Defined at malloc.c#3111

Initialize mparams

change_mparam

static int change_mparam(int param_number, int value)

Defined at malloc.c#3201

support for mallopt

internal_mallinfo

static struct mallinfo internal_mallinfo(mstate m)

Defined at malloc.c#3496

mmap_alloc

static void * mmap_alloc(mstate m, size_t nb)

Defined at malloc.c#3835

Malloc using mmap

mmap_resize

static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags)

Defined at malloc.c#3867

Realloc using mmap

init_top

static void init_top(mstate m, mchunkptr p, size_t psize)

Defined at malloc.c#3905

Initialize top chunk and its size

init_bins

static void init_bins(mstate m)

Defined at malloc.c#3920

Initialize bins for a new mstate that is otherwise zeroed out

prepend_alloc

static void * prepend_alloc(mstate m, char * newbase, char * oldbase, size_t nb)

Defined at malloc.c#3949

Allocate chunk and prepend remainder with chunk in successor base.

add_segment

static void add_segment(mstate m, char * tbase, size_t tsize, flag_t mmapped)

Defined at malloc.c#3991

Add a segment to hold a new noncontiguous region

sys_alloc

static void * sys_alloc(mstate m, size_t nb)

Defined at malloc.c#4046

Get memory from system using MORECORE or MMAP

release_unused_segments

static size_t release_unused_segments(mstate m)

Defined at malloc.c#4257

Unmap and unlink any mmapped segments that don't contain used chunks

sys_trim

static int sys_trim(mstate m, size_t pad)

Defined at malloc.c#4304

dispose_chunk

static void dispose_chunk(mstate m, mchunkptr p, size_t psize)

Defined at malloc.c#4372

Consolidate and bin a chunk. Differs from exported versions of free mainly in that the chunk need not be marked as inuse.

tmalloc_large

static void * tmalloc_large(mstate m, size_t nb)

Defined at malloc.c#4443

allocate a large request from the best fitting chunk in a treebin

tmalloc_small

static void * tmalloc_small(mstate m, size_t nb)

Defined at malloc.c#4514

allocate a small request from the best fitting chunk in a treebin

malloc

void * malloc(size_t )

Defined at malloc.c#4553

malloc(size_t n) Returns a pointer to a newly allocated chunk of at least n bytes, or null if no space is available, in which case errno is set to ENOMEM on ANSI C systems.

If n is zero, malloc returns a minimum-sized chunk. (The minimum size is 16 bytes on most 32bit systems, and 32 bytes on 64bit systems.) Note that size_t is an unsigned type, so calls with arguments that would be negative if signed are interpreted as requests for huge amounts of space, which will often fail. The maximum supported value of n differs across systems, but is in all cases less than the maximum representable value of a size_t.

free

void free(void * )

Defined at malloc.c#4691

free(void* p) Releases the chunk of memory pointed to by p, that had been previously allocated using malloc or a related routine such as realloc. It has no effect if p is null. If p was not malloced or already freed, free(p) will by default cause the current program to abort.

---------------------------- free ---------------------------

calloc

void * calloc(size_t , size_t )

Defined at malloc.c#4800

calloc(size_t n_elements, size_t element_size); Returns a pointer to n_elements * element_size bytes, with all locations set to zero.

try_realloc_chunk

static mchunkptr try_realloc_chunk(mstate m, mchunkptr p, size_t nb, int can_move)

Defined at malloc.c#4820

Try to realloc; only in-place unless can_move true

internal_memalign

static void * internal_memalign(mstate m, size_t alignment, size_t bytes)

Defined at malloc.c#4899

ialloc

static void ** ialloc(mstate m, size_t n_elements, size_t * sizes, int opts, void [] chunks)*

Defined at malloc.c#4980

Common support for independent_X routines, handling all of the combinations that can result. The opts arg has: bit 0 set if all elements are same size (using sizes[0]) bit 1 set if elements should be zeroed

internal_bulk_free

static size_t internal_bulk_free(mstate m, void [] array, size_t nelem)*

Defined at malloc.c#5106

Try to free all pointers in the given array. Note: this could be made faster, by delaying consolidation, at the price of disabling some user integrity checks, We still optimize some consolidations by combining adjacent chunks before freeing, which will occur often if allocated with ialloc or the array is sorted.

realloc

void * realloc(void * , size_t )

Defined at malloc.c#5194

realloc(void* p, size_t n) Returns a pointer to a chunk of size n that contains the same data as does chunk p up to the minimum of (n, p's size) bytes, or null if no space is available.

The returned pointer may or may not be the same as p. The algorithm prefers extending p in most cases when possible, otherwise it employs the equivalent of a malloc-copy-free sequence.

If p is null, realloc is equivalent to malloc.

If space is not available, realloc returns null, errno is set (if on ANSI) and p is NOT freed.

if n is for fewer bytes than already held by p, the newly unused space is lopped off and freed if possible. realloc with a size argument of zero (re)allocates a minimum-sized chunk.

The old unix realloc convention of allowing the last-free'd chunk to be used as an argument to realloc is not supported.

realloc_in_place

void * realloc_in_place(void * , size_t )

Defined at malloc.c#5239

realloc_in_place(void* p, size_t n) Resizes the space allocated for p to size n, only if this can be done without moving p (i.e., only if there is adjacent space available if n is greater than p's current allocated size, or n is less than or equal to p's size). This may be used instead of plain realloc if an alternative allocation strategy is needed upon failure to expand space; for example, reallocation of a buffer that must be memory-aligned or cleared. You can use realloc_in_place to trigger these alternatives only when needed.

Returns p if successful; otherwise null.

memalign

void * memalign(size_t , size_t )

Defined at malloc.c#5270

memalign(size_t alignment, size_t n); Returns a pointer to a newly allocated chunk of n bytes, aligned in accord with the alignment argument.

The alignment argument should be a power of two. If the argument is not a power of two, the nearest greater power is used. 8-byte alignment is guaranteed by normal malloc calls, so don't bother calling memalign with an argument of 8 or less.

Overreliance on memalign is a sure way to fragment space.

posix_memalign

int posix_memalign(void ** , size_t , size_t )

Defined at malloc.c#5277

int posix_memalign(void* pp, size_t alignment, size_t n); Allocates a chunk of n bytes, aligned in accord with the alignment argument. Differs from memalign only in that it (1) assigns the allocated memory to pp rather than returning it, (2) fails and returns EINVAL if the alignment is not a power of two (3) fails and returns ENOMEM if memory cannot be allocated.

valloc

void * valloc(size_t )

Defined at malloc.c#5300

valloc(size_t n); Equivalent to memalign(pagesize, n), where pagesize is the page size of the system. If the pagesize is unknown, 4096 is used.

mallopt

int mallopt(int , int )

Defined at malloc.c#5389

mallopt(int parameter_number, int parameter_value) Sets tunable parameters The format is to provide a (parameter-number, parameter-value) pair. mallopt then sets the corresponding parameter to the argument value if it can (i.e., so long as the value is meaningful), and returns 1 if successful else 0. To workaround the fact that mallopt is specified to use int, not size_t parameters, the value -1 is specially treated as the maximum unsigned size_t value.

SVID/XPG/ANSI defines four standard param numbers for mallopt, normally defined in malloc.h. None of these are use in this malloc, so setting them has no effect. But this malloc also supports other options in mallopt. See below for details. Briefly, supported parameters are as follows (listed defaults are for "typical" configurations).

Symbol param # default allowed param values M_TRIM_THRESHOLD -1 210241024 any (-1 disables) M_GRANULARITY -2 page size any power of 2 >= page size M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)

malloc_footprint

size_t malloc_footprint()

Defined at malloc.c#5353

malloc_footprint(); Returns the number of bytes obtained from the system. The total number of bytes allocated by malloc, realloc etc., is less than this value. Unlike mallinfo, this function returns only a precomputed result, so can be called frequently to monitor memory consumption. Even if locks are otherwise defined, this function does not use them, so results might not be up to date.

malloc_max_footprint

size_t malloc_max_footprint()

Defined at malloc.c#5357

malloc_max_footprint(); Returns the maximum number of bytes obtained from the system. This value will be greater than current footprint if deallocated space has been reclaimed by the system. The peak number of bytes allocated by malloc, realloc etc., is less than this value. Unlike mallinfo, this function returns only a precomputed result, so can be called frequently to monitor memory consumption. Even if locks are otherwise defined, this function does not use them, so results might not be up to date.

malloc_footprint_limit

size_t malloc_footprint_limit()

Defined at malloc.c#5361

malloc_footprint_limit(); Returns the number of bytes that the heap is allowed to obtain from the system, returning the last value returned by malloc_set_footprint_limit, or the maximum size_t value if never set. The returned value reflects a permission. There is no guarantee that this number of bytes can actually be obtained from the system.

malloc_set_footprint_limit

size_t malloc_set_footprint_limit(size_t bytes)

Defined at malloc.c#5366

malloc_set_footprint_limit(); Sets the maximum number of bytes to obtain from the system, causing failure returns from malloc and related functions upon attempts to exceed this value. The argument value may be subject to page rounding to an enforceable limit; this actual value is returned. Using an argument of the maximum possible size_t effectively disables checks. If the argument is less than or equal to the current malloc_footprint, then all future allocations that require additional system memory will fail. However, invocation cannot retroactively deallocate existing used memory.

mallinfo

struct mallinfo mallinfo()

Defined at malloc.c#5378

mallinfo() Returns (by copy) a struct containing various summary statistics:

arena: current total non-mmapped bytes allocated from system ordblks: the number of free chunks smblks: always zero. hblks: current number of mmapped regions hblkhd: total bytes held in mmapped regions usmblks: the maximum total allocated space. This will be greater than current total if trimming has occurred. fsmblks: always zero uordblks: current total allocated space (normal or mmapped) fordblks: total free space keepcost: the maximum number of bytes that could ideally be released back to system via malloc_trim. ("ideally" means that it ignores page restrictions etc.)

Because these fields are ints, but internal bookkeeping may be kept as longs, the reported values may wrap around zero and thus be inaccurate.

independent_calloc

void ** independent_calloc(size_t , size_t , void ** )

Defined at malloc.c#5314

independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);

independent_calloc is similar to calloc, but instead of returning a single cleared space, it returns an array of pointers to n_elements independent elements that can hold contents of size elem_size, each of which starts out cleared, and can be independently freed, realloc'ed etc. The elements are guaranteed to be adjacently allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications.

The "chunks" argument is optional (i.e., may be null, which is probably the most typical usage). If it is null, the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks.

In either case, independent_calloc returns this pointer array, or null if the allocation failed. If n_elements is zero and "chunks" is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted).

Each element must be freed when it is no longer needed. This can be done all at once using bulk_free.

independent_calloc simplifies and speeds up implementations of many kinds of pools. It may also be useful when constructing large data structures that initially have a fixed number of fixed-sized nodes, but the number is not known at compile time, and some of the nodes may later need to be freed. For example:

struct Node { int item; struct Node* next; };

struct Node build_list() { struct Node pool; int n = read_number_of_nodes_needed(); if (n <= 0) return 0; pool = (struct Node)(independent_calloc(n, sizeof(struct Node), 0); if (pool == 0) die(); // organize into a linked list... struct Node first = pool[0]; for (i = 0; i < n-1; ++i) pool[i]->next = pool[i+1]; free(pool); // Can now free the array (or not, if it is needed later) return first; }

independent_comalloc

void ** independent_comalloc(size_t , size_t * , void ** )

Defined at malloc.c#5320

independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);

independent_comalloc allocates, all at once, a set of n_elements chunks with sizes indicated in the "sizes" array. It returns an array of pointers to these elements, each of which can be independently freed, realloc'ed etc. The elements are guaranteed to be adjacently allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications.

The "chunks" argument is optional (i.e., may be null). If it is null the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks.

In either case, independent_comalloc returns this pointer array, or null if the allocation failed. If n_elements is zero and chunks is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted).

Each element must be freed when it is no longer needed. This can be done all at once using bulk_free.

independent_comallac differs from independent_calloc in that each element may have a different size, and also that it does not automatically clear elements.

independent_comalloc can be used to speed up allocation in cases where several structs or objects must always be allocated at the same time. For example:

struct Head { ... } struct Foot { ... }

void send_message(char msg) { int msglen = strlen(msg); size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; void chunks[3]; if (independent_comalloc(3, sizes, chunks) == 0) die(); struct Head head = (struct Head)(chunks[0]); char body = (char)(chunks[1]); struct Foot foot = (struct Foot)(chunks[2]); // ... }

In general though, independent_comalloc is worth using only for larger values of n_elements. For small values, you probably won't detect enough difference from series of malloc calls to bother.

Overuse of independent_comalloc can increase overall memory usage, since it cannot reuse existing noncontiguous small chunks that might be available for some of the elements.

bulk_free

size_t bulk_free(void ** , size_t n_elements)

Defined at malloc.c#5325

bulk_free(void* array[], size_t n_elements) Frees and clears (sets to null) each non-null pointer in the given array. This is likely to be faster than freeing them one-by-one. If footers are used, pointers that have been allocated in different mspaces are not freed or cleared, and the count of all such pointers is returned. For large arrays of pointers with poor locality, it may be worthwhile to sort this array before calling bulk_free.

pvalloc

void * pvalloc(size_t )

Defined at malloc.c#5307

pvalloc(size_t n); Equivalent to valloc(minimum-page-that-holds(n)), that is, round up n to nearest pagesize.

malloc_trim

int malloc_trim(size_t )

Defined at malloc.c#5343

malloc_trim(size_t pad);

If possible, gives memory back to the system (via negative arguments to sbrk) if there is unused memory at the `high' end of the malloc pool or in unused MMAP segments. You can call this after freeing large blocks of memory to potentially reduce the system-level memory requirements of a program. However, it cannot guarantee to reduce memory. Under some allocation patterns, some large free blocks of memory will be locked between two used chunks, so they cannot be given back to the system.

The `pad' argument to malloc_trim represents the amount of free trailing space to leave untrimmed. If this argument is zero, only the minimum amount of memory to maintain internal data structures will be left. Non-zero arguments can be supplied to maintain enough trailing space to service future expected allocations without having to re-obtain memory from the system.

Malloc_trim returns 1 if it actually released any memory, else 0.

malloc_usable_size

size_t malloc_usable_size(void * )

Defined at malloc.c#5393

malloc_usable_size(void* p);

Returns the number of bytes you can actually use in an allocated chunk, which may be more than you requested (although often not) due to alignment and minimum size constraints. You can use this many bytes without worrying about overwriting other allocated objects. This is not a particularly great programming practice. malloc_usable_size can be more useful in debugging and assertions, for example:

p = malloc(n); assert(malloc_usable_size(p) >= 256);