Skip to content

Commit a95ad10

Browse files
committed
Small memory context changes.
1 parent c8582f5 commit a95ad10

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

source/parin/joka/memory.d

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -128,33 +128,12 @@ debug {
128128
_MallocGroupInfo[_MallocInfo] groupBuffer;
129129
}
130130

131-
version (JokaGlobalTracking) {
132-
pragma(msg, "Joka: Using global (non-TLS) tracking.");
133-
__gshared _MemoryTrackingState _memoryTrackingState;
134-
} else {
135-
_MemoryTrackingState _memoryTrackingState;
136-
}
131+
_MemoryTrackingState _memoryTrackingState;
137132
}
138133
} else {
139134
enum isTrackingMemory = false;
140135
}
141136

142-
struct MemoryState {
143-
void* allocatorState;
144-
AllocatorMallocFunc allocatorMallocFunc; // NOTE: If this is null, then we should return to the default allocator setup.
145-
AllocatorReallocFunc allocatorReallocFunc;
146-
AllocatorFreeFunc allocatorFreeFunc;
147-
}
148-
149-
extern(C) nothrow {
150-
alias AllocatorMallocFunc = void* function(void* allocatorState, Sz alignment, Sz size, IStr file, Sz line);
151-
alias AllocatorReallocFunc = void* function(void* allocatorState, Sz alignment, void* oldPtr, Sz oldSize, Sz newSize, IStr file, Sz line);
152-
}
153-
154-
extern(C) nothrow @nogc {
155-
alias AllocatorFreeFunc = void function(void* allocatorState, Sz alignment, void* oldPtr, Sz oldSize, IStr file, Sz line);
156-
}
157-
158137
struct AllocationGroup {
159138
IStr _currentGroup;
160139

@@ -171,12 +150,27 @@ struct AllocationGroup {
171150
}
172151
}
173152

153+
struct MemoryState {
154+
void* allocatorState;
155+
AllocatorMallocFunc allocatorMallocFunc; // NOTE: If null, then the default allocator setup should be used.
156+
AllocatorReallocFunc allocatorReallocFunc;
157+
AllocatorFreeFunc allocatorFreeFunc;
158+
}
159+
160+
extern(C) nothrow {
161+
alias AllocatorMallocFunc = void* function(void* allocatorState, Sz alignment, Sz size, IStr file, Sz line);
162+
alias AllocatorReallocFunc = void* function(void* allocatorState, Sz alignment, void* oldPtr, Sz oldSize, Sz newSize, IStr file, Sz line);
163+
}
164+
165+
extern(C) nothrow @nogc {
166+
alias AllocatorFreeFunc = void function(void* allocatorState, Sz alignment, void* oldPtr, Sz oldSize, IStr file, Sz line);
167+
}
168+
174169
MemoryState __memoryState;
175170

176171
@system nothrow:
177172

178-
// NOTE: This part has the main allocation functions.
179-
// NOTE: Some `JokaCustomMemory` functions are defined also in `types.d`.
173+
// NOTE: Memory allocation related things are here.
180174
version (JokaCustomMemory) {
181175
extern(C) nothrow void* jokaMalloc(Sz size, IStr file = __FILE__, Sz line = __LINE__);
182176
extern(C) nothrow void* jokaRealloc(void* ptr, Sz size, Sz oldSize = 0, IStr file = __FILE__, Sz line = __LINE__);
@@ -202,6 +196,7 @@ version (JokaCustomMemory) {
202196
jokaFree(ptr, 0, file, line);
203197
}
204198

199+
// NOTE: Some `JokaNoTypes` functions are defined also in `types.d`.
205200
version (JokaNoTypes) {
206201
private extern(C) nothrow @nogc pure void* jokaMemset(void* ptr, int value, Sz size);
207202
private extern(C) nothrow @nogc pure void* jokaMemcpy(void* ptr, const(void)* source, Sz size);
@@ -264,6 +259,7 @@ version (JokaCustomMemory) {
264259
__memoryState.allocatorFreeFunc(&__memoryState.allocatorState, 0, ptr, oldSize, file, line);
265260
}
266261

262+
// NOTE: Some `JokaNoTypes` functions are defined also in `types.d`.
267263
version (JokaNoTypes) {
268264
extern(C) nothrow @nogc pure
269265
void* jokaMemset(void* ptr, int value, Sz size) {
@@ -398,6 +394,7 @@ version (JokaCustomMemory) {
398394
__memoryState.allocatorFreeFunc(&__memoryState.allocatorState, 0, ptr, oldSize, file, line);
399395
}
400396

397+
// NOTE: Some `JokaNoTypes` functions are defined also in `types.d`.
401398
version (JokaNoTypes) {
402399
extern(C) nothrow @nogc pure
403400
void* jokaMemset(void* ptr, int value, Sz size) {
@@ -417,43 +414,6 @@ void jokaEnsureCapture(MemoryState* capture) {
417414
*capture = __memoryState;
418415
}
419416

420-
@trusted @nogc
421-
auto ignoreLeak(T)(T ptr) {
422-
static if (is(T : const(A)[], A)) {
423-
static if (isTrackingMemory) {
424-
if (auto mallocValue = ptr.ptr in _memoryTrackingState.table) {
425-
mallocValue.canIgnore = true;
426-
}
427-
}
428-
return ptr;
429-
} else static if (is(T : const(void)*)) {
430-
static if (isTrackingMemory) {
431-
if (auto mallocValue = ptr in _memoryTrackingState.table) {
432-
mallocValue.canIgnore = true;
433-
}
434-
}
435-
return ptr;
436-
} else static if (__traits(hasMember, T, "ignoreLeak")) {
437-
return ptr.ignoreLeak();
438-
} else {
439-
static assert(0, "Type doesn't implement the `ignoreLeak` function.");
440-
}
441-
}
442-
443-
@trusted
444-
void beginAllocationGroup(IStr group) {
445-
static if (isTrackingMemory) {
446-
_memoryTrackingState.currentGroupStack ~= group.idup;
447-
}
448-
}
449-
450-
@trusted @nogc
451-
void endAllocationGroup() {
452-
static if (isTrackingMemory) {
453-
if (_memoryTrackingState.currentGroupStack.length) _memoryTrackingState.currentGroupStack = _memoryTrackingState.currentGroupStack[0 .. $ - 1];
454-
}
455-
}
456-
457417
@trusted
458418
T* jokaMakeBlank(T)(IStr file = __FILE__, Sz line = __LINE__) {
459419
return cast(T*) jokaMalloc(T.sizeof, file, line);
@@ -541,6 +501,43 @@ T jokaMakeJoint(T)(Sz[] lengths...) if (is(T == struct)) {
541501
return result;
542502
}
543503

504+
@trusted @nogc
505+
auto ignoreLeak(T)(T ptr) {
506+
static if (is(T : const(A)[], A)) {
507+
static if (isTrackingMemory) {
508+
if (auto mallocValue = ptr.ptr in _memoryTrackingState.table) {
509+
mallocValue.canIgnore = true;
510+
}
511+
}
512+
return ptr;
513+
} else static if (is(T : const(void)*)) {
514+
static if (isTrackingMemory) {
515+
if (auto mallocValue = ptr in _memoryTrackingState.table) {
516+
mallocValue.canIgnore = true;
517+
}
518+
}
519+
return ptr;
520+
} else static if (__traits(hasMember, T, "ignoreLeak")) {
521+
return ptr.ignoreLeak();
522+
} else {
523+
static assert(0, "Type doesn't implement the `ignoreLeak` function.");
524+
}
525+
}
526+
527+
@trusted
528+
void beginAllocationGroup(IStr group) {
529+
static if (isTrackingMemory) {
530+
_memoryTrackingState.currentGroupStack ~= group.idup;
531+
}
532+
}
533+
534+
@trusted @nogc
535+
void endAllocationGroup() {
536+
static if (isTrackingMemory) {
537+
if (_memoryTrackingState.currentGroupStack.length) _memoryTrackingState.currentGroupStack = _memoryTrackingState.currentGroupStack[0 .. $ - 1];
538+
}
539+
}
540+
544541
/// Joint allocation test.
545542
unittest {
546543
// First Jai example.

0 commit comments

Comments
 (0)