PoolMalloc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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/Pool.h"
15 #include "mem/PoolMalloc.h"
16 #include "mem/Stats.h"
17 
18 #include <cassert>
19 #include <cstring>
20 
21 extern time_t squid_curtime;
22 
23 void *
25 {
26  void *obj = nullptr;
27  if (!freelist.empty()) {
28  obj = freelist.top();
29  freelist.pop();
30  }
31  if (obj) {
32  --meter.idle;
34  if (doZero)
36  else
38  } else {
39  if (doZero)
40  obj = xcalloc(1, objectSize);
41  else
42  obj = xmalloc(objectSize);
43  ++meter.alloc;
44  }
45  ++meter.inuse;
46  return obj;
47 }
48 
49 void
51 {
52  --meter.inuse;
53  if (MemPools::GetInstance().idleLimit() == 0) {
54  xfree(obj);
55  --meter.alloc;
56  } else {
57  if (doZero)
58  memset(obj, 0, objectSize);
60  ++meter.idle;
61  freelist.push(obj);
62  }
63 }
64 
65 /* TODO extract common logic to MemAllocate */
66 size_t
68 {
69  stats.pool = this;
70  stats.label = label;
71  stats.meter = &meter;
72  stats.obj_size = objectSize;
73  stats.chunk_capacity = 0;
74 
77  stats.items_idle += meter.idle.currentLevel();
78 
79  stats.overhead += sizeof(*this) + strlen(label) + 1;
80 
81  return getInUseCount();
82 }
83 
84 MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) :
85  Mem::Allocator(aLabel, aSize)
86 {
87 }
88 
90 {
91  assert(getInUseCount() == 0);
92  clean(0);
93 }
94 
95 bool
97 {
98  return freelist.size() >> (shift ? 8 : 0);
99 }
100 
101 void
103 {
104  while (!freelist.empty()) {
105  void *obj = freelist.top();
106  freelist.pop();
107  --meter.idle;
108  --meter.alloc;
109  xfree(obj);
110  }
111 }
112 
Allocator * pool
Definition: Stats.h:20
#define VALGRIND_MAKE_MEM_DEFINED
Definition: valgrind.h:27
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71
int items_idle
Definition: Stats.h:34
PoolMeter * meter
Definition: Stats.h:22
#define xmalloc
std::stack< void * > freelist
Definition: PoolMalloc.h:53
void deallocate(void *) override
freeOne(void *)
Definition: PoolMalloc.cc:50
int obj_size
Definition: Stats.h:23
const char * label
Definition: Stats.h:21
int items_inuse
Definition: Stats.h:33
Meter alloc
Definition: Meter.h:89
ssize_t currentLevel() const
Definition: Meter.h:26
Memory Management.
Definition: Allocator.h:16
int getInUseCount() const
the difference between the number of alloc() and freeOne() calls
Definition: Allocator.h:59
int overhead
Definition: Stats.h:36
int chunk_capacity
Definition: Stats.h:24
size_t countSavedAllocs
the number of malloc()/calloc() calls avoided since last flush
Definition: Allocator.h:101
~MemPoolMalloc() override
Definition: PoolMalloc.cc:89
const char * label
brief description of objects returned by alloc()
Definition: Allocator.h:109
MemPoolMalloc(char const *label, size_t aSize)
Definition: PoolMalloc.cc:84
#define assert(EX)
Definition: assert.h:17
const size_t objectSize
the size (in bytes) of objects managed by this allocator
Definition: Allocator.h:112
time_t squid_curtime
Definition: stub_libtime.cc:20
void * allocate() override
*alloc()
Definition: PoolMalloc.cc:24
#define VALGRIND_MAKE_MEM_NOACCESS
Definition: valgrind.h:25
#define xfree
Meter inuse
Definition: Meter.h:90
void clean(time_t) override
Definition: PoolMalloc.cc:102
#define VALGRIND_MAKE_MEM_UNDEFINED
Definition: valgrind.h:26
PoolMeter meter
statistics tracked for this allocator
Definition: Allocator.h:115
static MemPools & GetInstance()
Definition: Pool.cc:27
int items_alloc
Definition: Stats.h:32
Meter idle
Definition: Meter.h:91
size_t getStats(Mem::PoolStats &) override
Definition: PoolMalloc.cc:67
bool idleTrigger(int) const override
Definition: PoolMalloc.cc:96

 

Introduction

Documentation

Support

Miscellaneous