Allocator.h
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#ifndef SQUID_SRC_MEM_ALLOCATOR_H
10#define SQUID_SRC_MEM_ALLOCATOR_H
11
12#include "base/TypeTraits.h"
13#include "mem/forward.h"
14#include "mem/Meter.h"
15
16namespace Mem
17{
18
21class Allocator : public Interface
22{
23public:
25 static const size_t FlushLimit = 1000;
26
27 Allocator(const char * const aLabel, const size_t sz):
28 label(aLabel),
30 {}
31
34 void relabel(const char * const aLabel) { label = aLabel; }
35
36 // TODO make this method const
41 virtual size_t getStats(PoolStats &) = 0;
42
44 void *alloc() {
45 if (++countAlloc == FlushLimit)
47 return allocate();
48 }
49
51 void freeOne(void *obj) {
52 assert(obj != nullptr);
54 deallocate(obj);
56 }
57
59 int getInUseCount() const { return meter.inuse.currentLevel(); }
60
62 void zeroBlocks(const bool doIt) { doZero = doIt; }
63
65 virtual void setChunkSize(size_t) {}
66
67 virtual bool idleTrigger(int shift) const = 0;
68
69 virtual void clean(time_t maxage) = 0;
70
75 if (countFreeOne) {
77 countFreeOne = 0;
78 }
79 if (countAlloc) {
81 countAlloc = 0;
82 }
83 if (countSavedAllocs) {
86 }
87 }
88
93 static size_t RoundedSize(const size_t minSize) { return ((minSize + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*); }
94
95public:
96
98 size_t countAlloc = 0;
99
102
104 size_t countFreeOne = 0;
105
106 // XXX: no counter for the number of free() calls avoided
107
109 const char *label;
110
112 const size_t objectSize;
113
116
117protected:
119 virtual void *allocate() = 0;
121 virtual void deallocate(void *) = 0;
122
130 bool doZero = true;
131};
132
133} // namespace Mem
134
135#endif /* SQUID_SRC_MEM_ALLOCATOR_H */
#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
static size_t RoundedSize(const size_t minSize)
Definition: Allocator.h:93
static const size_t FlushLimit
Flush counters to 'meter' after flush limit allocations.
Definition: Allocator.h:25
virtual void deallocate(void *)=0
freeOne(void *)
void flushCounters()
Definition: Allocator.h:74
size_t countFreeOne
the number of calls to Mem::Allocator::freeOne() since last flush
Definition: Allocator.h:104
int getInUseCount() const
the difference between the number of alloc() and freeOne() calls
Definition: Allocator.h:59
virtual size_t getStats(PoolStats &)=0
virtual void setChunkSize(size_t)
XXX: Misplaced – not all allocators have a notion of a "chunk". See MemPoolChunked.
Definition: Allocator.h:65
Allocator(const char *const aLabel, const size_t sz)
Definition: Allocator.h:27
const char * label
brief description of objects returned by alloc()
Definition: Allocator.h:109
void relabel(const char *const aLabel)
Definition: Allocator.h:34
virtual void clean(time_t maxage)=0
void freeOne(void *obj)
return memory reserved by alloc()
Definition: Allocator.h:51
virtual bool idleTrigger(int shift) const =0
void zeroBlocks(const bool doIt)
Definition: Allocator.h:62
void * alloc()
provide (and reserve) memory suitable for storing one object
Definition: Allocator.h:44
virtual void * allocate()=0
*alloc()
size_t countAlloc
the number of calls to Mem::Allocator::alloc() since last flush
Definition: Allocator.h:98
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
void update(size_t items, size_t itemSize)
account for memory actions taking place
Definition: Meter.h:68
mgb_t gb_allocated
Definition: Meter.h:94
Meter inuse
Definition: Meter.h:90
mgb_t gb_saved
Definition: Meter.h:98
mgb_t gb_freed
Definition: Meter.h:101
convenience base for any class with pure virtual method(s)
Definition: TypeTraits.h:19
Memory Management.
Definition: Allocator.h:17
#define VALGRIND_CHECK_MEM_IS_ADDRESSABLE
Definition: valgrind.h:28

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors