| 5600 | */ |
| 5601 | |
| 5602 | void* mspace_malloc(mspace msp, size_t bytes) { |
| 5603 | mstate ms = (mstate)msp; |
| 5604 | if (!ok_magic(ms)) { |
| 5605 | USAGE_ERROR_ACTION(ms,ms); |
| 5606 | return 0; |
| 5607 | } |
| 5608 | if (!PREACTION(ms)) { |
| 5609 | void* mem; |
| 5610 | size_t nb; |
| 5611 | if (bytes <= MAX_SMALL_REQUEST) { |
| 5612 | bindex_t idx; |
| 5613 | binmap_t smallbits; |
| 5614 | nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); |
| 5615 | idx = small_index(nb); |
| 5616 | smallbits = ms->smallmap >> idx; |
| 5617 | |
| 5618 | if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ |
| 5619 | mchunkptr b, p; |
| 5620 | idx += ~smallbits & 1; /* Uses next bin if idx empty */ |
| 5621 | b = smallbin_at(ms, idx); |
| 5622 | p = b->fd; |
| 5623 | assert(chunksize(p) == small_index2size(idx)); |
| 5624 | unlink_first_small_chunk(ms, b, p, idx); |
| 5625 | set_inuse_and_pinuse(ms, p, small_index2size(idx)); |
| 5626 | mem = chunk2mem(p); |
| 5627 | check_malloced_chunk(ms, mem, nb); |
| 5628 | goto postaction; |
| 5629 | } |
| 5630 | |
| 5631 | else if (nb > ms->dvsize) { |
| 5632 | if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ |
| 5633 | mchunkptr b, p, r; |
| 5634 | size_t rsize; |
| 5635 | bindex_t i; |
| 5636 | binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); |
| 5637 | binmap_t leastbit = least_bit(leftbits); |
| 5638 | compute_bit2idx(leastbit, i); |
| 5639 | b = smallbin_at(ms, i); |
| 5640 | p = b->fd; |
| 5641 | assert(chunksize(p) == small_index2size(i)); |
| 5642 | unlink_first_small_chunk(ms, b, p, i); |
| 5643 | rsize = small_index2size(i) - nb; |
| 5644 | /* Fit here cannot be remainderless if 4byte sizes */ |
| 5645 | if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) |
| 5646 | set_inuse_and_pinuse(ms, p, small_index2size(i)); |
| 5647 | else { |
| 5648 | set_size_and_pinuse_of_inuse_chunk(ms, p, nb); |
| 5649 | r = chunk_plus_offset(p, nb); |
| 5650 | set_size_and_pinuse_of_free_chunk(r, rsize); |
| 5651 | replace_dv(ms, r, rsize); |
| 5652 | } |
| 5653 | mem = chunk2mem(p); |
| 5654 | check_malloced_chunk(ms, mem, nb); |
| 5655 | goto postaction; |
| 5656 | } |
| 5657 | |
| 5658 | else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { |
| 5659 | check_malloced_chunk(ms, mem, nb); |
no test coverage detected