MCPcopy Create free account
hub / github.com/1a1a11a/libCacheSim / _malloc

Method _malloc

libCacheSim/dataStructure/sparsepp/spp_dlalloc.h:2996–3119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2994/* ---------------------------- malloc --------------------------- */
2995
2996void* malloc_state::_malloc(size_t bytes)
2997{
2998 if (1)
2999 {
3000 void* mem;
3001 size_t nb;
3002 if (bytes <= MAX_SMALL_REQUEST)
3003 {
3004 bindex_t idx;
3005 binmap_t smallbits;
3006 nb = (bytes < MIN_REQUEST) ? MIN_CHUNK_SIZE : pad_request(bytes);
3007 idx = small_index(nb);
3008 smallbits = _smallmap >> idx;
3009
3010 if ((smallbits & 0x3U) != 0)
3011 {
3012 // Remainderless fit to a smallbin.
3013 mchunkptr b, p;
3014 idx += ~smallbits & 1; // Uses next bin if idx empty
3015 b = smallbin_at(idx);
3016 p = b->_fd;
3017 assert(p->chunksize() == small_index2size(idx));
3018 unlink_first_small_chunk(b, p, idx);
3019 set_inuse_and_pinuse(p, small_index2size(idx));
3020 mem = chunk2mem(p);
3021 check_malloced_chunk(mem, nb);
3022 goto postaction;
3023 }
3024
3025 else if (nb > _dvsize)
3026 {
3027 if (smallbits != 0)
3028 {
3029 // Use chunk in next nonempty smallbin
3030 mchunkptr b, p, r;
3031 size_t rsize;
3032 binmap_t leftbits = (smallbits << idx) & left_bits(malloc_state::idx2bit(idx));
3033 binmap_t leastbit = least_bit(leftbits);
3034 bindex_t i = compute_bit2idx(leastbit);
3035 b = smallbin_at(i);
3036 p = b->_fd;
3037 assert(p->chunksize() == small_index2size(i));
3038 unlink_first_small_chunk(b, p, i);
3039 rsize = small_index2size(i) - nb;
3040 // Fit here cannot be remainderless if 4byte sizes
3041 if (sizeof(size_t) != 4 && rsize < MIN_CHUNK_SIZE)
3042 set_inuse_and_pinuse(p, small_index2size(i));
3043 else
3044 {
3045 set_size_and_pinuse_of_inuse_chunk(p, nb);
3046 r = (mchunkptr)p->chunk_plus_offset(nb);
3047 r->set_size_and_pinuse_of_free_chunk(rsize);
3048 replace_dv(r, rsize);
3049 }
3050 mem = chunk2mem(p);
3051 check_malloced_chunk(mem, nb);
3052 goto postaction;
3053 }

Callers 1

mspace_mallocFunction · 0.80

Calls 6

pad_requestFunction · 0.85
assertFunction · 0.85
chunk2memFunction · 0.85
chunksizeMethod · 0.80
chunk_plus_offsetMethod · 0.80

Tested by

no test coverage detected