]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mem/PoolMalloc.cc
Bug 5194: Remove all unused debug sections (#969)
[thirdparty/squid.git] / src / mem / PoolMalloc.cc
1 /*
2 * Copyright (C) 1996-2021 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/PoolMalloc.h"
15
16 #include <cassert>
17 #include <cstring>
18
19 extern time_t squid_curtime;
20
21 void *
22 MemPoolMalloc::allocate()
23 {
24 void *obj = NULL;
25 if (!freelist.empty()) {
26 obj = freelist.top();
27 freelist.pop();
28 }
29 if (obj) {
30 --meter.idle;
31 ++saved_calls;
32 } else {
33 if (doZero)
34 obj = xcalloc(1, obj_size);
35 else
36 obj = xmalloc(obj_size);
37 ++meter.alloc;
38 }
39 ++meter.inuse;
40 return obj;
41 }
42
43 void
44 MemPoolMalloc::deallocate(void *obj, bool aggressive)
45 {
46 --meter.inuse;
47 if (aggressive) {
48 xfree(obj);
49 --meter.alloc;
50 } else {
51 if (doZero)
52 memset(obj, 0, obj_size);
53 ++meter.idle;
54 freelist.push(obj);
55 }
56 }
57
58 /* TODO extract common logic to MemAllocate */
59 int
60 MemPoolMalloc::getStats(MemPoolStats * stats, int accumulate)
61 {
62 if (!accumulate) /* need skip memset for GlobalStats accumulation */
63 memset(stats, 0, sizeof(MemPoolStats));
64
65 stats->pool = this;
66 stats->label = objectType();
67 stats->meter = &meter;
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
76 stats->items_alloc += meter.alloc.currentLevel();
77 stats->items_inuse += meter.inuse.currentLevel();
78 stats->items_idle += meter.idle.currentLevel();
79
80 stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
81
82 return meter.inuse.currentLevel();
83 }
84
85 int
86 MemPoolMalloc::getInUseCount()
87 {
88 return meter.inuse.currentLevel();
89 }
90
91 MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
92 {
93 }
94
95 MemPoolMalloc::~MemPoolMalloc()
96 {
97 assert(meter.inuse.currentLevel() == 0);
98 clean(0);
99 }
100
101 bool
102 MemPoolMalloc::idleTrigger(int shift) const
103 {
104 return freelist.size() >> (shift ? 8 : 0);
105 }
106
107 void
108 MemPoolMalloc::clean(time_t)
109 {
110 while (!freelist.empty()) {
111 void *obj = freelist.top();
112 freelist.pop();
113 --meter.idle;
114 --meter.alloc;
115 xfree(obj);
116 }
117 }
118