]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/mem/Pool.h
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
13 \defgroup MemPoolsAPI Memory Management (Memory Pool Allocator)
17 * MemPools are a pooled memory allocator running on top of malloc(). It's
18 * purpose is to reduce memory fragmentation and provide detailed statistics
19 * on memory consumption.
22 * Preferably all memory allocations in Squid should be done using MemPools
23 * or one of the types built on top of it (i.e. cbdata).
25 \note Usually it is better to use cbdata types as these gives you additional
26 * safeguards in references and typechecking. However, for high usage pools where
27 * the cbdata functionality of cbdata is not required directly using a MemPool
28 * might be the way to go.
31 #include "mem/Allocator.h"
32 #include "mem/Meter.h"
37 #include <gnumalloc.h>
51 /// \ingroup MemPoolsAPI
52 #define toMB(size) ( ((double) size) / ((double)(1024*1024)) )
53 /// \ingroup MemPoolsAPI
54 #define toKB(size) ( (size + 1024 - 1) / 1024 )
56 /// \ingroup MemPoolsAPI
57 #define MEM_PAGE_SIZE 4096
58 /// \ingroup MemPoolsAPI
59 #define MEM_MIN_FREE 32
60 /// \ingroup MemPoolsAPI
61 #define MEM_MAX_FREE 65535 /* unsigned short is max number of items per chunk */
63 class MemImplementingAllocator
;
65 /// memory usage totals as of latest MemPools::flushMeters() event
66 extern Mem::PoolMeter TheMeter
;
68 /// \ingroup MemPoolsAPI
72 static MemPools
&GetInstance();
77 * Create an allocator with given name to allocate fixed-size objects
78 * of the specified size.
80 MemImplementingAllocator
*create(const char *, size_t);
83 * Sets upper limit in bytes to amount of free ram kept in pools. This is
84 * not strict upper limit, but a hint. When MemPools are over this limit,
85 * deallocate attempts to release memory to the system instead of pooling.
87 void setIdleLimit(const ssize_t newLimit
) { idleLimit_
= newLimit
; }
88 /// \copydoc idleLimit_
89 ssize_t
idleLimit() const { return idleLimit_
; }
93 * Main cleanup handler. For MemPools to stay within upper idle limits,
94 * this function needs to be called periodically, preferably at some
95 * constant rate, eg. from Squid event. It looks through all pools and
96 * chunks, cleans up internal states and checks for releasable chunks.
99 * Between the calls to this function objects are placed onto internal
100 * cache instead of returning to their home chunks, mainly for speedup
101 * purpose. During that time state of chunk is not known, it is not
102 * known whether chunk is free or in use. This call returns all objects
103 * to their chunks and restores consistency.
106 * Should be called relatively often, as it sorts chunks in suitable
107 * order as to reduce free memory fragmentation and increase chunk
109 * Suitable frequency for cleanup is in range of few tens of seconds to
110 * few minutes, depending of memory activity.
112 * TODO: DOCS: Re-write this shorter!
114 \param maxage Release all totally idle chunks that
115 * have not been referenced for maxage seconds.
117 void clean(time_t maxage
);
119 void setDefaultPoolChunking(bool const &);
121 std::list
<MemImplementingAllocator
*> pools
;
122 bool defaultIsChunked
= false;
125 /// Limits the cumulative size of allocated (but unused) memory in all pools.
126 /// Initial value is 2MB until first configuration,
127 /// See squid.conf memory_pools_limit directive.
128 ssize_t idleLimit_
= (2 << 20);
131 /// \ingroup MemPoolsAPI
132 class MemImplementingAllocator
: public Mem::Allocator
135 typedef Mem::PoolMeter PoolMeter
; // TODO remove
137 MemImplementingAllocator(char const *aLabel
, size_t aSize
);
139 virtual PoolMeter
&getMeter();
140 virtual void flushMetersFull();
141 virtual void flushMeters();
142 virtual bool idleTrigger(int shift
) const = 0;
143 virtual void clean(time_t maxage
) = 0;
145 /* Mem::Allocator API */
146 virtual PoolMeter
const &getMeter() const;
147 virtual void *alloc();
148 virtual void freeOne(void *);
149 virtual size_t objectSize() const;
150 virtual int getInUseCount() = 0;
153 virtual void *allocate() = 0;
154 virtual void deallocate(void *, bool aggressive
) = 0;
163 /// Creates a named MemPool of elements with the given size
164 #define memPoolCreate MemPools::GetInstance().create
166 #endif /* _MEM_POOL_H_ */