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