Skip to content

Commit 95c5bad

Browse files
committed
Apply suggestions from code review
Remove `glibc` guards for new functions to support all libc implementations. Updates tests to skip if compilation fails on a specific platform. Signed-off-by: Nana Adjei Manu <[email protected]>
1 parent c49c4e2 commit 95c5bad

File tree

6 files changed

+212
-129
lines changed

6 files changed

+212
-129
lines changed

src/memray/_memray/hooks.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ free(void* ptr) noexcept
196196
}
197197
}
198198

199-
#if defined(__GLIBC__)
200199
void
201200
free_sized(void* ptr, size_t size) noexcept
202201
{
@@ -208,7 +207,6 @@ free_aligned_sized(void* ptr, size_t alignment, size_t size) noexcept
208207
{
209208
memray::intercept::free(ptr);
210209
}
211-
#endif
212210

213211
void*
214212
realloc(void* ptr, size_t size) noexcept

src/memray/_memray/hooks.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@
3030
FOR_EACH_HOOKED_FUNCTION(memalign) \
3131
FOR_EACH_HOOKED_FUNCTION(prctl) \
3232
FOR_EACH_HOOKED_FUNCTION(pvalloc) \
33-
FOR_EACH_HOOKED_FUNCTION(mmap64)
34-
FOR_EACH_HOOKED_FUNCTION(free_sized) \
35-
FOR_EACH_HOOKED_FUNCTION(free_aligned_sized)
33+
FOR_EACH_HOOKED_FUNCTION(mmap64) \
3634
#else
3735
# define MEMRAY_PLATFORM_HOOKED_FUNCTIONS \
3836
FOR_EACH_HOOKED_FUNCTION(memalign) \
3937
FOR_EACH_HOOKED_FUNCTION(prctl)
4038
#endif
4139

40+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
41+
# define MEMRAY_C23_HOOKED_FUNCTIONS \
42+
FOR_EACH_HOOKED_FUNCTION(free_sized) \
43+
FOR_EACH_HOOKED_FUNCTION(free_aligned_sized)
44+
#else
45+
# define MEMRAY_C23_HOOKED_FUNCTIONS
46+
#endif
47+
4248
#define MEMRAY_HOOKED_FUNCTIONS \
4349
FOR_EACH_HOOKED_FUNCTION(malloc) \
4450
FOR_EACH_HOOKED_FUNCTION(free) \
@@ -52,6 +58,7 @@
5258
FOR_EACH_HOOKED_FUNCTION(dlopen) \
5359
FOR_EACH_HOOKED_FUNCTION(dlclose) \
5460
FOR_EACH_HOOKED_FUNCTION(PyGILState_Ensure) \
61+
MEMRAY_C23_HOOKED_FUNCTIONS \
5562
MEMRAY_PLATFORM_HOOKED_FUNCTIONS
5663

5764
namespace memray::hooks {
@@ -201,13 +208,13 @@ mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) noexc
201208
#if defined(__GLIBC__)
202209
void*
203210
mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) noexcept;
211+
#endif
204212

205213
void
206214
free_sized(void* ptr, size_t size) noexcept;
207215

208216
void
209217
free_aligned_sized(void* ptr, size_t alignment, size_t size) noexcept;
210-
#endif
211218

212219
int
213220
munmap(void* addr, size_t length) noexcept;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
#include <assert.h>
5+
#include <stdlib.h>
6+
#include <dlfcn.h>
7+
8+
__attribute__((weak)) void free_sized(void* ptr, size_t size);
9+
__attribute__((weak)) void free_aligned_sized(void* ptr, size_t alignment, size_t size);
10+
11+
// Check if C23 functions are available
12+
static int functions_available = -1;
13+
14+
static void check_functions_available(void) {
15+
if (functions_available == -1) {
16+
functions_available = (free_sized != NULL && free_aligned_sized != NULL);
17+
}
18+
}
19+
20+
// Structure to hold allocation info
21+
void*
22+
test_free_sized(void)
23+
{
24+
void* address;
25+
26+
check_functions_available();
27+
if (!functions_available) {
28+
return address;
29+
}
30+
31+
void* ptr = malloc(1024);
32+
assert(ptr != NULL);
33+
34+
address = ptr;
35+
36+
free_sized(ptr, 1024);
37+
return address;
38+
}
39+
40+
void*
41+
test_free_aligned_sized(void)
42+
{
43+
void* address;
44+
45+
check_functions_available();
46+
if (!functions_available) {
47+
return address;
48+
}
49+
50+
void* ptr = aligned_alloc(64, 1024);
51+
assert(ptr != NULL);
52+
53+
address = ptr;
54+
55+
free_aligned_sized(ptr, 64, 1024);
56+
return address;
57+
}
58+
59+
void*
60+
test_both_free_functions(void)
61+
{
62+
void* address;
63+
64+
check_functions_available();
65+
if (!functions_available) {
66+
return NULL;
67+
}
68+
69+
void* ptr1 = malloc(512);
70+
assert(ptr1 != NULL);
71+
free_sized(ptr1, 512);
72+
73+
void* ptr2 = aligned_alloc(32, 256);
74+
assert(ptr2 != NULL);
75+
free_aligned_sized(ptr2, 32, 256);
76+
77+
address = ptr2;
78+
79+
return address;
80+
}
81+
82+
PyObject*
83+
run_free_sized_test(PyObject* self, PyObject* args)
84+
{
85+
check_functions_available();
86+
if (!functions_available) {
87+
Py_RETURN_NONE;
88+
}
89+
90+
void* address = test_free_sized();
91+
92+
// Return address for verification
93+
PyObject* result = Py_BuildValue("(KII)", address);
94+
return result;
95+
}
96+
97+
PyObject*
98+
run_free_aligned_sized_test(PyObject* self, PyObject* args)
99+
{
100+
check_functions_available();
101+
if (!functions_available) {
102+
Py_RETURN_NONE;
103+
}
104+
105+
void* address = test_free_aligned_sized();
106+
107+
PyObject* result = Py_BuildValue("(KII)", address);
108+
return result;
109+
}
110+
111+
PyObject*
112+
run_both_tests(PyObject* self, PyObject* args)
113+
{
114+
check_functions_available();
115+
if (!functions_available) {
116+
Py_RETURN_NONE; // Skip test if functions not available
117+
}
118+
119+
void* address = test_both_free_functions();
120+
121+
PyObject* result = Py_BuildValue("(KII)", address);
122+
return result;
123+
}
124+
125+
static PyMethodDef
126+
free_sized_methods[] = {
127+
{"run_free_sized_test", run_free_sized_test, METH_NOARGS, "Test free_sized function"},
128+
{"run_free_aligned_sized_test", run_free_aligned_sized_test, METH_NOARGS, "Test free_aligned_sized function"},
129+
{"run_both_tests", run_both_tests, METH_NOARGS, "Test both free functions"},
130+
{NULL, NULL, 0, NULL}
131+
};
132+
133+
static PyModuleDef
134+
free_sized_module = {
135+
PyModuleDef_HEAD_INIT,
136+
"free_sized_test",
137+
"Test module for free_sized and free_aligned_sized functions",
138+
-1,
139+
free_sized_methods,
140+
NULL,
141+
NULL,
142+
NULL,
143+
NULL
144+
};
145+
146+
PyMODINIT_FUNC
147+
PyInit_free_sized_test(void)
148+
{
149+
return PyModule_Create(&free_sized_module);
150+
}

tests/integration/free_sized_extension/free_sized_test.cpp

Lines changed: 0 additions & 111 deletions
This file was deleted.

tests/integration/free_sized_extension/setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
ext_modules=[
66
Extension(
77
"free_sized_test",
8-
sources=["free_sized_test.cpp"],
9-
language="c++",
8+
sources=["free_sized_test.c"],
9+
language="c",
10+
extra_compile_args=["-std=c23"]
1011
)
1112
],
1213
)

0 commit comments

Comments
 (0)