alloc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. #include <unistd.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include "alloc.h"
  8. #include "bitops.h"
  9. #include "tiny.h"
  10. #include <ccan/build_assert/build_assert.h>
  11. #include <ccan/likely/likely.h>
  12. #include <ccan/alignof/alignof.h>
  13. #include <ccan/short_types/short_types.h>
  14. #include "config.h"
  15. /*
  16. Inspired by (and parts taken from) Andrew Tridgell's alloc_mmap:
  17. http://samba.org/~tridge/junkcode/alloc_mmap/
  18. Copyright (C) Andrew Tridgell 2007
  19. This library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2 of the License, or (at your option) any later version.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with this library; if not, write to the Free Software
  29. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. /* We divide the pool into this many large pages (nearest power of 2) */
  32. #define MAX_LARGE_PAGES (1024UL)
  33. /* 32 small pages == 1 large page. */
  34. #define BITS_FROM_SMALL_TO_LARGE_PAGE 5
  35. #define MAX_SMALL_PAGES (MAX_LARGE_PAGES << BITS_FROM_SMALL_TO_LARGE_PAGE)
  36. /* Smallest pool size for this scheme: 128-byte small pages. That's
  37. * 9/13% overhead for 32/64 bit. */
  38. #define MIN_USEFUL_SIZE (MAX_SMALL_PAGES * 128)
  39. /* Every 4 buckets, we jump up a power of 2. ...8 10 12 14 16 20 24 28 32... */
  40. #define INTER_BUCKET_SPACE 4
  41. #define SMALL_PAGES_PER_LARGE_PAGE (1 << BITS_FROM_SMALL_TO_LARGE_PAGE)
  42. /* FIXME: Figure this out properly. */
  43. #define MAX_SIZE (1 << 30)
  44. /* How few object to fit in a page before using a larger one? (8) */
  45. #define MAX_PAGE_OBJECT_ORDER 3
  46. #define BITS_PER_LONG (sizeof(long) * CHAR_BIT)
  47. struct bucket_state {
  48. u32 elements_per_page;
  49. u16 page_list;
  50. u16 full_list;
  51. };
  52. struct header {
  53. /* Bitmap of which pages are large. */
  54. unsigned long pagesize[MAX_LARGE_PAGES / BITS_PER_LONG];
  55. /* List of unused small/large pages. */
  56. u16 small_free_list;
  57. u16 large_free_list;
  58. /* List of huge allocs. */
  59. unsigned long huge;
  60. /* This is less defined: we have two buckets for each power of 2 */
  61. struct bucket_state bs[1];
  62. };
  63. struct huge_alloc {
  64. unsigned long next, prev;
  65. unsigned long off, len;
  66. };
  67. struct page_header {
  68. u16 next, prev;
  69. /* FIXME: We can just count all-0 and all-1 used[] elements. */
  70. unsigned elements_used : 25;
  71. unsigned bucket : 7;
  72. unsigned long used[1]; /* One bit per element. */
  73. };
  74. /*
  75. * Every 4 buckets, the size doubles.
  76. * Between buckets, sizes increase linearly.
  77. *
  78. * eg. bucket 40 = 2^10 = 1024
  79. * bucket 41 = 2^10 + 2^10*4 = 1024 + 256
  80. * bucket 42 = 2^10 + 2^10*4 = 1024 + 512
  81. * bucket 43 = 2^10 + 2^10*4 = 1024 + 768
  82. * bucket 45 = 2^11 = 2048
  83. *
  84. * Care is taken to handle low numbered buckets, at cost of overflow.
  85. */
  86. static unsigned long bucket_to_size(unsigned int bucket)
  87. {
  88. unsigned long base = 1 << (bucket / INTER_BUCKET_SPACE);
  89. return base + ((bucket % INTER_BUCKET_SPACE)
  90. << (bucket / INTER_BUCKET_SPACE))
  91. / INTER_BUCKET_SPACE;
  92. }
  93. /*
  94. * Say size is 10.
  95. * fls(size/2) == 3. 1 << 3 == 8, so we're 2 too large, out of a possible
  96. * 8 too large. That's 1/4 of the way to the next power of 2 == 1 bucket.
  97. *
  98. * We make sure we round up. Note that this fails on 32 bit at size
  99. * 1879048193 (around bucket 120).
  100. */
  101. static unsigned int size_to_bucket(unsigned long size)
  102. {
  103. unsigned int base = fls(size/2);
  104. unsigned long overshoot;
  105. overshoot = size - (1 << base);
  106. return base * INTER_BUCKET_SPACE
  107. + ((overshoot * INTER_BUCKET_SPACE + (1 << base)-1) >> base);
  108. }
  109. static unsigned int small_page_bits(unsigned long poolsize)
  110. {
  111. return fls(poolsize / MAX_SMALL_PAGES / 2);
  112. }
  113. static struct page_header *from_pgnum(struct header *head,
  114. unsigned long pgnum,
  115. unsigned sp_bits)
  116. {
  117. return (struct page_header *)((char *)head + (pgnum << sp_bits));
  118. }
  119. static u16 to_pgnum(struct header *head, void *p, unsigned sp_bits)
  120. {
  121. return ((char *)p - (char *)head) >> sp_bits;
  122. }
  123. static size_t used_size(unsigned int num_elements)
  124. {
  125. return align_up(num_elements, BITS_PER_LONG) / CHAR_BIT;
  126. }
  127. /*
  128. * We always align the first entry to the lower power of 2.
  129. * eg. the 12-byte bucket gets 8-byte aligned. The 4096-byte bucket
  130. * gets 4096-byte aligned.
  131. */
  132. static unsigned long page_header_size(unsigned int align_bits,
  133. unsigned long num_elements)
  134. {
  135. unsigned long size;
  136. size = sizeof(struct page_header)
  137. - sizeof(((struct page_header *)0)->used)
  138. + used_size(num_elements);
  139. return align_up(size, 1 << align_bits);
  140. }
  141. static void add_to_list(struct header *head,
  142. u16 *list, struct page_header *ph, unsigned sp_bits)
  143. {
  144. unsigned long h = *list, offset = to_pgnum(head, ph, sp_bits);
  145. ph->next = h;
  146. if (h) {
  147. struct page_header *prev = from_pgnum(head, h, sp_bits);
  148. assert(prev->prev == 0);
  149. prev->prev = offset;
  150. }
  151. *list = offset;
  152. ph->prev = 0;
  153. }
  154. static void del_from_list(struct header *head,
  155. u16 *list, struct page_header *ph, unsigned sp_bits)
  156. {
  157. /* Front of list? */
  158. if (ph->prev == 0) {
  159. *list = ph->next;
  160. } else {
  161. struct page_header *prev = from_pgnum(head, ph->prev, sp_bits);
  162. prev->next = ph->next;
  163. }
  164. if (ph->next != 0) {
  165. struct page_header *next = from_pgnum(head, ph->next, sp_bits);
  166. next->prev = ph->prev;
  167. }
  168. }
  169. static u16 pop_from_list(struct header *head,
  170. u16 *list,
  171. unsigned int sp_bits)
  172. {
  173. u16 h = *list;
  174. struct page_header *ph = from_pgnum(head, h, sp_bits);
  175. if (likely(h)) {
  176. *list = ph->next;
  177. if (*list)
  178. from_pgnum(head, *list, sp_bits)->prev = 0;
  179. }
  180. return h;
  181. }
  182. static void add_to_huge_list(struct header *head, struct huge_alloc *ha)
  183. {
  184. unsigned long h = head->huge;
  185. unsigned long offset = (char *)ha - (char *)head;
  186. ha->next = h;
  187. if (h) {
  188. struct huge_alloc *prev = (void *)((char *)head + h);
  189. assert(prev->prev == 0);
  190. prev->prev = offset;
  191. }
  192. head->huge = offset;
  193. ha->prev = 0;
  194. }
  195. static void del_from_huge(struct header *head, struct huge_alloc *ha)
  196. {
  197. /* Front of list? */
  198. if (ha->prev == 0) {
  199. head->huge = ha->next;
  200. } else {
  201. struct huge_alloc *prev = (void *)((char *)head + ha->prev);
  202. prev->next = ha->next;
  203. }
  204. if (ha->next != 0) {
  205. struct huge_alloc *next = (void *)((char *)head + ha->next);
  206. next->prev = ha->prev;
  207. }
  208. }
  209. static void add_small_page_to_freelist(struct header *head,
  210. struct page_header *ph,
  211. unsigned int sp_bits)
  212. {
  213. add_to_list(head, &head->small_free_list, ph, sp_bits);
  214. }
  215. static void add_large_page_to_freelist(struct header *head,
  216. struct page_header *ph,
  217. unsigned int sp_bits)
  218. {
  219. add_to_list(head, &head->large_free_list, ph, sp_bits);
  220. }
  221. static void add_to_bucket_list(struct header *head,
  222. struct bucket_state *bs,
  223. struct page_header *ph,
  224. unsigned int sp_bits)
  225. {
  226. add_to_list(head, &bs->page_list, ph, sp_bits);
  227. }
  228. static void del_from_bucket_list(struct header *head,
  229. struct bucket_state *bs,
  230. struct page_header *ph,
  231. unsigned int sp_bits)
  232. {
  233. del_from_list(head, &bs->page_list, ph, sp_bits);
  234. }
  235. static void del_from_bucket_full_list(struct header *head,
  236. struct bucket_state *bs,
  237. struct page_header *ph,
  238. unsigned int sp_bits)
  239. {
  240. del_from_list(head, &bs->full_list, ph, sp_bits);
  241. }
  242. static void add_to_bucket_full_list(struct header *head,
  243. struct bucket_state *bs,
  244. struct page_header *ph,
  245. unsigned int sp_bits)
  246. {
  247. add_to_list(head, &bs->full_list, ph, sp_bits);
  248. }
  249. static void clear_bit(unsigned long bitmap[], unsigned int off)
  250. {
  251. bitmap[off / BITS_PER_LONG] &= ~(1 << (off % BITS_PER_LONG));
  252. }
  253. static bool test_bit(const unsigned long bitmap[], unsigned int off)
  254. {
  255. return bitmap[off / BITS_PER_LONG] & (1 << (off % BITS_PER_LONG));
  256. }
  257. static void set_bit(unsigned long bitmap[], unsigned int off)
  258. {
  259. bitmap[off / BITS_PER_LONG] |= (1 << (off % BITS_PER_LONG));
  260. }
  261. /* There must be a bit to be found. */
  262. static unsigned int find_free_bit(const unsigned long bitmap[])
  263. {
  264. unsigned int i;
  265. for (i = 0; bitmap[i] == -1UL; i++);
  266. return (i*BITS_PER_LONG) + ffsl(~bitmap[i]) - 1;
  267. }
  268. /* How many elements can we fit in a page? */
  269. static unsigned long elements_per_page(unsigned long align_bits,
  270. unsigned long esize,
  271. unsigned long psize)
  272. {
  273. unsigned long num, overhead;
  274. /* First approximation: no extra room for bitmap. */
  275. overhead = align_up(sizeof(struct page_header), 1 << align_bits);
  276. num = (psize - overhead) / esize;
  277. while (page_header_size(align_bits, num) + esize * num > psize)
  278. num--;
  279. return num;
  280. }
  281. static bool large_page_bucket(unsigned int bucket, unsigned int sp_bits)
  282. {
  283. unsigned long max_smallsize;
  284. /* Note: this doesn't take into account page header. */
  285. max_smallsize = (1UL << sp_bits) >> MAX_PAGE_OBJECT_ORDER;
  286. return bucket_to_size(bucket) > max_smallsize;
  287. }
  288. static unsigned int max_bucket(unsigned int lp_bits)
  289. {
  290. return (lp_bits - MAX_PAGE_OBJECT_ORDER) * INTER_BUCKET_SPACE;
  291. }
  292. void alloc_init(void *pool, unsigned long poolsize)
  293. {
  294. struct header *head = pool;
  295. struct page_header *ph;
  296. unsigned int lp_bits, sp_bits, num_buckets;
  297. unsigned long header_size, i;
  298. if (poolsize < MIN_USEFUL_SIZE) {
  299. tiny_alloc_init(pool, poolsize);
  300. return;
  301. }
  302. /* We rely on page numbers fitting in 16 bit. */
  303. BUILD_ASSERT(MAX_SMALL_PAGES < 65536);
  304. sp_bits = small_page_bits(poolsize);
  305. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  306. num_buckets = max_bucket(lp_bits);
  307. head = pool;
  308. header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
  309. memset(head, 0, header_size);
  310. for (i = 0; i < num_buckets; i++) {
  311. unsigned long pagesize;
  312. if (large_page_bucket(i, sp_bits))
  313. pagesize = 1UL << lp_bits;
  314. else
  315. pagesize = 1UL << sp_bits;
  316. head->bs[i].elements_per_page
  317. = elements_per_page(i / INTER_BUCKET_SPACE,
  318. bucket_to_size(i),
  319. pagesize);
  320. }
  321. /* They start as all large pages. */
  322. memset(head->pagesize, 0xFF, sizeof(head->pagesize));
  323. /* FIXME: small pages for last bit? */
  324. /* Split first page into small pages. */
  325. assert(header_size < (1UL << lp_bits));
  326. clear_bit(head->pagesize, 0);
  327. /* Skip over page(s) used by header, add rest to free list */
  328. for (i = align_up(header_size, (1 << sp_bits)) >> sp_bits;
  329. i < SMALL_PAGES_PER_LARGE_PAGE;
  330. i++) {
  331. ph = from_pgnum(head, i, sp_bits);
  332. ph->elements_used = 0;
  333. add_small_page_to_freelist(head, ph, sp_bits);
  334. }
  335. /* Add the rest of the pages as large pages. */
  336. i = SMALL_PAGES_PER_LARGE_PAGE;
  337. while ((i << sp_bits) + (1 << lp_bits) <= poolsize) {
  338. ph = from_pgnum(head, i, sp_bits);
  339. ph->elements_used = 0;
  340. add_large_page_to_freelist(head, ph, sp_bits);
  341. i += SMALL_PAGES_PER_LARGE_PAGE;
  342. }
  343. }
  344. /* A large page worth of small pages are free: delete them from free list. */
  345. static void del_large_from_small_free_list(struct header *head,
  346. struct page_header *ph,
  347. unsigned int sp_bits)
  348. {
  349. unsigned long i;
  350. for (i = 0; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
  351. del_from_list(head, &head->small_free_list,
  352. (void *)ph + (i << sp_bits),
  353. sp_bits);
  354. }
  355. }
  356. static bool all_empty(struct header *head,
  357. unsigned long pgnum,
  358. unsigned sp_bits)
  359. {
  360. unsigned long i;
  361. for (i = 0; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
  362. struct page_header *ph = from_pgnum(head, pgnum + i, sp_bits);
  363. if (ph->elements_used)
  364. return false;
  365. }
  366. return true;
  367. }
  368. static void recombine_small_pages(struct header *head, unsigned long poolsize,
  369. unsigned int sp_bits)
  370. {
  371. unsigned long i;
  372. unsigned int lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  373. /* Look for small pages to coalesce, after first large page. */
  374. for (i = SMALL_PAGES_PER_LARGE_PAGE;
  375. i < (poolsize >> lp_bits) << BITS_FROM_SMALL_TO_LARGE_PAGE;
  376. i += SMALL_PAGES_PER_LARGE_PAGE) {
  377. /* Already a large page? */
  378. if (test_bit(head->pagesize, i / SMALL_PAGES_PER_LARGE_PAGE))
  379. continue;
  380. if (all_empty(head, i, sp_bits)) {
  381. struct page_header *ph = from_pgnum(head, i, sp_bits);
  382. set_bit(head->pagesize,
  383. i / SMALL_PAGES_PER_LARGE_PAGE);
  384. del_large_from_small_free_list(head, ph, sp_bits);
  385. add_large_page_to_freelist(head, ph, sp_bits);
  386. }
  387. }
  388. }
  389. static u16 get_large_page(struct header *head, unsigned long poolsize,
  390. unsigned int sp_bits)
  391. {
  392. unsigned int lp_bits, page;
  393. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  394. page = pop_from_list(head, &head->large_free_list, sp_bits);
  395. if (likely(page))
  396. return page;
  397. recombine_small_pages(head, poolsize, sp_bits);
  398. return pop_from_list(head, &head->large_free_list, sp_bits);
  399. }
  400. /* Returns small page. */
  401. static unsigned long break_up_large_page(struct header *head,
  402. unsigned int sp_bits,
  403. u16 lpage)
  404. {
  405. unsigned int i;
  406. clear_bit(head->pagesize, lpage >> BITS_FROM_SMALL_TO_LARGE_PAGE);
  407. for (i = 1; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
  408. struct page_header *ph = from_pgnum(head, lpage + i, sp_bits);
  409. add_small_page_to_freelist(head, ph, sp_bits);
  410. }
  411. return lpage;
  412. }
  413. static u16 get_small_page(struct header *head, unsigned long poolsize,
  414. unsigned int sp_bits)
  415. {
  416. u16 ret;
  417. ret = pop_from_list(head, &head->small_free_list, sp_bits);
  418. if (likely(ret))
  419. return ret;
  420. ret = get_large_page(head, poolsize, sp_bits);
  421. if (likely(ret))
  422. ret = break_up_large_page(head, sp_bits, ret);
  423. return ret;
  424. }
  425. void where_is_page(struct header *head, struct page_header *where,
  426. unsigned int sp_bits)
  427. {
  428. struct page_header *pg;
  429. unsigned long off, bucket,
  430. num_buckets = max_bucket(sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE);
  431. for (off = head->small_free_list; off; off = pg->next) {
  432. pg = from_pgnum(head, off, sp_bits);
  433. if (pg == where) {
  434. printf("It's in the small free list\n");
  435. return;
  436. }
  437. }
  438. for (off = head->large_free_list; off; off = pg->next) {
  439. pg = from_pgnum(head, off, sp_bits);
  440. if (pg == where) {
  441. printf("It's in the large free list\n");
  442. return;
  443. }
  444. }
  445. for (bucket = 0; bucket < num_buckets; bucket++) {
  446. for (off = head->bs[bucket].page_list; off; off = pg->next) {
  447. pg = from_pgnum(head, off, sp_bits);
  448. if (pg == where) {
  449. printf("It's in %lu bucket page list\n", bucket);
  450. return;
  451. }
  452. }
  453. for (off = head->bs[bucket].full_list; off; off = pg->next) {
  454. pg = from_pgnum(head, off, sp_bits);
  455. if (pg == where) {
  456. printf("It's in %lu bucket full list\n", bucket);
  457. return;
  458. }
  459. }
  460. }
  461. printf("It's nowhere!\n");
  462. }
  463. static bool huge_allocated(struct header *head, unsigned long offset)
  464. {
  465. unsigned long i;
  466. struct huge_alloc *ha;
  467. for (i = head->huge; i; i = ha->next) {
  468. ha = (void *)((char *)head + i);
  469. if (ha->off <= offset && ha->off + ha->len > offset)
  470. return true;
  471. }
  472. return false;
  473. }
  474. /* They want something really big. Aim for contiguous pages (slow). */
  475. static void *unlikely_func huge_alloc(void *pool, unsigned long poolsize,
  476. unsigned long size, unsigned long align)
  477. {
  478. struct header *head = pool;
  479. struct huge_alloc *ha;
  480. unsigned long i, sp_bits, lp_bits, num, header_size;
  481. sp_bits = small_page_bits(poolsize);
  482. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  483. /* Allocate tracking structure optimistically. */
  484. ha = alloc_get(pool, poolsize, sizeof(*ha), ALIGNOF(*ha));
  485. if (!ha)
  486. return NULL;
  487. /* First search for contiguous small pages... */
  488. header_size = sizeof(*head) + sizeof(head->bs) * (max_bucket(lp_bits)-1);
  489. num = 0;
  490. for (i = (header_size + (1 << sp_bits) - 1) >> sp_bits;
  491. i << sp_bits < poolsize;
  492. i++) {
  493. struct page_header *pg;
  494. unsigned long off = (i << sp_bits);
  495. /* Skip over large pages. */
  496. if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
  497. i += (1 << BITS_FROM_SMALL_TO_LARGE_PAGE)-1;
  498. continue;
  499. }
  500. /* Does this page meet alignment requirements? */
  501. if (!num && off % align != 0)
  502. continue;
  503. /* FIXME: This makes us O(n^2). */
  504. if (huge_allocated(head, off)) {
  505. num = 0;
  506. continue;
  507. }
  508. pg = (struct page_header *)((char *)head + off);
  509. if (pg->elements_used) {
  510. num = 0;
  511. continue;
  512. }
  513. num++;
  514. if (num << sp_bits >= size) {
  515. unsigned long pgnum;
  516. /* Remove from free list. */
  517. for (pgnum = i; pgnum > i - num; pgnum--) {
  518. pg = from_pgnum(head, pgnum, sp_bits);
  519. del_from_list(head,
  520. &head->small_free_list,
  521. pg, sp_bits);
  522. }
  523. ha->off = (i - num + 1) << sp_bits;
  524. ha->len = num << sp_bits;
  525. goto done;
  526. }
  527. }
  528. /* Now search for large pages... */
  529. recombine_small_pages(head, poolsize, sp_bits);
  530. num = 0;
  531. for (i = (header_size + (1 << lp_bits) - 1) >> lp_bits;
  532. (i << lp_bits) < poolsize; i++) {
  533. struct page_header *pg;
  534. unsigned long off = (i << lp_bits);
  535. /* Ignore small pages. */
  536. if (!test_bit(head->pagesize, i))
  537. continue;
  538. /* Does this page meet alignment requirements? */
  539. if (!num && off % align != 0)
  540. continue;
  541. /* FIXME: This makes us O(n^2). */
  542. if (huge_allocated(head, off)) {
  543. num = 0;
  544. continue;
  545. }
  546. pg = (struct page_header *)((char *)head + off);
  547. if (pg->elements_used) {
  548. num = 0;
  549. continue;
  550. }
  551. num++;
  552. if (num << lp_bits >= size) {
  553. unsigned long pgnum;
  554. /* Remove from free list. */
  555. for (pgnum = i; pgnum > i - num; pgnum--) {
  556. pg = from_pgnum(head, pgnum, lp_bits);
  557. del_from_list(head,
  558. &head->large_free_list,
  559. pg, sp_bits);
  560. }
  561. ha->off = (i - num + 1) << lp_bits;
  562. ha->len = num << lp_bits;
  563. goto done;
  564. }
  565. }
  566. /* Unable to satisfy: free huge alloc structure. */
  567. alloc_free(pool, poolsize, ha);
  568. return NULL;
  569. done:
  570. add_to_huge_list(pool, ha);
  571. return (char *)pool + ha->off;
  572. }
  573. static void unlikely_func huge_free(struct header *head,
  574. unsigned long poolsize, void *free)
  575. {
  576. unsigned long i, off, pgnum, free_off = (char *)free - (char *)head;
  577. unsigned int sp_bits, lp_bits;
  578. struct huge_alloc *ha;
  579. for (i = head->huge; i; i = ha->next) {
  580. ha = (void *)((char *)head + i);
  581. if (free_off == ha->off)
  582. break;
  583. }
  584. assert(i);
  585. /* Free up all the pages, delete and free ha */
  586. sp_bits = small_page_bits(poolsize);
  587. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  588. pgnum = free_off >> sp_bits;
  589. if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
  590. for (off = ha->off; off < ha->off + ha->len; off += 1 << lp_bits) {
  591. add_large_page_to_freelist(head,
  592. (void *)((char *)head + off),
  593. sp_bits);
  594. }
  595. } else {
  596. for (off = ha->off; off < ha->off + ha->len; off += 1 << sp_bits) {
  597. add_small_page_to_freelist(head,
  598. (void *)((char *)head + off),
  599. sp_bits);
  600. }
  601. }
  602. del_from_huge(head, ha);
  603. alloc_free(head, poolsize, ha);
  604. }
  605. static unsigned long unlikely_func huge_size(struct header *head, void *p)
  606. {
  607. unsigned long i, off = (char *)p - (char *)head;
  608. struct huge_alloc *ha;
  609. for (i = head->huge; i; i = ha->next) {
  610. ha = (void *)((char *)head + i);
  611. if (off == ha->off) {
  612. return ha->len;
  613. }
  614. }
  615. abort();
  616. }
  617. void *alloc_get(void *pool, unsigned long poolsize,
  618. unsigned long size, unsigned long align)
  619. {
  620. struct header *head = pool;
  621. unsigned int bucket;
  622. unsigned long i;
  623. struct bucket_state *bs;
  624. struct page_header *ph;
  625. unsigned int sp_bits;
  626. if (poolsize < MIN_USEFUL_SIZE) {
  627. return tiny_alloc_get(pool, poolsize, size, align);
  628. }
  629. size = align_up(size, align);
  630. if (unlikely(!size))
  631. size = 1;
  632. bucket = size_to_bucket(size);
  633. sp_bits = small_page_bits(poolsize);
  634. if (bucket >= max_bucket(sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE)) {
  635. return huge_alloc(pool, poolsize, size, align);
  636. }
  637. bs = &head->bs[bucket];
  638. if (!bs->page_list) {
  639. struct page_header *ph;
  640. if (large_page_bucket(bucket, sp_bits))
  641. bs->page_list = get_large_page(head, poolsize,
  642. sp_bits);
  643. else
  644. bs->page_list = get_small_page(head, poolsize,
  645. sp_bits);
  646. /* FIXME: Try large-aligned alloc? Header stuffing? */
  647. if (unlikely(!bs->page_list))
  648. return NULL;
  649. ph = from_pgnum(head, bs->page_list, sp_bits);
  650. ph->bucket = bucket;
  651. ph->elements_used = 0;
  652. ph->next = 0;
  653. memset(ph->used, 0, used_size(bs->elements_per_page));
  654. }
  655. ph = from_pgnum(head, bs->page_list, sp_bits);
  656. i = find_free_bit(ph->used);
  657. set_bit(ph->used, i);
  658. ph->elements_used++;
  659. /* check if this page is now full */
  660. if (unlikely(ph->elements_used == bs->elements_per_page)) {
  661. del_from_bucket_list(head, bs, ph, sp_bits);
  662. add_to_bucket_full_list(head, bs, ph, sp_bits);
  663. }
  664. return (char *)ph + page_header_size(ph->bucket / INTER_BUCKET_SPACE,
  665. bs->elements_per_page)
  666. + i * bucket_to_size(bucket);
  667. }
  668. void alloc_free(void *pool, unsigned long poolsize, void *free)
  669. {
  670. struct header *head = pool;
  671. struct bucket_state *bs;
  672. unsigned int sp_bits;
  673. unsigned long i, pgnum, pgoffset, offset = (char *)free - (char *)pool;
  674. bool smallpage;
  675. struct page_header *ph;
  676. if (poolsize < MIN_USEFUL_SIZE) {
  677. return tiny_alloc_free(pool, poolsize, free);
  678. }
  679. /* Get page header. */
  680. sp_bits = small_page_bits(poolsize);
  681. pgnum = offset >> sp_bits;
  682. /* Big page? Round down further. */
  683. if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
  684. smallpage = false;
  685. pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
  686. } else
  687. smallpage = true;
  688. /* Step back to page header. */
  689. ph = from_pgnum(head, pgnum, sp_bits);
  690. if ((void *)ph == free) {
  691. huge_free(head, poolsize, free);
  692. return;
  693. }
  694. bs = &head->bs[ph->bucket];
  695. pgoffset = offset - (pgnum << sp_bits)
  696. - page_header_size(ph->bucket / INTER_BUCKET_SPACE,
  697. bs->elements_per_page);
  698. if (unlikely(ph->elements_used == bs->elements_per_page)) {
  699. del_from_bucket_full_list(head, bs, ph, sp_bits);
  700. add_to_bucket_list(head, bs, ph, sp_bits);
  701. }
  702. /* Which element are we? */
  703. i = pgoffset / bucket_to_size(ph->bucket);
  704. clear_bit(ph->used, i);
  705. ph->elements_used--;
  706. if (unlikely(ph->elements_used == 0)) {
  707. bs = &head->bs[ph->bucket];
  708. del_from_bucket_list(head, bs, ph, sp_bits);
  709. if (smallpage)
  710. add_small_page_to_freelist(head, ph, sp_bits);
  711. else
  712. add_large_page_to_freelist(head, ph, sp_bits);
  713. }
  714. }
  715. unsigned long alloc_size(void *pool, unsigned long poolsize, void *p)
  716. {
  717. struct header *head = pool;
  718. unsigned int pgnum, sp_bits;
  719. unsigned long offset = (char *)p - (char *)pool;
  720. struct page_header *ph;
  721. if (poolsize < MIN_USEFUL_SIZE)
  722. return tiny_alloc_size(pool, poolsize, p);
  723. /* Get page header. */
  724. sp_bits = small_page_bits(poolsize);
  725. pgnum = offset >> sp_bits;
  726. /* Big page? Round down further. */
  727. if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE))
  728. pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
  729. /* Step back to page header. */
  730. ph = from_pgnum(head, pgnum, sp_bits);
  731. if ((void *)ph == p)
  732. return huge_size(head, p);
  733. return bucket_to_size(ph->bucket);
  734. }
  735. /* Useful for gdb breakpoints. */
  736. static bool check_fail(void)
  737. {
  738. return false;
  739. }
  740. static unsigned long count_bits(const unsigned long bitmap[],
  741. unsigned long limit)
  742. {
  743. unsigned long i, count = 0;
  744. while (limit >= BITS_PER_LONG) {
  745. count += popcount(bitmap[0]);
  746. bitmap++;
  747. limit -= BITS_PER_LONG;
  748. }
  749. for (i = 0; i < limit; i++)
  750. if (test_bit(bitmap, i))
  751. count++;
  752. return count;
  753. }
  754. static bool out_of_bounds(unsigned long pgnum,
  755. unsigned int sp_bits,
  756. unsigned long pagesize,
  757. unsigned long poolsize)
  758. {
  759. if (((pgnum << sp_bits) >> sp_bits) != pgnum)
  760. return true;
  761. if ((pgnum << sp_bits) > poolsize)
  762. return true;
  763. return ((pgnum << sp_bits) + pagesize > poolsize);
  764. }
  765. static bool check_bucket(struct header *head,
  766. unsigned long poolsize,
  767. unsigned long pages[],
  768. struct bucket_state *bs,
  769. unsigned int bindex)
  770. {
  771. bool lp_bucket;
  772. struct page_header *ph;
  773. unsigned long taken, i, prev, pagesize, sp_bits, lp_bits;
  774. sp_bits = small_page_bits(poolsize);
  775. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  776. lp_bucket = large_page_bucket(bindex, sp_bits);
  777. pagesize = 1UL << (lp_bucket ? lp_bits : sp_bits);
  778. /* This many elements fit? */
  779. taken = page_header_size(bindex / INTER_BUCKET_SPACE,
  780. bs->elements_per_page);
  781. taken += bucket_to_size(bindex) * bs->elements_per_page;
  782. if (taken > pagesize)
  783. return check_fail();
  784. /* One more wouldn't fit? */
  785. taken = page_header_size(bindex / INTER_BUCKET_SPACE,
  786. bs->elements_per_page + 1);
  787. taken += bucket_to_size(bindex) * (bs->elements_per_page + 1);
  788. if (taken <= pagesize)
  789. return check_fail();
  790. /* Walk used list. */
  791. prev = 0;
  792. for (i = bs->page_list; i; i = ph->next) {
  793. /* Bad pointer? */
  794. if (out_of_bounds(i, sp_bits, pagesize, poolsize))
  795. return check_fail();
  796. /* Wrong size page? */
  797. if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
  798. != lp_bucket)
  799. return check_fail();
  800. /* Large page not on boundary? */
  801. if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
  802. return check_fail();
  803. ph = from_pgnum(head, i, sp_bits);
  804. /* Linked list corrupt? */
  805. if (ph->prev != prev)
  806. return check_fail();
  807. /* Already seen this page? */
  808. if (test_bit(pages, i))
  809. return check_fail();
  810. set_bit(pages, i);
  811. /* Empty or full? */
  812. if (ph->elements_used == 0)
  813. return check_fail();
  814. if (ph->elements_used >= bs->elements_per_page)
  815. return check_fail();
  816. /* Used bits don't agree? */
  817. if (ph->elements_used != count_bits(ph->used,
  818. bs->elements_per_page))
  819. return check_fail();
  820. /* Wrong bucket? */
  821. if (ph->bucket != bindex)
  822. return check_fail();
  823. prev = i;
  824. }
  825. /* Walk full list. */
  826. prev = 0;
  827. for (i = bs->full_list; i; i = ph->next) {
  828. /* Bad pointer? */
  829. if (out_of_bounds(i, sp_bits, pagesize, poolsize))
  830. return check_fail();
  831. /* Wrong size page? */
  832. if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
  833. != lp_bucket)
  834. /* Large page not on boundary? */
  835. if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
  836. return check_fail();
  837. ph = from_pgnum(head, i, sp_bits);
  838. /* Linked list corrupt? */
  839. if (ph->prev != prev)
  840. return check_fail();
  841. /* Already seen this page? */
  842. if (test_bit(pages, i))
  843. return check_fail();
  844. set_bit(pages, i);
  845. /* Not full? */
  846. if (ph->elements_used != bs->elements_per_page)
  847. return check_fail();
  848. /* Used bits don't agree? */
  849. if (ph->elements_used != count_bits(ph->used,
  850. bs->elements_per_page))
  851. return check_fail();
  852. /* Wrong bucket? */
  853. if (ph->bucket != bindex)
  854. return check_fail();
  855. prev = i;
  856. }
  857. return true;
  858. }
  859. bool alloc_check(void *pool, unsigned long poolsize)
  860. {
  861. struct header *head = pool;
  862. unsigned long prev, i, lp_bits, sp_bits, header_size, num_buckets;
  863. struct page_header *ph;
  864. struct huge_alloc *ha;
  865. unsigned long pages[MAX_SMALL_PAGES / BITS_PER_LONG] = { 0 };
  866. if (poolsize < MIN_USEFUL_SIZE)
  867. return tiny_alloc_check(pool, poolsize);
  868. sp_bits = small_page_bits(poolsize);
  869. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  870. num_buckets = max_bucket(lp_bits);
  871. header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
  872. /* First, set all bits taken by header. */
  873. for (i = 0; i < header_size; i += (1UL << sp_bits))
  874. set_bit(pages, i >> sp_bits);
  875. /* Check small page free list. */
  876. prev = 0;
  877. for (i = head->small_free_list; i; i = ph->next) {
  878. /* Bad pointer? */
  879. if (out_of_bounds(i, sp_bits, 1 << sp_bits, poolsize))
  880. return check_fail();
  881. /* Large page? */
  882. if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
  883. return check_fail();
  884. ph = from_pgnum(head, i, sp_bits);
  885. /* Linked list corrupt? */
  886. if (ph->prev != prev)
  887. return check_fail();
  888. /* Already seen this page? */
  889. if (test_bit(pages, i))
  890. return check_fail();
  891. set_bit(pages, i);
  892. prev = i;
  893. }
  894. /* Check large page free list. */
  895. prev = 0;
  896. for (i = head->large_free_list; i; i = ph->next) {
  897. /* Bad pointer? */
  898. if (out_of_bounds(i, sp_bits, 1 << lp_bits, poolsize))
  899. return check_fail();
  900. /* Not large page? */
  901. if (!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
  902. return check_fail();
  903. /* Not page boundary? */
  904. if ((i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
  905. return check_fail();
  906. ph = from_pgnum(head, i, sp_bits);
  907. /* Linked list corrupt? */
  908. if (ph->prev != prev)
  909. return check_fail();
  910. /* Already seen this page? */
  911. if (test_bit(pages, i))
  912. return check_fail();
  913. set_bit(pages, i);
  914. prev = i;
  915. }
  916. /* Check the buckets. */
  917. for (i = 0; i < max_bucket(lp_bits); i++) {
  918. struct bucket_state *bs = &head->bs[i];
  919. if (!check_bucket(head, poolsize, pages, bs, i))
  920. return false;
  921. }
  922. /* Check the huge alloc list. */
  923. prev = 0;
  924. for (i = head->huge; i; i = ha->next) {
  925. unsigned long pgbits, j;
  926. /* Bad pointer? */
  927. if (i >= poolsize || i + sizeof(*ha) > poolsize)
  928. return check_fail();
  929. ha = (void *)((char *)head + i);
  930. /* Check contents of ha. */
  931. if (ha->off > poolsize || ha->off + ha->len > poolsize)
  932. return check_fail();
  933. /* Large or small page? */
  934. pgbits = test_bit(head->pagesize, ha->off >> lp_bits)
  935. ? lp_bits : sp_bits;
  936. /* Not page boundary? */
  937. if ((ha->off % (1UL << pgbits)) != 0)
  938. return check_fail();
  939. /* Not page length? */
  940. if ((ha->len % (1UL << pgbits)) != 0)
  941. return check_fail();
  942. /* Linked list corrupt? */
  943. if (ha->prev != prev)
  944. return check_fail();
  945. for (j = ha->off; j < ha->off + ha->len; j += (1 << sp_bits)) {
  946. /* Already seen this page? */
  947. if (test_bit(pages, j >> sp_bits))
  948. return check_fail();
  949. set_bit(pages, j >> sp_bits);
  950. }
  951. prev = i;
  952. }
  953. /* Make sure every page accounted for. */
  954. for (i = 0; i < poolsize >> sp_bits; i++) {
  955. if (!test_bit(pages, i))
  956. return check_fail();
  957. if (test_bit(head->pagesize,
  958. i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
  959. /* Large page, skip rest. */
  960. i += SMALL_PAGES_PER_LARGE_PAGE - 1;
  961. }
  962. }
  963. return true;
  964. }
  965. static unsigned long print_overhead(FILE *out, const char *desc,
  966. unsigned long bytes,
  967. unsigned long poolsize)
  968. {
  969. fprintf(out, "Overhead (%s): %lu bytes (%.3g%%)\n",
  970. desc, bytes, 100.0 * bytes / poolsize);
  971. return bytes;
  972. }
  973. static unsigned long count_list(struct header *head,
  974. u16 pgnum,
  975. unsigned int sp_bits,
  976. unsigned long *total_elems)
  977. {
  978. struct page_header *p;
  979. unsigned long ret = 0;
  980. while (pgnum) {
  981. p = from_pgnum(head, pgnum, sp_bits);
  982. if (total_elems)
  983. (*total_elems) += p->elements_used;
  984. ret++;
  985. pgnum = p->next;
  986. }
  987. return ret;
  988. }
  989. static unsigned long visualize_bucket(FILE *out, struct header *head,
  990. unsigned int bucket,
  991. unsigned long poolsize,
  992. unsigned int sp_bits)
  993. {
  994. unsigned long num_full, num_partial, num_pages, page_size,
  995. elems, hdr_min, hdr_size, elems_per_page, overhead = 0;
  996. elems_per_page = head->bs[bucket].elements_per_page;
  997. /* If we used byte-based bitmaps, we could get pg hdr to: */
  998. hdr_min = sizeof(struct page_header)
  999. - sizeof(((struct page_header *)0)->used)
  1000. + align_up(elems_per_page, CHAR_BIT) / CHAR_BIT;
  1001. hdr_size = page_header_size(bucket / INTER_BUCKET_SPACE,
  1002. elems_per_page);
  1003. elems = 0;
  1004. num_full = count_list(head, head->bs[bucket].full_list, sp_bits,
  1005. &elems);
  1006. num_partial = count_list(head, head->bs[bucket].page_list, sp_bits,
  1007. &elems);
  1008. num_pages = num_full + num_partial;
  1009. if (!num_pages)
  1010. return 0;
  1011. fprintf(out, "Bucket %u (%lu bytes):"
  1012. " %lu full, %lu partial = %lu elements\n",
  1013. bucket, bucket_to_size(bucket), num_full, num_partial, elems);
  1014. /* Strict requirement of page header size. */
  1015. overhead += print_overhead(out, "page headers",
  1016. hdr_min * num_pages, poolsize);
  1017. /* Gap between minimal page header and actual start. */
  1018. overhead += print_overhead(out, "page post-header alignments",
  1019. (hdr_size - hdr_min) * num_pages, poolsize);
  1020. /* Between last element and end of page. */
  1021. page_size = (1 << sp_bits);
  1022. if (large_page_bucket(bucket, sp_bits))
  1023. page_size <<= BITS_FROM_SMALL_TO_LARGE_PAGE;
  1024. overhead += print_overhead(out, "page tails",
  1025. (page_size - (hdr_size
  1026. + (elems_per_page
  1027. * bucket_to_size(bucket))))
  1028. * num_pages, poolsize);
  1029. return overhead;
  1030. }
  1031. void alloc_visualize(FILE *out, void *pool, unsigned long poolsize)
  1032. {
  1033. struct header *head = pool;
  1034. unsigned long i, lp_bits, sp_bits, header_size, num_buckets, count,
  1035. overhead = 0;
  1036. fprintf(out, "Pool %p size %lu: (%s allocator)\n", pool, poolsize,
  1037. poolsize < MIN_USEFUL_SIZE ? "tiny" : "standard");
  1038. if (poolsize < MIN_USEFUL_SIZE) {
  1039. tiny_alloc_visualize(out, pool, poolsize);
  1040. return;
  1041. }
  1042. sp_bits = small_page_bits(poolsize);
  1043. lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
  1044. num_buckets = max_bucket(lp_bits);
  1045. header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
  1046. fprintf(out, "Large page size %lu, small page size %lu.\n",
  1047. 1UL << lp_bits, 1UL << sp_bits);
  1048. overhead += print_overhead(out, "unused pool tail",
  1049. poolsize % (1 << lp_bits), poolsize);
  1050. fprintf(out, "Main header %lu bytes (%lu small pages).\n",
  1051. header_size, align_up(header_size, 1 << sp_bits) >> sp_bits);
  1052. overhead += print_overhead(out, "partial header page",
  1053. align_up(header_size, 1 << sp_bits)
  1054. - header_size, poolsize);
  1055. /* Total large pages. */
  1056. i = count_bits(head->pagesize, poolsize >> lp_bits);
  1057. /* Used pages. */
  1058. count = i - count_list(head, head->large_free_list, sp_bits, NULL);
  1059. fprintf(out, "%lu/%lu large pages used (%.3g%%)\n",
  1060. count, i, count ? 100.0 * count / i : 0.0);
  1061. /* Total small pages. */
  1062. i = ((poolsize >> lp_bits) - i) << BITS_FROM_SMALL_TO_LARGE_PAGE;
  1063. /* Used pages */
  1064. count = i - count_list(head, head->small_free_list, sp_bits, NULL);
  1065. fprintf(out, "%lu/%lu small pages used (%.3g%%)\n",
  1066. count, i, count ? 100.0 * count / i : 0.0);
  1067. /* Summary of each bucket. */
  1068. fprintf(out, "%lu buckets:\n", num_buckets);
  1069. for (i = 0; i < num_buckets; i++)
  1070. overhead += visualize_bucket(out, head, i, poolsize, sp_bits);
  1071. print_overhead(out, "total", overhead, poolsize);
  1072. }