|
| 1 | +/* |
| 2 | + * Reflect Library by Parra Studios |
| 3 | + * A library for provide reflection and metadata representation. |
| 4 | + * |
| 5 | + * Copyright (C) 2016 - 2022 Vicente Eduardo Ferrer Garcia <[email protected]> |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef REFLECT_MEMORY_TRACKER_H |
| 22 | +#define REFLECT_MEMORY_TRACKER_H 1 |
| 23 | + |
| 24 | +#include <reflect/reflect_api.h> |
| 25 | + |
| 26 | +#ifdef __cplusplus |
| 27 | +extern "C" { |
| 28 | +#endif |
| 29 | + |
| 30 | +#if defined(REFLECT_MEMORY_TRACKER) && REFLECT_MEMORY_TRACKER == 1 |
| 31 | + |
| 32 | + #include <threading/threading_atomic.h> |
| 33 | + |
| 34 | + #include <format/format_specifier.h> |
| 35 | + |
| 36 | + #include <stdio.h> |
| 37 | + |
| 38 | + #define reflect_memory_tracker(name) \ |
| 39 | + static struct \ |
| 40 | + { \ |
| 41 | + atomic_uintmax_t allocations; \ |
| 42 | + atomic_uintmax_t deallocations; \ |
| 43 | + atomic_uintmax_t increments; \ |
| 44 | + atomic_uintmax_t decrements; \ |
| 45 | + } name = { 0, 0, 0, 0 } |
| 46 | + |
| 47 | + #define reflect_memory_tracker_allocation(name) \ |
| 48 | + atomic_fetch_add_explicit(&name.allocations, 1, memory_order_relaxed) |
| 49 | + |
| 50 | + #define reflect_memory_tracker_deallocation(name) \ |
| 51 | + atomic_fetch_add_explicit(&name.deallocations, 1, memory_order_relaxed) |
| 52 | + |
| 53 | + #define reflect_memory_tracker_increment(name) \ |
| 54 | + atomic_fetch_add_explicit(&name.increments, 1, memory_order_relaxed) |
| 55 | + |
| 56 | + #define reflect_memory_tracker_decrement(name) \ |
| 57 | + atomic_fetch_add_explicit(&name.decrements, 1, memory_order_relaxed) |
| 58 | + |
| 59 | + #if !defined(NDEBUG) || defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG) || defined(__DEBUG__) |
| 60 | + #define reflect_memory_tracker_print(name, title) \ |
| 61 | + do \ |
| 62 | + { \ |
| 63 | + printf("----------------- " title " -----------------\n"); \ |
| 64 | + printf("Allocations: %" PRIuS "\n", atomic_load_explicit(&name.allocations, memory_order_relaxed)); \ |
| 65 | + printf("Deallocations: %" PRIuS "\n", atomic_load_explicit(&name.deallocations, memory_order_relaxed)); \ |
| 66 | + printf("Increments: %" PRIuS "\n", atomic_load_explicit(&name.increments, memory_order_relaxed)); \ |
| 67 | + printf("Decrements: %" PRIuS "\n", atomic_load_explicit(&name.decrements, memory_order_relaxed)); \ |
| 68 | + fflush(stdout); \ |
| 69 | + } while (0) |
| 70 | + #else |
| 71 | + #define reflect_memory_tracker_print(name, title) \ |
| 72 | + do \ |
| 73 | + { \ |
| 74 | + uintmax_t allocations = atomic_load_explicit(&name.allocations, memory_order_relaxed); \ |
| 75 | + uintmax_t deallocations = atomic_load_explicit(&name.deallocations, memory_order_relaxed); \ |
| 76 | + uintmax_t increments = atomic_load_explicit(&name.increments, memory_order_relaxed); \ |
| 77 | + uintmax_t decrements = atomic_load_explicit(&name.decrements, memory_order_relaxed); \ |
| 78 | + /* This comparison is safe to be done like this because it is done once execution has finalized */ \ |
| 79 | + if (allocations != deallocations || increments != decrements) \ |
| 80 | + { \ |
| 81 | + printf("----------------- " title " -----------------\n"); \ |
| 82 | + printf("Allocations: %" PRIuS "\n", allocations); \ |
| 83 | + printf("Deallocations: %" PRIuS "\n", deallocations); \ |
| 84 | + printf("Increments: %" PRIuS "\n", increments); \ |
| 85 | + printf("Decrements: %" PRIuS "\n", decrements); \ |
| 86 | + fflush(stdout); \ |
| 87 | + } \ |
| 88 | + } while (0) |
| 89 | + #endif |
| 90 | +#else |
| 91 | + #define reflect_memory_tracker(name) \ |
| 92 | + typedef char reflect_memory_tracker_disabled |
| 93 | + |
| 94 | + #define reflect_memory_tracker_allocation(name) \ |
| 95 | + do \ |
| 96 | + { \ |
| 97 | + } while (0) |
| 98 | + |
| 99 | + #define reflect_memory_tracker_deallocation(name) \ |
| 100 | + do \ |
| 101 | + { \ |
| 102 | + } while (0) |
| 103 | + |
| 104 | + #define reflect_memory_tracker_increment(name) \ |
| 105 | + do \ |
| 106 | + { \ |
| 107 | + } while (0) |
| 108 | + |
| 109 | + #define reflect_memory_tracker_decrement(name) \ |
| 110 | + do \ |
| 111 | + { \ |
| 112 | + } while (0) |
| 113 | + |
| 114 | + #define reflect_memory_tracker_print(name, title) \ |
| 115 | + do \ |
| 116 | + { \ |
| 117 | + } while (0) |
| 118 | +#endif |
| 119 | + |
| 120 | +void reflect_memory_tracker_debug(void); |
| 121 | + |
| 122 | +#ifdef __cplusplus |
| 123 | +} |
| 124 | +#endif |
| 125 | + |
| 126 | +#endif /* REFLECT_MEMORY_TRACKER_H */ |
0 commit comments