$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
mem.h
Go to the documentation of this file.
1 #ifndef MEM_H
2 #define MEM_H
3 
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #ifdef TRACY_ENABLE
8 #include "tracy/TracyC.h"
9 
10 #pragma GCC diagnostic push
11 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
12 
13 static inline void* tracy_malloc(size_t size)
14 {
15  void* ptr = NULL;
16  ptr = malloc(size);
17  if (ptr) {
18  TracyCAlloc(ptr, size);
19  }
20  return ptr;
21 }
22 
23 static inline void* tracy_calloc(size_t num, size_t size)
24 {
25  void* ptr = NULL;
26  ptr = calloc(num, size);
27  if (ptr) {
28  TracyCAlloc(ptr, num * size);
29  }
30  return ptr;
31 }
32 
33 static inline void* tracy_realloc(void* ptr, size_t size)
34 {
35  if (ptr) {
36  TracyCFree(ptr);
37  }
38  void* new_ptr = NULL;
39  new_ptr = realloc(ptr, size);
40  if (new_ptr) {
41  TracyCAlloc(new_ptr, size);
42  }
43  return new_ptr;
44 }
45 
46 static inline void tracy_free(void* ptr)
47 {
48  if (ptr) {
49  TracyCFree(ptr);
50  free(ptr);
51  }
52 }
53 
54 static inline char* tracy_strdup(const char* str)
55 {
56  char* ptr = NULL;
57  ptr = strdup(str);
58  if (ptr) {
59  TracyCAlloc(ptr, strlen(ptr) + 1);
60  }
61  return ptr;
62 }
63 
64 static inline int tracy_posix_memalign(void** memptr, size_t alignment,
65  size_t size)
66 {
67  int res = posix_memalign(memptr, alignment, size);
68  if (res == 0 && *memptr) {
69  TracyCAlloc(*memptr, size);
70  }
71  return res;
72 }
73 
74 static inline void* tracy_aligned_alloc(size_t alignment, size_t size)
75 {
76  void* ptr = NULL;
77  ptr = aligned_alloc(alignment, size);
78  if (ptr) {
79  TracyCAlloc(ptr, size);
80  }
81  return ptr;
82 }
83 
84 #define malloc(x) tracy_malloc(x)
85 #define calloc(x, y) tracy_calloc(x, y)
86 #define realloc(x, y) tracy_realloc(x, y)
87 #define free(x) tracy_free(x)
88 #define strdup(x) tracy_strdup(x)
89 #define posix_memalign(x, y, z) tracy_posix_memalign(x, y, z)
90 #define aligned_alloc(x, y) tracy_aligned_alloc(x, y)
91 
92 #pragma GCC diagnostic pop
93 
94 #endif // TRACY_ENABLE
95 
96 #endif // MEM_H