Moar manual memory allocation

This commit is contained in:
Hackerpilot 2014-05-06 19:31:55 -07:00
parent 88883e6e0f
commit 88a54f3fa5
1 changed files with 21 additions and 6 deletions

View File

@ -773,9 +773,6 @@ struct LexerRange
size_t line; size_t line;
} }
/**
* FREAKIN' MAAAGIC
*/
shared struct StringCache shared struct StringCache
{ {
import core.sync.mutex; import core.sync.mutex;
@ -783,6 +780,8 @@ public:
@disable this(); @disable this();
bool special;
/** /**
* Params: bucketCount = the initial number of buckets. * Params: bucketCount = the initial number of buckets.
*/ */
@ -792,6 +791,20 @@ public:
allocating = false; 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. * Caches a string.
* Params: str = the string to intern * Params: str = the string to intern
@ -862,7 +875,6 @@ private:
if (bytes is null || bytes.length == 0) if (bytes is null || bytes.length == 0)
return ""; return "";
import core.atomic; import core.atomic;
import core.memory;
shared ubyte[] mem; shared ubyte[] mem;
shared(Node*)* oldBucketRoot = &buckets[hash % buckets.length]; shared(Node*)* oldBucketRoot = &buckets[hash % buckets.length];
while (true) while (true)
@ -936,7 +948,7 @@ private:
import core.atomic; import core.atomic;
import core.memory; import core.memory;
if (numBytes > (blockSize / 4)) 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; shared(Block)* r = rootBlock;
while (true) while (true)
{ {
@ -957,7 +969,7 @@ private:
if (cas(&allocating, false, true)) if (cas(&allocating, false, true))
{ {
shared(Block)* b = new shared Block( 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, numBytes,
r); r);
atomicStore(rootBlock, b); atomicStore(rootBlock, b);
@ -1056,3 +1068,6 @@ private:
shared(Node)*[] buckets; shared(Node)*[] buckets;
shared(Block)* rootBlock; shared(Block)* rootBlock;
} }
private extern(C) void* malloc(size_t) nothrow pure;
private extern(C) void free(void*) nothrow pure;