AllocatorProxy.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_ALLOCATORPROXY_H
10 #define SQUID_SRC_MEM_ALLOCATORPROXY_H
11 
12 // XXX: remove AllocatorProxy.h include from mem/forward.h
13 namespace Mem {
14 class Allocator;
15 class PoolStats;
16 }
17 
28 #define MEMPROXY_CLASS(CLASS) \
29  private: \
30  static inline Mem::AllocatorProxy &Pool() { \
31  static Mem::AllocatorProxy thePool(#CLASS, sizeof(CLASS), false); \
32  return thePool; \
33  } \
34  public: \
35  void *operator new(size_t byteCount) { \
36  /* derived classes with different sizes must implement their own new */ \
37  assert(byteCount == sizeof(CLASS)); \
38  return Pool().alloc(); \
39  } \
40  void operator delete(void *address) { \
41  if (address) \
42  Pool().freeOne(address); \
43  } \
44  static int UseCount() { return Pool().inUseCount(); } \
45  private:
46 
47 namespace Mem
48 {
49 
50 class PoolMeter;
51 
56 {
57 public:
58  AllocatorProxy(char const *aLabel, size_t const &aSize, bool doZeroBlocks = true):
59  label(aLabel),
60  size(aSize),
61  theAllocator(nullptr),
62  doZero(doZeroBlocks)
63  {}
64 
66  void *alloc();
67 
69  void freeOne(void *);
70 
71  int inUseCount() const;
72  size_t objectSize() const {return size;}
73  char const * objectType() const {return label;}
74 
75  PoolMeter const &getMeter() const;
76 
81  size_t getStats(PoolStats &stats);
82 
83  void zeroBlocks(bool doIt);
84 
86  void relabel(const char * const aLabel);
87 
88 private:
89  Allocator *getAllocator() const;
90 
91  const char *label;
92  size_t size;
94  bool doZero;
95 };
96 
97 } // namespace Mem
98 
99 #endif /* SQUID_SRC_MEM_ALLOCATORPROXY_H */
100 
void freeOne(void *)
Free a element allocated by Mem::AllocatorProxy::alloc()
size_t getStats(PoolStats &stats)
int inUseCount() const
void * alloc()
Allocate one element from the pool.
const char * objectType() const
Memory Management.
Definition: Allocator.h:16
Allocator * getAllocator() const
const PoolMeter & getMeter() const
size_t objectSize() const
AllocatorProxy(char const *aLabel, size_t const &aSize, bool doZeroBlocks=true)
void zeroBlocks(bool doIt)
Allocator * theAllocator
void relabel(const char *const aLabel)
const char * label

 

Introduction

Documentation

Support

Miscellaneous