3 malloc, mallocz, free, realloc, calloc, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, malloctopoolblock \- memory allocator
11 void* malloc(ulong size)
14 void* mallocz(ulong size, int clr)
20 void* realloc(void *ptr, ulong size)
23 void* calloc(ulong nelem, ulong elsize)
26 ulong msize(void *ptr)
29 void setmalloctag(void *ptr, ulong tag)
32 ulong getmalloctag(void *ptr, ulong tag)
35 void setrealloctag(void *ptr, ulong tag)
38 ulong getrealloctag(void *ptr, ulong tag)
41 void* malloctopoolblock(void*)
47 provide a simple memory allocation package.
49 returns a pointer to a new block of at least
52 The block is suitably aligned for storage of any type of object.
53 No two active pointers from
55 will have the same value.
58 returns a valid pointer rather than null.
62 is a pointer to a block previously allocated by
64 this space is made available for further allocation.
65 It is legal to free a null pointer; the effect is a no-op.
66 The contents of the space returned by
74 is non-zero, the memory returned will be zeroed.
77 changes the size of the block pointed to by
81 bytes and returns a pointer to the (possibly moved)
83 The contents will be unchanged up to the
84 lesser of the new and old sizes.
86 takes on special meanings when one or both arguments are zero:
91 returns a pointer to the newly-allocated memory
108 The space is initialized to zeros.
112 When a block is allocated, sometimes there is some extra unused space at the end.
114 grows the block to encompass this unused space and returns the new number
115 of bytes that may be used.
117 The memory allocator maintains two word-sized fields
118 associated with each block, the ``malloc tag'' and the ``realloc tag''.
119 By convention, the malloc tag is the PC that allocated the block,
120 and the realloc tag the PC that last reallocated the block.
121 These may be set or examined with
127 When allocating blocks directly with
131 these tags will be set properly.
132 If a custom allocator wrapper is used,
133 the allocator wrapper can set the tags
134 itself (usually by passing the result of
138 to provide more useful information about
139 the source of allocation.
142 takes the address of a block returned by
144 and returns the address of the corresponding
145 block allocated by the
149 .B /usr/local/plan9/src/libc/port/malloc.c
162 return 0 if there is no available memory.
165 If the allocated blocks have no malloc or realloc tags,
175 .B poolcheck(mainmem)
176 can be used to scan the storage arena for inconsistencies
177 such as data written beyond the bounds of allocated blocks.
178 It is often useful to combine this with with setting
180 mainmem->flags |= POOL_NOREUSE;
182 at the beginning of your program.
183 This will cause malloc not to reallocate blocks even
185 .B poolcheck(mainmem)
186 will then detect writes to freed blocks.
192 can be used to obtain traces of malloc execution; see
195 The different specification of
199 User errors can corrupt the storage arena.
200 The most common gaffes are (1) freeing an already freed block,
201 (3) storing beyond the bounds of an allocated block, and (3)
202 freeing data that was not obtained from the allocator.
207 detect such corruption, they abort.