Skip to content

Commit

Permalink
ncd: Store the NCDStingIndex pointer in NCDValMem.
Browse files Browse the repository at this point in the history
This cuts down on the boilerplate especially with ncd_make_boolean().
  • Loading branch information
ambrop72 committed May 8, 2015
1 parent 0ebb349 commit 347fa1a
Show file tree
Hide file tree
Showing 41 changed files with 142 additions and 142 deletions.
10 changes: 9 additions & 1 deletion examples/ncd_value_parser_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ int main (int argc, char *argv[])

BLog_InitStdout();

NCDStringIndex string_index;
if (!NCDStringIndex_Init(&string_index)) {
DEBUG("NCDStringIndex_Init failed");
goto fail01;
}

NCDValMem mem;
NCDValMem_Init(&mem);
NCDValMem_Init(&mem, &string_index);

// parse
NCDValRef val;
Expand All @@ -72,6 +78,8 @@ int main (int argc, char *argv[])
free(str);
fail1:
NCDValMem_Free(&mem);
NCDStringIndex_Free(&string_index);
fail01:
BLog_Free();
fail0:
return res;
Expand Down
8 changes: 4 additions & 4 deletions examples/ncdval_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main ()
// Some basic usage of values.

NCDValMem mem;
NCDValMem_Init(&mem);
NCDValMem_Init(&mem, &string_index);

NCDValRef s1 = NCDVal_NewString(&mem, "Hello World");
test_string(s1, "Hello World", 11);
Expand Down Expand Up @@ -160,7 +160,7 @@ int main ()
ASSERT( NCDVal_MapGetValue(m1, "K2").idx == v2.idx )
ASSERT( NCDVal_IsInvalid(NCDVal_MapGetValue(m1, "K3")) )

NCDValRef ids1 = NCDVal_NewIdString(&mem, NCD_STRING_ARG1, &string_index);
NCDValRef ids1 = NCDVal_NewIdString(&mem, NCD_STRING_ARG1);
test_string(ids1, "_arg1", 5);
ASSERT( !memcmp(NCDVal_StringData(ids1), "_arg1", 5) )
ASSERT( NCDVal_StringLength(ids1) == 5 )
Expand All @@ -169,7 +169,7 @@ int main ()
ASSERT( NCDVal_Type(ids1) == NCDVAL_STRING )
ASSERT( NCDVal_IsIdString(ids1) )

NCDValRef ids2 = NCDVal_NewIdString(&mem, NCD_STRING_ARG2, &string_index);
NCDValRef ids2 = NCDVal_NewIdString(&mem, NCD_STRING_ARG2);
test_string(ids2, "_arg2", 5);
ASSERT( !memcmp(NCDVal_StringData(ids2), "_arg2", 5) )
ASSERT( NCDVal_StringLength(ids2) == 5 )
Expand Down Expand Up @@ -198,7 +198,7 @@ int main ()
// an uninitialized string using NCDVal_NewStringUninitialized() and
// then copyng the data.

NCDValMem_Init(&mem);
NCDValMem_Init(&mem, &string_index);

NCDValRef s[100];

Expand Down
11 changes: 9 additions & 2 deletions examples/ncdvalcons_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <ncd/NCDValCons.h>
#include <ncd/NCDValGenerator.h>

static NCDStringIndex string_index;
static NCDValMem mem;
static NCDValCons cons;

Expand Down Expand Up @@ -87,9 +88,14 @@ static NCDValRef complete (NCDValConsVal cval)

int main ()
{
NCDValMem_Init(&mem);
int res;

int res = NCDValCons_Init(&cons, &mem);
res = NCDStringIndex_Init(&string_index);
ASSERT_FORCE(res)

NCDValMem_Init(&mem, &string_index);

res = NCDValCons_Init(&cons, &mem);
ASSERT_FORCE(res)

NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list()));
Expand All @@ -107,5 +113,6 @@ int main ()

NCDValCons_Free(&cons);
NCDValMem_Free(&mem);
NCDStringIndex_Free(&string_index);
return 0;
}
12 changes: 10 additions & 2 deletions ncd-request/ncd-request.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void request_handler_finished (void *user, int is_error);
static int write_all (int fd, const uint8_t *data, size_t len);
static int make_connect_addr (const char *str, struct BConnection_addr *out_addr);

NCDStringIndex string_index;
NCDValMem request_mem;
NCDValRef request_value;
BReactor reactor;
Expand All @@ -75,7 +76,12 @@ int main (int argc, char *argv[])

BTime_Init();

NCDValMem_Init(&request_mem);
if (!NCDStringIndex_Init(&string_index)) {
BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
goto fail01;
}

NCDValMem_Init(&request_mem, &string_index);

if (!NCDValParser_Parse(MemRef_MakeCstr(request_payload_string), &request_mem, &request_value)) {
BLog(BLOG_ERROR, "BReactor_Init failed");
Expand All @@ -97,7 +103,7 @@ int main (int argc, char *argv[])
goto fail2;
}

if (!NCDRequestClient_Init(&client, addr, &reactor, NULL, client_handler_error, client_handler_connected)) {
if (!NCDRequestClient_Init(&client, addr, &reactor, &string_index, NULL, client_handler_error, client_handler_connected)) {
BLog(BLOG_ERROR, "NCDRequestClient_Init failed");
goto fail2;
}
Expand All @@ -114,6 +120,8 @@ int main (int argc, char *argv[])
BReactor_Free(&reactor);
fail1:
NCDValMem_Free(&request_mem);
NCDStringIndex_Free(&string_index);
fail01:
BLog_Free();
fail0:
DebugObjectGlobal_Finish();
Expand Down
6 changes: 3 additions & 3 deletions ncd/NCDEvaluator.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int expr_init (struct NCDEvaluator__Expr *o, NCDEvaluator *eval, NCDValue
{
ASSERT((NCDValue_Type(value), 1))

NCDValMem_Init(&o->mem);
NCDValMem_Init(&o->mem, eval->string_index);

NCDValRef ref;
if (!add_expr_recurser(eval, value, &o->mem, &ref)) {
Expand Down Expand Up @@ -110,7 +110,7 @@ static int expr_eval (struct NCDEvaluator__Expr *o, struct NCDEvaluator__eval_co

*out_val = NCDVal_FromSafe(out_newmem, o->ref);
} else {
NCDValMem_Init(out_newmem);
NCDValMem_Init(out_newmem, context->eval->string_index);

NCDValRef ref;
if (!replace_placeholders_callback((void *)context, NCDVal_GetSafeRefPlaceholderId(o->ref), out_newmem, &ref) || NCDVal_IsInvalid(ref)) {
Expand Down Expand Up @@ -141,7 +141,7 @@ static int add_expr_recurser (NCDEvaluator *o, NCDValue *value, NCDValMem *mem,
goto fail;
}

*out = NCDVal_NewIdString(mem, string_id, o->string_index);
*out = NCDVal_NewIdString(mem, string_id);
if (NCDVal_IsInvalid(*out)) {
goto fail;
}
Expand Down
60 changes: 23 additions & 37 deletions ncd/NCDVal.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ struct NCDVal__mapelem {
struct NCDVal__idstring {
int type;
NCD_string_id_t string_id;
NCDStringIndex *string_index;
};

struct NCDVal__externalstring {
Expand Down Expand Up @@ -232,6 +231,7 @@ static NCDValRef NCDVal__Ref (NCDValMem *mem, NCDVal__idx idx)
static void NCDVal__AssertMem (NCDValMem *mem)
{
ASSERT(mem)
ASSERT(mem->string_index)
ASSERT(mem->size == NCDVAL_FASTBUF_SIZE || mem->size >= NCDVAL_FIRST_SIZE)
ASSERT(mem->used >= 0)
ASSERT(mem->used <= mem->size)
Expand Down Expand Up @@ -289,7 +289,6 @@ static void NCDVal__AssertValOnly (NCDValMem *mem, NCDVal__idx idx)
ASSERT(idx + sizeof(struct NCDVal__idstring) <= mem->used)
struct NCDVal__idstring *ids_e = NCDValMem__BufAt(mem, idx);
ASSERT(ids_e->string_id >= 0)
ASSERT(ids_e->string_index)
} break;
case EXTERNALSTRING_TYPE: {
ASSERT(idx + sizeof(struct NCDVal__externalstring) <= mem->used)
Expand Down Expand Up @@ -370,8 +369,11 @@ static void NCDValMem__RegisterRef (NCDValMem *o, NCDVal__idx refidx, struct NCD
#include "NCDVal_maptree.h"
#include <structure/CAvl_impl.h>

void NCDValMem_Init (NCDValMem *o)
void NCDValMem_Init (NCDValMem *o, NCDStringIndex *string_index)
{
ASSERT(string_index)

o->string_index = string_index;
o->size = NCDVAL_FASTBUF_SIZE;
o->used = 0;
o->first_ref = -1;
Expand All @@ -398,6 +400,7 @@ int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other)
{
NCDVal__AssertMem(other);

o->string_index = other->string_index;
o->size = other->size;
o->used = other->used;
o->first_ref = other->first_ref;
Expand Down Expand Up @@ -438,6 +441,13 @@ fail1:;
return 0;
}

NCDStringIndex * NCDValMem_StringIndex (NCDValMem *o)
{
NCDVal__AssertMem(o);

return o->string_index;
}

void NCDVal_Assert (NCDValRef val)
{
ASSERT(val.idx == -1 || (NCDVal__AssertVal(val), 1))
Expand Down Expand Up @@ -839,11 +849,10 @@ NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len)
return NCDVal_NewInvalid();
}

NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id, NCDStringIndex *string_index)
NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id)
{
NCDVal__AssertMem(mem);
ASSERT(string_id >= 0)
ASSERT(string_index)

NCDVal__idx size = sizeof(struct NCDVal__idstring);
NCDVal__idx idx = NCDValMem__Alloc(mem, size, __alignof(struct NCDVal__idstring));
Expand All @@ -854,7 +863,6 @@ NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id, NCDStri
struct NCDVal__idstring *ids_e = NCDValMem__BufAt(mem, idx);
ids_e->type = make_type(IDSTRING_TYPE, 0);
ids_e->string_id = string_id;
ids_e->string_index = string_index;

return NCDVal__Ref(mem, idx);

Expand Down Expand Up @@ -911,7 +919,7 @@ const char * NCDVal_StringData (NCDValRef string)

case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
const char *value = NCDStringIndex_Value(ids_e->string_index, ids_e->string_id).ptr;
const char *value = NCDStringIndex_Value(string.mem->string_index, ids_e->string_id).ptr;
return value;
} break;

Expand Down Expand Up @@ -940,7 +948,7 @@ size_t NCDVal_StringLength (NCDValRef string)

case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
return NCDStringIndex_Value(ids_e->string_index, ids_e->string_id).len;
return NCDStringIndex_Value(string.mem->string_index, ids_e->string_id).len;
} break;

case EXTERNALSTRING_TYPE: {
Expand Down Expand Up @@ -968,7 +976,7 @@ MemRef NCDVal_StringMemRef (NCDValRef string)

case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
return NCDStringIndex_Value(ids_e->string_index, ids_e->string_id);
return NCDStringIndex_Value(string.mem->string_index, ids_e->string_id);
} break;

case EXTERNALSTRING_TYPE: {
Expand Down Expand Up @@ -1000,7 +1008,7 @@ int NCDVal_StringNullTerminate (NCDValRef string, NCDValNullTermString *out)

case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
out->data = (char *)NCDStringIndex_Value(ids_e->string_index, ids_e->string_id).ptr;
out->data = (char *)NCDStringIndex_Value(string.mem->string_index, ids_e->string_id).ptr;
out->is_allocated = 0;
return 1;
} break;
Expand Down Expand Up @@ -1039,18 +1047,6 @@ void NCDValNullTermString_Free (NCDValNullTermString *o)
}
}

void NCDVal_IdStringGet (NCDValRef idstring, NCD_string_id_t *out_string_id,
NCDStringIndex **out_string_index)
{
ASSERT(NCDVal_IsIdString(idstring))
ASSERT(out_string_id)
ASSERT(out_string_index)

struct NCDVal__idstring *ids_e = NCDValMem__BufAt(idstring.mem, idstring.idx);
*out_string_id = ids_e->string_id;
*out_string_index = ids_e->string_index;
}

NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring)
{
ASSERT(NCDVal_IsIdString(idstring))
Expand All @@ -1059,14 +1055,6 @@ NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring)
return ids_e->string_id;
}

NCDStringIndex * NCDVal_IdStringStringIndex (NCDValRef idstring)
{
ASSERT(NCDVal_IsIdString(idstring))

struct NCDVal__idstring *ids_e = NCDValMem__BufAt(idstring.mem, idstring.idx);
return ids_e->string_index;
}

BRefTarget * NCDVal_ExternalStringTarget (NCDValRef externalstring)
{
ASSERT(NCDVal_IsExternalString(externalstring))
Expand All @@ -1084,7 +1072,7 @@ int NCDVal_StringHasNulls (NCDValRef string)
switch (get_internal_type(*(int *)ptr)) {
case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
return NCDStringIndex_HasNulls(ids_e->string_index, ids_e->string_id);
return NCDStringIndex_HasNulls(string.mem->string_index, ids_e->string_id);
} break;

case STOREDSTRING_TYPE:
Expand All @@ -1108,30 +1096,27 @@ int NCDVal_StringEquals (NCDValRef string, const char *data)
return NCDVal_StringLength(string) == data_len && NCDVal_StringRegionEquals(string, 0, data_len, data);
}

int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id,
NCDStringIndex *string_index)
int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id)
{
ASSERT(NCDVal_IsString(string))
ASSERT(string_id >= 0)
ASSERT(string_index)

void *ptr = NCDValMem__BufAt(string.mem, string.idx);

switch (get_internal_type(*(int *)ptr)) {
case STOREDSTRING_TYPE: {
struct NCDVal__string *str_e = ptr;
return MemRef_Equal(NCDStringIndex_Value(string_index, string_id), MemRef_Make(str_e->data, str_e->length));
return MemRef_Equal(NCDStringIndex_Value(string.mem->string_index, string_id), MemRef_Make(str_e->data, str_e->length));
} break;

case IDSTRING_TYPE: {
struct NCDVal__idstring *ids_e = ptr;
ASSERT(ids_e->string_index == string_index)
return ids_e->string_id == string_id;
} break;

case EXTERNALSTRING_TYPE: {
struct NCDVal__externalstring *exs_e = ptr;
return MemRef_Equal(NCDStringIndex_Value(string_index, string_id), MemRef_Make(exs_e->data, exs_e->length));
return MemRef_Equal(NCDStringIndex_Value(string.mem->string_index, string_id), MemRef_Make(exs_e->data, exs_e->length));
} break;

default:
Expand Down Expand Up @@ -1524,6 +1509,7 @@ NCDValRef NCDVal_MapGetValue (NCDValRef map, const char *key_str)
ASSERT(key_str)

NCDValMem mem;
mem.string_index = map.mem->string_index;
mem.size = NCDVAL_FASTBUF_SIZE;
mem.used = sizeof(struct NCDVal__externalstring);
mem.first_ref = -1;
Expand Down
Loading

0 comments on commit 347fa1a

Please sign in to comment.