test/util_allocator.h

changeset 518
74d0372f5c6f
parent 500
eb9e7bd40a8e
child 571
f83583a0bbac
equal deleted inserted replaced
517:b3baaf9b7e3c 518:74d0372f5c6f
29 #ifndef UCX_UTIL_ALLOCATOR_H 29 #ifndef UCX_UTIL_ALLOCATOR_H
30 #define UCX_UTIL_ALLOCATOR_H 30 #define UCX_UTIL_ALLOCATOR_H
31 31
32 #include "cx/allocator.h" 32 #include "cx/allocator.h"
33 33
34 #ifdef __cplusplus 34 #include <set>
35 extern "C" {
36 #endif
37 35
38 #define CX_TESTING_ALLOCATOR_MAX_LIVE 1024 36 struct CxTestingAllocator : public CxAllocator {
39
40 typedef struct {
41 /** 37 /**
42 * Total number of all allocations (malloc, calloc, realloc). 38 * Total number of all allocations (malloc, calloc, realloc).
43 * A realloc() does only count when the memory has to be moved. 39 * A realloc() does only count when the memory has to be moved.
44 */ 40 */
45 int alloc_total; 41 unsigned alloc_total = 0;
46 /** 42 /**
47 * Number of failed allocations (malloc, calloc, realloc). 43 * Number of failed allocations (malloc, calloc, realloc).
48 */ 44 */
49 int alloc_failed; 45 unsigned alloc_failed = 0;
50 /** 46 /**
51 * Total number of freed pointers. 47 * Total number of freed pointers.
52 * A reallocation also counts as a free when the memory has to be moved. 48 * A reallocation also counts as a free when the memory has to be moved.
53 */ 49 */
54 int free_total; 50 unsigned free_total = 0;
55 /** 51 /**
56 * Number of failed free invocations. 52 * Number of failed free invocations.
57 * A free() is considered failed, if it has not been performed on tracked memory. 53 * A free() is considered failed, if it has not been performed on tracked memory.
58 */ 54 */
59 int free_failed; 55 unsigned free_failed = 0;
60 /** 56 /**
61 * Number of memory blocks that are currently live (and tracked). 57 * The set of tracked memory blocks.
62 * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE.
63 */ 58 */
64 int live; 59 std::set<void *> tracked;
60
65 /** 61 /**
66 * The array of tracked memory blocks. 62 * Constructs a new testing allocator.
67 */ 63 */
68 void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE]; 64 CxTestingAllocator();
69 } cx_testing_allocator_s;
70 65
71 extern CxAllocator *cxTestingAllocator; 66 /**
72 67 * Verifies that all allocated memory blocks are freed and no free occurred twice.
73 /** 68 *
74 * Resets the testing allocator information. 69 * @return true iff all tracked allocations / deallocations were valid
75 * This function SHOULD be called prior to any use of this allocator. 70 */
76 */ 71 bool verify() const;
77 void cxTestingAllocatorReset(void); 72 };
78
79 /**
80 * Checks whether all allocated memory is properly freed and no failed (de)allocations happened.
81 *
82 * @return true on success, false if there was any problem
83 */
84 bool cxTestingAllocatorVerify(void);
85
86 #ifdef __cplusplus
87 }; /* extern "C" */
88 #endif
89 73
90 #endif /* UCX_UTIL_ALLOCATOR_H */ 74 #endif /* UCX_UTIL_ALLOCATOR_H */

mercurial