3 malloc, mallocalign, mallocz, free, realloc, calloc, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, malloctopoolblock \- memory allocator
11 void* malloc(ulong size)
14 void* mallocalign(ulong size, ulong align, long offset, ulong span)
17 void* mallocz(ulong size, int clr)
23 void* realloc(void *ptr, ulong size)
26 void* calloc(ulong nelem, ulong elsize)
29 ulong msize(void *ptr)
32 void setmalloctag(void *ptr, ulong tag)
35 ulong getmalloctag(void *ptr)
38 void setrealloctag(void *ptr, ulong tag)
41 ulong getrealloctag(void *ptr)
44 void* malloctopoolblock(void*)
50 provide a simple memory allocation package.
52 returns a pointer to a new block of at least
55 The block is suitably aligned for storage of any type of object.
56 No two active pointers from
58 will have the same value.
61 returns a valid pointer rather than null.
65 is a pointer to a block previously allocated by
67 this space is made available for further allocation.
68 It is legal to free a null pointer; the effect is a no-op.
69 The contents of the space returned by
77 is non-zero, the memory returned will be zeroed.
80 allocates a block of at least
82 bytes of memory respecting alignment contraints.
86 the returned pointer is aligned to be equal to
95 byte block allocated will not span a
100 changes the size of the block pointed to by
104 bytes and returns a pointer to the (possibly moved)
106 The contents will be unchanged up to the
107 lesser of the new and old sizes.
109 takes on special meanings when one or both arguments are zero:
111 .B "realloc(0,\ size)
114 returns a pointer to the newly-allocated memory
131 The space is initialized to zeros.
135 When a block is allocated, sometimes there is some extra unused space at the end.
137 grows the block to encompass this unused space and returns the new number
138 of bytes that may be used.
140 The memory allocator maintains two word-sized fields
141 associated with each block, the ``malloc tag'' and the ``realloc tag''.
142 By convention, the malloc tag is the PC that allocated the block,
143 and the realloc tag the PC that last reallocated the block.
144 These may be set or examined with
150 When allocating blocks directly with
154 these tags will be set properly.
155 If a custom allocator wrapper is used,
156 the allocator wrapper can set the tags
157 itself (usually by passing the result of
161 to provide more useful information about
162 the source of allocation.
165 takes the address of a block returned by
167 and returns the address of the corresponding
168 block allocated by the
172 .B /sys/src/libc/port/malloc.c
185 return 0 if there is no available memory.
188 If the allocated blocks have no malloc or realloc tags,
198 .B poolcheck(mainmem)
199 can be used to scan the storage arena for inconsistencies
200 such as data written beyond the bounds of allocated blocks.
201 It is often useful to combine this with with setting
203 mainmem->flags |= POOL_NOREUSE;
205 at the beginning of your program.
206 This will cause malloc not to reallocate blocks even
208 .B poolcheck(mainmem)
209 will then detect writes to freed blocks.
215 can be used to obtain traces of malloc execution; see
218 The different specification of
222 User errors can corrupt the storage arena.
223 The most common gaffes are (1) freeing an already freed block,
224 (2) storing beyond the bounds of an allocated block, and (3)
225 freeing data that was not obtained from the allocator.
230 detect such corruption, they abort.