Pool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef _MEM_POOL_H_
10 #define _MEM_POOL_H_
11 
31 #include "mem/Meter.h"
32 #include "util.h"
33 
34 #if HAVE_GNUMALLOC_H
35 #include <gnumalloc.h>
36 #elif HAVE_MALLOC_H
37 #include <malloc.h>
38 #endif
39 #if HAVE_MEMORY_H
40 #include <memory.h>
41 #endif
42 
43 #if !M_MMAP_MAX
44 #if USE_DLMALLOC
45 #define M_MMAP_MAX -4
46 #endif
47 #endif
48 
50 #define toMB(size) ( ((double) size) / ((double)(1024*1024)) )
51 #define toKB(size) ( (size + 1024 - 1) / 1024 )
53 
55 #define MEM_PAGE_SIZE 4096
56 #define MEM_MIN_FREE 32
58 #define MEM_MAX_FREE 65535 /* unsigned short is max number of items per chunk */
60 
62 class MemPoolStats;
63 
67 
70 {
71 public:
74 };
75 
80 class mgb_t
81 {
82 public:
83  mgb_t() : count(0), bytes(0) {}
84  double count;
85  double bytes;
86 };
87 
93 {
94 public:
95  MemPoolMeter();
96  void flush();
97 
101 
105 
108 
111 };
112 
114 
116 class MemPools
117 {
118 public:
119  static MemPools &GetInstance();
120  MemPools();
121  void flushMeters();
122 
127  MemImplementingAllocator * create(const char *label, size_t obj_size);
128 
135  void setIdleLimit(ssize_t new_idle_limit);
136  ssize_t idleLimit() const;
137 
164  void clean(time_t maxage);
165 
166  void setDefaultPoolChunking(bool const &);
167 
169  ssize_t mem_idle_limit = (2 << 20) /* 2MB */;
170  int poolCount = 0;
171  bool defaultIsChunked = false;
172 };
173 
179 {
180 public:
181  MemAllocator (char const *aLabel);
182  virtual ~MemAllocator() {}
183 
188  virtual int getStats(MemPoolStats * stats, int accumulate = 0) = 0;
189 
190  virtual MemPoolMeter const &getMeter() const = 0;
191 
195  virtual void *alloc() = 0;
196 
200  virtual void freeOne(void *) = 0;
201 
202  virtual char const *objectType() const;
203  virtual size_t objectSize() const = 0;
204  virtual int getInUseCount() = 0;
205  void zeroBlocks(bool doIt) {doZero = doIt;}
206  int inUseCount();
207 
218  virtual void setChunkSize(size_t) {}
219 
224  static size_t RoundedSize(size_t minSize);
225 
226 protected:
233  bool doZero;
234 
235 private:
236  const char *label;
237 };
238 
241 {
242 public:
243  MemImplementingAllocator(char const *aLabel, size_t aSize);
244  virtual ~MemImplementingAllocator();
245  virtual MemPoolMeter const &getMeter() const;
246  virtual MemPoolMeter &getMeter();
247  virtual void flushMetersFull();
248  virtual void flushMeters();
249 
253  virtual void *alloc();
254 
258  virtual void freeOne(void *);
259 
260  virtual bool idleTrigger(int shift) const = 0;
261  virtual void clean(time_t maxage) = 0;
262  virtual size_t objectSize() const;
263  virtual int getInUseCount() = 0;
264 protected:
265  virtual void *allocate() = 0;
266  virtual void deallocate(void *, bool aggressive) = 0;
268  int memPID;
269 public:
271 public:
272  size_t alloc_calls;
273  size_t free_calls;
274  size_t saved_calls;
275  size_t obj_size;
276 };
277 
280 {
281 public:
283  const char *label;
285  int obj_size;
288 
293 
297 
298  int overhead;
299 };
300 
305 
309 
314 
318 
320  ssize_t mem_idle_limit;
321 };
322 
325 #define memPoolCreate MemPools::GetInstance().create
326 
327 /* Allocator API */
333 extern MemPoolIterator * memPoolIterate(void);
334 
340 
345 extern void memPoolIterateDone(MemPoolIterator ** iter);
346 
360 
362 extern int memPoolsTotalAllocated(void);
363 
364 #endif /* _MEM_POOL_H_ */
365 
virtual void deallocate(void *, bool aggressive)=0
int chunks_partial
Definition: Pool.h:291
Definition: Pool.h:80
MemImplementingAllocator * memPoolIterateNext(MemPoolIterator *iter)
Definition: Pool.cc:55
MemImplementingAllocator * pool
Definition: Pool.h:72
MemImplementingAllocator(char const *aLabel, size_t aSize)
Definition: Pool.cc:306
void flush()
Definition: Pool.cc:150
mgb_t gb_freed
Definition: Pool.h:110
virtual ~MemImplementingAllocator()
Definition: Pool.cc:327
virtual int getInUseCount()=0
int chunks_inuse
Definition: Pool.h:290
MemPoolMeter * meter
Definition: Pool.h:284
int items_idle
Definition: Pool.h:296
bool doZero
Definition: Pool.h:233
virtual void freeOne(void *)
Definition: Pool.cc:206
static MemPools & GetInstance()
Definition: Pool.cc:30
int items_alloc
Definition: Pool.h:294
virtual MemPoolMeter const & getMeter() const
Definition: Pool.cc:349
virtual void freeOne(void *)=0
int items_inuse
Definition: Pool.h:295
MemPoolIterator * next
Definition: Pool.h:73
MemImplementingAllocator * create(const char *label, size_t obj_size)
Definition: Pool.cc:91
virtual void * alloc()
Definition: Pool.cc:197
int overhead
Definition: Pool.h:298
int tot_pools_alloc
Definition: Pool.h:306
void flushMeters()
Definition: Pool.cc:174
virtual bool idleTrigger(int shift) const =0
void setDefaultPoolChunking(bool const &)
Definition: Pool.cc:101
class Ping::pingStats_ stats
int obj_size
Definition: Pool.h:285
static size_t RoundedSize(size_t minSize)
Definition: Pool.cc:293
virtual int getInUseCount()=0
int chunks_alloc
Definition: Pool.h:289
mgb_t gb_saved
Definition: Pool.h:107
mgb_t gb_allocated
Definition: Pool.h:103
int tot_items_alloc
Definition: Pool.h:315
int chunk_capacity
Definition: Pool.h:286
int tot_chunks_inuse
Definition: Pool.h:311
const char * label
Definition: Pool.h:236
ssize_t mem_idle_limit
Definition: Pool.h:320
Mem::Meter idle
Definition: Pool.h:100
Definition: Pool.h:116
MemAllocator * pool
Definition: Pool.h:282
int tot_chunks_alloc
Definition: Pool.h:310
int chunks_free
Definition: Pool.h:292
MemPoolMeter meter
Definition: Pool.h:267
virtual void setChunkSize(size_t)
Definition: Pool.h:218
int inUseCount()
Definition: Pool.cc:113
Mem::Meter alloc
Definition: Pool.h:98
virtual char const * objectType() const
Definition: Pool.cc:107
int tot_pools_inuse
Definition: Pool.h:307
void clean(time_t maxage)
Definition: Pool.cc:224
int memPoolGetGlobalStats(MemPoolGlobalStats *stats)
Definition: Pool.cc:250
int chunk_size
Definition: Pool.h:287
int tot_chunks_partial
Definition: Pool.h:312
MemImplementingAllocator * next
Definition: Pool.h:270
const char * label
Definition: Pool.h:283
virtual int getStats(MemPoolStats *stats, int accumulate=0)=0
MemAllocator(char const *aLabel)
Definition: Pool.cc:289
int tot_items_inuse
Definition: Pool.h:316
virtual void * allocate()=0
virtual void clean(time_t maxage)=0
virtual void flushMeters()
Definition: Pool.cc:119
Mem::Meter inuse
Definition: Pool.h:99
int tot_chunks_free
Definition: Pool.h:313
MemPoolMeter * TheMeter
Definition: Pool.h:304
virtual MemPoolMeter const & getMeter() const =0
ssize_t mem_idle_limit
Definition: Pool.h:169
int tot_items_idle
Definition: Pool.h:317
int memPoolsTotalAllocated(void)
Definition: Pool.cc:299
mgb_t()
Definition: Pool.h:83
MemImplementingAllocator * pools
Definition: Pool.h:168
virtual void flushMetersFull()
Definition: Pool.cc:141
double count
Definition: Pool.h:84
MemPoolMeter()
Definition: Pool.cc:165
void memPoolIterateDone(MemPoolIterator **iter)
Definition: Pool.cc:47
MemPools()
Definition: Pool.cc:84
virtual size_t objectSize() const =0
int tot_pools_mempid
Definition: Pool.h:308
virtual size_t objectSize() const
Definition: Pool.cc:361
int poolCount
Definition: Pool.h:170
ssize_t idleLimit() const
Definition: Pool.cc:75
MemPoolIterator * memPoolIterate(void)
Definition: Pool.cc:40
void zeroBlocks(bool doIt)
Definition: Pool.h:205
bool defaultIsChunked
Definition: Pool.h:171
virtual ~MemAllocator()
Definition: Pool.h:182
void setIdleLimit(ssize_t new_idle_limit)
Definition: Pool.cc:69
mgb_t gb_oallocated
Definition: Pool.h:104
virtual void * alloc()=0
double bytes
Definition: Pool.h:85

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors