- int b_scale = GET_SCALE(n);
- assert(b_scale <= MAX_SCALE);
- TRACE("m0", "nbd_malloc(): size %llu scale %llu", n, b_scale);
- private_list_t *pri = &pri_free_list_[tid_][b_scale]; // our private free list
- TRACE("m0", "nbd_malloc(): private free list first block %p", pri->head, 0);
-
- // If our private free list is empty, try to find blocks on our public free list. If that fails,
- // allocate a new region.
- if (EXPECT_FALSE(pri->head == NULL)) {
- block_t **pubs = pub_free_list_[tid_][b_scale]; // our public free lists
- while (1) {
- // look for blocks on our public free lists round robin
- pri->next_pub = (pri->next_pub+1) & (MAX_NUM_THREADS-1);
-
- TRACE("m0", "nbd_malloc(): searching public free list %llu", pri->next_pub, 0);
- if (pri->next_pub == tid_) {
- uint32_t count = pri->count;
- pri->count = 0;
- // If our private list is empty and we haven't gotten at least half a region's worth
- // of block's from our public lists, we allocate a new region. This guarentees that
- // we amortize the cost of accessing our public lists accross enough nbd_malloc()
- // calls.
- uint32_t min_count = b_scale > REGION_SCALE ? 1 << (b_scale-REGION_SCALE-1) : 1;
- if (count < min_count) {
- char *region = get_new_region(b_scale);
- size_t b_size = 1 << b_scale;
- size_t region_size = (b_size < REGION_SIZE) ? REGION_SIZE : b_size;
- for (int i = region_size; i != 0; i -= b_size) {
- block_t *b = (block_t *)(region + i - b_size);
- b->next = pri->head;
- pri->head = b;
- }
- break;
- }
- continue;
- }