PoolMalloc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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  */
8 
9 /*
10  * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom
11  */
12 
13 #include "squid.h"
14 #include "mem/PoolMalloc.h"
15 
16 #include <cassert>
17 #include <cstring>
18 
19 extern time_t squid_curtime;
20 
21 void *
23 {
24  void *obj = NULL;
25  if (!freelist.empty()) {
26  obj = freelist.top();
27  freelist.pop();
28  }
29  if (obj) {
30  --meter.idle;
31  ++saved_calls;
32  } else {
33  if (doZero)
34  obj = xcalloc(1, obj_size);
35  else
36  obj = xmalloc(obj_size);
37  ++meter.alloc;
38  }
39  ++meter.inuse;
40  return obj;
41 }
42 
43 void
44 MemPoolMalloc::deallocate(void *obj, bool aggressive)
45 {
46  --meter.inuse;
47  if (aggressive) {
48  xfree(obj);
49  --meter.alloc;
50  } else {
51  if (doZero)
52  memset(obj, 0, obj_size);
53  ++meter.idle;
54  freelist.push(obj);
55  }
56 }
57 
58 /* TODO extract common logic to MemAllocate */
59 int
61 {
62  if (!accumulate) /* need skip memset for GlobalStats accumulation */
63  memset(stats, 0, sizeof(MemPoolStats));
64 
65  stats->pool = this;
66  stats->label = objectType();
67  stats->meter = &meter;
68  stats->obj_size = obj_size;
69  stats->chunk_capacity = 0;
70 
71  stats->chunks_alloc += 0;
72  stats->chunks_inuse += 0;
73  stats->chunks_partial += 0;
74  stats->chunks_free += 0;
75 
76  stats->items_alloc += meter.alloc.currentLevel();
77  stats->items_inuse += meter.inuse.currentLevel();
78  stats->items_idle += meter.idle.currentLevel();
79 
80  stats->overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1;
81 
82  return meter.inuse.currentLevel();
83 }
84 
85 int
87 {
88  return meter.inuse.currentLevel();
89 }
90 
91 MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize)
92 {
93 }
94 
96 {
98  clean(0);
99 }
100 
101 bool
103 {
104  return freelist.size() >> (shift ? 8 : 0);
105 }
106 
107 void
109 {
110  while (!freelist.empty()) {
111  void *obj = freelist.top();
112  freelist.pop();
113  --meter.idle;
114  --meter.alloc;
115  xfree(obj);
116  }
117 }
118 
class Ping::pingStats_ stats
Mem::Meter inuse
Definition: Pool.h:99
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71
#define xmalloc
std::stack< void * > freelist
Definition: PoolMalloc.h:55
virtual void deallocate(void *, bool aggressive)
Definition: PoolMalloc.cc:44
virtual bool idleTrigger(int shift) const
Definition: PoolMalloc.cc:102
virtual int getStats(MemPoolStats *stats, int accumulate)
Definition: PoolMalloc.cc:60
MemPoolMeter meter
Definition: Pool.h:267
ssize_t currentLevel() const
Definition: Meter.h:28
bool doZero
Definition: Pool.h:233
virtual void * allocate()
Definition: PoolMalloc.cc:22
#define NULL
Definition: types.h:166
virtual void clean(time_t maxage)
Definition: PoolMalloc.cc:108
MemPoolMalloc(char const *label, size_t aSize)
Definition: PoolMalloc.cc:91
#define assert(EX)
Definition: assert.h:19
time_t squid_curtime
Definition: stub_libtime.cc:20
#define xfree
virtual int getInUseCount()
Definition: PoolMalloc.cc:86
Mem::Meter alloc
Definition: Pool.h:98
virtual char const * objectType() const
Definition: Pool.cc:106
Mem::Meter idle
Definition: Pool.h:100

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors