]>
Commit | Line | Data |
---|---|---|
ed6e9fb9 | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
ed6e9fb9 AJ |
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 | class MemAllocator; | |
13 | class MemPoolStats; | |
14 | class MemPoolMeter; | |
15 | ||
16 | /** | |
17 | * \hideinitializer | |
18 | * | |
19 | * Pool and account the memory used for the CLASS object. | |
20 | * This macro is intended for use within the declaration of a class. | |
341d719c FC |
21 | * |
22 | * The memory block allocated by operator new is not zeroed; it is the | |
23 | * responsibility of users to ensure that constructors correctly | |
24 | * initialize all data members. | |
ed6e9fb9 AJ |
25 | */ |
26 | #define MEMPROXY_CLASS(CLASS) \ | |
27 | private: \ | |
28 | static inline Mem::AllocatorProxy &Pool() { \ | |
341d719c | 29 | static Mem::AllocatorProxy thePool(#CLASS, sizeof(CLASS), false); \ |
ed6e9fb9 AJ |
30 | return thePool; \ |
31 | } \ | |
32 | public: \ | |
33 | void *operator new(size_t byteCount) { \ | |
34 | /* derived classes with different sizes must implement their own new */ \ | |
35 | assert(byteCount == sizeof(CLASS)); \ | |
36 | return Pool().alloc(); \ | |
37 | } \ | |
6ba24a7c FC |
38 | void operator delete(void *address) { \ |
39 | if (address) \ | |
40 | Pool().freeOne(address); \ | |
41 | } \ | |
6b37e890 | 42 | static int UseCount() { return Pool().inUseCount(); } \ |
ed6e9fb9 AJ |
43 | private: |
44 | ||
45 | namespace Mem | |
46 | { | |
47 | ||
48 | /** | |
49 | * Support late binding of pool type for allocator agnostic classes | |
50 | */ | |
51 | class AllocatorProxy | |
52 | { | |
53 | public: | |
341d719c | 54 | AllocatorProxy(char const *aLabel, size_t const &aSize, bool doZeroBlocks = true): |
ed6e9fb9 AJ |
55 | label(aLabel), |
56 | size(aSize), | |
341d719c FC |
57 | theAllocator(nullptr), |
58 | doZero(doZeroBlocks) | |
ed6e9fb9 AJ |
59 | {} |
60 | ||
61 | /// Allocate one element from the pool | |
62 | void *alloc(); | |
63 | ||
64 | /// Free a element allocated by Mem::AllocatorProxy::alloc() | |
65 | void freeOne(void *); | |
66 | ||
67 | int inUseCount() const; | |
68 | size_t objectSize() const {return size;} | |
69 | char const * objectType() const {return label;} | |
70 | ||
71 | MemPoolMeter const &getMeter() const; | |
72 | ||
73 | /** | |
f53969cc SM |
74 | * \param stats Object to be filled with statistical data about pool. |
75 | * \retval Number of objects in use, ie. allocated. | |
ed6e9fb9 AJ |
76 | */ |
77 | int getStats(MemPoolStats * stats); | |
78 | ||
341d719c FC |
79 | void zeroBlocks(bool doIt); |
80 | ||
ed6e9fb9 AJ |
81 | private: |
82 | MemAllocator *getAllocator() const; | |
83 | ||
84 | const char *label; | |
85 | size_t size; | |
86 | mutable MemAllocator *theAllocator; | |
341d719c | 87 | bool doZero; |
ed6e9fb9 AJ |
88 | }; |
89 | ||
90 | } // namespace Mem | |
91 | ||
92 | #endif /* _SQUID_SRC_MEM_ALLOCATORPROXY_H */ | |
f53969cc | 93 |