allocb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. .TH ALLOCB 9
  2. .SH NAME
  3. allocb, iallocb, freeb, freeblist, BLEN, blocklen, concatblock, copyblock, trimblock, packblock, padblock, pullblock, pullupblock, adjustblock, checkb \- data block management
  4. .SH SYNOPSIS
  5. .ta \w'\fLBlock* 'u
  6. .B
  7. Block* allocb(int size)
  8. .PP
  9. .B
  10. Block* iallocb(int size)
  11. .PP
  12. .B
  13. void freeb(Block *b)
  14. .PP
  15. .B
  16. void freeblist(Block *b)
  17. .PP
  18. .B
  19. long BLEN(Block *b)
  20. .PP
  21. .B
  22. int blocklen(Block *b)
  23. .PP
  24. .B
  25. Block* concatblock(Block *b)
  26. .PP
  27. .B
  28. Block* copyblock(Block *b, int n)
  29. .PP
  30. .B
  31. Block* trimblock(Block *b, int offset, int n)
  32. .PP
  33. .B
  34. Block* packblock(Block *b)
  35. .PP
  36. .B
  37. Block* padblock(Block *b, int n)
  38. .PP
  39. .B
  40. int pullblock(Block **bph, int n)
  41. .PP
  42. .B
  43. Block* pullupblock(Block *b, int n)
  44. .PP
  45. .B
  46. Block* adjustblock(Block *b, int n)
  47. .PP
  48. .B
  49. void checkb(Block *b, char *msg)
  50. .SH DESCRIPTION
  51. A
  52. .B Block
  53. provides a receptacle for data:
  54. .IP
  55. .EX
  56. .DT
  57. typedef
  58. struct Block
  59. {
  60. Block* next;
  61. Block* list;
  62. uchar* rp; /* first unconsumed byte */
  63. uchar* wp; /* first empty byte */
  64. uchar* lim; /* 1 past the end of the buffer */
  65. uchar* base; /* start of the buffer */
  66. void (*free)(Block*);
  67. ulong flag;
  68. } Block;
  69. .EE
  70. .PP
  71. Each
  72. .B Block
  73. has an associated buffer, located at
  74. .BR base ,
  75. and accessed via
  76. .B wp
  77. when filling the buffer, or
  78. .B rp
  79. when fetching data from it.
  80. Each pointer should be incremented to reflect the amount of data written or read.
  81. A
  82. .B Block
  83. is empty when
  84. .B rp
  85. reaches
  86. .BR wp .
  87. The pointer
  88. .B lim
  89. bounds the allocated space.
  90. Some operations described below accept lists of
  91. .BR Block s,
  92. which are
  93. chained via their
  94. .B next
  95. pointers, with a null pointer ending the list.
  96. .B Blocks
  97. are usually intended for a
  98. .B Queue
  99. (see
  100. .IR qio (9)),
  101. but can be used independently.
  102. .PP
  103. A
  104. .B Block
  105. and its buffer are normally allocated by one call to
  106. .IR malloc (9)
  107. and aligned on an 8 byte (\fLBY2V\fP) boundary.
  108. Some devices with particular allocation constraints
  109. (eg, requiring certain addresses for DMA) might allocate their own
  110. .B Block
  111. and buffer;
  112. .B free
  113. must then point to a function that can deallocate the specially allocated
  114. .BR Block .
  115. .PP
  116. Many
  117. .B Block
  118. operations cannot be used in interrupt handlers
  119. because they either
  120. .IR sleep (9)
  121. or raise an
  122. .IR error (9).
  123. Of operations that allocate blocks, only
  124. .IR iallocb
  125. is usable.
  126. .PP
  127. .I Allocb
  128. allocates a
  129. .B Block
  130. of at least
  131. .IR size
  132. bytes.
  133. The block
  134. is initially empty:
  135. .B rp
  136. and
  137. .B wp
  138. point to the start of the data.
  139. If it cannot allocate memory,
  140. .I allocb
  141. raises an
  142. .IR error (9);
  143. it cannot be used by an interrupt handler.
  144. .PP
  145. .IR Iallocb
  146. is similar to
  147. .IR allocb
  148. but is intended for use by interrupt handlers,
  149. and returns a null pointer if no memory is available.
  150. It also limits its allocation to a quota allocated at system initialisation to interrupt-time buffering.
  151. .PP
  152. .I Freeb
  153. frees a single
  154. .B Block
  155. (and its buffer).
  156. .PP
  157. .I Freeblist
  158. frees the whole
  159. list of blocks headed by
  160. .IR b .
  161. .PP
  162. .I BLEN
  163. returns the number of unread bytes in a single block
  164. .IR b ;
  165. it is implemented as a macro.
  166. .PP
  167. .I Blocklen
  168. returns the number of bytes of unread data in the whole list of blocks headed by
  169. .IR b .
  170. .PP
  171. .I Concatblock
  172. returns
  173. .I b
  174. if it is not a list, and otherwise
  175. returns a single
  176. .B Block
  177. containing all the data in the list of blocks
  178. .IR b ,
  179. which it frees.
  180. .PP
  181. .I Copyblock
  182. by contrast returns a single
  183. .B Block
  184. containing a copy of the first
  185. .I n
  186. bytes of data in the block list
  187. .IR b ,
  188. padding with zeroes if the list contained less than
  189. .I n
  190. bytes.
  191. The list
  192. .I b
  193. is unchanged.
  194. .PP
  195. .I Padblock
  196. can pad a single
  197. .B Block
  198. at either end, to reserve space for protocol headers or trailers.
  199. If
  200. .IR n ≥ 0 ,
  201. it inserts
  202. .I n
  203. bytes at the start of the block,
  204. setting the read pointer
  205. .B rp
  206. to point to the new space.
  207. If
  208. .IR n < 0 ,
  209. it adds
  210. .I n
  211. bytes at the end of the block,
  212. leaving the write pointer
  213. .B wp
  214. pointing at the new space.
  215. In both cases, it allocates a new
  216. .B Block
  217. if necessary, freeing the old, and
  218. it always returns a pointer to the resulting
  219. .BR Block .
  220. .PP
  221. .I Trimblock
  222. trims the list
  223. .I b
  224. to contain no more than
  225. .I n
  226. bytes starting at
  227. .I offset
  228. bytes into the data of the original list.
  229. It returns a new list, freeing unneeded parts of the old.
  230. If no data remains, it returns a null pointer.
  231. .PP
  232. .I Packblock
  233. examines each
  234. .B Block
  235. in the list
  236. .IR b ,
  237. reallocating any block in the list that has four times more available space than actual data.
  238. It returns a pointer to the revised list.
  239. .PP
  240. .I Pullblock
  241. discards up to
  242. .I n
  243. bytes from the start of the list headed by
  244. .BI * bph \f1.\f0
  245. Unneeded blocks are freed.
  246. .I Pullblock
  247. sets
  248. .BI * bph
  249. to point to the new list head
  250. and returns the number of bytes discarded (which might be less than
  251. .IR n ).
  252. It is used by transport protocols to discard ack'd data at
  253. the head of a retransmission queue.
  254. .PP
  255. .I Pullupblock
  256. rearranges the data in the list of blocks
  257. .I b
  258. to ensure that there are at least
  259. .I n
  260. bytes of contiguous data in the first block,
  261. and returns a pointer to the new list head.
  262. It frees any blocks that it empties.
  263. It returns a null pointer if there is not enough data in the list.
  264. .PP
  265. .I Adjustblock
  266. ensures that the block
  267. .I b
  268. has at least
  269. .I n
  270. bytes of data, reallocating or padding with zero if necessary.
  271. It returns a pointer to the new
  272. .BR Block .
  273. (If
  274. .I n
  275. is negative, it frees the block and returns a null pointer.)
  276. .PP
  277. .I Checkb
  278. does some consistency checking of
  279. the state of
  280. .IR b ;
  281. a
  282. .IR panic (9)
  283. results if things look grim.
  284. It is intended for internal use by the queue I/O routines (see
  285. .IR qio (9))
  286. but could be used elsewhere.
  287. .PP
  288. The only functions that can be called at interrupt level are
  289. .IR iallocb ,
  290. .IR freeb ,
  291. .IR freeblist ,
  292. .IR BLEN ,
  293. .IR blocklen ,
  294. .IR trimblock
  295. and
  296. .IR pullupblock .
  297. The others allocate memory and can potentially block.
  298. .SH SOURCE
  299. .B /sys/src/9/port/allocb.c
  300. .SH DIAGNOSTICS
  301. Many functions directly or indirectly can raise an
  302. .IR error (9),
  303. and callers must therefore provide for proper error recovery
  304. as described therein to prevent memory leaks and other bugs.
  305. Except for
  306. .IR iallocb ,
  307. any functions that allocate new blocks or lists
  308. are unsuitable for use by interrupt handlers.
  309. .IR Iallocb
  310. returns a null pointer when it runs out of memory.
  311. .SH SEE ALSO
  312. .IR qio (9)