| 1 | /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/wrappers/ansiwrapper.h`/ |
| 2 | |
| 3 | #ifndef HL_ANSIWRAPPER_H |
| 4 | #define HL_ANSIWRAPPER_Hcxx . . $ `vendor/Heap-Layers/wrappers/ansiwrapper.h:4:9`! |
| 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 HLcxx . . $ HL/ { |
| 25 | |
| 26 | template <class SuperHeap> |
| 27 | class ANSIWrappercxx . . $ HL/ANSIWrapper# : public SuperHeap { |
| 28 | private: |
| 29 | static constexpr int gcdcxx . . $ HL/ANSIWrapper#gcd(9b79fb6aee4c0440).(int a, int b) { |
| 30 | return (b == 0) ? a : gcdcxx . . $ HL/ANSIWrapper#gcd(9b79fb6aee4c0440).(b, a % b); |
| 31 | } |
| 32 | |
| 33 | public: |
| 34 | |
| 35 | ANSIWrappercxx . . $ HL/ANSIWrapper#`ANSIWrapper<SuperHeap>`(49f6e7a06ebc5aa8).() { |
| 36 | static_assert(gcdcxx . . $ HL/ANSIWrapper#gcd(9b79fb6aee4c0440).(SuperHeap::Alignment, 8) ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). 8, "Alignment mismatch"); |
| 37 | } |
| 38 | using SuperHeap::SuperHeap; |
| 39 | |
| 40 | inline void * malloccxx . . $ HL/ANSIWrapper#malloc(bfcfa4d6b7f7ef64). (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_BITcxx . . $ `<external>/67164224788392828/limits.h:64:9`! - 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 freecxx . . $ HL/ANSIWrapper#free(5d22bdacc48458e8). (void * ptr) { |
| 62 | if (ptr != 0) { |
| 63 | SuperHeap::free (ptr); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | inline void free_sizedcxx . . $ HL/ANSIWrapper#free_sized(5500c4293b1bbef0). (void * ptr, size_t sz) { |
| 68 | if (ptr != 0) { |
| 69 | SuperHeap::free_sized (ptr, sz); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | inline void * calloccxx . . $ HL/ANSIWrapper#calloc(63a7bf2941bfcd7a). (size_t s1, size_t s2) { |
| 74 | auto * ptr = (char *) malloccxx . . $ HL/ANSIWrapper#malloc(bfcfa4d6b7f7ef64). (s1 * s2); |
| 75 | if (ptr) { |
| 76 | memset (ptr, 0, s1 * s2); |
| 77 | } |
| 78 | return (void *) ptr; |
| 79 | } |
| 80 | |
| 81 | inline void * realloccxx . . $ HL/ANSIWrapper#realloc(417ab160f6baadc6). (void * ptr, const size_t sz) { |
| 82 | if (ptr == 0) { |
| 83 | return malloccxx . . $ HL/ANSIWrapper#malloc(bfcfa4d6b7f7ef64). (sz); |
| 84 | } |
| 85 | if (sz == 0) { |
| 86 | freecxx . . $ HL/ANSIWrapper#free(5d22bdacc48458e8). (ptr); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | auto objSize = getSizecxx . . $ HL/ANSIWrapper#getSize(751e5d26f8574c32). (ptr); |
| 91 | if (objSize ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). sz) { |
| 92 | return ptr; |
| 93 | } |
| 94 | |
| 95 | // Allocate a new block of size sz. |
| 96 | auto * buf = malloccxx . . $ HL/ANSIWrapper#malloc(bfcfa4d6b7f7ef64). (sz); |
| 97 | |
| 98 | // Copy the contents of the original object |
| 99 | // up to the size of the new block. |
| 100 | |
| 101 | auto minSize = (objSize <cxx . . $ std/`operator<`(39b98f4fe21ae589). sz) ? objSize : sz; |
| 102 | if (buf) { |
| 103 | memcpy (buf, ptr, minSize); |
| 104 | } |
| 105 | |
| 106 | // Free the old block. |
| 107 | freecxx . . $ HL/ANSIWrapper#free(5d22bdacc48458e8). (ptr); |
| 108 | return buf; |
| 109 | } |
| 110 | |
| 111 | inline size_t getSizecxx . . $ HL/ANSIWrapper#getSize(751e5d26f8574c32). (void * ptr) { |
| 112 | if (ptr) { |
| 113 | return SuperHeap::getSize (ptr); |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | #endif |