pool_alloc.3 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. .ds d \-\^\-
  2. .ds o \fR[\fP
  3. .ds c \fR]\fP
  4. .ds | \fR|\fP
  5. .de D
  6. \\.B \*d\\$1
  7. ..
  8. .de DI
  9. \\.BI \*d\\$1 \\$2
  10. ..
  11. .de DR
  12. \\.BR \*d\\$1 \\$2
  13. ..
  14. .de Di
  15. \\.BI \*d\\$1 " \\$2"
  16. ..
  17. .de Db
  18. \\.B \*d\\$1 " \\$2"
  19. ..
  20. .de Df
  21. \\.B \*d\*ono\*c\\$1
  22. ..
  23. .de See
  24. See \fB\\$1\fP for details.
  25. ..
  26. .de SeeIn
  27. See \fB\\$1\fP in \fB\\$2\fP for details.
  28. ..
  29. .TH POOL_ALLOC 3
  30. .SH NAME
  31. pool_alloc, pool_free, pool_free_old, pool_talloc, pool_tfree, pool_create, pool_destroy, pool_boundary
  32. \- Allocate and free memory in managed allocation pools.
  33. .SH SYNOPSIS
  34. .B #include "pool_alloc.h"
  35. \fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char *), int \fIflags\fB);
  36. \fBvoid pool_destroy(struct alloc_pool *\fIpool\fB);
  37. \fBvoid *pool_alloc(struct alloc_pool *\fIpool\fB, size_t \fIsize\fB, char *\fImsg\fB);
  38. \fBvoid pool_free(struct alloc_pool *\fIpool\fB, size_t \fIsize\fB, void *\fIaddr\fB);
  39. \fBvoid pool_free_old(struct alloc_pool *\fIpool\fB, void *\fIaddr\fB);
  40. \fBvoid *pool_talloc(struct alloc_pool *\fIpool\fB, \fItype\fB), int \fIcount\fB, char *\fImsg\fB);
  41. \fBvoid pool_tfree(struct alloc_pool *\fIpool\fB, \fItype\fB, int \fIcount\fB, void *\fIaddr\fB);
  42. \fBvoid pool_boundary(struct alloc_pool *\fIpool\fB, sise_t \fIsize\fB);
  43. .SH DESCRIPTION
  44. .P
  45. The pool allocation routines use
  46. .B malloc()
  47. for underlying memory management.
  48. What allocation pools do is cause memory within a given pool
  49. to be allocated in large contiguous blocks
  50. (called extents) that will be reusable when freed. Unlike
  51. .BR malloc() ,
  52. the allocations are not managed individually.
  53. Instead, each extent tracks the total free memory within the
  54. extent. Each extent can either be used to allocate memory
  55. or to manage the freeing of memory within that extent.
  56. When an extent has less free memory than a given
  57. allocation request, the current extent ceases to be used
  58. for allocation. See also the
  59. .B pool_boundary()
  60. function.
  61. .P
  62. This form of memory management is suited to large numbers of small
  63. related allocations that are held for a while
  64. and then freed as a group.
  65. Because the
  66. underlying allocations are done in large contiguous extents,
  67. when an extent is freed, it can release a large enough
  68. contiguous block of memory to allow the memory to be returned
  69. to the OS for use by whatever program needs it.
  70. You can allocate from one or more memory pools and/or
  71. .B malloc()
  72. all at the same time without interfering with how pools work.
  73. .P
  74. .B pool_create()
  75. Creates an allocation pool for subsequent calls to the pool
  76. allocation functions.
  77. When an extent is created for allocations it will be
  78. .I size
  79. bytes.
  80. Allocations from the pool have their sizes rounded up to a
  81. multiple of
  82. .I quantum
  83. bytes in length.
  84. Specifying
  85. .B 0
  86. for
  87. .I quantum
  88. will produce a quantum that should meet maximal alignment
  89. on most platforms.
  90. If
  91. .B POOL_QALIGN
  92. is set in the
  93. .IR flags ,
  94. allocations will be aligned to addresses that are a
  95. multiple of
  96. .IR quantum .
  97. If
  98. .B POOL_CLEAR
  99. is set in the
  100. .IR flags ,
  101. all allocations from the pool will be initialized to zeros.
  102. You may specify a
  103. .B NULL
  104. for the
  105. .I bomb
  106. function pointer if you don't wish to use it. (See the
  107. .B pool_alloc()
  108. function for how it is used.)
  109. .P
  110. .B pool_destroy()
  111. destroys an allocation
  112. .I pool
  113. and frees all its associated memory.
  114. .P
  115. .B pool_alloc()
  116. allocates
  117. .I size
  118. bytes from the specified
  119. .IR pool .
  120. If
  121. .I size
  122. is
  123. .BR 0 ,
  124. .I quantum
  125. bytes will be allocated.
  126. If the pool has been created with
  127. .BR POOL_QALIGN ,
  128. every chunk of memory that is returned will be suitably aligned.
  129. You can use this with the default
  130. .I quantum
  131. size to ensure that all memory can store a variable of any type.
  132. If the requested memory cannot be allocated, the
  133. .I bomb()
  134. function will be called with
  135. .I msg
  136. as its sole argument (if the function was defined at the time
  137. the pool was created), and then a
  138. .B NULL
  139. address is returned (assuming that the bomb function didn't exit).
  140. .P
  141. .B pool_free()
  142. frees
  143. .I size
  144. bytes pointed to by an
  145. .I addr
  146. that was previously allocated in the specified
  147. .IR pool .
  148. If
  149. .I size
  150. is
  151. .BR 0 ,
  152. .I quantum
  153. bytes will be freed.
  154. The memory freed within an extent will not be reusable until
  155. all of the memory in that extent has been freed with one
  156. exception: the most recent pool allocation may be freed back
  157. into the pool prior to making any further allocations.
  158. If enough free calls are made to indicate that an extent has no
  159. remaining allocated objects (as computed by the total freed size for
  160. an extent), its memory will be completely freed back to the system.
  161. If
  162. .I addr
  163. is
  164. .BR 0 ,
  165. no memory will be freed, but subsequent allocations will come
  166. from a new extent.
  167. .P
  168. .B pool_free_old()
  169. takes a boundary
  170. .I addr
  171. value that was returned by
  172. .B pool_boundary()
  173. and frees up any extents in the
  174. .I pool
  175. that have data allocated from that point backward in time.
  176. NOTE: you must NOT mix calls to both
  177. .B pool_free
  178. and
  179. .B pool_free_old
  180. on the same pool!
  181. .P
  182. .B pool_boundary()
  183. asks for a boundary value that can be sent to
  184. .B pool_free_old()
  185. at a later time to free up all memory allocated prior to a particular
  186. moment in time.
  187. If the extent that holds the boundary point has allocations from after the
  188. boundary point, it will not be freed until a future
  189. .B pool_free_old()
  190. call encompasses the entirety of the extent's data.
  191. If
  192. .I len
  193. is non-zero, the call will also check if the active extent has at least
  194. that much free memory available in it, and if not, it will mark the
  195. extent as inactive, forcing a new extent to be used for future allocations.
  196. (You can specify -1 for
  197. .I len
  198. if you want to force a new extent to start.)
  199. .P
  200. .B pool_talloc()
  201. is a macro that takes a
  202. .I type
  203. and a
  204. .I count
  205. instead of a
  206. .IR size .
  207. It casts the return value to the correct pointer type.
  208. .P
  209. .B pool_tfree
  210. is a macro that calls
  211. .B pool_free
  212. on memory that was allocated by
  213. .BR pool_talloc() .
  214. .SH RETURN VALUE
  215. .B pool_create()
  216. returns a pointer to
  217. .BR "struct alloc_pool" .
  218. .P
  219. .B pool_alloc()
  220. and
  221. .B pool_talloc()
  222. return pointers to the allocated memory,
  223. or NULL if the request fails.
  224. The return type of
  225. .B pool_alloc()
  226. will normally require casting to the desired type but
  227. .B pool_talloc()
  228. will returns a pointer of the requested
  229. .IR type .
  230. .P
  231. .B pool_boundary()
  232. returns a pointer that should only be used in a call to
  233. .BR pool_free_old() .
  234. .P
  235. .BR pool_free() ,
  236. .BR pool_free_old() ,
  237. .B pool_tfree()
  238. and
  239. .B pool_destroy()
  240. return no value.
  241. .SH SEE ALSO
  242. .nf
  243. malloc(3)
  244. .SH AUTHOR
  245. pool_alloc was created by J.W. Schultz of Pegasystems Technologies.
  246. .SH BUGS AND ISSUES