Skip to content

Commit 8a42768

Browse files
committed
update mir_rc_context deallocation logic
1 parent acf0985 commit 8a42768

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

source/mir/rc/context.d

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import mir.type_info;
1010
struct mir_rc_context
1111
{
1212
///
13-
void* allocator;
13+
extern (C) void function(mir_rc_context*) @system nothrow @nogc pure deallocator;
1414
///
1515
immutable(mir_type_info)* typeInfo;
1616
///
@@ -68,23 +68,26 @@ export extern(C)
6868
void mir_rc_delete(ref mir_rc_context context)
6969
@system nothrow @nogc pure
7070
{
71+
assert(context.deallocator);
7172
with(context)
7273
{
7374
with(typeInfo)
7475
{
7576
if (destructor)
7677
{
7778
auto ptr = cast(void*)(&context + 1);
78-
foreach(i; 0 .. length)
79+
auto i = length;
80+
assert(i);
81+
do
7982
{
8083
destructor(ptr);
8184
ptr += size;
8285
}
86+
while(--i);
8387
}
8488
}
8589
}
86-
import mir.internal.memory: free;
87-
free(&context);
90+
context.deallocator(&context);
8891
}
8992

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

104107
assert(length);
105108
auto size = length * typeInfo.size;
106109
if (auto context = cast(mir_rc_context*)malloc(mir_rc_context.sizeof + size))
107110
{
108-
context.allocator = null;
111+
context.deallocator = &free;
109112
context.typeInfo = &typeInfo;
110113
context.counter = deallocate;
111114
context.length = length;

0 commit comments

Comments
 (0)