Skip to content

Commit

Permalink
Add stubs for pvalloc
Browse files Browse the repository at this point in the history
`pvalloc` is a `glibc` extension as is not always available in the
system. To simplify the test suite, add stubs for `pvalloc` if is not
defined and skip the tests that involve this allocator if is not
available.

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal committed May 6, 2022
1 parent a2d0e7c commit bc74b66
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/memray/_memray/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <malloc.h>
#include <stdlib.h>

#ifndef __GLIBC__
extern "C" {
void*
pvalloc(size_t size)
{
return NULL;
}
}
#endif
4 changes: 1 addition & 3 deletions src/memray/_memray/alloc.pxd
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
cdef extern from "<stdlib.h>" nogil:
cdef extern from "alloc.h" nogil:
void *calloc (size_t count, size_t eltsize)
void free (void *ptr)
void *malloc (size_t size)
int posix_memalign(void** memptr, size_t alignment, size_t size)
void *realloc (void *ptr, size_t newsize)
void* valloc(size_t size)

cdef extern from "<malloc.h>" nogil:
void* memalign(size_t alignment, size_t size)
void* pvalloc(size_t size)
7 changes: 7 additions & 0 deletions src/memray/_memray_test_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,38 @@ cdef class MemoryAllocator:
@cython.profile(True)
def malloc(self, size_t size):
self.ptr = malloc(size)
return self.ptr != NULL

@cython.profile(True)
def calloc(self, size_t size):
self.ptr = calloc(1, size)
return self.ptr != NULL

@cython.profile(True)
def realloc(self, size_t size):
self.ptr = malloc(1)
self.ptr = realloc(self.ptr, size)
return self.ptr != NULL

@cython.profile(True)
def posix_memalign(self, size_t size):
posix_memalign(&self.ptr, sizeof(void*), size)
return self.ptr != NULL

@cython.profile(True)
def memalign(self, size_t size):
self.ptr = memalign(sizeof(void*), size)
return self.ptr != NULL

@cython.profile(True)
def valloc(self, size_t size):
self.ptr = valloc(size)
return self.ptr != NULL

@cython.profile(True)
def pvalloc(self, size_t size):
self.ptr = pvalloc(size)
return self.ptr != NULL

@cython.profile(True)
def run_in_pthread(self, callback):
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/multithreaded_extension/testext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <pthread.h>
#include <malloc.h>

#pragma GCC push_options
#pragma GCC optimize ("O0")

const int NUM_THREADS = 100;
const int NUM_BUFFERS = 100;
pthread_t threads[NUM_THREADS];
Expand Down Expand Up @@ -74,7 +77,7 @@ run_valloc_at_exit(PyObject*, PyObject*)
Py_RETURN_NONE;
}


#pragma GCC pop_options

static PyMethodDef methods[] = {
{"run", run, METH_NOARGS, "Run a bunch of threads"},
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ def test_cython_traceback(tmpdir):

traceback = list(alloc1.stack_trace())
assert traceback[-3:] == [
("valloc", ANY, 74),
("_cython_nested_allocation", ANY, 92),
("valloc", ANY, 79),
("_cython_nested_allocation", ANY, 99),
("test_cython_traceback", ANY, 132),
]

traceback = list(alloc2.stack_trace())
assert traceback[-3:] == [
("_cython_nested_allocation", ANY, 92),
("_cython_nested_allocation", ANY, 99),
("test_cython_traceback", ANY, 132),
]

Expand Down
16 changes: 12 additions & 4 deletions tests/integration/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ def test_simple_allocation_tracking(allocator_func, allocator_type, tmp_path):

# WHEN
with Tracker(output):
getattr(allocator, allocator_func)(1234)
allocator.free()
res = getattr(allocator, allocator_func)(1234)
if res:
allocator.free()

if not res:
pytest.skip("Allocator {allocator_func} not supported in this platform")

# THEN
allocations = list(FileReader(output).get_allocation_records())
Expand Down Expand Up @@ -188,8 +192,12 @@ def test_simple_allocation_tracking(self, tmp_path, allocator_func, allocator_ty

# WHEN
with Tracker(output):
getattr(allocator, allocator_func)(1234)
allocator.free()
res = getattr(allocator, allocator_func)(1234)
if res:
allocator.free()

if not res:
pytest.skip("Allocator {allocator_func} not supported in this platform")

# THEN
peak_allocations_unfiltered = FileReader(
Expand Down

0 comments on commit bc74b66

Please sign in to comment.