commit 924d9a4017b0e087eb621803422e3305b4e73ab3
parent 1ab15ecc0b7027970c9b1d4e6f64a8692a91e86f
Author: vaplv <vaplv@free.fr>
Date: Wed, 12 Apr 2017 11:09:46 +0200
Fix the proxy allocator
The proxy allocator assumed that it was the proxy of a default
allocator. As a consequence it uses the regular 'mem_size' to retrieve
the size of an allocated memory while it had to use the specific
'mem_size' function of its attached allocator. This led to crashes on
the invocation of the mem_allocated_size and mem_size functions of such
allocators.
Diffstat:
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/mem_allocator.c b/src/mem_allocator.c
@@ -574,7 +574,7 @@ proxy_allocated_size(const void* data)
proxy_data = data;
mutex_lock(proxy_data->mutex);
for(node = proxy_data->node_list; node != NULL; node = node->next) {
- allocated_size += mem_size(node);
+ allocated_size += MEM_SIZE(proxy_data->allocator, node);
}
mutex_unlock(proxy_data->mutex);
return allocated_size;
@@ -601,7 +601,7 @@ proxy_dump
(dump,
avaible_dump_space,
"%lu bytes allocated at %s:%u%s",
- (long unsigned)mem_size(node),
+ (long unsigned)MEM_SIZE(proxy_data->allocator, node),
node->filename ? node->filename : "none",
node->fileline,
node->next ? ".\n" : ".");
diff --git a/src/mem_allocator.h b/src/mem_allocator.h
@@ -106,7 +106,6 @@ RSYS_API struct mem_allocator mem_default_allocator;
/*******************************************************************************
* Regular allocation functions
******************************************************************************/
-
RSYS_API void* mem_alloc(const size_t size);
RSYS_API void* mem_calloc(const size_t nelmts, const size_t size);
RSYS_API void* mem_realloc(void* ptr, const size_t size);
diff --git a/src/test_mem_allocator.c b/src/test_mem_allocator.c
@@ -144,6 +144,7 @@ int
main(int argc, char** argv)
{
struct mem_allocator allocator;
+ struct mem_allocator allocator2;
int mem[8];
(void)argc;
@@ -155,9 +156,15 @@ main(int argc, char** argv)
printf("-- Default allocator\n");
test_allocator(&mem_default_allocator);
- printf("\n-- Proxy allocator\n");
+ printf("\n-- Proxy allocator of default allocator\n");
mem_init_proxy_allocator(&allocator, &mem_default_allocator);
test_allocator(&allocator);
+
+ printf("\n-- Proxy allocator of proxy allocator\n");
+ mem_init_proxy_allocator(&allocator2, &allocator);
+ test_allocator(&allocator2);
+
+ mem_shutdown_proxy_allocator(&allocator2);
mem_shutdown_proxy_allocator(&allocator);
CHECK(MEM_AREA_OVERLAP(mem, sizeof(int[2]), mem + 2, sizeof(int[6])), 0);
@@ -165,7 +172,6 @@ main(int argc, char** argv)
CHECK(MEM_AREA_OVERLAP(mem, sizeof(int[2]), mem + 1, sizeof(int[7])), 1);
CHECK(MEM_AREA_OVERLAP(mem + 7, sizeof(int[1]), mem, sizeof(int[8])), 1);
-
CHECK(MEM_ALLOCATED_SIZE(&mem_default_allocator), 0);
return 0;