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 add_small_page_to_freelist(head, ph, sp_bits);
498 static u16 get_small_page(struct header *head, unsigned long poolsize,
499 unsigned int sp_bits)
503 ret = pop_from_list(head, &head->small_free_list, sp_bits);
506 ret = get_large_page(head, poolsize, sp_bits);
508 ret = break_up_large_page(head, sp_bits, ret);
512 static bool huge_allocated(struct header *head, unsigned long offset)
515 struct huge_alloc *ha;
517 for (i = head->huge; i; i = ha->next) {
518 ha = (void *)((char *)head + i);
519 if (ha->off <= offset && ha->off + ha->len > offset)
525 /* They want something really big. Aim for contiguous pages (slow). */
526 static void *unlikely_func huge_alloc(void *pool, unsigned long poolsize,
527 unsigned long size, unsigned long align)
529 struct header *head = pool;
530 struct huge_alloc *ha;
531 unsigned long i, sp_bits, lp_bits, num, header_size;
533 sp_bits = small_page_bits(poolsize);
534 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
536 /* Allocate tracking structure optimistically. */
537 ha = alloc_get(pool, poolsize, sizeof(*ha), ALIGNOF(*ha));
541 /* First search for contiguous small pages... */
542 header_size = sizeof(*head) + sizeof(head->bs) * (max_bucket(lp_bits)-1);
545 for (i = (header_size + (1 << sp_bits) - 1) >> sp_bits;
546 i << sp_bits < poolsize;
548 struct page_header *pg;
549 unsigned long off = (i << sp_bits);
551 /* Skip over large pages. */
552 if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
553 i += (1 << BITS_FROM_SMALL_TO_LARGE_PAGE)-1;
557 /* Does this page meet alignment requirements? */
558 if (!num && off % align != 0)
561 /* FIXME: This makes us O(n^2). */
562 if (huge_allocated(head, off)) {
567 pg = (struct page_header *)((char *)head + off);
568 if (pg->elements_used) {
574 if (num << sp_bits >= size) {
577 /* Remove from free list. */
578 for (pgnum = i; pgnum > i - num; pgnum--) {
579 pg = from_pgnum(head, pgnum, sp_bits);
581 &head->small_free_list,
584 ha->off = (i - num + 1) << sp_bits;
585 ha->len = num << sp_bits;
590 /* Now search for large pages... */
591 recombine_small_pages(head, poolsize, sp_bits);
594 for (i = (header_size + (1 << lp_bits) - 1) >> lp_bits;
595 (i << lp_bits) < poolsize; i++) {
596 struct page_header *pg;
597 unsigned long off = (i << lp_bits);
599 /* Ignore small pages. */
600 if (!test_bit(head->pagesize, i))
603 /* Does this page meet alignment requirements? */
604 if (!num && off % align != 0)
607 /* FIXME: This makes us O(n^2). */
608 if (huge_allocated(head, off)) {
613 pg = (struct page_header *)((char *)head + off);
614 if (pg->elements_used) {
620 if (num << lp_bits >= size) {
623 /* Remove from free list. */
624 for (pgnum = i; pgnum > i - num; pgnum--) {
625 pg = from_pgnum(head, pgnum, lp_bits);
627 &head->large_free_list,
630 ha->off = (i - num + 1) << lp_bits;
631 ha->len = num << lp_bits;
636 /* Unable to satisfy: free huge alloc structure. */
637 alloc_free(pool, poolsize, ha);
641 add_to_huge_list(pool, ha);
642 return (char *)pool + ha->off;
645 static void unlikely_func huge_free(struct header *head,
646 unsigned long poolsize, void *free)
648 unsigned long i, off, pgnum, free_off = (char *)free - (char *)head;
649 unsigned int sp_bits, lp_bits;
650 struct huge_alloc *ha;
652 for (i = head->huge; i; i = ha->next) {
653 ha = (void *)((char *)head + i);
654 if (free_off == ha->off)
659 /* Free up all the pages, delete and free ha */
660 sp_bits = small_page_bits(poolsize);
661 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
662 pgnum = free_off >> sp_bits;
664 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
665 for (off = ha->off; off < ha->off + ha->len; off += 1 << lp_bits) {
666 add_large_page_to_freelist(head,
667 (void *)((char *)head + off),
671 for (off = ha->off; off < ha->off + ha->len; off += 1 << sp_bits) {
672 add_small_page_to_freelist(head,
673 (void *)((char *)head + off),
677 del_from_huge(head, ha);
678 alloc_free(head, poolsize, ha);
681 static unsigned long unlikely_func huge_size(struct header *head, void *p)
683 unsigned long i, off = (char *)p - (char *)head;
684 struct huge_alloc *ha;
686 for (i = head->huge; i; i = ha->next) {
687 ha = (void *)((char *)head + i);
688 if (off == ha->off) {
695 void *alloc_get(void *pool, unsigned long poolsize,
696 unsigned long size, unsigned long align)
698 struct header *head = pool;
701 struct bucket_state *bs;
702 struct page_header *ph;
703 unsigned int sp_bits;
705 if (poolsize < MIN_USEFUL_SIZE) {
706 return tiny_alloc_get(pool, poolsize, size, align);
709 size = align_up(size, align);
712 bucket = size_to_bucket(size);
714 sp_bits = small_page_bits(poolsize);
716 if (bucket >= max_bucket(sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE)) {
717 return huge_alloc(pool, poolsize, size, align);
720 bs = &head->bs[bucket];
722 if (!bs->page_list) {
723 struct page_header *ph;
725 if (large_page_bucket(bucket, sp_bits))
726 bs->page_list = get_large_page(head, poolsize,
729 bs->page_list = get_small_page(head, poolsize,
731 /* FIXME: Try large-aligned alloc? Header stuffing? */
732 if (unlikely(!bs->page_list))
734 ph = from_pgnum(head, bs->page_list, sp_bits);
736 ph->elements_used = 0;
738 memset(ph->used, 0, used_size(bs->elements_per_page));
741 ph = from_pgnum(head, bs->page_list, sp_bits);
743 i = find_free_bit(ph->used);
744 set_bit(ph->used, i);
747 /* check if this page is now full */
748 if (unlikely(ph->elements_used == bs->elements_per_page)) {
749 del_from_bucket_list(head, bs, ph, sp_bits);
750 add_to_bucket_full_list(head, bs, ph, sp_bits);
753 return (char *)ph + page_header_size(ph->bucket / INTER_BUCKET_SPACE,
754 bs->elements_per_page)
755 + i * bucket_to_size(bucket);
758 void alloc_free(void *pool, unsigned long poolsize, void *free)
760 struct header *head = pool;
761 struct bucket_state *bs;
762 unsigned int sp_bits;
763 unsigned long i, pgnum, pgoffset, offset = (char *)free - (char *)pool;
765 struct page_header *ph;
767 if (poolsize < MIN_USEFUL_SIZE) {
768 return tiny_alloc_free(pool, poolsize, free);
771 /* Get page header. */
772 sp_bits = small_page_bits(poolsize);
773 pgnum = offset >> sp_bits;
775 /* Big page? Round down further. */
776 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
778 pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
782 /* Step back to page header. */
783 ph = from_pgnum(head, pgnum, sp_bits);
784 if ((void *)ph == free) {
785 huge_free(head, poolsize, free);
789 bs = &head->bs[ph->bucket];
790 pgoffset = offset - (pgnum << sp_bits)
791 - page_header_size(ph->bucket / INTER_BUCKET_SPACE,
792 bs->elements_per_page);
794 if (unlikely(ph->elements_used == bs->elements_per_page)) {
795 del_from_bucket_full_list(head, bs, ph, sp_bits);
796 add_to_bucket_list(head, bs, ph, sp_bits);
799 /* Which element are we? */
800 i = pgoffset / bucket_to_size(ph->bucket);
801 clear_bit(ph->used, i);
804 if (unlikely(ph->elements_used == 0)) {
805 bs = &head->bs[ph->bucket];
806 del_from_bucket_list(head, bs, ph, sp_bits);
808 add_small_page_to_freelist(head, ph, sp_bits);
810 add_large_page_to_freelist(head, ph, sp_bits);
814 unsigned long alloc_size(void *pool, unsigned long poolsize, void *p)
816 struct header *head = pool;
817 unsigned int pgnum, sp_bits;
818 unsigned long offset = (char *)p - (char *)pool;
819 struct page_header *ph;
821 if (poolsize < MIN_USEFUL_SIZE)
822 return tiny_alloc_size(pool, poolsize, p);
824 /* Get page header. */
825 sp_bits = small_page_bits(poolsize);
826 pgnum = offset >> sp_bits;
828 /* Big page? Round down further. */
829 if (test_bit(head->pagesize, pgnum >> BITS_FROM_SMALL_TO_LARGE_PAGE))
830 pgnum &= ~(SMALL_PAGES_PER_LARGE_PAGE - 1);
832 /* Step back to page header. */
833 ph = from_pgnum(head, pgnum, sp_bits);
835 return huge_size(head, p);
837 return bucket_to_size(ph->bucket);
840 /* Useful for gdb breakpoints. */
841 static bool check_fail(void)
846 static unsigned long count_bits(const unsigned long bitmap[],
849 unsigned long i, count = 0;
851 while (limit >= BITS_PER_LONG) {
852 count += popcount(bitmap[0]);
854 limit -= BITS_PER_LONG;
857 for (i = 0; i < limit; i++)
858 if (test_bit(bitmap, i))
863 static bool out_of_bounds(unsigned long pgnum,
864 unsigned int sp_bits,
865 unsigned long pagesize,
866 unsigned long poolsize)
868 if (((pgnum << sp_bits) >> sp_bits) != pgnum)
871 if ((pgnum << sp_bits) > poolsize)
874 return ((pgnum << sp_bits) + pagesize > poolsize);
877 static bool check_bucket(struct header *head,
878 unsigned long poolsize,
879 unsigned long pages[],
880 struct bucket_state *bs,
884 struct page_header *ph;
885 unsigned long taken, i, prev, pagesize, sp_bits, lp_bits;
887 sp_bits = small_page_bits(poolsize);
888 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
890 lp_bucket = large_page_bucket(bindex, sp_bits);
892 pagesize = 1UL << (lp_bucket ? lp_bits : sp_bits);
894 /* This many elements fit? */
895 taken = page_header_size(bindex / INTER_BUCKET_SPACE,
896 bs->elements_per_page);
897 taken += bucket_to_size(bindex) * bs->elements_per_page;
898 if (taken > pagesize)
901 /* One more wouldn't fit? */
902 taken = page_header_size(bindex / INTER_BUCKET_SPACE,
903 bs->elements_per_page + 1);
904 taken += bucket_to_size(bindex) * (bs->elements_per_page + 1);
905 if (taken <= pagesize)
908 /* Walk used list. */
910 for (i = bs->page_list; i; i = ph->next) {
912 if (out_of_bounds(i, sp_bits, pagesize, poolsize))
914 /* Wrong size page? */
915 if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
918 /* Large page not on boundary? */
919 if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
921 ph = from_pgnum(head, i, sp_bits);
922 /* Linked list corrupt? */
923 if (ph->prev != prev)
925 /* Already seen this page? */
926 if (test_bit(pages, i))
930 if (ph->elements_used == 0)
932 if (ph->elements_used >= bs->elements_per_page)
934 /* Used bits don't agree? */
935 if (ph->elements_used != count_bits(ph->used,
936 bs->elements_per_page))
939 if (ph->bucket != bindex)
944 /* Walk full list. */
946 for (i = bs->full_list; i; i = ph->next) {
948 if (out_of_bounds(i, sp_bits, pagesize, poolsize))
950 /* Wrong size page? */
951 if (!!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE)
953 /* Large page not on boundary? */
954 if (lp_bucket && (i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
956 ph = from_pgnum(head, i, sp_bits);
957 /* Linked list corrupt? */
958 if (ph->prev != prev)
960 /* Already seen this page? */
961 if (test_bit(pages, i))
965 if (ph->elements_used != bs->elements_per_page)
967 /* Used bits don't agree? */
968 if (ph->elements_used != count_bits(ph->used,
969 bs->elements_per_page))
972 if (ph->bucket != bindex)
979 bool alloc_check(void *pool, unsigned long poolsize)
981 struct header *head = pool;
982 unsigned long prev, i, lp_bits, sp_bits, header_size, num_buckets;
983 struct page_header *ph;
984 struct huge_alloc *ha;
985 unsigned long pages[MAX_SMALL_PAGES / BITS_PER_LONG] = { 0 };
987 if (poolsize < MIN_USEFUL_SIZE)
988 return tiny_alloc_check(pool, poolsize);
990 sp_bits = small_page_bits(poolsize);
991 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
993 num_buckets = max_bucket(lp_bits);
995 header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
997 /* First, set all bits taken by header. */
998 for (i = 0; i < header_size; i += (1UL << sp_bits))
999 set_bit(pages, i >> sp_bits);
1001 /* Check small page free list. */
1003 for (i = head->small_free_list; i; i = ph->next) {
1005 if (out_of_bounds(i, sp_bits, 1 << sp_bits, poolsize))
1006 return check_fail();
1008 if (test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
1009 return check_fail();
1010 ph = from_pgnum(head, i, sp_bits);
1011 /* Linked list corrupt? */
1012 if (ph->prev != prev)
1013 return check_fail();
1014 /* Already seen this page? */
1015 if (test_bit(pages, i))
1016 return check_fail();
1021 /* Check large page free list. */
1023 for (i = head->large_free_list; i; i = ph->next) {
1025 if (out_of_bounds(i, sp_bits, 1 << lp_bits, poolsize))
1026 return check_fail();
1027 /* Not large page? */
1028 if (!test_bit(head->pagesize, i >> BITS_FROM_SMALL_TO_LARGE_PAGE))
1029 return check_fail();
1030 /* Not page boundary? */
1031 if ((i % SMALL_PAGES_PER_LARGE_PAGE) != 0)
1032 return check_fail();
1033 ph = from_pgnum(head, i, sp_bits);
1034 /* Linked list corrupt? */
1035 if (ph->prev != prev)
1036 return check_fail();
1037 /* Already seen this page? */
1038 if (test_bit(pages, i))
1039 return check_fail();
1044 /* Check the buckets. */
1045 for (i = 0; i < max_bucket(lp_bits); i++) {
1046 struct bucket_state *bs = &head->bs[i];
1048 if (!check_bucket(head, poolsize, pages, bs, i))
1052 /* Check the huge alloc list. */
1054 for (i = head->huge; i; i = ha->next) {
1055 unsigned long pgbits, j;
1058 if (i >= poolsize || i + sizeof(*ha) > poolsize)
1059 return check_fail();
1060 ha = (void *)((char *)head + i);
1062 /* Check contents of ha. */
1063 if (ha->off > poolsize || ha->off + ha->len > poolsize)
1064 return check_fail();
1066 /* Large or small page? */
1067 pgbits = test_bit(head->pagesize, ha->off >> lp_bits)
1068 ? lp_bits : sp_bits;
1070 /* Not page boundary? */
1071 if ((ha->off % (1UL << pgbits)) != 0)
1072 return check_fail();
1074 /* Not page length? */
1075 if ((ha->len % (1UL << pgbits)) != 0)
1076 return check_fail();
1078 /* Linked list corrupt? */
1079 if (ha->prev != prev)
1080 return check_fail();
1082 for (j = ha->off; j < ha->off + ha->len; j += (1 << sp_bits)) {
1083 /* Already seen this page? */
1084 if (test_bit(pages, j >> sp_bits))
1085 return check_fail();
1086 set_bit(pages, j >> sp_bits);
1092 /* Make sure every page accounted for. */
1093 for (i = 0; i < poolsize >> sp_bits; i++) {
1094 if (!test_bit(pages, i))
1095 return check_fail();
1096 if (test_bit(head->pagesize,
1097 i >> BITS_FROM_SMALL_TO_LARGE_PAGE)) {
1098 /* Large page, skip rest. */
1099 i += SMALL_PAGES_PER_LARGE_PAGE - 1;
1106 static unsigned long print_overhead(FILE *out, const char *desc,
1107 unsigned long bytes,
1108 unsigned long poolsize)
1110 fprintf(out, "Overhead (%s): %lu bytes (%.3g%%)\n",
1111 desc, bytes, 100.0 * bytes / poolsize);
1115 static unsigned long count_list(struct header *head,
1117 unsigned int sp_bits,
1118 unsigned long *total_elems)
1120 struct page_header *p;
1121 unsigned long ret = 0;
1124 p = from_pgnum(head, pgnum, sp_bits);
1126 (*total_elems) += p->elements_used;
1133 static unsigned long visualize_bucket(FILE *out, struct header *head,
1134 unsigned int bucket,
1135 unsigned long poolsize,
1136 unsigned int sp_bits)
1138 unsigned long num_full, num_partial, num_pages, page_size,
1139 elems, hdr_min, hdr_size, elems_per_page, overhead = 0;
1141 elems_per_page = head->bs[bucket].elements_per_page;
1143 /* If we used byte-based bitmaps, we could get pg hdr to: */
1144 hdr_min = sizeof(struct page_header)
1145 - sizeof(((struct page_header *)0)->used)
1146 + align_up(elems_per_page, CHAR_BIT) / CHAR_BIT;
1147 hdr_size = page_header_size(bucket / INTER_BUCKET_SPACE,
1151 num_full = count_list(head, head->bs[bucket].full_list, sp_bits,
1153 num_partial = count_list(head, head->bs[bucket].page_list, sp_bits,
1155 num_pages = num_full + num_partial;
1159 fprintf(out, "Bucket %u (%lu bytes):"
1160 " %lu full, %lu partial = %lu elements\n",
1161 bucket, bucket_to_size(bucket), num_full, num_partial, elems);
1162 /* Strict requirement of page header size. */
1163 overhead += print_overhead(out, "page headers",
1164 hdr_min * num_pages, poolsize);
1165 /* Gap between minimal page header and actual start. */
1166 overhead += print_overhead(out, "page post-header alignments",
1167 (hdr_size - hdr_min) * num_pages, poolsize);
1168 /* Between last element and end of page. */
1169 page_size = (1 << sp_bits);
1170 if (large_page_bucket(bucket, sp_bits))
1171 page_size <<= BITS_FROM_SMALL_TO_LARGE_PAGE;
1173 overhead += print_overhead(out, "page tails",
1174 (page_size - (hdr_size
1176 * bucket_to_size(bucket))))
1177 * num_pages, poolsize);
1181 void alloc_visualize(FILE *out, void *pool, unsigned long poolsize)
1183 struct header *head = pool;
1184 unsigned long i, lp_bits, sp_bits, header_size, num_buckets, count,
1187 fprintf(out, "Pool %p size %lu: (%s allocator)\n", pool, poolsize,
1188 poolsize < MIN_USEFUL_SIZE ? "tiny" : "standard");
1190 if (poolsize < MIN_USEFUL_SIZE) {
1191 tiny_alloc_visualize(out, pool, poolsize);
1195 sp_bits = small_page_bits(poolsize);
1196 lp_bits = sp_bits + BITS_FROM_SMALL_TO_LARGE_PAGE;
1198 num_buckets = max_bucket(lp_bits);
1199 header_size = sizeof(*head) + sizeof(head->bs) * (num_buckets-1);
1201 fprintf(out, "Large page size %lu, small page size %lu.\n",
1202 1UL << lp_bits, 1UL << sp_bits);
1203 overhead += print_overhead(out, "unused pool tail",
1204 poolsize % (1 << lp_bits), poolsize);
1205 fprintf(out, "Main header %lu bytes (%lu small pages).\n",
1206 header_size, align_up(header_size, 1 << sp_bits) >> sp_bits);
1207 overhead += print_overhead(out, "partial header page",
1208 align_up(header_size, 1 << sp_bits)
1209 - header_size, poolsize);
1210 /* Total large pages. */
1211 i = count_bits(head->pagesize, poolsize >> lp_bits);
1213 count = i - count_list(head, head->large_free_list, sp_bits, NULL);
1214 fprintf(out, "%lu/%lu large pages used (%.3g%%)\n",
1215 count, i, count ? 100.0 * count / i : 0.0);
1217 /* Total small pages. */
1218 i = ((poolsize >> lp_bits) - i) << BITS_FROM_SMALL_TO_LARGE_PAGE;
1220 count = i - count_list(head, head->small_free_list, sp_bits, NULL);
1221 fprintf(out, "%lu/%lu small pages used (%.3g%%)\n",
1222 count, i, count ? 100.0 * count / i : 0.0);
1224 /* Summary of each bucket. */
1225 fprintf(out, "%lu buckets:\n", num_buckets);
1226 for (i = 0; i < num_buckets; i++)
1227 overhead += visualize_bucket(out, head, i, poolsize, sp_bits);
1229 print_overhead(out, "total", overhead, poolsize);