A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SlabAllocator.h
Go to the documentation of this file.
1 
7 #ifndef SLAB_ALLOCATOR_H
8 #define SLAB_ALLOCATOR_H
9 #include <memory>
10 using namespace std;
11 
13 typedef shared_ptr<SlabAllocator> SlabAllocatorPtr;
14 
15 /* Slab sizing definitions. */
16 #define POWER_SMALLEST 1
17 #define POWER_LARGEST 256 /* actual cap is 255 */
18 #define SLAB_GLOBAL_PAGE_POOL 0 /* magic slab class for storing pages for reassignment */
19 #define CHUNK_ALIGN_BYTES 8
20 /* slab class max is a 6-bit number, -1. */
21 #define MAX_NUMBER_OF_SLAB_CLASSES (63 + 1) /* Slab sizing definitions. */
22 #define POWER_SMALLEST 1
23 #define POWER_LARGEST 256 /* actual cap is 255 */
24 #define SLAB_GLOBAL_PAGE_POOL 0 /* magic slab class for storing pages for reassignment */
25 #define CHUNK_ALIGN_BYTES 8
26 /* slab class max is a 6-bit number, -1. */
27 #define MAX_NUMBER_OF_SLAB_CLASSES (63 + 1)
28 
29 /* When adding a setting, be sure to update process_stat_settings */
33 struct settings_t {
34  int verbose = 0;
35  double factor = 2; /* chunk size growth factor */
36  int chunk_size = 24; /* space for a modest key and value */
37  int item_size_max = 10 * 1024 * 1024; /* Maximum item size, and upper end for slabs */
38  bool slab_reassign = false; /* Whether or not slab reassignment is allowed */
39  // int slab_automove; /* Whether or not to automatically move slabs */
40 };
41 
45 typedef struct _stritem {
46  /* Protected by LRU locks */
47  struct _stritem* next;
48  struct _stritem* prev;
49  // uint8_t slabs_clsid; /* which slab class we are in */
50  // int nBytes; /* size of data */
51  char data[]; /* data */
52 } item;
53 
54 
55 /* powers-of-N allocation structures */
56 
57 typedef struct {
58  unsigned int size; /* sizes of items */
59  unsigned int perslab; /* how many items per slab */
60 
61  void* slots; /* list of item ptrs */
62  unsigned int sl_curr; /* total free items in list */
63 
64  unsigned int slabs; /* how many slabs were allocated for this class */
65 
66  void** slab_list; /* array of slab pointers */
67  unsigned int list_size; /* size of prev array */
68 
69  size_t requested; /* The number of requested bytes */
70 } slabclass_t;
71 
72 
74 
75 public:
76  SlabAllocator(const size_t limit, bool opt4hashmap = false);
77  SlabAllocator(void* memPool, const size_t limit, size_t pageSize, size_t alignment);
78  SlabAllocator(void* memPool, const size_t limit, bool opt4hashmap = false);
79  ~SlabAllocator();
80 
87  void init(const size_t limit, const double factor, const bool prealloc);
88  /*@null@*/
90 #define SLABS_ALLOC_NO_NEWPAGE 1
91  void* slabs_alloc(const size_t size);
92  void* slabs_alloc_unsafe(const size_t size);
94  void slabs_free(void* ptr, size_t size);
95  void slabs_free_unsafe(void* ptr, size_t size);
96  void* do_slabs_alloc(const size_t size);
97  void do_slabs_free(void* ptr, const size_t size);
98  void do_slabs_free(void* ptr, const size_t size, unsigned int id);
99  unsigned int slabs_clsid(const size_t size);
100  int do_slabs_newslab(const unsigned int id);
101  void* get_page_from_global_pool(void);
102  int grow_slab_list(const unsigned int id);
103  void* memory_allocate(size_t size);
104  void slabs_preallocate(const unsigned int maxslabs);
105  void split_slab_page_into_freelist(char* ptr, const unsigned int id);
106 
107 private:
108  size_t mem_limit = 0;
109  size_t mem_malloced = 0;
110  bool mem_limit_reached = false;
112  void* mem_base = NULL;
113  void* mem_current = NULL;
114  size_t mem_avail = 0;
115  pthread_mutex_t slabs_lock;
116  struct settings_t settings;
118  bool opt4hashmap = false;
119  bool opt4sharedmem = false;
120  bool useExternalMemory = false;
121 };
122 
123 #endif
struct _stritem * prev
Definition: SlabAllocator.h:48
pthread_mutex_t slabs_lock
shared_ptr< SlabAllocator > SlabAllocatorPtr
Definition: SlabAllocator.h:12
unsigned int perslab
Definition: SlabAllocator.h:59
struct _stritem * next
Definition: SlabAllocator.h:47
void * slots
Definition: SlabAllocator.h:61
unsigned int slabs
Definition: SlabAllocator.h:64
#define MAX_NUMBER_OF_SLAB_CLASSES
Definition: SlabAllocator.h:27
size_t requested
Definition: SlabAllocator.h:69
struct _stritem item
unsigned int size
Definition: SlabAllocator.h:58
unsigned int sl_curr
Definition: SlabAllocator.h:62
char data[]
Definition: SlabAllocator.h:51
unsigned int list_size
Definition: SlabAllocator.h:67
void ** slab_list
Definition: SlabAllocator.h:66