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>
17 Inspired by (and parts taken from) Andrew Tridgell's alloc_mmap:
18 http://samba.org/~tridge/junkcode/alloc_mmap/
20 Copyright (C) Andrew Tridgell 2007
22 This library is free software; you can redistribute it and/or
23 modify it under the terms of the GNU Lesser General Public
24 License as published by the Free Software Foundation; either
25 version 2 of the License, or (at your option) any later version.
27 This library is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 Lesser General Public License for more details.
32 You should have received a copy of the GNU Lesser General Public
33 License along with this library; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 /* We divide the pool into this many large pages (nearest power of 2) */
38 #define MAX_LARGE_PAGES (256UL)
40 /* 32 small pages == 1 large page. */
41 #define BITS_FROM_SMALL_TO_LARGE_PAGE 5
43 #define MAX_SMALL_PAGES (MAX_LARGE_PAGES << BITS_FROM_SMALL_TO_LARGE_PAGE)
45 /* Smallest pool size for this scheme: 128-byte small pages. That's
46 * 9/13% overhead for 32/64 bit. */
47 #define MIN_USEFUL_SIZE (MAX_SMALL_PAGES * 128)
49 /* Every 4 buckets, we jump up a power of 2. ...8 10 12 14 16 20 24 28 32... */
50 #define INTER_BUCKET_SPACE 4
52 #define SMALL_PAGES_PER_LARGE_PAGE (1 << BITS_FROM_SMALL_TO_LARGE_PAGE)
54 /* FIXME: Figure this out properly. */
55 #define MAX_SIZE (1 << 30)
57 /* How few object to fit in a page before using a larger one? (8) */
58 #define MAX_PAGE_OBJECT_ORDER 3
60 #define BITS_PER_LONG (sizeof(long) * CHAR_BIT)
63 u32 elements_per_page;
69 /* Bitmap of which pages are large. */
70 unsigned long pagesize[MAX_LARGE_PAGES / BITS_PER_LONG];
72 /* List of unused small/large pages. */
76 /* List of huge allocs. */
79 /* This is less defined: we have two buckets for each power of 2 */
80 struct bucket_state bs[1];
84 unsigned long next, prev;
85 unsigned long off, len;
90 /* FIXME: We can just count all-0 and all-1 used[] elements. */
91 unsigned elements_used : 25;
93 unsigned long used[1]; /* One bit per element. */
97 * Every 4 buckets, the size doubles.
98 * Between buckets, sizes increase linearly.
100 * eg. bucket 40 = 2^10 = 1024
101 * bucket 41 = 2^10 + 2^10*4 = 1024 + 256
102 * bucket 42 = 2^10 + 2^10*4 = 1024 + 512
103 * bucket 43 = 2^10 + 2^10*4 = 1024 + 768
104 * bucket 45 = 2^11 = 2048
106 * Care is taken to handle low numbered buckets, at cost of overflow.
108 static unsigned long bucket_to_size(unsigned int bucket)
110 unsigned long base = 1 << (bucket / INTER_BUCKET_SPACE);
111 return base + ((bucket % INTER_BUCKET_SPACE)
112 << (bucket / INTER_BUCKET_SPACE))
113 / INTER_BUCKET_SPACE;
118 * fls(size/2) == 3. 1 << 3 == 8, so we're 2 too large, out of a possible
119 * 8 too large. That's 1/4 of the way to the next power of 2 == 1 bucket.
121 * We make sure we round up. Note that this fails on 32 bit at size
122 * 1879048193 (around bucket 120).
124 static unsigned int size_to_bucket(unsigned long size)
126 unsigned int base = fls(size/2);
127 unsigned long overshoot;
129 overshoot = size - (1 << base);
130 return base * INTER_BUCKET_SPACE
131 + ((overshoot * INTER_BUCKET_SPACE + (1 << base)-1) >> base);
134 static unsigned int small_page_bits(unsigned long poolsize)
136 return fls(poolsize / MAX_SMALL_PAGES / 2);
139 static struct page_header *from_pgnum(struct header *head,
143 return (struct page_header *)((char *)head + (pgnum << sp_bits));
146 static u16 to_pgnum(struct header *head, void *p, unsigned sp_bits)
148 return ((char *)p - (char *)head) >> sp_bits;
151 static size_t used_size(unsigned int num_elements)
153 return align_up(num_elements, BITS_PER_LONG) / CHAR_BIT;
157 * We always align the first entry to the lower power of 2.
158 * eg. the 12-byte bucket gets 8-byte aligned. The 4096-byte bucket
159 * gets 4096-byte aligned.
161 static unsigned long page_header_size(unsigned int align_bits,
162 unsigned long num_elements)
166 size = sizeof(struct page_header)
167 - sizeof(((struct page_header *)0)->used)
168 + used_size(num_elements);
169 return align_up(size, 1 << align_bits);
172 static void add_to_list(struct header *head,
173 u16 *list, struct page_header *ph, unsigned sp_bits)
175 unsigned long h = *list, offset = to_pgnum(head, ph, sp_bits);
179 struct page_header *prev = from_pgnum(head, h, sp_bits);
180 assert(prev->prev == 0);
187 static void del_from_list(struct header *head,
188 u16 *list, struct page_header *ph, unsigned sp_bits)
194 struct page_header *prev = from_pgnum(head, ph->prev, sp_bits);
195 prev->next = ph->next;
198 struct page_header *next = from_pgnum(head, ph->next, sp_bits);
199 next->prev = ph->prev;
203 static u16 pop_from_list(struct header *head,
205 unsigned int sp_bits)
208 struct page_header *ph = from_pgnum(head, h, sp_bits);
213 from_pgnum(head, *list, sp_bits)->prev = 0;
218 static void add_to_huge_list(struct header *head, struct huge_alloc *ha)
220 unsigned long h = head->huge;
221 unsigned long offset = (char *)ha - (char *)head;
225 struct huge_alloc *prev = (void *)((char *)head + h);
226 assert(prev->prev == 0);
233 static void del_from_huge(struct header *head, struct huge_alloc *ha)
237 head->huge = ha->next;
239 struct huge_alloc *prev = (void *)((char *)head + ha->prev);
240 prev->next = ha->next;
243 struct huge_alloc *next = (void *)((char *)head + ha->next);
244 next->prev = ha->prev;
248 static void add_small_page_to_freelist(struct header *head,
249 struct page_header *ph,
250 unsigned int sp_bits)
252 add_to_list(head, &head->small_free_list, ph, sp_bits);
255 static void add_large_page_to_freelist(struct header *head,
256 struct page_header *ph,
257 unsigned int sp_bits)
259 add_to_list(head, &head->large_free_list, ph, sp_bits);
262 static void add_to_bucket_list(struct header *head,
263 struct bucket_state *bs,
264 struct page_header *ph,
265 unsigned int sp_bits)
267 add_to_list(head, &bs->page_list, ph, sp_bits);
270 static void del_from_bucket_list(struct header *head,
271 struct bucket_state *bs,
272 struct page_header *ph,
273 unsigned int sp_bits)
275 del_from_list(head, &bs->page_list, ph, sp_bits);
278 static void del_from_bucket_full_list(struct header *head,
279 struct bucket_state *bs,
280 struct page_header *ph,
281 unsigned int sp_bits)
283 del_from_list(head, &bs->full_list, ph, sp_bits);
286 static void add_to_bucket_full_list(struct header *head,
287 struct bucket_state *bs,
288 struct page_header *ph,
289 unsigned int sp_bits)
291 add_to_list(head, &bs->full_list, ph, sp_bits);
294 static void clear_bit(unsigned long bitmap[], unsigned int off)
296 bitmap[off / BITS_PER_LONG] &= ~(1 << (off % BITS_PER_LONG));
299 static bool test_bit(const unsigned long bitmap[], unsigned int off)
301 return bitmap[off / BITS_PER_LONG] & (1 << (off % BITS_PER_LONG));
304 static void set_bit(unsigned long bitmap[], unsigned int off)
306 bitmap[off / BITS_PER_LONG] |= (1 << (off % BITS_PER_LONG));
309 /* There must be a bit to be found. */
310 static unsigned int find_free_bit(const unsigned long bitmap[])
314 for (i = 0; bitmap[i] == -1UL; i++);
315 return (i*BITS_PER_LONG) + ffsl(~bitmap[i]) - 1;
318 /* How many elements can we fit in a page? */
319 static unsigned long elements_per_page(unsigned long align_bits,
323 unsigned long num, overhead;
325 /* First approximation: no extra room for bitmap. */
326 overhead = align_up(sizeof(struct page_header), 1 << align_bits);
327 num = (psize - overhead) / esize;
329 while (page_header_size(align_bits, num) + esize * num > psize)
334 static bool large_page_bucket(unsigned int bucket, unsigned int sp_bits)
336 unsigned long max_smallsize;
338 /* Note: this doesn't take into account page header. */
339 max_smallsize = (1UL << sp_bits) >> MAX_PAGE_OBJECT_ORDER;
341 return bucket_to_size(bucket) > max_smallsize;
344 static unsigned int max_bucket(unsigned int lp_bits)
346 return (lp_bits - MAX_PAGE_OBJECT_ORDER) * INTER_BUCKET_SPACE;
349 void alloc_init(void *pool, unsigned long poolsize)
351 struct header *head = pool;
352 struct page_header *ph;
353 unsigned int lp_bits, sp_bits, num_buckets;
354 unsigned long header_size, i;
356 if (poolsize < MIN_USEFUL_SIZE) {
357 tiny_alloc_init(pool, poolsize);
361 /* We rely on page numbers fitting in 16 bit. */
362 BUILD_ASSERT(MAX_SMALL_PAGES < 65536);
364 sp_bits = small_page_bits(poolsize);
365 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
367 num_buckets = max_bucket(lp_bits);
370 header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
372 memset(head, 0, header_size);
373 for (i = 0; i < num_buckets; i++) {
374 unsigned long pagesize;
376 if (large_page_bucket(i, sp_bits))
377 pagesize = 1UL << lp_bits;
379 pagesize = 1UL << sp_bits;
381 head->bs[i].elements_per_page
382 = elements_per_page(i / INTER_BUCKET_SPACE,
387 /* They start as all large pages. */
388 memset(head->pagesize, 0xFF, sizeof(head->pagesize));
389 /* FIXME: small pages for last bit? */
391 /* Split first page into small pages. */
392 assert(header_size < (1UL << lp_bits));
393 clear_bit(head->pagesize, 0);
395 /* Skip over page(s) used by header, add rest to free list */
396 for (i = align_up(header_size, (1 << sp_bits)) >> sp_bits;
397 i < SMALL_PAGES_PER_LARGE_PAGE;
399 ph = from_pgnum(head, i, sp_bits);
400 ph->elements_used = 0;
401 add_small_page_to_freelist(head, ph, sp_bits);
404 /* Add the rest of the pages as large pages. */
405 i = SMALL_PAGES_PER_LARGE_PAGE;
406 while ((i << sp_bits) + (1 << lp_bits) <= poolsize) {
407 ph = from_pgnum(head, i, sp_bits);
408 ph->elements_used = 0;
409 add_large_page_to_freelist(head, ph, sp_bits);
410 i += SMALL_PAGES_PER_LARGE_PAGE;
414 /* A large page worth of small pages are free: delete them from free list. */
415 static void del_large_from_small_free_list(struct header *head,
416 struct page_header *ph,
417 unsigned int sp_bits)
421 for (i = 0; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
422 del_from_list(head, &head->small_free_list,
423 (void *)ph + (i << sp_bits),
428 static bool all_empty(struct header *head,
434 for (i = 0; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
435 struct page_header *ph = from_pgnum(head, pgnum + i, sp_bits);
436 if (ph->elements_used)
442 static void recombine_small_pages(struct header *head, unsigned long poolsize,
443 unsigned int sp_bits)
446 unsigned int lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
448 /* Look for small pages to coalesce, after first large page. */
449 for (i = SMALL_PAGES_PER_LARGE_PAGE;
450 i < (poolsize >> lp_bits) << BITS_FROM_SMALL_TO_LARGE_PAGE;
451 i += SMALL_PAGES_PER_LARGE_PAGE) {
452 /* Already a large page? */
453 if (test_bit(head->pagesize, i / SMALL_PAGES_PER_LARGE_PAGE))
455 if (all_empty(head, i, sp_bits)) {
456 struct page_header *ph = from_pgnum(head, i, sp_bits);
457 set_bit(head->pagesize,
458 i / SMALL_PAGES_PER_LARGE_PAGE);
459 del_large_from_small_free_list(head, ph, sp_bits);
460 add_large_page_to_freelist(head, ph, sp_bits);
465 static u16 get_large_page(struct header *head, unsigned long poolsize,
466 unsigned int sp_bits)
468 unsigned int lp_bits, page;
470 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
472 page = pop_from_list(head, &head->large_free_list, sp_bits);
476 recombine_small_pages(head, poolsize, sp_bits);
478 return pop_from_list(head, &head->large_free_list, sp_bits);
481 /* Returns small page. */
482 static unsigned long break_up_large_page(struct header *head,
483 unsigned int sp_bits,
488 clear_bit(head->pagesize, lpage >> BITS_FROM_SMALL_TO_LARGE_PAGE);
490 for (i = 1; i < SMALL_PAGES_PER_LARGE_PAGE; i++) {
491 struct page_header *ph = from_pgnum(head, lpage + i, sp_bits);
492 /* Initialize this: huge_alloc reads it. */
493 ph->elements_used = 0;
494 add_small_page_to_freelist(head, ph, sp_bits);
500 static u16 get_small_page(struct header *head, unsigned long poolsize,
501 unsigned int sp_bits)
505 ret = pop_from_list(head, &head->small_free_list, sp_bits);
508 ret = get_large_page(head, poolsize, sp_bits);
510 ret = break_up_large_page(head, sp_bits, ret);
514 static bool huge_allocated(struct header *head, unsigned long offset)
517 struct huge_alloc *ha;
519 for (i = head->huge; i; i = ha->next) {
520 ha = (void *)((char *)head + i);
521 if (ha->off <= offset && ha->off + ha->len > offset)
527 /* They want something really big. Aim for contiguous pages (slow). */
528 static void *unlikely_func huge_alloc(void *pool, unsigned long poolsize,
529 unsigned long size, unsigned long align)
531 struct header *head = pool;
532 struct huge_alloc *ha;
533 unsigned long i, sp_bits, lp_bits, num, header_size;
535 sp_bits = small_page_bits(poolsize);
536 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
538 /* Allocate tracking structure optimistically. */
539 ha = alloc_get(pool, poolsize, sizeof(*ha), ALIGNOF(*ha));
543 /* First search for contiguous small pages... */
544 header_size = sizeof(*head) + sizeof(head->bs) * (max_bucket(lp_bits)-1);
547 for (i = (header_size + (1 << sp_bits) - 1) >> sp_bits;
548 i << sp_bits < poolsize;
550 struct page_header *pg;
551 unsigned long off = (i << sp_bits);
553 /* Skip over large pages. */
554 if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
555 i += (1 << BITS_FROM_SMALL_TO_LARGE_PAGE)-1;
559 /* Does this page meet alignment requirements? */
560 if (!num && off % align != 0)
563 /* FIXME: This makes us O(n^2). */
564 if (huge_allocated(head, off)) {
569 pg = (struct page_header *)((char *)head + off);
570 if (pg->elements_used) {
576 if (num << sp_bits >= size) {
579 /* Remove from free list. */
580 for (pgnum = i; pgnum > i - num; pgnum--) {
581 pg = from_pgnum(head, pgnum, sp_bits);
583 &head->small_free_list,
586 ha->off = (i - num + 1) << sp_bits;
587 ha->len = num << sp_bits;
592 /* Now search for large pages... */
593 recombine_small_pages(head, poolsize, sp_bits);
596 for (i = (header_size + (1 << lp_bits) - 1) >> lp_bits;
597 (i << lp_bits) < poolsize; i++) {
598 struct page_header *pg;
599 unsigned long off = (i << lp_bits);
601 /* Ignore small pages. */
602 if (!test_bit(head->pagesize, i))
605 /* Does this page meet alignment requirements? */
606 if (!num && off % align != 0)
609 /* FIXME: This makes us O(n^2). */
610 if (huge_allocated(head, off)) {
615 pg = (struct page_header *)((char *)head + off);
616 if (pg->elements_used) {
622 if (num << lp_bits >= size) {
625 /* Remove from free list. */
626 for (pgnum = i; pgnum > i - num; pgnum--) {
627 pg = from_pgnum(head, pgnum, lp_bits);
629 &head->large_free_list,
632 ha->off = (i - num + 1) << lp_bits;
633 ha->len = num << lp_bits;
638 /* Unable to satisfy: free huge alloc structure. */
639 alloc_free(pool, poolsize, ha);
643 add_to_huge_list(pool, ha);
644 return (char *)pool + ha->off;
647 static void unlikely_func huge_free(struct header *head,
648 unsigned long poolsize, void *free)
650 unsigned long i, off, pgnum, free_off = (char *)free - (char *)head;
651 unsigned int sp_bits, lp_bits;
652 struct huge_alloc *ha;
654 for (i = head->huge; i; i = ha->next) {
655 ha = (void *)((char *)head + i);
656 if (free_off == ha->off)
661 /* Free up all the pages, delete and free ha */
662 sp_bits = small_page_bits(poolsize);
663 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
664 pgnum = free_off >> sp_bits;
666 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
667 for (off = ha->off; off < ha->off + ha->len; off += 1 << lp_bits) {
668 add_large_page_to_freelist(head,
669 (void *)((char *)head + off),
673 for (off = ha->off; off < ha->off + ha->len; off += 1 << sp_bits) {
674 add_small_page_to_freelist(head,
675 (void *)((char *)head + off),
679 del_from_huge(head, ha);
680 alloc_free(head, poolsize, ha);
683 static unsigned long unlikely_func huge_size(struct header *head, void *p)
685 unsigned long i, off = (char *)p - (char *)head;
686 struct huge_alloc *ha;
688 for (i = head->huge; i; i = ha->next) {
689 ha = (void *)((char *)head + i);
690 if (off == ha->off) {
697 void *alloc_get(void *pool, unsigned long poolsize,
698 unsigned long size, unsigned long align)
700 struct header *head = pool;
703 struct bucket_state *bs;
704 struct page_header *ph;
705 unsigned int sp_bits;
707 if (poolsize < MIN_USEFUL_SIZE) {
708 return tiny_alloc_get(pool, poolsize, size, align);
711 size = align_up(size, align);
714 bucket = size_to_bucket(size);
716 sp_bits = small_page_bits(poolsize);
718 if (bucket >= max_bucket(sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE)) {
719 return huge_alloc(pool, poolsize, size, align);
722 bs = &head->bs[bucket];
724 if (!bs->page_list) {
725 struct page_header *ph;
727 if (large_page_bucket(bucket, sp_bits))
728 bs->page_list = get_large_page(head, poolsize,
731 bs->page_list = get_small_page(head, poolsize,
733 /* FIXME: Try large-aligned alloc? Header stuffing? */
734 if (unlikely(!bs->page_list))
736 ph = from_pgnum(head, bs->page_list, sp_bits);
738 ph->elements_used = 0;
740 memset(ph->used, 0, used_size(bs->elements_per_page));
743 ph = from_pgnum(head, bs->page_list, sp_bits);
745 i = find_free_bit(ph->used);
746 set_bit(ph->used, i);
749 /* check if this page is now full */
750 if (unlikely(ph->elements_used == bs->elements_per_page)) {
751 del_from_bucket_list(head, bs, ph, sp_bits);
752 add_to_bucket_full_list(head, bs, ph, sp_bits);
755 return (char *)ph + page_header_size(ph->bucket / INTER_BUCKET_SPACE,
756 bs->elements_per_page)
757 + i * bucket_to_size(bucket);
760 void alloc_free(void *pool, unsigned long poolsize, void *free)
762 struct header *head = pool;
763 struct bucket_state *bs;
764 unsigned int sp_bits;
765 unsigned long i, pgnum, pgoffset, offset = (char *)free - (char *)pool;
767 struct page_header *ph;
769 if (poolsize < MIN_USEFUL_SIZE) {
770 return tiny_alloc_free(pool, poolsize, free);
773 /* Get page header. */
774 sp_bits = small_page_bits(poolsize);
775 pgnum = offset >> sp_bits;
777 /* Big page? Round down further. */
778 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
780 pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
784 /* Step back to page header. */
785 ph = from_pgnum(head, pgnum, sp_bits);
786 if ((void *)ph == free) {
787 huge_free(head, poolsize, free);
791 bs = &head->bs[ph->bucket];
792 pgoffset = offset - (pgnum << sp_bits)
793 - page_header_size(ph->bucket / INTER_BUCKET_SPACE,
794 bs->elements_per_page);
796 if (unlikely(ph->elements_used == bs->elements_per_page)) {
797 del_from_bucket_full_list(head, bs, ph, sp_bits);
798 add_to_bucket_list(head, bs, ph, sp_bits);
801 /* Which element are we? */
802 i = pgoffset / bucket_to_size(ph->bucket);
803 clear_bit(ph->used, i);
806 if (unlikely(ph->elements_used == 0)) {
807 bs = &head->bs[ph->bucket];
808 del_from_bucket_list(head, bs, ph, sp_bits);
810 add_small_page_to_freelist(head, ph, sp_bits);
812 add_large_page_to_freelist(head, ph, sp_bits);
816 unsigned long alloc_size(void *pool, unsigned long poolsize, void *p)
818 struct header *head = pool;
819 unsigned int pgnum, sp_bits;
820 unsigned long offset = (char *)p - (char *)pool;
821 struct page_header *ph;
823 if (poolsize < MIN_USEFUL_SIZE)
824 return tiny_alloc_size(pool, poolsize, p);
826 /* Get page header. */
827 sp_bits = small_page_bits(poolsize);
828 pgnum = offset >> sp_bits;
830 /* Big page? Round down further. */
831 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE))
832 pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
834 /* Step back to page header. */
835 ph = from_pgnum(head, pgnum, sp_bits);
837 return huge_size(head, p);
839 return bucket_to_size(ph->bucket);
842 /* Useful for gdb breakpoints. */
843 static bool check_fail(void)
848 static unsigned long count_bits(const unsigned long bitmap[],
851 unsigned long i, count = 0;
853 while (limit >= BITS_PER_LONG) {
854 count += popcount(bitmap[0]);
856 limit -= BITS_PER_LONG;
859 for (i = 0; i < limit; i++)
860 if (test_bit(bitmap, i))
865 static bool out_of_bounds(unsigned long pgnum,
866 unsigned int sp_bits,
867 unsigned long pagesize,
868 unsigned long poolsize)
870 if (((pgnum << sp_bits) >> sp_bits) != pgnum)
873 if ((pgnum << sp_bits) > poolsize)
876 return ((pgnum << sp_bits) + pagesize > poolsize);
879 static bool check_bucket(struct header *head,
880 unsigned long poolsize,
881 unsigned long pages[],
882 struct bucket_state *bs,
886 struct page_header *ph;
887 unsigned long taken, i, prev, pagesize, sp_bits, lp_bits;
889 sp_bits = small_page_bits(poolsize);
890 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
892 lp_bucket = large_page_bucket(bindex, sp_bits);
894 pagesize = 1UL << (lp_bucket ? lp_bits : sp_bits);
896 /* This many elements fit? */
897 taken = page_header_size(bindex / INTER_BUCKET_SPACE,
898 bs->elements_per_page);
899 taken += bucket_to_size(bindex) * bs->elements_per_page;
900 if (taken > pagesize)
903 /* One more wouldn't fit? */
904 taken = page_header_size(bindex / INTER_BUCKET_SPACE,
905 bs->elements_per_page + 1);
906 taken += bucket_to_size(bindex) * (bs->elements_per_page + 1);
907 if (taken <= pagesize)
910 /* Walk used list. */
912 for (i = bs->page_list; i; i = ph->next) {
914 if (out_of_bounds(i, sp_bits, pagesize, poolsize))
916 /* Wrong size page? */
917 if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
920 /* Large page not on boundary? */
921 if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
923 ph = from_pgnum(head, i, sp_bits);
924 /* Linked list corrupt? */
925 if (ph->prev != prev)
927 /* Already seen this page? */
928 if (test_bit(pages, i))
932 if (ph->elements_used == 0)
934 if (ph->elements_used >= bs->elements_per_page)
936 /* Used bits don't agree? */
937 if (ph->elements_used != count_bits(ph->used,
938 bs->elements_per_page))
941 if (ph->bucket != bindex)
946 /* Walk full list. */
948 for (i = bs->full_list; i; i = ph->next) {
950 if (out_of_bounds(i, sp_bits, pagesize, poolsize))
952 /* Wrong size page? */
953 if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
955 /* Large page not on boundary? */
956 if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
958 ph = from_pgnum(head, i, sp_bits);
959 /* Linked list corrupt? */
960 if (ph->prev != prev)
962 /* Already seen this page? */
963 if (test_bit(pages, i))
967 if (ph->elements_used != bs->elements_per_page)
969 /* Used bits don't agree? */
970 if (ph->elements_used != count_bits(ph->used,
971 bs->elements_per_page))
974 if (ph->bucket != bindex)
981 bool alloc_check(void *pool, unsigned long poolsize)
983 struct header *head = pool;
984 unsigned long prev, i, lp_bits, sp_bits, header_size, num_buckets;
985 struct page_header *ph;
986 struct huge_alloc *ha;
987 unsigned long pages[MAX_SMALL_PAGES / BITS_PER_LONG] = { 0 };
989 if (poolsize < MIN_USEFUL_SIZE)
990 return tiny_alloc_check(pool, poolsize);
992 sp_bits = small_page_bits(poolsize);
993 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
995 num_buckets = max_bucket(lp_bits);
997 header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
999 /* First, set all bits taken by header. */
1000 for (i = 0; i < header_size; i += (1UL << sp_bits))
1001 set_bit(pages, i >> sp_bits);
1003 /* Check small page free list. */
1005 for (i = head->small_free_list; i; i = ph->next) {
1007 if (out_of_bounds(i, sp_bits, 1 << sp_bits, poolsize))
1008 return check_fail();
1010 if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
1011 return check_fail();
1012 ph = from_pgnum(head, i, sp_bits);
1013 /* Linked list corrupt? */
1014 if (ph->prev != prev)
1015 return check_fail();
1016 /* Already seen this page? */
1017 if (test_bit(pages, i))
1018 return check_fail();
1023 /* Check large page free list. */
1025 for (i = head->large_free_list; i; i = ph->next) {
1027 if (out_of_bounds(i, sp_bits, 1 << lp_bits, poolsize))
1028 return check_fail();
1029 /* Not large page? */
1030 if (!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
1031 return check_fail();
1032 /* Not page boundary? */
1033 if ((i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
1034 return check_fail();
1035 ph = from_pgnum(head, i, sp_bits);
1036 /* Linked list corrupt? */
1037 if (ph->prev != prev)
1038 return check_fail();
1039 /* Already seen this page? */
1040 if (test_bit(pages, i))
1041 return check_fail();
1046 /* Check the buckets. */
1047 for (i = 0; i < max_bucket(lp_bits); i++) {
1048 struct bucket_state *bs = &head->bs[i];
1050 if (!check_bucket(head, poolsize, pages, bs, i))
1054 /* Check the huge alloc list. */
1056 for (i = head->huge; i; i = ha->next) {
1057 unsigned long pgbits, j;
1060 if (i >= poolsize || i + sizeof(*ha) > poolsize)
1061 return check_fail();
1062 ha = (void *)((char *)head + i);
1064 /* Check contents of ha. */
1065 if (ha->off > poolsize || ha->off + ha->len > poolsize)
1066 return check_fail();
1068 /* Large or small page? */
1069 pgbits = test_bit(head->pagesize, ha->off >> lp_bits)
1070 ? lp_bits : sp_bits;
1072 /* Not page boundary? */
1073 if ((ha->off % (1UL << pgbits)) != 0)
1074 return check_fail();
1076 /* Not page length? */
1077 if ((ha->len % (1UL << pgbits)) != 0)
1078 return check_fail();
1080 /* Linked list corrupt? */
1081 if (ha->prev != prev)
1082 return check_fail();
1084 for (j = ha->off; j < ha->off + ha->len; j += (1 << sp_bits)) {
1085 /* Already seen this page? */
1086 if (test_bit(pages, j >> sp_bits))
1087 return check_fail();
1088 set_bit(pages, j >> sp_bits);
1094 /* Make sure every page accounted for. */
1095 for (i = 0; i < poolsize >> sp_bits; i++) {
1096 if (!test_bit(pages, i))
1097 return check_fail();
1098 if (test_bit(head->pagesize,
1099 i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
1100 /* Large page, skip rest. */
1101 i += SMALL_PAGES_PER_LARGE_PAGE - 1;
1108 static unsigned long print_overhead(FILE *out, const char *desc,
1109 unsigned long bytes,
1110 unsigned long poolsize)
1112 fprintf(out, "Overhead (%s): %lu bytes (%.3g%%)\n",
1113 desc, bytes, 100.0 * bytes / poolsize);
1117 static unsigned long count_list(struct header *head,
1119 unsigned int sp_bits,
1120 unsigned long *total_elems)
1122 struct page_header *p;
1123 unsigned long ret = 0;
1126 p = from_pgnum(head, pgnum, sp_bits);
1128 (*total_elems) += p->elements_used;
1135 static unsigned long visualize_bucket(FILE *out, struct header *head,
1136 unsigned int bucket,
1137 unsigned long poolsize,
1138 unsigned int sp_bits)
1140 unsigned long num_full, num_partial, num_pages, page_size,
1141 elems, hdr_min, hdr_size, elems_per_page, overhead = 0;
1143 elems_per_page = head->bs[bucket].elements_per_page;
1145 /* If we used byte-based bitmaps, we could get pg hdr to: */
1146 hdr_min = sizeof(struct page_header)
1147 - sizeof(((struct page_header *)0)->used)
1148 + align_up(elems_per_page, CHAR_BIT) / CHAR_BIT;
1149 hdr_size = page_header_size(bucket / INTER_BUCKET_SPACE,
1153 num_full = count_list(head, head->bs[bucket].full_list, sp_bits,
1155 num_partial = count_list(head, head->bs[bucket].page_list, sp_bits,
1157 num_pages = num_full + num_partial;
1161 fprintf(out, "Bucket %u (%lu bytes):"
1162 " %lu full, %lu partial = %lu elements\n",
1163 bucket, bucket_to_size(bucket), num_full, num_partial, elems);
1164 /* Strict requirement of page header size. */
1165 overhead += print_overhead(out, "page headers",
1166 hdr_min * num_pages, poolsize);
1167 /* Gap between minimal page header and actual start. */
1168 overhead += print_overhead(out, "page post-header alignments",
1169 (hdr_size - hdr_min) * num_pages, poolsize);
1170 /* Between last element and end of page. */
1171 page_size = (1 << sp_bits);
1172 if (large_page_bucket(bucket, sp_bits))
1173 page_size <<= BITS_FROM_SMALL_TO_LARGE_PAGE;
1175 overhead += print_overhead(out, "page tails",
1176 (page_size - (hdr_size
1178 * bucket_to_size(bucket))))
1179 * num_pages, poolsize);
1183 void alloc_visualize(FILE *out, void *pool, unsigned long poolsize)
1185 struct header *head = pool;
1186 unsigned long i, lp_bits, sp_bits, header_size, num_buckets, count,
1189 fprintf(out, "Pool %p size %lu: (%s allocator)\n", pool, poolsize,
1190 poolsize < MIN_USEFUL_SIZE ? "tiny" : "standard");
1192 if (poolsize < MIN_USEFUL_SIZE) {
1193 tiny_alloc_visualize(out, pool, poolsize);
1197 sp_bits = small_page_bits(poolsize);
1198 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
1200 num_buckets = max_bucket(lp_bits);
1201 header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
1203 fprintf(out, "Large page size %lu, small page size %lu.\n",
1204 1UL << lp_bits, 1UL << sp_bits);
1205 overhead += print_overhead(out, "unused pool tail",
1206 poolsize % (1 << lp_bits), poolsize);
1207 fprintf(out, "Main header %lu bytes (%lu small pages).\n",
1208 header_size, align_up(header_size, 1 << sp_bits) >> sp_bits);
1209 overhead += print_overhead(out, "partial header page",
1210 align_up(header_size, 1 << sp_bits)
1211 - header_size, poolsize);
1212 /* Total large pages. */
1213 i = count_bits(head->pagesize, poolsize >> lp_bits);
1215 count = i - count_list(head, head->large_free_list, sp_bits, NULL);
1216 fprintf(out, "%lu/%lu large pages used (%.3g%%)\n",
1217 count, i, count ? 100.0 * count / i : 0.0);
1219 /* Total small pages. */
1220 i = ((poolsize >> lp_bits) - i) << BITS_FROM_SMALL_TO_LARGE_PAGE;
1222 count = i - count_list(head, head->small_free_list, sp_bits, NULL);
1223 fprintf(out, "%lu/%lu small pages used (%.3g%%)\n",
1224 count, i, count ? 100.0 * count / i : 0.0);
1226 /* Summary of each bucket. */
1227 fprintf(out, "%lu buckets:\n", num_buckets);
1228 for (i = 0; i < num_buckets; i++)
1229 overhead += visualize_bucket(out, head, i, poolsize, sp_bits);
1231 print_overhead(out, "total", overhead, poolsize);