10 size_t slot_size; /* Size of each element in bytes. */
11 struct slot *free_list; /* Unused slots. */
12 struct arena *arenas; /* Arenas. */
17 struct desc *desc; /* Owning descriptor. */
18 struct arena *prev, *next; /* Doubly linked list of arenas. */
23 struct slot *next; /* Singly linked list of slots. */
26 struct desc descs[16];
34 for (slot_size = 16; slot_size < PGSIZE; slot_size *= 2)
36 struct desc *d = &descs[desc_cnt++];
37 ASSERT (desc_cnt <= sizeof descs / sizeof *descs);
38 d->slot_size = slot_size;
45 slot_to_arena (struct slot *s)
47 return (struct arena *) ((uint32_t) s & ~(PGSIZE - 1));
51 get_free_slot (struct desc *d)
53 void *block = d->free_list;
54 ASSERT (block != NULL);
55 d->free_list = d->free_list->next;
69 for (d = descs; d < descs + desc_cnt; d++)
70 if (size < d->slot_size)
72 if (d == descs + desc_cnt)
74 printk ("malloc: %zu byte allocation too big\n", size);
78 if (d->free_list != NULL)
79 return get_free_slot (d);
88 if (d->arenas != NULL)
90 for (ofs = sizeof *a; ofs + d->slot_size <= PGSIZE; ofs += d->slot_size)
92 struct slot *s = (struct slot *) ((uint8_t *) a + ofs);
93 s->next = d->free_list;
97 return get_free_slot (d);
101 calloc (size_t a, size_t b)
107 if (size < a || size < b)
121 struct arena *a = slot_to_arena (s);
122 struct desc *d = a->desc;
124 s->next = d->free_list;