|
4 | 4 | #include <stdlib.h>
|
5 | 5 | #include <unistd.h>
|
6 | 6 |
|
7 |
| - |
| 7 | +#include <stdatomic.h> |
8 | 8 | #include <string.h> // strlen
|
9 | 9 |
|
10 | 10 | #if defined(__APPLE__)
|
|
28 | 28 |
|
29 | 29 | typedef volatile uint64_t uint64_atomic_t;
|
30 | 30 |
|
31 |
| -uint64_atomic_t g_TotalNumAllocations = 0; |
32 |
| -uint64_atomic_t g_TotalMemAllocated = 0; |
33 |
| -uint64_atomic_t g_NumAllocations = 0; // Current |
34 |
| -uint64_atomic_t g_MemAllocated = 0; // Current |
35 |
| -uint64_atomic_t g_Initialized = 0; // Set after the initalization of the profiler is done |
36 |
| - |
37 |
| -#if defined(__GNUC__) |
38 |
| - #define ATOMICADD(_PTR, _VALUE) __sync_fetch_and_add(_PTR, _VALUE) |
39 |
| - #define ATOMICSUB(_PTR, _VALUE) __sync_fetch_and_sub(_PTR, _VALUE) |
40 |
| -#else // windows |
41 |
| - #define ATOMICADD(_PTR, _VALUE) InterlockedAdd64(_PTR, _VALUE) |
42 |
| - #define ATOMICSUB(_PTR, _VALUE) InterlockedSub64(_PTR, _VALUE) |
43 |
| -#endif |
| 31 | +atomic_size_t g_TotalNumAllocations = 0; |
| 32 | +atomic_size_t g_TotalMemAllocated = 0; |
| 33 | +atomic_size_t g_NumAllocations = 0; // Current |
| 34 | +atomic_size_t g_MemAllocated = 0; // Current |
| 35 | +atomic_size_t g_Initialized = 0; // Set after the initalization of the profiler is done |
| 36 | + |
| 37 | +#define ATOMICADD(_PTR, _VALUE) atomic_fetch_add(_PTR, _VALUE) |
| 38 | +#define ATOMICSUB(_PTR, _VALUE) atomic_fetch_sub(_PTR, _VALUE) |
44 | 39 |
|
45 | 40 | //#define DEBUG_ALLOCATIONS
|
46 | 41 |
|
@@ -106,12 +101,12 @@ void* FNNAME(realloc)(void* oldptr, size_t size)
|
106 | 101 | if (g_Initialized == 0)
|
107 | 102 | return realloc(oldptr, size);
|
108 | 103 |
|
| 104 | + size_t oldsize = MEMUSED(oldptr); |
109 | 105 | void* ptr = realloc(oldptr, size);
|
110 | 106 | // The allocated size may be larger than the requested size
|
111 | 107 | // so in order to avoid over flow when subtracting later...
|
112 | 108 | size = MEMUSED(ptr);
|
113 | 109 |
|
114 |
| - size_t oldsize = MEMUSED(oldptr); |
115 | 110 | debug("realloc: %zu (%zu) %p\n", size, oldsize, ptr);
|
116 | 111 | ATOMICADD(&g_TotalNumAllocations, 1);
|
117 | 112 | if(oldptr == 0)
|
|
0 commit comments