3 malloc, mallocz, free, realloc, calloc, setmalloctag, setrealloctag, getmalloctag, getrealloctag \- 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 void setmalloctag(void *ptr, ulong tag)
29 ulong getmalloctag(void *ptr)
32 void setrealloctag(void *ptr, ulong tag)
35 ulong getrealloctag(void *ptr)
40 provide a simple memory allocation package.
42 returns a pointer to a new block of at least
45 The block is suitably aligned for storage of any type of object.
46 No two active pointers from
48 will have the same value.
51 returns a valid pointer rather than null.
55 is a pointer to a block previously allocated by
57 this space is made available for further allocation.
58 It is legal to free a null pointer; the effect is a no-op.
59 The contents of the space returned by
67 is non-zero, the memory returned will be zeroed.
70 changes the size of the block pointed to by
74 bytes and returns a pointer to the (possibly moved)
76 The contents will be unchanged up to the
77 lesser of the new and old sizes.
79 takes on special meanings when one or both arguments are zero:
84 returns a pointer to the newly-allocated memory
101 The space is initialized to zeros.
105 The memory allocator on Plan 9 maintains two word-sized fields
106 associated with each block, the ``malloc tag'' and the ``realloc tag''.
107 By convention, the malloc tag is the PC that allocated the block,
108 and the realloc tag the PC that last reallocated the block.
109 These may be set or examined with
115 When allocating blocks directly with
119 these tags will be set properly.
120 If a custom allocator wrapper is used,
121 the allocator wrapper can set the tags
122 itself (usually by passing the result of
126 to provide more useful information about
127 the source of allocation.
129 .B \*9/src/lib9/malloc.c
131 .B \*9/src/lib9/malloctag.c
141 return 0 if there is no available memory.
144 If the allocated blocks have no malloc or realloc tags,
155 can be used to obtain traces of malloc execution; see
158 The different specification of
162 User errors can corrupt the storage arena.
163 The most common gaffes are (1) freeing an already freed block,
164 (2) storing beyond the bounds of an allocated block, and (3)
165 freeing data that was not obtained from the allocator.
170 detect such corruption, they abort.
172 To avoid name conflicts with the system versions of these functions,
178 are preprocessor macros defined as