Pool.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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)) )
52#define toKB(size) ( (size + 1024 - 1) / 1024 )
53
55#define MEM_PAGE_SIZE 4096
57#define MEM_MIN_FREE 32
59#define MEM_MAX_FREE 65535 /* unsigned short is max number of items per chunk */
60
62class MemPoolStats;
63
67
70{
71public:
74};
75
80class mgb_t
81{
82public:
83 mgb_t() : count(0), bytes(0) {}
84 double count;
85 double bytes;
86};
87
93{
94public:
96 void flush();
97
101
105
108
111};
112
114
117{
118public:
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{
180public:
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
226protected:
233 bool doZero;
234
235private:
236 const char *label;
237};
238
241{
242public:
243 MemImplementingAllocator(char const *aLabel, size_t aSize);
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;
264protected:
265 virtual void *allocate() = 0;
266 virtual void deallocate(void *, bool aggressive) = 0;
269public:
271public:
275 size_t obj_size;
276};
277
280{
281public:
283 const char *label;
288
293
297
299};
300
305
309
314
318
321};
322
325#define memPoolCreate MemPools::GetInstance().create
326
327/* Allocator API */
333extern MemPoolIterator * memPoolIterate(void);
334
340
345extern void memPoolIterateDone(MemPoolIterator ** iter);
346
359
361extern int memPoolsTotalAllocated(void);
362
363#endif /* _MEM_POOL_H_ */
364
MemAllocator(char const *aLabel)
Definition: Pool.cc:288
bool doZero
Definition: Pool.h:233
virtual void setChunkSize(size_t)
Definition: Pool.h:218
virtual ~MemAllocator()
Definition: Pool.h:182
virtual int getInUseCount()=0
virtual MemPoolMeter const & getMeter() const =0
virtual void freeOne(void *)=0
virtual void * alloc()=0
int inUseCount()
Definition: Pool.cc:112
virtual int getStats(MemPoolStats *stats, int accumulate=0)=0
static size_t RoundedSize(size_t minSize)
Definition: Pool.cc:292
virtual size_t objectSize() const =0
const char * label
Definition: Pool.h:236
void zeroBlocks(bool doIt)
Definition: Pool.h:205
virtual char const * objectType() const
Definition: Pool.cc:106
virtual void deallocate(void *, bool aggressive)=0
MemPoolMeter meter
Definition: Pool.h:267
virtual void clean(time_t maxage)=0
virtual ~MemImplementingAllocator()
Definition: Pool.cc:326
virtual void * alloc()
Definition: Pool.cc:196
MemImplementingAllocator * next
Definition: Pool.h:270
virtual MemPoolMeter const & getMeter() const
Definition: Pool.cc:348
virtual int getInUseCount()=0
MemImplementingAllocator(char const *aLabel, size_t aSize)
Definition: Pool.cc:305
virtual size_t objectSize() const
Definition: Pool.cc:360
virtual void freeOne(void *)
Definition: Pool.cc:205
virtual void flushMetersFull()
Definition: Pool.cc:140
virtual void flushMeters()
Definition: Pool.cc:118
virtual void * allocate()=0
virtual bool idleTrigger(int shift) const =0
MemPoolIterator * next
Definition: Pool.h:73
MemImplementingAllocator * pool
Definition: Pool.h:72
MemPoolMeter()
Definition: Pool.cc:164
Mem::Meter inuse
Definition: Pool.h:99
void flush()
Definition: Pool.cc:149
Mem::Meter alloc
Definition: Pool.h:98
Mem::Meter idle
Definition: Pool.h:100
mgb_t gb_freed
Definition: Pool.h:110
mgb_t gb_saved
Definition: Pool.h:107
mgb_t gb_oallocated
Definition: Pool.h:104
mgb_t gb_allocated
Definition: Pool.h:103
const char * label
Definition: Pool.h:283
int overhead
Definition: Pool.h:298
int chunks_alloc
Definition: Pool.h:289
MemAllocator * pool
Definition: Pool.h:282
MemPoolMeter * meter
Definition: Pool.h:284
int chunks_free
Definition: Pool.h:292
int items_idle
Definition: Pool.h:296
int chunk_capacity
Definition: Pool.h:286
int chunks_inuse
Definition: Pool.h:290
int chunk_size
Definition: Pool.h:287
int chunks_partial
Definition: Pool.h:291
int items_alloc
Definition: Pool.h:294
int items_inuse
Definition: Pool.h:295
int obj_size
Definition: Pool.h:285
Definition: Pool.h:117
bool defaultIsChunked
Definition: Pool.h:171
void setDefaultPoolChunking(bool const &)
Definition: Pool.cc:100
void setIdleLimit(ssize_t new_idle_limit)
Definition: Pool.cc:68
MemPools()
Definition: Pool.cc:83
MemImplementingAllocator * pools
Definition: Pool.h:168
ssize_t idleLimit() const
Definition: Pool.cc:74
int poolCount
Definition: Pool.h:170
MemImplementingAllocator * create(const char *label, size_t obj_size)
Definition: Pool.cc:90
void flushMeters()
Definition: Pool.cc:173
static MemPools & GetInstance()
Definition: Pool.cc:29
ssize_t mem_idle_limit
Definition: Pool.h:169
void clean(time_t maxage)
Definition: Pool.cc:223
Definition: Pool.h:81
double bytes
Definition: Pool.h:85
mgb_t()
Definition: Pool.h:83
double count
Definition: Pool.h:84
MemPoolIterator * memPoolIterate(void)
Definition: Pool.cc:39
void memPoolIterateDone(MemPoolIterator **iter)
Definition: Pool.cc:46
int memPoolsTotalAllocated(void)
Definition: Pool.cc:298
MemImplementingAllocator * memPoolIterateNext(MemPoolIterator *iter)
Definition: Pool.cc:54
int memPoolGetGlobalStats(MemPoolGlobalStats *stats)
Definition: Pool.cc:249
class Ping::pingStats_ stats
int tot_pools_inuse
Definition: Pool.h:307
int tot_pools_mempid
Definition: Pool.h:308
int tot_pools_alloc
Definition: Pool.h:306
int tot_items_idle
Definition: Pool.h:317
int tot_chunks_partial
Definition: Pool.h:312
ssize_t mem_idle_limit
Definition: Pool.h:320
int tot_items_alloc
Definition: Pool.h:315
int tot_items_inuse
Definition: Pool.h:316
int tot_chunks_inuse
Definition: Pool.h:311
int tot_chunks_free
Definition: Pool.h:313
int tot_chunks_alloc
Definition: Pool.h:310
MemPoolMeter * TheMeter
Definition: Pool.h:304

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors