1
0

malloc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. .TH MALLOC 2
  2. .SH NAME
  3. malloc, mallocalign, mallocz, free, realloc, calloc, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, malloctopoolblock \- memory allocator
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .ta \w'\fLvoid* 'u
  10. .B
  11. void* malloc(ulong size)
  12. .PP
  13. .B
  14. void* mallocalign(ulong size, ulong align, long offset, ulong span)
  15. .PP
  16. .B
  17. void* mallocz(ulong size, int clr)
  18. .PP
  19. .B
  20. void free(void *ptr)
  21. .PP
  22. .B
  23. void* realloc(void *ptr, ulong size)
  24. .PP
  25. .B
  26. void* calloc(ulong nelem, ulong elsize)
  27. .PP
  28. .B
  29. ulong msize(void *ptr)
  30. .PP
  31. .B
  32. void setmalloctag(void *ptr, ulong tag)
  33. .PP
  34. .B
  35. ulong getmalloctag(void *ptr)
  36. .PP
  37. .B
  38. void setrealloctag(void *ptr, ulong tag)
  39. .PP
  40. .B
  41. ulong getrealloctag(void *ptr)
  42. .PP
  43. .B
  44. void* malloctopoolblock(void*)
  45. .PP
  46. .SH DESCRIPTION
  47. .I Malloc
  48. and
  49. .I free
  50. provide a simple memory allocation package.
  51. .I Malloc
  52. returns a pointer to a new block of at least
  53. .I size
  54. bytes.
  55. The block is suitably aligned for storage of any type of object.
  56. No two active pointers from
  57. .I malloc
  58. will have the same value.
  59. The call
  60. .B malloc(0)
  61. returns a valid pointer rather than null.
  62. .PP
  63. The argument to
  64. .I free
  65. is a pointer to a block previously allocated by
  66. .IR malloc ;
  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
  70. .I malloc
  71. are undefined.
  72. .I Mallocz
  73. behaves as
  74. .IR malloc ,
  75. except that if
  76. .I clr
  77. is non-zero, the memory returned will be zeroed.
  78. .PP
  79. .I Mallocalign
  80. allocates a block of at least
  81. .I size
  82. bytes of memory respecting alignment contraints.
  83. If
  84. .I align
  85. is non-zero,
  86. the returned pointer is aligned to be equal to
  87. .I offset
  88. modulo
  89. .IR align .
  90. If
  91. .I span
  92. is non-zero,
  93. the
  94. .IR size -byte
  95. block allocated will not span a
  96. .IR span -byte
  97. boundary.
  98. .PP
  99. .I Realloc
  100. changes the size of the block pointed to by
  101. .I ptr
  102. to
  103. .I size
  104. bytes and returns a pointer to the (possibly moved)
  105. block.
  106. The contents will be unchanged up to the
  107. lesser of the new and old sizes.
  108. .I Realloc
  109. takes on special meanings when one or both arguments are zero:
  110. .TF "\fLrealloc(0, sz)
  111. .PD
  112. .TP
  113. .B "realloc(0, sz)
  114. means
  115. .LR malloc(sz) ;
  116. returns a pointer to the newly-allocated memory
  117. .TP
  118. .B "realloc(ptr, 0)
  119. means
  120. .LR free(ptr) ;
  121. returns null
  122. .TP
  123. .B "realloc(0, 0)
  124. no-op; returns null
  125. .PD
  126. .PP
  127. .I Calloc
  128. allocates space for
  129. an array of
  130. .I nelem
  131. elements of size
  132. .IR elsize .
  133. The space is initialized to zeros.
  134. .I Free
  135. frees such a block.
  136. .PP
  137. When a block is allocated, sometimes there is some extra unused space at the end.
  138. .I Msize
  139. grows the block to encompass this unused space and returns the new number
  140. of bytes that may be used.
  141. .PP
  142. The memory allocator maintains two word-sized fields
  143. associated with each block, the ``malloc tag'' and the ``realloc tag''.
  144. By convention, the malloc tag is the PC that allocated the block,
  145. and the realloc tag the PC that last reallocated the block.
  146. These may be set or examined with
  147. .IR setmalloctag ,
  148. .IR getmalloctag ,
  149. .IR setrealloctag ,
  150. and
  151. .IR getrealloctag .
  152. When allocating blocks directly with
  153. .I malloc
  154. and
  155. .IR realloc ,
  156. these tags will be set properly.
  157. If a custom allocator wrapper is used,
  158. the allocator wrapper can set the tags
  159. itself (usually by passing the result of
  160. .IR getcallerpc (2)
  161. to
  162. .IR setmalloctag )
  163. to provide more useful information about
  164. the source of allocation.
  165. .PP
  166. .I Malloctopoolblock
  167. takes the address of a block returned by
  168. .I malloc
  169. and returns the address of the corresponding
  170. block allocated by the
  171. .IR pool (2)
  172. routines.
  173. .SH SOURCE
  174. .B /sys/src/libc/port/malloc.c
  175. .SH SEE ALSO
  176. .IR leak (1),
  177. .I trump
  178. (in
  179. .IR acid (1)),
  180. .IR brk (2),
  181. .IR getcallerpc (2),
  182. .IR pool (2)
  183. .SH DIAGNOSTICS
  184. .I Malloc, realloc
  185. and
  186. .I calloc
  187. return 0 if there is no available memory.
  188. .I Errstr
  189. is likely to be set.
  190. If the allocated blocks have no malloc or realloc tags,
  191. .I getmalloctag
  192. and
  193. .I getrealloctag
  194. return
  195. .BR ~0 .
  196. .PP
  197. After including
  198. .BR pool.h ,
  199. the call
  200. .B poolcheck(mainmem)
  201. can be used to scan the storage arena for inconsistencies
  202. such as data written beyond the bounds of allocated blocks.
  203. It is often useful to combine this with with setting
  204. .EX
  205. mainmem->flags |= POOL_NOREUSE;
  206. .EE
  207. at the beginning of your program.
  208. This will cause malloc not to reallocate blocks even
  209. once they are freed;
  210. .B poolcheck(mainmem)
  211. will then detect writes to freed blocks.
  212. .PP
  213. The
  214. .I trump
  215. library for
  216. .I acid
  217. can be used to obtain traces of malloc execution; see
  218. .IR acid (1).
  219. .SH BUGS
  220. The different specification of
  221. .I calloc
  222. is bizarre.
  223. .PP
  224. User errors can corrupt the storage arena.
  225. The most common gaffes are (1) freeing an already freed block,
  226. (2) storing beyond the bounds of an allocated block, and (3)
  227. freeing data that was not obtained from the allocator.
  228. When
  229. .I malloc
  230. and
  231. .I free
  232. detect such corruption, they abort.