]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/MemPoolMalloc.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / lib / MemPoolMalloc.cc
CommitLineData
8cfaefed
HN
1
2/*
3 * $Id$
4 *
5 * DEBUG: section 63 Low Level Memory Pool Management
2be7332c 6 * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom
8cfaefed
HN
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
f7f3304a 37#include "squid.h"
8cfaefed
HN
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 */
52extern time_t squid_curtime;
53
54void *
55MemPoolMalloc::allocate()
56{
2be7332c
HN
57 void *obj = freelist.pop();
58 if (obj) {
3b32112a
A
59 memMeterDec(meter.idle);
60 saved_calls++;
2be7332c 61 } else {
3b32112a
A
62 obj = xcalloc(1, obj_size);
63 memMeterInc(meter.alloc);
2be7332c 64 }
8cfaefed 65 memMeterInc(meter.inuse);
2be7332c 66 return obj;
8cfaefed
HN
67}
68
69void
2be7332c 70MemPoolMalloc::deallocate(void *obj, bool aggressive)
8cfaefed
HN
71{
72 memMeterDec(meter.inuse);
2be7332c 73 if (aggressive) {
3b32112a
A
74 xfree(obj);
75 memMeterDec(meter.alloc);
2be7332c 76 } else {
3b32112a
A
77 if (doZeroOnPush)
78 memset(obj, 0, obj_size);
79 memMeterInc(meter.idle);
80 freelist.push_back(obj);
2be7332c 81 }
8cfaefed
HN
82}
83
84/* TODO extract common logic to MemAllocate */
85int
86MemPoolMalloc::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();
2be7332c 93 stats->meter = &meter;
8cfaefed
HN
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
111int
112MemPoolMalloc::getInUseCount()
113{
114 return meter.inuse.level;
115}
116
117MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
118{
119}
120
2be7332c
HN
121MemPoolMalloc::~MemPoolMalloc()
122{
12489dcb 123 assert(meter.inuse.level == 0);
2be7332c
HN
124 clean(0);
125}
126
8cfaefed
HN
127bool
128MemPoolMalloc::idleTrigger(int shift) const
129{
2be7332c 130 return freelist.count >> (shift ? 8 : 0);
8cfaefed
HN
131}
132
133void
134MemPoolMalloc::clean(time_t maxage)
135{
2be7332c 136 while (void *obj = freelist.pop()) {
3b32112a
A
137 memMeterDec(meter.idle);
138 memMeterDec(meter.alloc);
139 xfree(obj);
2be7332c 140 }
8cfaefed
HN
141}
142