File: MemPool.h

package info (click to toggle)
squid3 3.0.PRE5-5%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,188 kB
  • ctags: 20,388
  • sloc: cpp: 119,851; ansic: 30,259; sh: 10,465; makefile: 3,289; perl: 1,267; awk: 84; xml: 58
file content (321 lines) | stat: -rw-r--r-- 7,652 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

#ifndef _MEM_POOLS_H_
#define _MEM_POOLS_H_

#include "config.h"
#include "assert.h"
#include "util.h"

#include "memMeter.h"
#include "splay.h"

#if HAVE_GNUMALLOC_H
#include <gnumalloc.h>
#elif HAVE_MALLOC_H
#include <malloc.h>
#endif

#if HAVE_MEMORY_H
#include <memory.h>
#endif

#if !M_MMAP_MAX
#if USE_DLMALLOC
#define M_MMAP_MAX -4
#endif
#endif

#define MB ((size_t)1024*1024)
#define mem_unlimited_size 2 * 1024 * MB
#define toMB(size) ( ((double) size) / MB )
#define toKB(size) ( (size + 1024 - 1) / 1024 )

#define MEM_PAGE_SIZE 4096
#define MEM_CHUNK_SIZE 4096 * 4
#define MEM_CHUNK_MAX_SIZE  256 * 1024	/* 2MB */
#define MEM_MIN_FREE  32
#define MEM_MAX_FREE  65535	/* ushort is max number of items per chunk */

class MemImplementingAllocator;
class MemChunk;
class MemPoolStats;

typedef struct _MemPoolGlobalStats MemPoolGlobalStats;

class MemPoolIterator
{
  public:
    MemImplementingAllocator *pool;
    MemPoolIterator * next;
};

/* object to track per-pool cumulative counters */

class mgb_t
{
  public:
    mgb_t() : count(0), bytes(0){}
    double count;
    double bytes;
};

/* object to track per-pool memory usage (alloc = inuse+idle) */

class MemPoolMeter
{
  public:
    void flush();
    MemMeter alloc;
    MemMeter inuse;
    MemMeter idle;
    mgb_t gb_saved;		/* account Allocations */
    mgb_t gb_osaved;		/* history Allocations */
    mgb_t gb_freed;		/* account Free calls */
};

class MemImplementingAllocator;

class MemPools 
{
  public:
    static MemPools &GetInstance();
    MemPools();
    void init();
    void flushMeters();
    MemImplementingAllocator * create(const char *label, size_t obj_size);
    MemImplementingAllocator * create(const char *label, size_t obj_size, bool const chunked);
    void setIdleLimit(size_t new_idle_limit);
    size_t const idleLimit() const;
    void clean(time_t maxage);
    void setDefaultPoolChunking(bool const &);
    MemImplementingAllocator *pools;
    int mem_idle_limit;
    int poolCount;
    bool defaultIsChunked;
  private:
    static MemPools *Instance;
};

/* a pool is a [growing] space for objects of the same size */

class MemAllocator
{
public:
    MemAllocator (char const *aLabel);
    virtual ~MemAllocator() {}
    virtual int getStats(MemPoolStats * stats) = 0;
    virtual MemPoolMeter const &getMeter() const = 0;
    virtual void *alloc() = 0;
    virtual void free(void *) = 0;
    virtual char const *objectType() const;
    virtual size_t objectSize() const = 0;
    virtual int getInUseCount() = 0;
    int inUseCount();
    virtual void setChunkSize(size_t chunksize) {}
private:
    const char *label;
};

/* Support late binding of pool type for allocator agnostic classes */
class MemAllocatorProxy
{
  public:
    inline MemAllocatorProxy(char const *aLabel, size_t const &);
    void *alloc();
    void free(void *);
    int inUseCount() const;
    size_t objectSize() const;
    MemPoolMeter const &getMeter() const;
    int getStats(MemPoolStats * stats);
    char const * objectType() const;
  private:
    MemAllocator *getAllocator() const;
    const char *label;
    size_t size;
    mutable MemAllocator *theAllocator;
};
/* help for classes */
/* Put this in the class */
#define MEMPROXY_CLASS(CLASS) \
/* TODO change syntax to allow moving into .cci files */ \
    inline void *operator new(size_t); \
    inline void operator delete(void *); \
    static inline MemAllocatorProxy &Pool()

/* put this in the class .h, or .cci as appropriate */
#define MEMPROXY_CLASS_INLINE(CLASS) \
MemAllocatorProxy& CLASS::Pool() \
{ \
    static MemAllocatorProxy thePool(#CLASS, sizeof (CLASS)); \
    return thePool; \
} \
\
void * \
CLASS::operator new (size_t byteCount) \
{ \
    /* derived classes with different sizes must implement their own new */ \
    assert (byteCount == sizeof (CLASS)); \
\
    return Pool().alloc(); \
}  \
\
void \
CLASS::operator delete (void *address) \
{ \
    Pool().free(address); \
}

class MemImplementingAllocator : public MemAllocator
{
  public:
    MemImplementingAllocator(char const *aLabel, size_t aSize);
    virtual MemPoolMeter const &getMeter() const;
    virtual MemPoolMeter &getMeter();
    virtual void flushMetersFull();
    virtual void flushMeters();
    virtual void *alloc();
    virtual void free(void *);
    virtual bool idleTrigger(int shift) const = 0;
    virtual void clean(time_t maxage) = 0;
    /* Hint to the allocator - may be ignored */
    virtual void setChunkSize(size_t chunksize) {}
    virtual size_t objectSize() const;
    virtual int getInUseCount() = 0;
  protected:
    virtual void *allocate() = 0;
    virtual void deallocate(void *) = 0;
  private:
    MemPoolMeter meter;
  public:
    MemImplementingAllocator *next;
  public:
    size_t alloc_calls;
    size_t free_calls;
    size_t obj_size;
};

class MemPool : public MemImplementingAllocator
{
  public:
    friend class MemChunk;
    MemPool(const char *label, size_t obj_size);
    ~MemPool();
    void convertFreeCacheToChunkFreeCache();
    virtual void clean(time_t maxage);
    virtual int getStats(MemPoolStats * stats);
    void createChunk();
    void *get();
    void push(void *obj);
    virtual int getInUseCount();
  protected:
    virtual void *allocate();
    virtual void deallocate(void *);
  public:
    virtual void setChunkSize(size_t chunksize);
    virtual bool idleTrigger(int shift) const;

    size_t chunk_size;
    int chunk_capacity;
    int memPID;
    int chunkCount;
    size_t inuse;
    size_t idle;
    void *freeCache;
    MemChunk *nextFreeChunk;
    MemChunk *Chunks;
    Splay<MemChunk *> allChunks;
};

class MemMalloc : public MemImplementingAllocator
{
  public:
    MemMalloc(char const *label, size_t aSize);
    virtual bool idleTrigger(int shift) const;
    virtual void clean(time_t maxage);
    virtual int getStats(MemPoolStats * stats);
    virtual int getInUseCount();
  protected:
    virtual void *allocate();
    virtual void deallocate(void *);
  private:
    int inuse;
};

class MemChunk
{
  public:
    MemChunk(MemPool *pool);
    ~MemChunk();
    void *freeList;
    void *objCache;
    int inuse_count;
    MemChunk *nextFreeChunk;
    MemChunk *next;
    time_t lastref;
    MemPool *pool;
};

class MemPoolStats
{
  public:
    MemAllocator *pool;
    const char *label;
    MemPoolMeter *meter;
    int obj_size;
    int chunk_capacity;
    int chunk_size;

    int chunks_alloc;
    int chunks_inuse;
    int chunks_partial;
    int chunks_free;

    int items_alloc;
    int items_inuse;
    int items_idle;

    int overhead;
};

struct _MemPoolGlobalStats
{
    MemPoolMeter *TheMeter;

    int tot_pools_alloc;
    int tot_pools_inuse;
    int tot_pools_mempid;

    int tot_chunks_alloc;
    int tot_chunks_inuse;
    int tot_chunks_partial;
    int tot_chunks_free;

    int tot_items_alloc;
    int tot_items_inuse;
    int tot_items_idle;

    int tot_overhead;
    int mem_idle_limit;
};

#define SIZEOF_CHUNK  ( ( sizeof(MemChunk) + sizeof(double) -1) / sizeof(double) ) * sizeof(double);

#define memPoolCreate MemPools::GetInstance().create

/* Allocator API */
extern MemPoolIterator * memPoolIterate(void);
extern MemImplementingAllocator * memPoolIterateNext(MemPoolIterator * iter);
extern void memPoolIterateDone(MemPoolIterator ** iter);

/* Stats API - not sured how to refactor yet */
extern int memPoolGetGlobalStats(MemPoolGlobalStats * stats);

extern int memPoolInUseCount(MemAllocator *);
extern int memPoolsTotalAllocated(void);

MemAllocatorProxy::MemAllocatorProxy(char const *aLabel, size_t const &aSize) : label (aLabel), size(aSize), theAllocator (NULL)
{
}


#endif /* _MEM_POOLS_H_ */