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