MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / alloc_from_reserved_mem

Function alloc_from_reserved_mem

dlmalloc/dlmalloc.c:4828–4930  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4826}
4827
4828static void *alloc_from_reserved_mem(mstate ms, size_t bytes, size_t *nactualbytes)
4829{
4830 void* mem = 0;
4831 size_t nb;
4832 if (bytes <= MAX_SMALL_REQUEST) {
4833 bindex_t idx;
4834 binmap_t smallbits;
4835 nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
4836 idx = small_index(nb);
4837 smallbits = ms->smallmap >> idx;
4838
4839 if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
4840 mchunkptr b, p;
4841 idx += ~smallbits & 1; /* Uses next bin if idx empty */
4842 b = smallbin_at(ms, idx);
4843 p = b->fd;
4844 assert(chunksize(p) == small_index2size(idx));
4845 unlink_first_small_chunk(ms, b, p, idx);
4846 set_inuse_and_pinuse(ms, p, small_index2size(idx));
4847 mem = chunk2mem(p);
4848 check_malloced_chunk(ms, mem, nb);
4849 goto postaction;
4850 }
4851
4852 else if (nb > ms->dvsize) {
4853 if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
4854 mchunkptr b, p, r;
4855 size_t rsize;
4856 bindex_t i;
4857 binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
4858 binmap_t leastbit = least_bit(leftbits);
4859 compute_bit2idx(leastbit, i);
4860 b = smallbin_at(ms, i);
4861 p = b->fd;
4862 assert(chunksize(p) == small_index2size(i));
4863 unlink_first_small_chunk(ms, b, p, i);
4864 rsize = small_index2size(i) - nb;
4865 /* Fit here cannot be remainderless if 4byte sizes */
4866 if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
4867 set_inuse_and_pinuse(ms, p, small_index2size(i));
4868 else {
4869 set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
4870 r = chunk_plus_offset(p, nb);
4871 set_size_and_pinuse_of_free_chunk(r, rsize);
4872 replace_dv(ms, r, rsize);
4873 }
4874 mem = chunk2mem(p);
4875 check_malloced_chunk(ms, mem, nb);
4876 goto postaction;
4877 }
4878
4879 else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) {
4880 check_malloced_chunk(ms, mem, nb);
4881 goto postaction;
4882 }
4883 }
4884 }
4885 else if (bytes >= MAX_REQUEST)

Callers 2

mspace_mallocFunction · 0.85
mspace_callocFunction · 0.85

Calls 2

tmalloc_smallFunction · 0.85
tmalloc_largeFunction · 0.85

Tested by

no test coverage detected