mirror of
https://github.com/NixOS/nix.git
synced 2025-11-17 16:02:43 +01:00
Garbage-collect strings
This commit is contained in:
parent
9b822de4ef
commit
2995f9c48f
9 changed files with 83 additions and 36 deletions
|
|
@ -5,6 +5,7 @@
|
|||
#include <stack>
|
||||
#include <limits>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
//#define GC_DEBUG 1
|
||||
|
||||
|
|
@ -16,6 +17,7 @@ enum Tag {
|
|||
tFree = 3,
|
||||
|
||||
// Misc types
|
||||
tString,
|
||||
tBindings,
|
||||
tValueList,
|
||||
tEnv,
|
||||
|
|
@ -27,6 +29,7 @@ enum Tag {
|
|||
tInt,
|
||||
tBool,
|
||||
tShortString,
|
||||
tStaticString,
|
||||
tLongString,
|
||||
tPath,
|
||||
tNull,
|
||||
|
|
@ -436,4 +439,30 @@ struct Root
|
|||
}
|
||||
};
|
||||
|
||||
struct String : Object
|
||||
{
|
||||
char s[0];
|
||||
|
||||
String(size_t len, const char * src)
|
||||
: Object(tString, len)
|
||||
{
|
||||
std::memcpy(s, src, len + 1);
|
||||
}
|
||||
|
||||
size_t words() const { return wordsFor(getMisc()); }
|
||||
|
||||
/* Return the number of words needed to store a string of 'len'
|
||||
characters (where 'len' excludes the terminator). */
|
||||
static size_t wordsFor(size_t len)
|
||||
{
|
||||
return len / WORD_SIZE + 2;
|
||||
}
|
||||
|
||||
static String * alloc(const char * src)
|
||||
{
|
||||
auto len = strlen(src);
|
||||
return gc.alloc<String>(wordsFor(len), len, src);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue