qmalloc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .TH QMALLOC 9
  2. .SH NAME
  3. qmalloc \- quickfit malloc
  4. malloc, mallocz, smalloc, realloc, calloc, free, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, mallocinit, mallocsummary \- quickfit kernel memory allocator
  5. .SH SYNOPSIS
  6. .ta \w'\fLvoid* 'u
  7. .B
  8. void* malloc(usize size)
  9. .PP
  10. .B
  11. void* mallocalign(usize size, usize align, long offset, usize span)
  12. .PP
  13. .B
  14. void* mallocz(usize size, int clr)
  15. .PP
  16. .B
  17. void* smalloc(usize size)
  18. .PP
  19. .B
  20. void* realloc(void *p, usize size)
  21. .PP
  22. .B
  23. void* calloc(ulong n, usize szelem)
  24. .PP
  25. .B
  26. void free(void *ptr)
  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 mallocinit(void)
  45. .B
  46. void mallocsummary(void)
  47. .PP
  48. .SH DESCRIPTION
  49. These are kernel versions of the functions in
  50. .IR malloc (2).
  51. They allocate memory with Quickfit with an allocator
  52. from Kernighan & Ritchie.
  53. The allocation is currently fixed, but could call
  54. .IR physalloc (9k)
  55. in the future.
  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 does not maintain ``malloc tag'' and the ``realloc tag'',
  149. but the functions
  150. .IR setmalloctag ,
  151. .IR getmalloctag ,
  152. .IR setrealloctag ,
  153. and
  154. .IR getrealloctag
  155. are provided for compaibility with the
  156. .IR pool (2)
  157. library.
  158. .PP
  159. .I Mallocinit
  160. must be called before
  161. .I malloc
  162. is used.
  163. .I Mallocsummary
  164. prints a short summary of the allocator's state on the console.
  165. .SH SOURCE
  166. .B /sys/src/9/port/qmalloc.c
  167. .br
  168. .B /sys/src/9k/port/qmalloc.c
  169. .SH DIAGNOSTICS
  170. All functions except
  171. .I smalloc
  172. return a null pointer if space is unavailable.
  173. .SH SEE ALSO
  174. .IR "The C Programming Language" ,
  175. 2ed, Brian Kernighan and Dennis Ritchie,
  176. chapter 8 §7: Example—Storage Allocator.
  177. .br
  178. .I "Quickfit: An efficient algorithm"
  179. .I for heap storage allocation" ,
  180. Charles B. Weinstock and William A. Wulf.
  181. ACM SIGPLAN Notices, 23(10):141—144, October 1988.
  182. .br
  183. .IR pool (2),
  184. .IR physalloc (9k)