1 1.1 christos #ifndef JEMALLOC_INTERNAL_BIN_H 2 1.1 christos #define JEMALLOC_INTERNAL_BIN_H 3 1.1 christos 4 1.1 christos #include "jemalloc/internal/extent_types.h" 5 1.1 christos #include "jemalloc/internal/extent_structs.h" 6 1.1 christos #include "jemalloc/internal/mutex.h" 7 1.1 christos #include "jemalloc/internal/bin_stats.h" 8 1.1 christos 9 1.1 christos /* 10 1.1 christos * A bin contains a set of extents that are currently being used for slab 11 1.1 christos * allocations. 12 1.1 christos */ 13 1.1 christos 14 1.1 christos /* 15 1.1 christos * Read-only information associated with each element of arena_t's bins array 16 1.1 christos * is stored separately, partly to reduce memory usage (only one copy, rather 17 1.1 christos * than one per arena), but mainly to avoid false cacheline sharing. 18 1.1 christos * 19 1.1 christos * Each slab has the following layout: 20 1.1 christos * 21 1.1 christos * /--------------------\ 22 1.1 christos * | region 0 | 23 1.1 christos * |--------------------| 24 1.1 christos * | region 1 | 25 1.1 christos * |--------------------| 26 1.1 christos * | ... | 27 1.1 christos * | ... | 28 1.1 christos * | ... | 29 1.1 christos * |--------------------| 30 1.1 christos * | region nregs-1 | 31 1.1 christos * \--------------------/ 32 1.1 christos */ 33 1.1 christos typedef struct bin_info_s bin_info_t; 34 1.1 christos struct bin_info_s { 35 1.1 christos /* Size of regions in a slab for this bin's size class. */ 36 1.1 christos size_t reg_size; 37 1.1 christos 38 1.1 christos /* Total size of a slab for this bin's size class. */ 39 1.1 christos size_t slab_size; 40 1.1 christos 41 1.1 christos /* Total number of regions in a slab for this bin's size class. */ 42 1.1 christos uint32_t nregs; 43 1.1 christos 44 1.1 christos /* 45 1.1 christos * Metadata used to manipulate bitmaps for slabs associated with this 46 1.1 christos * bin. 47 1.1 christos */ 48 1.1 christos bitmap_info_t bitmap_info; 49 1.1 christos }; 50 1.1 christos 51 1.1 christos extern const bin_info_t bin_infos[NBINS]; 52 1.1 christos 53 1.1 christos 54 1.1 christos typedef struct bin_s bin_t; 55 1.1 christos struct bin_s { 56 1.1 christos /* All operations on bin_t fields require lock ownership. */ 57 1.1 christos malloc_mutex_t lock; 58 1.1 christos 59 1.1 christos /* 60 1.1 christos * Current slab being used to service allocations of this bin's size 61 1.1 christos * class. slabcur is independent of slabs_{nonfull,full}; whenever 62 1.1 christos * slabcur is reassigned, the previous slab must be deallocated or 63 1.1 christos * inserted into slabs_{nonfull,full}. 64 1.1 christos */ 65 1.1 christos extent_t *slabcur; 66 1.1 christos 67 1.1 christos /* 68 1.1 christos * Heap of non-full slabs. This heap is used to assure that new 69 1.1 christos * allocations come from the non-full slab that is oldest/lowest in 70 1.1 christos * memory. 71 1.1 christos */ 72 1.1 christos extent_heap_t slabs_nonfull; 73 1.1 christos 74 1.1 christos /* List used to track full slabs. */ 75 1.1 christos extent_list_t slabs_full; 76 1.1 christos 77 1.1 christos /* Bin statistics. */ 78 1.1 christos bin_stats_t stats; 79 1.1 christos }; 80 1.1 christos 81 1.1 christos /* Initializes a bin to empty. Returns true on error. */ 82 1.1 christos bool bin_init(bin_t *bin); 83 1.1 christos 84 1.1 christos /* Forking. */ 85 1.1 christos void bin_prefork(tsdn_t *tsdn, bin_t *bin); 86 1.1 christos void bin_postfork_parent(tsdn_t *tsdn, bin_t *bin); 87 1.1 christos void bin_postfork_child(tsdn_t *tsdn, bin_t *bin); 88 1.1 christos 89 1.1 christos /* Stats. */ 90 1.1 christos static inline void 91 1.1 christos bin_stats_merge(tsdn_t *tsdn, bin_stats_t *dst_bin_stats, bin_t *bin) { 92 1.1 christos malloc_mutex_lock(tsdn, &bin->lock); 93 1.1 christos malloc_mutex_prof_read(tsdn, &dst_bin_stats->mutex_data, &bin->lock); 94 1.1 christos dst_bin_stats->nmalloc += bin->stats.nmalloc; 95 1.1 christos dst_bin_stats->ndalloc += bin->stats.ndalloc; 96 1.1 christos dst_bin_stats->nrequests += bin->stats.nrequests; 97 1.1 christos dst_bin_stats->curregs += bin->stats.curregs; 98 1.1 christos dst_bin_stats->nfills += bin->stats.nfills; 99 1.1 christos dst_bin_stats->nflushes += bin->stats.nflushes; 100 1.1 christos dst_bin_stats->nslabs += bin->stats.nslabs; 101 1.1 christos dst_bin_stats->reslabs += bin->stats.reslabs; 102 1.1 christos dst_bin_stats->curslabs += bin->stats.curslabs; 103 1.1 christos malloc_mutex_unlock(tsdn, &bin->lock); 104 1.1 christos } 105 1.1 christos 106 1.1 christos #endif /* JEMALLOC_INTERNAL_BIN_H */ 107