Skip to content

Commit

Permalink
update mir_rc_context deallocation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Jan 11, 2020
1 parent acf0985 commit 8a42768
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions source/mir/rc/context.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import mir.type_info;
struct mir_rc_context
{
///
void* allocator;
extern (C) void function(mir_rc_context*) @system nothrow @nogc pure deallocator;
///
immutable(mir_type_info)* typeInfo;
///
Expand Down Expand Up @@ -68,23 +68,26 @@ export extern(C)
void mir_rc_delete(ref mir_rc_context context)
@system nothrow @nogc pure
{
assert(context.deallocator);
with(context)
{
with(typeInfo)
{
if (destructor)
{
auto ptr = cast(void*)(&context + 1);
foreach(i; 0 .. length)
auto i = length;
assert(i);
do
{
destructor(ptr);
ptr += size;
}
while(--i);
}
}
}
import mir.internal.memory: free;
free(&context);
context.deallocator(&context);
}

/++
Expand All @@ -98,14 +101,14 @@ mir_rc_context* mir_rc_create(
bool deallocate = true,
) @system nothrow @nogc pure
{
import mir.internal.memory: malloc;
import mir.internal.memory: malloc, free;
import core.stdc.string: memset, memcpy;

assert(length);
auto size = length * typeInfo.size;
if (auto context = cast(mir_rc_context*)malloc(mir_rc_context.sizeof + size))
{
context.allocator = null;
context.deallocator = &free;
context.typeInfo = &typeInfo;
context.counter = deallocate;
context.length = length;
Expand Down

0 comments on commit 8a42768

Please sign in to comment.