Debian Codegraph @

vendor/Heap-Layers/heaps/debug/checkheap.h

raw symbols SCIP
1 // -*- C++ -*-
2
3 #ifndef HL_CHECKHEAP_H
4 #define HL_CHECKHEAP_H
5
6 #include <cstring>
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 /**
13 * @class CheckHeap
14 * @brief Performs simple checks on memory allocations.
15 *
16 **/
17
18 namespace HL {
19
20 template <class SuperHeap>
21 class CheckHeap : public SuperHeap {
22 private:
23
24 enum { RECEIVED_A_NULL_OBJECT_FROM_MALLOC = 0 };
25 enum { RECEIVED_AN_UNALIGNED_OBJECT_FROM_MALLOC = 0 };
26
27 public:
28
29 inline void * malloc (size_t sz) {
30 void * addr = SuperHeap::malloc (sz);
31 #if !defined(NDEBUG)
32
33 // Check for null (this should in general not happen).
34 if (addr == NULL) {
35 assert (RECEIVED_A_NULL_OBJECT_FROM_MALLOC);
36 printf ("RECEIVED_A_NULL_OBJECT_FROM_MALLOC\n");
37 abort();
38 }
39 // Ensure object size is correct.
40 assert (SuperHeap::getSize(addr) >= sz);
41
42 // Check alignment.
43 if ((unsigned long) addr % SuperHeap::Alignment != 0) {
44 assert (RECEIVED_AN_UNALIGNED_OBJECT_FROM_MALLOC);
45 printf ("RECEIVED_AN_UNALIGNED_OBJECT_FROM_MALLOC\n");
46 abort();
47 }
48
49 // Wipe out the old contents.
50 std::memset (addr, 0, SuperHeap::getSize(addr));
51 #endif
52 return addr;
53 }
54
55 inline void free (void * ptr) {
56 #if !defined(NDEBUG)
57 // Wipe out the old contents.
58 std::memset (ptr, 0, SuperHeap::getSize(ptr));
59 #endif
60 SuperHeap::free (ptr);
61 }
62
63 };
64
65 }
66
67 #endif