mirror of
https://github.com/ccxvii/mujs.git
synced 2026-09-24 01:03:35 +08:00
Alloc interned strings at the end of the string nodes.
Only use one malloc rather than two for each interned string.
This commit is contained in:
+5
-5
@@ -5,19 +5,20 @@
|
||||
|
||||
struct js_StringNode
|
||||
{
|
||||
char *string;
|
||||
js_StringNode *left, *right;
|
||||
int level;
|
||||
char string[1];
|
||||
};
|
||||
|
||||
static js_StringNode sentinel = { "", &sentinel, &sentinel, 0 };
|
||||
static js_StringNode sentinel = { &sentinel, &sentinel, 0, ""};
|
||||
|
||||
static js_StringNode *newstringnode(const char *string, const char **result)
|
||||
{
|
||||
js_StringNode *node = malloc(sizeof(js_StringNode));
|
||||
node->string = strdup(string);
|
||||
int n = strlen(string);
|
||||
js_StringNode *node = malloc(offsetof(js_StringNode, string) + n + 1);
|
||||
node->left = node->right = &sentinel;
|
||||
node->level = 1;
|
||||
strcpy(node->string, string);
|
||||
return *result = node->string, node;
|
||||
}
|
||||
|
||||
@@ -91,7 +92,6 @@ static void js_freestringnode(js_State *J, js_StringNode *node)
|
||||
{
|
||||
if (node->left != &sentinel) js_freestringnode(J, node->left);
|
||||
if (node->right != &sentinel) js_freestringnode(J, node->right);
|
||||
free(node->string);
|
||||
free(node);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user