]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/MemPoolMalloc.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[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
37 #include "squid.h"
38 #if HAVE_ASSERT_H
39 #include <assert.h>
40 #endif
41
42 #include "MemPoolMalloc.h"
43
44 #if HAVE_STRING_H
45 #include <string.h>
46 #endif
47
48 /*
49 * XXX This is a boundary violation between lib and src.. would be good
50 * if it could be solved otherwise, but left for now.
51 */
52 extern time_t squid_curtime;
53
54 void *
55 MemPoolMalloc::allocate()
56 {
57 void *obj = freelist.pop();
58 if (obj) {
59 memMeterDec(meter.idle);
60 saved_calls++;
61 } else {
62 obj = xcalloc(1, obj_size);
63 memMeterInc(meter.alloc);
64 }
65 memMeterInc(meter.inuse);
66 return obj;
67 }
68
69 void
70 MemPoolMalloc::deallocate(void *obj, bool aggressive)
71 {
72 memMeterDec(meter.inuse);
73 if (aggressive) {
74 xfree(obj);
75 memMeterDec(meter.alloc);
76 } else {
77 if (doZeroOnPush)
78 memset(obj, 0, obj_size);
79 memMeterInc(meter.idle);
80 freelist.push_back(obj);
81 }
82 }
83
84 /* TODO extract common logic to MemAllocate */
85 int
86 MemPoolMalloc::getStats(MemPoolStats * stats, int accumulate)
87 {
88 if (!accumulate) /* need skip memset for GlobalStats accumulation */
89 memset(stats, 0, sizeof(MemPoolStats));
90
91 stats->pool = this;
92 stats->label = objectType();
93 stats->meter = &meter;
94 stats->obj_size = obj_size;
95 stats->chunk_capacity = 0;
96
97 stats->chunks_alloc += 0;
98 stats->chunks_inuse += 0;
99 stats->chunks_partial += 0;
100 stats->chunks_free += 0;
101
102 stats->items_alloc += meter.alloc.level;
103 stats->items_inuse += meter.inuse.level;
104 stats->items_idle += meter.idle.level;
105
106 stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
107
108 return meter.inuse.level;
109 }
110
111 int
112 MemPoolMalloc::getInUseCount()
113 {
114 return meter.inuse.level;
115 }
116
117 MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
118 {
119 }
120
121 MemPoolMalloc::~MemPoolMalloc()
122 {
123 assert(meter.inuse.level == 0);
124 clean(0);
125 }
126
127 bool
128 MemPoolMalloc::idleTrigger(int shift) const
129 {
130 return freelist.count >> (shift ? 8 : 0);
131 }
132
133 void
134 MemPoolMalloc::clean(time_t maxage)
135 {
136 while (void *obj = freelist.pop()) {
137 memMeterDec(meter.idle);
138 memMeterDec(meter.alloc);
139 xfree(obj);
140 }
141 }
142