From 88a54f3fa5940a16b23326a32a284ca13e300288 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Tue, 6 May 2014 19:31:55 -0700 Subject: [PATCH] Moar manual memory allocation --- std/lexer.d | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/std/lexer.d b/std/lexer.d index 1072e45..9994954 100644 --- a/std/lexer.d +++ b/std/lexer.d @@ -773,9 +773,6 @@ struct LexerRange size_t line; } -/** - * FREAKIN' MAAAGIC - */ shared struct StringCache { import core.sync.mutex; @@ -783,6 +780,8 @@ public: @disable this(); + bool special; + /** * Params: bucketCount = the initial number of buckets. */ @@ -792,6 +791,20 @@ public: allocating = false; } + ~this() + { + import core.memory; + shared(Block)* current = rootBlock; + while (current !is null) + { + shared(Block)* prev = current; + current = current.next; + free(cast(void*) prev.bytes.ptr); + } + rootBlock = null; + buckets = []; + } + /** * Caches a string. * Params: str = the string to intern @@ -862,7 +875,6 @@ private: if (bytes is null || bytes.length == 0) return ""; import core.atomic; - import core.memory; shared ubyte[] mem; shared(Node*)* oldBucketRoot = &buckets[hash % buckets.length]; while (true) @@ -936,7 +948,7 @@ private: import core.atomic; import core.memory; if (numBytes > (blockSize / 4)) - return cast(shared) (cast(ubyte*) GC.malloc(numBytes, GC.BlkAttr.NO_SCAN))[0 .. numBytes]; + return cast(shared) (cast(ubyte*) malloc(numBytes))[0 .. numBytes]; shared(Block)* r = rootBlock; while (true) { @@ -957,7 +969,7 @@ private: if (cas(&allocating, false, true)) { shared(Block)* b = new shared Block( - cast(shared) (cast(ubyte*) GC.malloc(blockSize, GC.BlkAttr.NO_SCAN))[0 .. blockSize], + cast(shared) (cast(ubyte*) malloc(blockSize))[0 .. blockSize], numBytes, r); atomicStore(rootBlock, b); @@ -1056,3 +1068,6 @@ private: shared(Node)*[] buckets; shared(Block)* rootBlock; } + +private extern(C) void* malloc(size_t) nothrow pure; +private extern(C) void free(void*) nothrow pure;