Squid Web Cache master
Loading...
Searching...
No Matches
PoolMalloc.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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/*
10 * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom
11 */
12
13#include "squid.h"
14#include "mem/Pool.h"
15#include "mem/PoolMalloc.h"
16#include "mem/Stats.h"
17
18#include <cassert>
19#include <cstring>
20
21extern time_t squid_curtime;
22
23void *
25{
26 void *obj = nullptr;
27 if (!freelist.empty()) {
28 obj = freelist.top();
29 freelist.pop();
30 }
31 if (obj) {
32 --meter.idle;
34 if (doZero)
36 else
38 } else {
39 if (doZero)
40 obj = xcalloc(1, objectSize);
41 else
42 obj = xmalloc(objectSize);
43 ++meter.alloc;
44 }
45 ++meter.inuse;
46 return obj;
47}
48
49void
51{
52 --meter.inuse;
53 if (MemPools::GetInstance().idleLimit() == 0) {
54 xfree(obj);
55 --meter.alloc;
56 } else {
57 if (doZero)
58 memset(obj, 0, objectSize);
60 ++meter.idle;
61 freelist.push(obj);
62 }
63}
64
65/* TODO extract common logic to MemAllocate */
66size_t
68{
69 stats.pool = this;
70 stats.label = label;
71 stats.meter = &meter;
72 stats.obj_size = objectSize;
73 stats.chunk_capacity = 0;
74
78
79 stats.overhead += sizeof(*this) + strlen(label) + 1;
80
81 return getInUseCount();
82}
83
84MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) :
85 Mem::Allocator(aLabel, aSize)
86{
87}
88
94
95bool
97{
98 return freelist.size() >> (shift ? 8 : 0);
99}
100
101void
103{
104 while (!freelist.empty()) {
105 void *obj = freelist.top();
106 freelist.pop();
107 --meter.idle;
108 --meter.alloc;
109 xfree(obj);
110 }
111}
112
time_t squid_curtime
#define assert(EX)
Definition assert.h:17
bool idleTrigger(int) const override
Definition PoolMalloc.cc:96
void * allocate() override
*alloc()
Definition PoolMalloc.cc:24
~MemPoolMalloc() override
Definition PoolMalloc.cc:89
MemPoolMalloc(char const *label, size_t aSize)
Definition PoolMalloc.cc:84
void deallocate(void *) override
freeOne(void *)
Definition PoolMalloc.cc:50
std::stack< void * > freelist
Definition PoolMalloc.h:53
void clean(time_t) override
size_t getStats(Mem::PoolStats &) override
Definition PoolMalloc.cc:67
static MemPools & GetInstance()
Definition Pool.cc:27
const size_t objectSize
the size (in bytes) of objects managed by this allocator
Definition Allocator.h:112
int getInUseCount() const
the difference between the number of alloc() and freeOne() calls
Definition Allocator.h:59
const char * label
brief description of objects returned by alloc()
Definition Allocator.h:109
PoolMeter meter
statistics tracked for this allocator
Definition Allocator.h:115
size_t countSavedAllocs
the number of malloc()/calloc() calls avoided since last flush
Definition Allocator.h:101
ssize_t currentLevel() const
Definition Meter.h:26
Meter idle
Definition Meter.h:91
Meter alloc
Definition Meter.h:89
Meter inuse
Definition Meter.h:90
int items_inuse
Definition Stats.h:33
const char * label
Definition Stats.h:21
int overhead
Definition Stats.h:36
int obj_size
Definition Stats.h:23
int items_alloc
Definition Stats.h:32
Allocator * pool
Definition Stats.h:20
int chunk_capacity
Definition Stats.h:24
PoolMeter * meter
Definition Stats.h:22
int items_idle
Definition Stats.h:34
Memory Management.
Definition Allocator.h:17
#define xfree
#define xmalloc
#define VALGRIND_MAKE_MEM_UNDEFINED
Definition valgrind.h:26
#define VALGRIND_MAKE_MEM_NOACCESS
Definition valgrind.h:25
#define VALGRIND_MAKE_MEM_DEFINED
Definition valgrind.h:27
void * xcalloc(size_t n, size_t sz)
Definition xalloc.cc:71