Skip to content

Commit dc1d3f9

Browse files
committed
Fixed issue with counting realloc memory
1 parent bd4d8a7 commit dc1d3f9

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/jc/memory.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdlib.h>
55
#include <unistd.h>
66

7-
7+
#include <stdatomic.h>
88
#include <string.h> // strlen
99

1010
#if defined(__APPLE__)
@@ -28,19 +28,14 @@
2828

2929
typedef volatile uint64_t uint64_atomic_t;
3030

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)
4439

4540
//#define DEBUG_ALLOCATIONS
4641

@@ -106,12 +101,12 @@ void* FNNAME(realloc)(void* oldptr, size_t size)
106101
if (g_Initialized == 0)
107102
return realloc(oldptr, size);
108103

104+
size_t oldsize = MEMUSED(oldptr);
109105
void* ptr = realloc(oldptr, size);
110106
// The allocated size may be larger than the requested size
111107
// so in order to avoid over flow when subtracting later...
112108
size = MEMUSED(ptr);
113109

114-
size_t oldsize = MEMUSED(oldptr);
115110
debug("realloc: %zu (%zu) %p\n", size, oldsize, ptr);
116111
ATOMICADD(&g_TotalNumAllocations, 1);
117112
if(oldptr == 0)

0 commit comments

Comments
 (0)