This commit is contained in:
Mattias Jansson
2023-10-30 17:35:46 +01:00
parent 4ec0455c98
commit b1b0ffb833
5 changed files with 25 additions and 22 deletions

2
configure.py vendored
View File

@@ -12,7 +12,7 @@ import generator
generator = generator.Generator(project = 'rpmalloc', variables = [('bundleidentifier', 'com.maniccoder.rpmalloc.$(binname)')])
rpmalloc_lib = generator.lib(module = 'rpmalloc', libname = 'rpmalloc', sources = ['rpmalloc.c'])
rpmalloc_test_lib = generator.lib(module = 'rpmalloc', libname = 'rpmalloc-test', sources = ['rpmalloc.c'], variables = {'defines': ['ENABLE_ASSERTS=1', 'ENABLE_STATISTICS=1', 'RPMALLOC_FIRST_CLASS_HEAPS=1']})
rpmalloc_test_lib = generator.lib(module = 'rpmalloc', libname = 'rpmalloc-test', sources = ['rpmalloc.c'], variables = {'defines': ['ENABLE_OVERRIDE=1', 'ENABLE_ASSERTS=1', 'ENABLE_STATISTICS=1', 'RPMALLOC_FIRST_CLASS_HEAPS=1']})
if not generator.target.is_android() and not generator.target.is_ios():
rpmalloc_so = generator.sharedlib(module = 'rpmalloc', libname = 'rpmalloc', sources = ['rpmalloc.c'], variables = {'defines': ['ENABLE_DYNAMIC_LINK=1']})

View File

@@ -110,7 +110,7 @@ rpreallocarray(void* ptr, size_t count, size_t size) {
#else
total = count * size;
#endif
return realloc(ptr, total);
return rprealloc(ptr, total);
}
#if USE_IMPLEMENT

View File

@@ -818,7 +818,10 @@ os_mdecommit(void* address, size_t size) {
rpmalloc_assert(0, "Failed to decommit virtual memory block");
}
#else
#if defined(MADV_DONTNEED) // && !defined(__APPLE__)
if (mprotect(address, size, PROT_NONE)) {
rpmalloc_assert(0, "Failed to decommit virtual memory block");
}
#if defined(MADV_DONTNEED)
if (madvise(address, size, MADV_DONTNEED)) {
#elif defined(MADV_FREE_REUSABLE)
int ret;
@@ -834,9 +837,6 @@ os_mdecommit(void* address, size_t size) {
#endif
rpmalloc_assert(0, "Failed to decommit virtual memory block");
}
if (mprotect(address, size, PROT_NONE)) {
rpmalloc_assert(0, "Failed to decommit virtual memory block");
}
#endif
#if ENABLE_STATISTICS
size_t page_count = size / global_config.page_size;

View File

@@ -37,6 +37,8 @@ static void*
pvalloc(size_t size) {
return rppvalloc(size);
}
#else
#include <malloc.h>
#endif
extern "C" int
@@ -124,8 +126,8 @@ test_malloc(int print_log) {
int
test_free(int print_log) {
free(rpmalloc(371));
free(new int);
free(new int[16]);
delete (new int);
delete[] (new int[16]);
free(pvalloc(1275));
if (print_log)
printf("Memory override free tests passed\n");

29
test/main.c vendored
View File

@@ -56,7 +56,7 @@ test_alloc(void) {
unsigned int icheck = 0;
unsigned int id = 0;
void* addr[18142];
char data[20000];
char data[40000];
unsigned int datasize[7] = {473, 39, 195, 24, 73, 376, 245};
rpmalloc_initialize(0);
@@ -68,7 +68,7 @@ test_alloc(void) {
return test_fail("Unexpected block granularity");
rpfree(zero_alloc);
for (id = 0; id < 20000; ++id)
for (id = 0; id < 30000; ++id)
data[id] = (char)(id % 139 + id % 17);
// Verify that blocks are aligned to small granularity
@@ -253,7 +253,7 @@ test_alloc(void) {
return test_fail("Zero allocation not zero");
}
memcpy(addr[ipass], data + ipass, cursize);
memcpy(addr[ipass], &data[ipass], cursize);
for (icheck = 0; icheck < ipass; ++icheck) {
if (addr[icheck] == addr[ipass])
@@ -270,7 +270,7 @@ test_alloc(void) {
for (ipass = 0; ipass < 18142; ++ipass) {
unsigned int cursize = datasize[ipass % 7] + ipass;
if (memcmp(addr[ipass], data + ipass, cursize))
if (memcmp(addr[ipass], &data[ipass], cursize))
return test_fail("Data corruption");
}
@@ -881,7 +881,7 @@ test_thread_implementation(void) {
static int
test_threaded(void) {
rpmalloc_config_t config = {0};
config.unmap_on_finalize = 1;
//config.unmap_on_finalize = 1;
rpmalloc_initialize_config(0, &config);
int ret = test_thread_implementation();
@@ -901,7 +901,7 @@ test_crossthread(void) {
thread_arg targ[32];
rpmalloc_config_t config = {0};
config.unmap_on_finalize = 1;
//config.unmap_on_finalize = 1;
rpmalloc_initialize_config(0, &config);
size_t num_alloc_threads = hardware_threads;
@@ -978,7 +978,7 @@ test_threadspam(void) {
allocator_thread_arg_t arg;
rpmalloc_config_t config = {0};
config.unmap_on_finalize = 1;
//config.unmap_on_finalize = 1;
rpmalloc_initialize_config(0, &config);
num_passes = 100;
@@ -1048,7 +1048,7 @@ test_first_class_heaps(void) {
allocator_thread_arg_t arg[32];
rpmalloc_config_t config = {0};
config.unmap_on_finalize = 1;
//config.unmap_on_finalize = 1;
rpmalloc_initialize_config(0, &config);
num_alloc_threads = hardware_threads * 2;
@@ -1135,7 +1135,7 @@ test_named_pages(void) {
char page_name[64] = {0};
snprintf(page_name, sizeof(page_name), "rpmalloc ::%s::", __func__);
config.page_name = page_name;
config.unmap_on_finalize = 1;
//config.unmap_on_finalize = 1;
rpmalloc_initialize_config(0, &config);
void* testptr = rpmalloc(16 * 1024 * 1024);
@@ -1145,7 +1145,8 @@ test_named_pages(void) {
snprintf(name, sizeof(name), "/proc/%d/maps", pid);
int fd = open(name, O_RDONLY);
if (fd != -1) {
read(fd, buf, sizeof(buf));
ssize_t was_read = read(fd, buf, sizeof(buf));
(void)sizeof(was_read);
close(fd);
}
#endif
@@ -1181,16 +1182,16 @@ test_run(int argc, char** argv) {
return -1;
if (test_realloc())
return -1;
if (test_malloc(1))
return -1;
if (test_free(1))
return -1;
if (test_superalign())
return -1;
if (test_crossthread())
return -1;
if (test_threaded())
return -1;
if (test_malloc(1))
return -1;
if (test_free(1))
return -1;
if (test_malloc_thread())
return -1;
if (test_threadspam())