7 #include "build_assert/build_assert.h"
11 #define ALIGNOF(t) __alignof__(t)
13 /* Alignment by measuring structure padding. */
14 #define ALIGNOF(t) (sizeof(struct { char c; t _h; }) - 1 - sizeof(t))
17 /* FIXME: Doesn't handle non-page-aligned poolsize. */
20 #define MIN_SIZE (getpagesize() * 2)
22 /* What's the granularity of sub-page allocs? */
23 #define BITMAP_GRANULARITY 4
27 * file := pagestates pad metadata
28 * pagestates := pages * 2-bits-per-page
29 * pad := pad to next ALIGNOF(metadata)
31 * metadata := metalen next-ptr metabits
32 * metabits := freeblock | bitblock
34 * bitblock := 2-bits-per-bit-in-page 1
38 /* Length (after this header). (FIXME: Could be in pages). */
39 unsigned long metalen;
40 /* Next meta header, or 0 */
42 /* Bits start here. */
45 #define BITS_PER_PAGE 2
46 /* FIXME: Don't use page states for bitblock. It's tacky and confusing. */
55 /* Assumes a is a power of two. */
56 static unsigned long align_up(unsigned long x, unsigned long a)
58 return (x + a - 1) & ~(a - 1);
61 static unsigned long div_up(unsigned long x, unsigned long a)
63 return (x + a - 1) / a;
66 /* The offset of metadata for a subpage allocation is found at the end
68 #define SUBPAGE_METAOFF (getpagesize() - sizeof(unsigned long))
70 /* This is the length of metadata in bits. It consists of two bits
71 * for every BITMAP_GRANULARITY of usable bytes in the page, then two
72 * bits for the TAKEN tailer.. */
73 #define BITMAP_METABITLEN \
74 ((div_up(SUBPAGE_METAOFF, BITMAP_GRANULARITY) + 1) * BITS_PER_PAGE)
76 /* This is the length in bytes. */
77 #define BITMAP_METALEN (div_up(BITMAP_METABITLEN, CHAR_BIT))
79 static enum page_state get_page_state(const uint8_t *bits, unsigned long page)
81 return bits[page * 2 / CHAR_BIT] >> (page * 2 % CHAR_BIT) & 3;
84 static void set_page_state(uint8_t *bits, unsigned long page, enum page_state s)
86 bits[page * 2 / CHAR_BIT] &= ~(3 << (page * 2 % CHAR_BIT));
87 bits[page * 2 / CHAR_BIT] |= ((uint8_t)s << (page * 2 % CHAR_BIT));
90 static struct metaheader *first_mheader(void *pool, unsigned long poolsize)
92 unsigned int pagestatelen;
94 pagestatelen = align_up(div_up(poolsize/getpagesize() * BITS_PER_PAGE,
96 ALIGNOF(struct metaheader));
97 return (struct metaheader *)((char *)pool + pagestatelen);
100 static struct metaheader *next_mheader(void *pool, struct metaheader *mh)
105 return (struct metaheader *)((char *)pool + mh->next);
108 static unsigned long pool_offset(void *pool, void *p)
110 return (char *)p - (char *)pool;
113 void alloc_init(void *pool, unsigned long poolsize)
115 /* FIXME: Alignment assumptions about pool. */
116 unsigned long len, i;
117 struct metaheader *mh;
119 if (poolsize < MIN_SIZE)
122 mh = first_mheader(pool, poolsize);
124 /* len covers all page states, plus the metaheader. */
125 len = (char *)(mh + 1) - (char *)pool;
126 /* Mark all page states FREE */
127 BUILD_ASSERT(FREE == 0);
128 memset(pool, 0, len);
130 /* metaheader len takes us up to next page boundary. */
131 mh->metalen = align_up(len, getpagesize()) - len;
133 /* Mark the pagestate and metadata page(s) allocated. */
134 set_page_state(pool, 0, TAKEN_START);
135 for (i = 1; i < div_up(len, getpagesize()); i++)
136 set_page_state(pool, i, TAKEN);
139 /* Two bits per element, representing page states. Returns 0 on fail. */
140 static unsigned long alloc_from_bitmap(uint8_t *bits, unsigned long elems,
141 unsigned long want, unsigned long align)
147 /* We allocate from far end, to increase ability to expand metadata. */
148 for (i = elems - 1; i >= 0; i--) {
149 switch (get_page_state(bits, i)) {
151 if (++free >= want) {
154 /* They might ask for large alignment. */
155 if (align && i % align)
158 for (j = i+1; j < i + want; j++)
159 set_page_state(bits, j, TAKEN);
160 set_page_state(bits, i, TAKEN_START);
175 static unsigned long alloc_get_pages(void *pool, unsigned long poolsize,
176 unsigned long pages, unsigned long align)
182 /* We allocate from far end, to increase ability to expand metadata. */
183 for (i = poolsize / getpagesize() - 1; i >= 0; i--) {
184 switch (get_page_state(pool, i)) {
186 if (++free >= pages) {
187 unsigned long j, addr;
189 addr = (unsigned long)pool + i * getpagesize();
191 /* They might ask for multi-page alignment. */
195 for (j = i+1; j < i + pages; j++)
196 set_page_state(pool, j, TAKEN);
197 set_page_state(pool, i, TAKEN_START);
212 /* Offset to metadata is at end of page. */
213 static unsigned long *metadata_off(void *pool, unsigned long page)
215 return (unsigned long *)
216 ((char *)pool + (page+1)*getpagesize() - sizeof(unsigned long));
219 static uint8_t *get_page_metadata(void *pool, unsigned long page)
221 return (uint8_t *)pool + *metadata_off(pool, page);
224 static void set_page_metadata(void *pool, unsigned long page, uint8_t *meta)
226 *metadata_off(pool, page) = meta - (uint8_t *)pool;
229 static void *sub_page_alloc(void *pool, unsigned long page,
230 unsigned long size, unsigned long align)
232 uint8_t *bits = get_page_metadata(pool, page);
235 /* TAKEN at end means a bitwise alloc. */
236 assert(get_page_state(bits, getpagesize()/BITMAP_GRANULARITY - 1)
239 /* Our bits are the same as the page bits. */
240 i = alloc_from_bitmap(bits, SUBPAGE_METAOFF/BITMAP_GRANULARITY,
241 div_up(size, BITMAP_GRANULARITY),
242 align / BITMAP_GRANULARITY);
244 /* Can't allocate? */
248 return (char *)pool + page*getpagesize() + i*BITMAP_GRANULARITY;
251 static uint8_t *alloc_metaspace(struct metaheader *mh, unsigned long bytes)
253 uint8_t *meta = (uint8_t *)(mh + 1);
254 unsigned long free = 0, len;
257 /* TAKEN tags end a subpage alloc. */
258 for (i = mh->metalen * CHAR_BIT / BITS_PER_PAGE - 1; i >= 0; i -= len) {
259 switch (get_page_state(meta, i)) {
263 if (free == bytes * CHAR_BIT / BITS_PER_PAGE) {
264 /* TAKEN marks end of metablock. */
265 set_page_state(meta, i + free - 1, TAKEN);
266 return meta + i / (CHAR_BIT / BITS_PER_PAGE);
270 /* Skip over this allocated part. */
271 len = BITMAP_METALEN * CHAR_BIT / BITS_PER_PAGE;
282 /* We need this many bytes of metadata. */
283 static uint8_t *new_metadata(void *pool, unsigned long poolsize,
286 struct metaheader *mh, *newmh;
289 for (mh = first_mheader(pool,poolsize); mh; mh = next_mheader(pool,mh)){
290 uint8_t *meta = alloc_metaspace(mh, bytes);
296 /* No room for metadata? Can we expand an existing one? */
297 for (mh = first_mheader(pool,poolsize); mh; mh = next_mheader(pool,mh)){
298 /* It should end on a page boundary. */
299 unsigned long nextpage;
301 nextpage = pool_offset(pool, (char *)(mh + 1) + mh->metalen);
302 assert(nextpage % getpagesize() == 0);
304 /* Now, can we grab that page? */
305 if (get_page_state(pool, nextpage / getpagesize()) != FREE)
308 /* OK, expand metadata, do it again. */
309 set_page_state(pool, nextpage / getpagesize(), TAKEN);
310 BUILD_ASSERT(FREE == 0);
311 memset((char *)pool + nextpage, 0, getpagesize());
312 mh->metalen += getpagesize();
313 return alloc_metaspace(mh, bytes);
316 /* No metadata left at all? */
317 page = alloc_get_pages(pool, poolsize, div_up(bytes, getpagesize()), 1);
321 newmh = (struct metaheader *)((char *)pool + page * getpagesize());
322 newmh->metalen = getpagesize() - sizeof(*mh);
323 BUILD_ASSERT(FREE == 0);
324 memset(newmh + 1, 0, newmh->metalen);
326 /* Sew it into linked list */
327 mh = first_mheader(pool,poolsize);
328 newmh->next = mh->next;
329 mh->next = (char *)newmh - (char *)pool;
331 return alloc_metaspace(newmh, bytes);
334 static void alloc_free_pages(void *pool, unsigned long pagenum)
336 assert(get_page_state(pool, pagenum) == TAKEN_START);
337 set_page_state(pool, pagenum, FREE);
338 while (get_page_state(pool, ++pagenum) == TAKEN)
339 set_page_state(pool, pagenum, FREE);
342 static void *alloc_sub_page(void *pool, unsigned long poolsize,
343 unsigned long size, unsigned long align)
348 /* Look for partial page. */
349 for (i = 0; i < poolsize / getpagesize(); i++) {
351 if (get_page_state(pool, i) != SUBPAGE)
354 ret = sub_page_alloc(pool, i, size, align);
359 /* Create new SUBPAGE page. */
360 i = alloc_get_pages(pool, poolsize, 1, 1);
364 /* Get metadata for page. */
365 metadata = new_metadata(pool, poolsize, BITMAP_METALEN);
367 alloc_free_pages(pool, i);
371 /* Actually, this is a SUBPAGE page now. */
372 set_page_state(pool, i, SUBPAGE);
374 /* Set metadata pointer for page. */
375 set_page_metadata(pool, i, metadata);
377 /* Do allocation like normal */
378 return sub_page_alloc(pool, i, size, align);
381 void *alloc_get(void *pool, unsigned long poolsize,
382 unsigned long size, unsigned long align)
384 if (poolsize < MIN_SIZE)
387 /* Sub-page allocations have an overhead of 25%. */
388 if (size + size/4 >= getpagesize() || align >= getpagesize()) {
389 unsigned long ret, pages = div_up(size, getpagesize());
391 ret = alloc_get_pages(pool, poolsize, pages, align);
394 return (char *)pool + ret * getpagesize();
397 return alloc_sub_page(pool, poolsize, size, align);
400 static void subpage_free(void *pool, unsigned long pagenum, void *free)
402 unsigned long off = (unsigned long)free % getpagesize();
405 assert(off < SUBPAGE_METAOFF);
406 assert(off % BITMAP_GRANULARITY == 0);
408 metadata = get_page_metadata(pool, pagenum);
410 off /= BITMAP_GRANULARITY;
412 set_page_state(metadata, off++, FREE);
413 while (off < SUBPAGE_METAOFF / BITMAP_GRANULARITY
414 && get_page_state(metadata, off) == TAKEN)
415 set_page_state(metadata, off++, FREE);
417 /* FIXME: If whole page free, free page and metadata. */
420 void alloc_free(void *pool, unsigned long poolsize, void *free)
422 unsigned long pagenum;
423 struct metaheader *mh;
428 assert(poolsize >= MIN_SIZE);
430 mh = first_mheader(pool, poolsize);
431 assert((char *)free >= (char *)(mh + 1) + mh->metalen);
432 assert((char *)pool + poolsize > (char *)free);
434 pagenum = pool_offset(pool, free) / getpagesize();
436 if (get_page_state(pool, pagenum) == SUBPAGE)
437 subpage_free(pool, pagenum, free);
439 assert((unsigned long)free % getpagesize() == 0);
440 alloc_free_pages(pool, pagenum);
444 static bool is_metadata_page(void *pool, unsigned long poolsize,
447 struct metaheader *mh;
449 for (mh = first_mheader(pool,poolsize); mh; mh = next_mheader(pool,mh)){
450 unsigned long start, end;
452 start = pool_offset(pool, mh);
453 end = pool_offset(pool, (char *)(mh+1) + mh->metalen);
454 if (page >= start/getpagesize() && page < end/getpagesize())
460 static bool check_subpage(void *pool, unsigned long poolsize,
463 unsigned long *mhoff = metadata_off(pool, page);
465 enum page_state last_state = FREE;
467 if (*mhoff + sizeof(struct metaheader) > poolsize)
470 if (*mhoff % ALIGNOF(struct metaheader) != 0)
473 /* It must point to a metadata page. */
474 if (!is_metadata_page(pool, poolsize, *mhoff / getpagesize()))
477 /* Marker at end of subpage allocation is "taken" */
478 if (get_page_state((uint8_t *)pool + *mhoff,
479 getpagesize()/BITMAP_GRANULARITY - 1) != TAKEN)
482 for (i = 0; i < SUBPAGE_METAOFF / BITMAP_GRANULARITY; i++) {
483 enum page_state state;
485 state = get_page_state((uint8_t *)pool + *mhoff, i);
490 if (last_state == FREE)
501 bool alloc_check(void *pool, unsigned long poolsize)
504 struct metaheader *mh;
505 enum page_state last_state = FREE;
506 bool was_metadata = false;
508 if (poolsize < MIN_SIZE)
511 if (get_page_state(pool, 0) != TAKEN_START)
514 /* First check metadata pages. */
515 /* Metadata pages will be marked TAKEN. */
516 for (mh = first_mheader(pool,poolsize); mh; mh = next_mheader(pool,mh)){
517 unsigned long start, end;
519 start = pool_offset(pool, mh);
520 if (start + sizeof(*mh) > poolsize)
523 end = pool_offset(pool, (char *)(mh+1) + mh->metalen);
527 /* Non-first pages should start on a page boundary. */
528 if (mh != first_mheader(pool, poolsize)
529 && start % getpagesize() != 0)
532 /* It should end on a page boundary. */
533 if (end % getpagesize() != 0)
537 for (i = 0; i < poolsize / getpagesize(); i++) {
538 enum page_state state = get_page_state(pool, i);
539 bool is_metadata = is_metadata_page(pool, poolsize,i);
543 /* metadata pages are never free. */
549 /* This should continue a previous block. */
550 if (last_state == FREE)
552 if (is_metadata != was_metadata)
556 /* Check metadata pointer etc. */
557 if (!check_subpage(pool, poolsize, i))
561 was_metadata = is_metadata;
566 void alloc_visualize(FILE *out, void *pool, unsigned long poolsize)
568 struct metaheader *mh;
569 unsigned long pagebitlen, metadata_pages, count[1<<BITS_PER_PAGE], tot;
572 if (poolsize < MIN_SIZE) {
573 fprintf(out, "Pool smaller than %u: no content\n", MIN_SIZE);
577 memset(count, 0, sizeof(count));
578 for (i = 0; i < poolsize / getpagesize(); i++)
579 count[get_page_state(pool, i)]++;
581 mh = first_mheader(pool, poolsize);
582 pagebitlen = (char *)mh - (char *)pool;
583 fprintf(out, "%lu bytes of page bits: FREE/TAKEN/TAKEN_START/SUBPAGE = %lu/%lu/%lu/%lu\n",
584 pagebitlen, count[0], count[1], count[2], count[3]);
586 /* One metadata page for every page of page bits. */
587 metadata_pages = div_up(pagebitlen, getpagesize());
589 /* Now do each metadata page. */
590 for (; mh; mh = next_mheader(pool,mh)) {
591 unsigned long free = 0, subpageblocks = 0, len = 0;
592 uint8_t *meta = (uint8_t *)(mh + 1);
594 metadata_pages += (sizeof(*mh) + mh->metalen) / getpagesize();
596 /* TAKEN tags end a subpage alloc. */
597 for (i = mh->metalen * CHAR_BIT/BITS_PER_PAGE - 1;
600 switch (get_page_state(meta, i)) {
606 /* Skip over this allocated part. */
607 len = BITMAP_METALEN * CHAR_BIT;
615 fprintf(out, "Metadata %lu-%lu: %lu free, %lu subpageblocks, %lu%% density\n",
616 pool_offset(pool, mh),
617 pool_offset(pool, (char *)(mh+1) + mh->metalen),
619 subpageblocks * BITMAP_METALEN * 100
620 / (free + subpageblocks * BITMAP_METALEN));
623 /* Account for total pages allocated. */
624 tot = (count[1] + count[2] - metadata_pages) * getpagesize();
626 fprintf(out, "Total metadata bytes = %lu\n",
627 metadata_pages * getpagesize());
629 /* Now do every subpage. */
630 for (i = 0; i < poolsize / getpagesize(); i++) {
633 if (get_page_state(pool, i) != SUBPAGE)
636 memset(count, 0, sizeof(count));
637 meta = get_page_metadata(pool, i);
638 for (j = 0; j < SUBPAGE_METAOFF/BITMAP_GRANULARITY; j++)
639 count[get_page_state(meta, j)]++;
641 fprintf(out, "Subpage %lu: "
642 "FREE/TAKEN/TAKEN_START = %lu/%lu/%lu %lu%% density\n",
643 i, count[0], count[1], count[2],
644 ((count[1] + count[2]) * BITMAP_GRANULARITY) * 100
646 tot += (count[1] + count[2]) * BITMAP_GRANULARITY;
649 /* This is optimistic, since we overalloc in several cases. */
650 fprintf(out, "Best possible allocation density = %lu%%\n",
651 tot * 100 / poolsize);