Skip to content

Commit

Permalink
core/heap.disabled: invoke underlying allocator directly
Browse files Browse the repository at this point in the history
The heap object stores its allocator for its entire lifetime, so
there's no need to call mowgli_alloc()/mowgli_free(), which also
stores the allocator tag behind the returned pointer.

This will (sometimes) reduce the memory consumption of every
allocation by a pointer and is slightly more efficient.

Also add some debugging assertions to validate the integrity of
the given/chosen allocator.
  • Loading branch information
aaronmdjones committed Jan 21, 2019
1 parent 2ea73fc commit 1a7a27f
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/libmowgli/core/heap.disabled.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ mowgli_heap_create_full(const size_t elem_size,
if (allocator == NULL)
allocator = mowgli_allocator_get_policy();

mowgli_heap_t *const heap = mowgli_alloc_using_policy(allocator, sizeof *heap);
return_null_if_fail(allocator != NULL);
return_null_if_fail(allocator->allocate != NULL);

mowgli_heap_t *const heap = allocator->allocate(sizeof *heap);

if (! heap)
{
#ifdef HEAP_DEBUG
(void) mowgli_log_warning("mowgli_alloc for heap returned NULL");
(void) mowgli_log_warning("cannot allocate memory for heap");
#endif
return NULL;
}
Expand Down Expand Up @@ -83,6 +86,8 @@ void
mowgli_heap_destroy(mowgli_heap_t *const restrict heap)
{
return_if_fail(heap != NULL);
return_if_fail(heap->allocator != NULL);
return_if_fail(heap->allocator->deallocate != NULL);

mowgli_node_t *node, *tnode;

Expand All @@ -96,11 +101,11 @@ mowgli_heap_destroy(mowgli_heap_t *const restrict heap)
#endif

(void) mowgli_node_delete(&block->node, &heap->blocks);
(void) mowgli_free(block->ptr);
(void) mowgli_free(block);
(void) heap->allocator->deallocate(block->ptr);
(void) heap->allocator->deallocate(block);
}

(void) mowgli_free(heap);
(void) heap->allocator->deallocate(heap);

#ifdef HEAP_DEBUG
(void) mowgli_log("pseudoheap@%p: done", heap);
Expand All @@ -111,23 +116,26 @@ void *
mowgli_heap_alloc(mowgli_heap_t *const restrict heap)
{
return_null_if_fail(heap != NULL);
return_null_if_fail(heap->allocator != NULL);
return_null_if_fail(heap->allocator->allocate != NULL);
return_null_if_fail(heap->allocator->deallocate != NULL);

mowgli_block_t *const block = mowgli_alloc_using_policy(heap->allocator, sizeof *block);
mowgli_block_t *const block = heap->allocator->allocate(sizeof *block);

if (! block)
{
#ifdef HEAP_DEBUG
(void) mowgli_log_warning("pseudoheap@%p: mowgli_alloc for block returned NULL", heap);
(void) mowgli_log_warning("pseudoheap@%p: cannot allocate memory for block", heap);
#endif
return NULL;
}

if (! (block->ptr = mowgli_alloc_using_policy(heap->allocator, heap->elem_size)))
if (! (block->ptr = heap->allocator->allocate(heap->elem_size)))
{
#ifdef HEAP_DEBUG
(void) mowgli_log_warning("pseudoheap@%p: mowgli_alloc for ptr returned NULL", heap);
(void) mowgli_log_warning("pseudoheap@%p: cannot allocate memory for ptr", heap);
#endif
(void) mowgli_free(block);
(void) heap->allocator->deallocate(block);
return NULL;
}

Expand All @@ -144,6 +152,8 @@ void
mowgli_heap_free(mowgli_heap_t *const restrict heap, void *const restrict ptr)
{
return_if_fail(heap != NULL);
return_if_fail(heap->allocator != NULL);
return_if_fail(heap->allocator->deallocate != NULL);
return_if_fail(ptr != NULL);

mowgli_node_t *node;
Expand All @@ -156,8 +166,8 @@ mowgli_heap_free(mowgli_heap_t *const restrict heap, void *const restrict ptr)
continue;

(void) mowgli_node_delete(&block->node, &heap->blocks);
(void) mowgli_free(block->ptr);
(void) mowgli_free(block);
(void) heap->allocator->deallocate(block->ptr);
(void) heap->allocator->deallocate(block);

#ifdef HEAP_DEBUG
(void) mowgli_log("pseudoheap@%p: freed ptr %p", heap, ptr);
Expand Down

0 comments on commit 1a7a27f

Please sign in to comment.