1 /* slabs memory allocation */
2 #ifndef SLABS_H
3 #define SLABS_H
4 
5 #include "default_engine.h"
6 
7 
8 
9 /* powers-of-N allocation structures */
10 
11 typedef struct {
12     unsigned int size;      /* sizes of items */
13     unsigned int perslab;   /* how many items per slab */
14 
15     void **slots;           /* list of item ptrs */
16     unsigned int sl_total;  /* size of previous array */
17     unsigned int sl_curr;   /* first free slot */
18 
19     void *end_page_ptr;         /* pointer to next free item at end of page, or 0 */
20     unsigned int end_page_free; /* number of items remaining at end of last alloced page */
21 
22     unsigned int slabs;     /* how many slabs were allocated for this class */
23 
24     void **slab_list;       /* array of slab pointers */
25     unsigned int list_size; /* size of prev array */
26 
27     unsigned int killing;  /* index+1 of dying slab, or zero if none */
28     size_t requested; /* The number of requested bytes */
29 } slabclass_t;
30 
31 struct slabs {
32    slabclass_t slabclass[MAX_NUMBER_OF_SLAB_CLASSES];
33    size_t mem_limit;
34    size_t mem_malloced;
35    int power_largest;
36 
37    void *mem_base;
38    void *mem_current;
39    size_t mem_avail;
40 
41    /**
42     * Access to the slab allocator is protected by this lock
43     */
44    pthread_mutex_t lock;
45 };
46 
47 
48 
49 
50 /** Init the subsystem. 1st argument is the limit on no. of bytes to allocate,
51     0 if no limit. 2nd argument is the growth factor; each slab will use a chunk
52     size equal to the previous slab's chunk size times this factor.
53     3rd argument specifies if the slab allocator should allocate all memory
54     up front (if true), or allocate memory in chunks as it is needed (if false)
55 */
56 ENGINE_ERROR_CODE slabs_init(struct default_engine *engine,
57                              const size_t limit,
58                              const double factor,
59                              const bool prealloc);
60 
61 
62 /**
63  * Given object size, return id to use when allocating/freeing memory for object
64  * 0 means error: can't store such a large object
65  */
66 
67 unsigned int slabs_clsid(struct default_engine *engine, const size_t size);
68 
69 /** Allocate object of given length. 0 on error */ /*@null@*/
70 void *slabs_alloc(struct default_engine *engine, const size_t size, unsigned int id);
71 
72 /** Free previously allocated object */
73 void slabs_free(struct default_engine *engine, void *ptr, size_t size, unsigned int id);
74 
75 /** Adjust the stats for memory requested */
76 void slabs_adjust_mem_requested(struct default_engine *engine, unsigned int id, size_t old, size_t ntotal);
77 
78 /** Fill buffer with stats */ /*@null@*/
79 void slabs_stats(struct default_engine *engine, ADD_STAT add_stats, const void *c);
80 
81 void add_statistics(const void *cookie, ADD_STAT add_stats,
82                     const char *prefix, int num, const char *key,
83                     const char *fmt, ...);
84 
85 #endif
86