| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | #ifndef HL_ANSIWRAPPER_H |
| 4 | #define HL_ANSIWRAPPER_H |
| 5 | |
| 6 | #include <assert.h> |
| 7 | #include <string.h> |
| 8 | #include <stddef.h> |
| 9 | #include <cstdint> |
| 10 | #include <climits> |
| 11 | |
| 12 | #ifndef _WIN32 |
| 13 | #include <stdalign.h> |
| 14 | #endif |
| 15 | |
| 16 | /* |
| 17 | * @class ANSIWrapper |
| 18 | * @brief Provide ANSI C behavior for malloc & free. |
| 19 | * |
| 20 | * Implements all prescribed ANSI behavior, including zero-sized |
| 21 | * requests & aligned request sizes to a double word (or long word). |
| 22 | */ |
| 23 | |
| 24 | namespace HL { |
| 25 | |
| 26 | template <class SuperHeap> |
| 27 | class ANSIWrapper : public SuperHeap { |
| 28 | private: |
| 29 | static constexpr int gcd(int a, int b) { |
| 30 | return (b == 0) ? a : gcd(b, a % b); |
| 31 | } |
| 32 | |
| 33 | public: |
| 34 | |
| 35 | ANSIWrapper() { |
| 36 | static_assert(gcd(SuperHeap::Alignment, 8) == 8, "Alignment mismatch"); |
| 37 | } |
| 38 | using SuperHeap::SuperHeap; |
| 39 | |
| 40 | inline void * malloc (size_t sz) { |
| 41 | #if !defined(HL_NO_MALLOC_SIZE_CHECKS) |
| 42 | static constexpr int alignment = 16; // safe for all platforms |
| 43 | if (sz < alignment) { |
| 44 | sz = alignment; |
| 45 | } |
| 46 | // Prevent integer underflows. This maximum should (and |
| 47 | // currently does) provide more than enough slack to compensate for any |
| 48 | // rounding below (in the alignment section). |
| 49 | if (sz >> (sizeof(size_t) * CHAR_BIT - 1)) { |
| 50 | return 0; |
| 51 | } |
| 52 | // Enforce alignment requirements: round up allocation sizes if needed. |
| 53 | // Enforce alignment. |
| 54 | sz = (sz + alignment - 1UL) & |
| 55 | ~(alignment - 1UL); |
| 56 | #endif |
| 57 | auto * ptr = SuperHeap::malloc (sz); |
| 58 | return ptr; |
| 59 | } |
| 60 | |
| 61 | inline void free (void * ptr) { |
| 62 | if (ptr != 0) { |
| 63 | SuperHeap::free (ptr); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | inline void free_sized (void * ptr, size_t sz) { |
| 68 | if (ptr != 0) { |
| 69 | SuperHeap::free_sized (ptr, sz); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | inline void * calloc (size_t s1, size_t s2) { |
| 74 | auto * ptr = (char *) malloc (s1 * s2); |
| 75 | if (ptr) { |
| 76 | memset (ptr, 0, s1 * s2); |
| 77 | } |
| 78 | return (void *) ptr; |
| 79 | } |
| 80 | |
| 81 | inline void * realloc (void * ptr, const size_t sz) { |
| 82 | if (ptr == 0) { |
| 83 | return malloc (sz); |
| 84 | } |
| 85 | if (sz == 0) { |
| 86 | free (ptr); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | auto objSize = getSize (ptr); |
| 91 | if (objSize == sz) { |
| 92 | return ptr; |
| 93 | } |
| 94 | |
| 95 | // Allocate a new block of size sz. |
| 96 | auto * buf = malloc (sz); |
| 97 | |
| 98 | // Copy the contents of the original object |
| 99 | // up to the size of the new block. |
| 100 | |
| 101 | auto minSize = (objSize < sz) ? objSize : sz; |
| 102 | if (buf) { |
| 103 | memcpy (buf, ptr, minSize); |
| 104 | } |
| 105 | |
| 106 | // Free the old block. |
| 107 | free (ptr); |
| 108 | return buf; |
| 109 | } |
| 110 | |
| 111 | inline size_t getSize (void * ptr) { |
| 112 | if (ptr) { |
| 113 | return SuperHeap::getSize (ptr); |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | #endif |