]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mem/PoolMalloc.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / mem / PoolMalloc.cc
CommitLineData
0545caaa 1/*
bde978a6 2 * Copyright (C) 1996-2015 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) {
3b32112a 31 memMeterDec(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);
3b32112a 38 memMeterInc(meter.alloc);
2be7332c 39 }
8cfaefed 40 memMeterInc(meter.inuse);
2be7332c 41 return obj;
8cfaefed
HN
42}
43
44void
2be7332c 45MemPoolMalloc::deallocate(void *obj, bool aggressive)
8cfaefed
HN
46{
47 memMeterDec(meter.inuse);
2be7332c 48 if (aggressive) {
3b32112a
A
49 xfree(obj);
50 memMeterDec(meter.alloc);
2be7332c 51 } else {
3b08e399 52 if (doZero)
3b32112a
A
53 memset(obj, 0, obj_size);
54 memMeterInc(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
77 stats->items_alloc += meter.alloc.level;
78 stats->items_inuse += meter.inuse.level;
79 stats->items_idle += meter.idle.level;
80
81 stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
82
83 return meter.inuse.level;
84}
85
86int
87MemPoolMalloc::getInUseCount()
88{
89 return meter.inuse.level;
90}
91
92MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
93{
94}
95
2be7332c
HN
96MemPoolMalloc::~MemPoolMalloc()
97{
12489dcb 98 assert(meter.inuse.level == 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();
3b32112a
A
114 memMeterDec(meter.idle);
115 memMeterDec(meter.alloc);
116 xfree(obj);
2be7332c 117 }
8cfaefed
HN
118}
119