]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/MemPoolMalloc.cc
SourceFormat Enforcement
[thirdparty/squid.git] / lib / MemPoolMalloc.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 63 Low Level Memory Pool Management
6 * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #if HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
40
41 #include "MemPoolMalloc.h"
42
43 #if HAVE_STRING_H
44 #include <string.h>
45 #endif
46
47 /*
48 * XXX This is a boundary violation between lib and src.. would be good
49 * if it could be solved otherwise, but left for now.
50 */
51 extern time_t squid_curtime;
52
53 void *
54 MemPoolMalloc::allocate()
55 {
56 void *obj = freelist.pop();
57 if (obj) {
58 memMeterDec(meter.idle);
59 ++saved_calls;
60 } else {
61 obj = xcalloc(1, obj_size);
62 memMeterInc(meter.alloc);
63 }
64 memMeterInc(meter.inuse);
65 return obj;
66 }
67
68 void
69 MemPoolMalloc::deallocate(void *obj, bool aggressive)
70 {
71 memMeterDec(meter.inuse);
72 if (aggressive) {
73 xfree(obj);
74 memMeterDec(meter.alloc);
75 } else {
76 if (doZeroOnPush)
77 memset(obj, 0, obj_size);
78 memMeterInc(meter.idle);
79 freelist.push_back(obj);
80 }
81 }
82
83 /* TODO extract common logic to MemAllocate */
84 int
85 MemPoolMalloc::getStats(MemPoolStats * stats, int accumulate)
86 {
87 if (!accumulate) /* need skip memset for GlobalStats accumulation */
88 memset(stats, 0, sizeof(MemPoolStats));
89
90 stats->pool = this;
91 stats->label = objectType();
92 stats->meter = &meter;
93 stats->obj_size = obj_size;
94 stats->chunk_capacity = 0;
95
96 stats->chunks_alloc += 0;
97 stats->chunks_inuse += 0;
98 stats->chunks_partial += 0;
99 stats->chunks_free += 0;
100
101 stats->items_alloc += meter.alloc.level;
102 stats->items_inuse += meter.inuse.level;
103 stats->items_idle += meter.idle.level;
104
105 stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
106
107 return meter.inuse.level;
108 }
109
110 int
111 MemPoolMalloc::getInUseCount()
112 {
113 return meter.inuse.level;
114 }
115
116 MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
117 {
118 }
119
120 MemPoolMalloc::~MemPoolMalloc()
121 {
122 assert(meter.inuse.level == 0);
123 clean(0);
124 }
125
126 bool
127 MemPoolMalloc::idleTrigger(int shift) const
128 {
129 return freelist.count >> (shift ? 8 : 0);
130 }
131
132 void
133 MemPoolMalloc::clean(time_t maxage)
134 {
135 while (void *obj = freelist.pop()) {
136 memMeterDec(meter.idle);
137 memMeterDec(meter.alloc);
138 xfree(obj);
139 }
140 }
141