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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors