malloc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. .TH MALLOC 9
  2. .SH NAME
  3. malloc, mallocz, smalloc, realloc, calloc, free, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, malloctopoolblock \- kernel memory allocator
  4. .SH SYNOPSIS
  5. .ta \w'\fLvoid* 'u
  6. .B
  7. void* malloc(ulong size)
  8. .PP
  9. .B
  10. void* mallocalign(ulong size, ulong align, long offset, ulong span)
  11. .PP
  12. .B
  13. void* mallocz(ulong size, int clr)
  14. .PP
  15. .B
  16. void* smalloc(ulong size)
  17. .PP
  18. .B
  19. void* realloc(void *p, ulong size)
  20. .PP
  21. .B
  22. void* calloc(ulong n, ulong szelem)
  23. .PP
  24. .B
  25. void free(void *ptr)
  26. .PP
  27. .B
  28. ulong msize(void *ptr)
  29. .PP
  30. .B
  31. void setmalloctag(void *ptr, ulong tag)
  32. .PP
  33. .B
  34. ulong getmalloctag(void *ptr)
  35. .PP
  36. .B
  37. void setrealloctag(void *ptr, ulong tag)
  38. .PP
  39. .B
  40. ulong getrealloctag(void *ptr)
  41. .PP
  42. .B
  43. void* malloctopoolblock(void*)
  44. .PP
  45. .SH DESCRIPTION
  46. These are kernel versions of the functions in
  47. .IR malloc (2).
  48. They allocate memory from the
  49. .B mainmem
  50. memory pool,
  51. which is managed by
  52. the allocator
  53. .IR pool (2),
  54. which in turn replenishes the pool as required by calling
  55. .IR xalloc (9).
  56. All but
  57. .I smalloc
  58. (which calls
  59. .IR sleep (9))
  60. may safely be called by interrupt handlers.
  61. .PP
  62. .I Malloc
  63. returns a pointer to a block of at least
  64. .I size
  65. bytes, initialised to zero.
  66. The block is suitably aligned for storage of any type of object.
  67. The call
  68. .B malloc(0)
  69. returns a valid pointer rather than null.
  70. .I Mallocz
  71. is similar, but only clears the memory if
  72. .I clr
  73. is non-zero.
  74. .PP
  75. .I Smalloc
  76. returns a pointer to a block of
  77. .I size
  78. bytes, initialised to zero.
  79. If the memory is not immediately available,
  80. .I smalloc
  81. retries every 100 milliseconds until the memory is acquired.
  82. .PP
  83. .I Mallocalign
  84. allocates a block of at least
  85. .I n
  86. bytes of memory respecting alignment contraints.
  87. If
  88. .I align
  89. is non-zero,
  90. the returned pointer is aligned to be equal to
  91. .I offset
  92. modulo
  93. .IR align .
  94. If
  95. .I span
  96. is non-zero,
  97. the
  98. .I n
  99. byte block allocated will not span a
  100. .IR span -byte
  101. boundary.
  102. .PP
  103. .I Realloc
  104. changes the size of the block pointed to by
  105. .I p
  106. to
  107. .I size
  108. bytes,
  109. if possible without moving the data,
  110. and returns a pointer to the block.
  111. The contents are unchanged up to the lesser of old and new sizes,
  112. and any new space allocated is initialised to zero.
  113. .I Realloc
  114. takes on special meanings when one or both arguments are zero:
  115. .TP
  116. .B "realloc(0,\ size)
  117. means
  118. .LR malloc(size) ;
  119. returns a pointer to the newly-allocated memory
  120. .TP
  121. .B "realloc(ptr,\ 0)
  122. means
  123. .LR free(ptr) ;
  124. returns null
  125. .TP
  126. .B "realloc(0,\ 0)
  127. no-op; returns null
  128. .PD
  129. .PP
  130. .I Calloc
  131. returns a pointer to a block of memory of at least
  132. .I "n*szelem"
  133. bytes, initialised to zero.
  134. New code should use
  135. .I mallocz
  136. instead.
  137. .PP
  138. The argument to
  139. .I free
  140. is a pointer to a block of memory allocated by one of the routines above, which
  141. is returned to the allocation pool, or a null pointer, which is ignored.
  142. .PP
  143. When a block is allocated, sometimes there is some extra unused space at the end.
  144. .I Msize
  145. grows the block to encompass this unused space and returns the new number
  146. of bytes that may be used.
  147. .PP
  148. The memory allocator maintains two word-sized fields
  149. associated with each block, the ``malloc tag'' and the ``realloc tag''.
  150. By convention, the malloc tag is the PC that allocated the block,
  151. and the realloc tag the PC that last reallocated the block.
  152. These may be set or examined with
  153. .IR setmalloctag ,
  154. .IR getmalloctag ,
  155. .IR setrealloctag ,
  156. and
  157. .IR getrealloctag .
  158. When allocating blocks directly with
  159. .I malloc
  160. and
  161. .IR realloc ,
  162. these tags will be set properly.
  163. If a custom allocator wrapper is used,
  164. the allocator wrapper can set the tags
  165. itself (usually by passing the result of
  166. .IR getcallerpc (2)
  167. to
  168. .IR setmalloctag )
  169. to provide more useful information about
  170. the source of allocation.
  171. .PP
  172. .I Malloctopoolblock
  173. takes the address of a block returned by
  174. .I malloc
  175. and returns the address of the corresponding
  176. block allocated by the
  177. .IR pool (2)
  178. routines.
  179. .SH SOURCE
  180. .B /sys/src/9/port/alloc.c
  181. .SH DIAGNOSTICS
  182. All functions except
  183. .I smalloc
  184. return a null pointer if space is unavailable.
  185. If the allocated blocks have no malloc or realloc tags,
  186. .I getmalloctag
  187. and
  188. .I getrealloctag
  189. return
  190. .BR ~0 .
  191. .SH SEE ALSO
  192. .IR pool (2),
  193. .IR xalloc (9)