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
32 // TODO make this method const
37 virtual size_t getStats(PoolStats &) = 0;
38
40 void *alloc() {
41 if (++countAlloc == FlushLimit)
43 return allocate();
44 }
45
47 void freeOne(void *obj) {
48 assert(obj != nullptr);
50 deallocate(obj);
52 }
53
55 int getInUseCount() const { return meter.inuse.currentLevel(); }
56
58 void zeroBlocks(const bool doIt) { doZero = doIt; }
59
61 virtual void setChunkSize(size_t) {}
62
63 virtual bool idleTrigger(int shift) const = 0;
64
65 virtual void clean(time_t maxage) = 0;
66
71 if (countFreeOne) {
73 countFreeOne = 0;
74 }
75 if (countAlloc) {
77 countAlloc = 0;
78 }
79 if (countSavedAllocs) {
82 }
83 }
84
89 static size_t RoundedSize(const size_t minSize) { return ((minSize + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*); }
90
91public:
92
94 size_t countAlloc = 0;
95
97 size_t countSavedAllocs = 0;
98
100 size_t countFreeOne = 0;
101
102 // XXX: no counter for the number of free() calls avoided
103
105 const char *const label;
106
108 const size_t objectSize;
109
112
113protected:
115 virtual void *allocate() = 0;
117 virtual void deallocate(void *) = 0;
118
126 bool doZero = true;
127};
128
129} // namespace Mem
130
131#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:108
static size_t RoundedSize(const size_t minSize)
Definition: Allocator.h:89
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:70
size_t countFreeOne
the number of calls to Mem::Allocator::freeOne() since last flush
Definition: Allocator.h:100
int getInUseCount() const
the difference between the number of alloc() and freeOne() calls
Definition: Allocator.h:55
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:61
Allocator(const char *const aLabel, const size_t sz)
Definition: Allocator.h:27
const char *const label
brief description of objects returned by alloc()
Definition: Allocator.h:105
virtual void clean(time_t maxage)=0
void freeOne(void *obj)
return memory reserved by alloc()
Definition: Allocator.h:47
virtual bool idleTrigger(int shift) const =0
void zeroBlocks(const bool doIt)
Definition: Allocator.h:58
void * alloc()
provide (and reserve) memory suitable for storing one object
Definition: Allocator.h:40
virtual void * allocate()=0
*alloc()
size_t countAlloc
the number of calls to Mem::Allocator::alloc() since last flush
Definition: Allocator.h:94
PoolMeter meter
statistics tracked for this allocator
Definition: Allocator.h:111
size_t countSavedAllocs
the number of malloc()/calloc() calls avoided since last flush
Definition: Allocator.h:97
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