]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mem/PoolMalloc.cc
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / mem / PoolMalloc.cc
CommitLineData
0545caaa 1/*
bf95c10a 2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
0545caaa
AJ
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 */
8cfaefed
HN
8
9/*
2be7332c 10 * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom
8cfaefed
HN
11 */
12
f7f3304a 13#include "squid.h"
ed6e9fb9 14#include "mem/PoolMalloc.h"
8cfaefed 15
074d6a40
AJ
16#include <cassert>
17#include <cstring>
8cfaefed 18
8cfaefed
HN
19extern time_t squid_curtime;
20
21void *
22MemPoolMalloc::allocate()
23{
3aa53107
FC
24 void *obj = NULL;
25 if (!freelist.empty()) {
26 obj = freelist.top();
27 freelist.pop();
28 }
2be7332c 29 if (obj) {
9663db1c 30 --meter.idle;
742a021b 31 ++saved_calls;
2be7332c 32 } else {
3b08e399
AJ
33 if (doZero)
34 obj = xcalloc(1, obj_size);
35 else
36 obj = xmalloc(obj_size);
9663db1c 37 ++meter.alloc;
2be7332c 38 }
9663db1c 39 ++meter.inuse;
2be7332c 40 return obj;
8cfaefed
HN
41}
42
43void
2be7332c 44MemPoolMalloc::deallocate(void *obj, bool aggressive)
8cfaefed 45{
9663db1c 46 --meter.inuse;
2be7332c 47 if (aggressive) {
3b32112a 48 xfree(obj);
9663db1c 49 --meter.alloc;
2be7332c 50 } else {
3b08e399 51 if (doZero)
3b32112a 52 memset(obj, 0, obj_size);
9663db1c 53 ++meter.idle;
3aa53107 54 freelist.push(obj);
2be7332c 55 }
8cfaefed
HN
56}
57
58/* TODO extract common logic to MemAllocate */
59int
60MemPoolMalloc::getStats(MemPoolStats * stats, int accumulate)
61{
f53969cc 62 if (!accumulate) /* need skip memset for GlobalStats accumulation */
8cfaefed
HN
63 memset(stats, 0, sizeof(MemPoolStats));
64
65 stats->pool = this;
66 stats->label = objectType();
2be7332c 67 stats->meter = &meter;
8cfaefed
HN
68 stats->obj_size = obj_size;
69 stats->chunk_capacity = 0;
70
71 stats->chunks_alloc += 0;
72 stats->chunks_inuse += 0;
73 stats->chunks_partial += 0;
74 stats->chunks_free += 0;
75
9663db1c
AJ
76 stats->items_alloc += meter.alloc.currentLevel();
77 stats->items_inuse += meter.inuse.currentLevel();
78 stats->items_idle += meter.idle.currentLevel();
8cfaefed
HN
79
80 stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
81
9663db1c 82 return meter.inuse.currentLevel();
8cfaefed
HN
83}
84
85int
86MemPoolMalloc::getInUseCount()
87{
9663db1c 88 return meter.inuse.currentLevel();
8cfaefed
HN
89}
90
91MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
92{
93}
94
2be7332c
HN
95MemPoolMalloc::~MemPoolMalloc()
96{
9663db1c 97 assert(meter.inuse.currentLevel() == 0);
2be7332c
HN
98 clean(0);
99}
100
8cfaefed
HN
101bool
102MemPoolMalloc::idleTrigger(int shift) const
103{
4c9eadc2 104 return freelist.size() >> (shift ? 8 : 0);
8cfaefed
HN
105}
106
107void
ced8def3 108MemPoolMalloc::clean(time_t)
8cfaefed 109{
3aa53107
FC
110 while (!freelist.empty()) {
111 void *obj = freelist.top();
112 freelist.pop();
9663db1c
AJ
113 --meter.idle;
114 --meter.alloc;
3b32112a 115 xfree(obj);
2be7332c 116 }
8cfaefed
HN
117}
118