]>
Commit | Line | Data |
---|---|---|
ed6e9fb9 | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 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. | |
21 | */ | |
22 | #define MEMPROXY_CLASS(CLASS) \ | |
23 | private: \ | |
24 | static inline Mem::AllocatorProxy &Pool() { \ | |
25 | static Mem::AllocatorProxy thePool(#CLASS, sizeof(CLASS)); \ | |
26 | return thePool; \ | |
27 | } \ | |
28 | public: \ | |
29 | void *operator new(size_t byteCount) { \ | |
30 | /* derived classes with different sizes must implement their own new */ \ | |
31 | assert(byteCount == sizeof(CLASS)); \ | |
32 | return Pool().alloc(); \ | |
33 | } \ | |
34 | void operator delete(void *address) {Pool().freeOne(address);} \ | |
35 | private: | |
36 | ||
37 | namespace Mem | |
38 | { | |
39 | ||
40 | /** | |
41 | * Support late binding of pool type for allocator agnostic classes | |
42 | */ | |
43 | class AllocatorProxy | |
44 | { | |
45 | public: | |
46 | AllocatorProxy(char const *aLabel, size_t const &aSize): | |
47 | label(aLabel), | |
48 | size(aSize), | |
49 | theAllocator(NULL) | |
50 | {} | |
51 | ||
52 | /// Allocate one element from the pool | |
53 | void *alloc(); | |
54 | ||
55 | /// Free a element allocated by Mem::AllocatorProxy::alloc() | |
56 | void freeOne(void *); | |
57 | ||
58 | int inUseCount() const; | |
59 | size_t objectSize() const {return size;} | |
60 | char const * objectType() const {return label;} | |
61 | ||
62 | MemPoolMeter const &getMeter() const; | |
63 | ||
64 | /** | |
f53969cc SM |
65 | * \param stats Object to be filled with statistical data about pool. |
66 | * \retval Number of objects in use, ie. allocated. | |
ed6e9fb9 AJ |
67 | */ |
68 | int getStats(MemPoolStats * stats); | |
69 | ||
70 | private: | |
71 | MemAllocator *getAllocator() const; | |
72 | ||
73 | const char *label; | |
74 | size_t size; | |
75 | mutable MemAllocator *theAllocator; | |
76 | }; | |
77 | ||
78 | } // namespace Mem | |
79 | ||
80 | #endif /* _SQUID_SRC_MEM_ALLOCATORPROXY_H */ | |
f53969cc | 81 |