Changeset: 6e60cb025d2e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/6e60cb025d2e
Modified Files:
monetdb5/modules/atoms/json.c
testing/Mtest.py.in
Branch: resource_management
Log Message:
use allocator in json module
diffs (truncated from 1644 to 300 lines):
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -84,19 +84,19 @@ int TYPE_json;
/* Internal constructors. */
static int jsonhint = 8;
-static JSON *JSONparse(const char *j);
+static JSON *JSONparse(Client ctx, const char *j);
static JSON *
-JSONnewtree(void)
+JSONnewtree(allocator *alloc)
{
JSON *js;
- js = GDKzalloc(sizeof(JSON));
+ js = ma_zalloc(alloc, sizeof(JSON));
if (js == NULL)
return NULL;
- js->elm = GDKzalloc(sizeof(JSONterm) * jsonhint);
+ js->elm = ma_zalloc(alloc, sizeof(JSONterm) * jsonhint);
if (js->elm == NULL) {
- GDKfree(js);
+ //GDKfree(js);
return NULL;
}
js->size = jsonhint;
@@ -104,12 +104,14 @@ JSONnewtree(void)
}
static int
-JSONnew(JSON *js)
+JSONnew(Client ctx, JSON *js)
{
JSONterm *term;
+ size_t osz = sizeof(JSONterm) * js->size;
if (js->free == js->size) {
- term = GDKrealloc(js->elm, sizeof(JSONterm) * (js->size + 8));
+ size_t nsz = sizeof(JSONterm) * (js->size + 8);
+ term = ma_realloc(ctx->alloc, js->elm, nsz, osz);
if (term == NULL) {
js->error = createException(MAL, "json.new",
SQLSTATE(HY013) MAL_MALLOC_FAIL);
@@ -131,8 +133,8 @@ JSONfree(JSON *c)
if (c == 0)
return;
freeException(c->error);
- GDKfree(c->elm);
- GDKfree(c);
+ //GDKfree(c->elm);
+ //GDKfree(c);
}
static str
@@ -240,7 +242,7 @@ JSONfromString(const char *src, size_t *
{
json *buf = (json *) J;
if(*buf) {
- GDKfree(*buf);
+ //GDKfree(*buf);
*buf = NULL;
}
if (strNil(src) || (external && strncmp(src, "nil", 3) == 0)) {
@@ -338,16 +340,16 @@ JSONtoString(str *s, size_t *len, const
}
static BAT *
-JSONdumpInternal(const JSON *jt, int depth)
+JSONdumpInternal(Client ctx, const JSON *jt, int depth)
{
int i, idx;
JSONterm *je;
size_t buflen = 1024;
- char *buffer = GDKmalloc(buflen);
+ char *buffer = ma_alloc(ctx->alloc, buflen);
BAT *bn = COLnew(0, TYPE_str, 0, TRANSIENT);
if (!buffer || !bn) {
- GDKfree(buffer);
+ //GDKfree(buffer);
BBPreclaim(bn);
return NULL;
}
@@ -357,12 +359,13 @@ JSONdumpInternal(const JSON *jt, int dep
je = jt->elm + idx;
if (datlen + depth * 4 + 512 > buflen) {
+ size_t osz = buflen;
do {
buflen += 1024;
} while (datlen + depth * 4 + 512 > buflen);
- char *newbuf = GDKrealloc(buffer, buflen);
+ char *newbuf = ma_realloc(ctx->alloc, buffer, buflen,
osz);
if (newbuf == NULL) {
- GDKfree(buffer);
+ //GDKfree(buffer);
BBPreclaim(bn);
return NULL;
}
@@ -403,11 +406,12 @@ JSONdumpInternal(const JSON *jt, int dep
datlen += snprintf(buffer + datlen, buflen - datlen, "child %d
list ",
je->child);
for (i = je->next; i; i = jt->elm[i].next) {
+ size_t osz = buflen;
if (datlen + 10 > buflen) {
buflen += 1024;
- char *newbuf = GDKrealloc(buffer, buflen);
+ char *newbuf = ma_realloc(ctx->alloc, buffer,
buflen, osz);
if (newbuf == NULL) {
- GDKfree(buffer);
+ //GDKfree(buffer);
BBPreclaim(bn);
return NULL;
}
@@ -417,12 +421,13 @@ JSONdumpInternal(const JSON *jt, int dep
}
if (je->name) {
if (datlen + 10 + je->namelen > buflen) {
+ size_t osz = buflen;
do {
buflen += 1024;
} while (datlen + 10 + je->namelen > buflen);
- char *newbuf = GDKrealloc(buffer, buflen);
+ char *newbuf = ma_realloc(ctx->alloc, buffer,
buflen, osz);
if (newbuf == NULL) {
- GDKfree(buffer);
+ //GDKfree(buffer);
BBPreclaim(bn);
return NULL;
}
@@ -433,12 +438,13 @@ JSONdumpInternal(const JSON *jt, int dep
}
if (je->value) {
if (datlen + 10 + je->valuelen > buflen) {
+ size_t osz = buflen;
do {
buflen += 1024;
} while (datlen + 10 + je->valuelen > buflen);
- char *newbuf = GDKrealloc(buffer, buflen);
+ char *newbuf = ma_realloc(ctx->alloc, buffer,
buflen, osz);
if (newbuf == NULL) {
- GDKfree(buffer);
+ //GDKfree(buffer);
BBPreclaim(bn);
return NULL;
}
@@ -449,26 +455,26 @@ JSONdumpInternal(const JSON *jt, int dep
}
if (BUNappend(bn, buffer, false) != GDK_SUCCEED) {
BBPreclaim(bn);
- GDKfree(buffer);
+ //GDKfree(buffer);
return NULL;
}
}
- GDKfree(buffer);
+ //GDKfree(buffer);
return bn;
}
static str
-JSONdump(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+JSONdump(Client ctx, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
{
(void) mb;
- (void) cntxt;
+ (void) ctx;
bat *ret = getArgReference_bat(stk, pci, 0);
const json *val = (json *) getArgReference(stk, pci, 1);
- JSON *jt = JSONparse(*val);
+ JSON *jt = JSONparse(ctx, *val);
CHECK_JSON(jt);
- BAT *bn = JSONdumpInternal(jt, 0);
+ BAT *bn = JSONdumpInternal(ctx, jt, 0);
JSONfree(jt);
if (bn == NULL)
throw(MAL, "json.dump", SQLSTATE(HY013) MAL_MALLOC_FAIL);
@@ -485,7 +491,7 @@ JSONjson2str(Client ctx, str *ret, json
if (*s == '"')
s++;
- if ((s = GDKstrdup(s)) == NULL)
+ if ((s = MA_STRDUP(ctx->alloc, s)) == NULL)
throw(MAL, "json.str", SQLSTATE(HY013) MAL_MALLOC_FAIL);
c = s + strlen(s) - 1;
if (*c == '"')
@@ -498,7 +504,7 @@ static str
JSON2json(Client ctx, json *ret, const json *j)
{
(void) ctx;
- *ret = GDKstrdup(*j);
+ *ret = MA_STRDUP(ctx->alloc, *j);
if (*ret == NULL)
throw(MAL, "json.json", SQLSTATE(HY013) MAL_MALLOC_FAIL);
return MAL_SUCCEED;
@@ -516,12 +522,12 @@ JSONstr2json(Client ctx, json *ret, cons
JSON *jt = NULL;
if (strNil(*j)) {
- buf = GDKstrdup(*j);
+ buf = MA_STRDUP(ctx->alloc, *j);
} else {
- jt = JSONparse(*j);
+ jt = JSONparse(ctx, *j);
CHECK_JSON(jt);
- buf = (json)GDKmalloc(ln);
+ buf = (json)ma_alloc(ctx->alloc, ln);
}
if (buf == NULL) {
msg = createException(MAL, "json.new", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
@@ -531,7 +537,7 @@ JSONstr2json(Client ctx, json *ret, cons
if (jt != NULL) {
msg = JSONtoStorageString(jt, 0, &buf, &out_size);
if (msg != MAL_SUCCEED) {
- GDKfree(buf);
+ //GDKfree(buf);
goto bailout;
}
}
@@ -550,7 +556,7 @@ JSONisvalid(Client ctx, bit *ret, const
if (strNil(*j)) {
*ret = bit_nil;
} else {
- JSON *jt = JSONparse(*j);
+ JSON *jt = JSONparse(ctx, *j);
if (jt == NULL)
throw(MAL, "json.isvalid", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
*ret = jt->error == MAL_SUCCEED;
@@ -612,11 +618,11 @@ jsonRead(str a, size_t *dstlen, stream *
if ((msg = JSONstr2json(/*ctx*/NULL, &out, &(const char *){a})) !=
MAL_SUCCEED) {
freeException(msg);
- GDKfree(a);
+ //GDKfree(a);
return NULL;
}
*dstlen = strlen(out) + 1;
- GDKfree(a);
+ //GDKfree(a);
return out;
}
@@ -635,7 +641,7 @@ JSONprelude(void)
char jsonupgrade[MAXPATH];
struct stat st;
if (GDKfilepath(jsonupgrade, sizeof(jsonupgrade), 0, BATDIR,
"jsonupgradeneeded", NULL) != GDK_SUCCEED)
- throw(MAL, "json.prelude", GDK_EXCEPTION);
+ throw(MAL, "json.prelude", "cannot allocate filename for json
upgrade signal file");
int r = stat(jsonupgrade, &st);
if (r == 0) {
/* The file exists so we need to run the upgrade code */
@@ -652,12 +658,12 @@ JSONprelude(void)
}
static void
-JSONappend(JSON *jt, int idx, int nxt)
+JSONappend(Client ctx, JSON *jt, int idx, int nxt)
{
int chld;
if (jt->elm[nxt].kind == JSON_OBJECT || jt->elm[nxt].kind ==
JSON_ARRAY) {
- chld = JSONnew(jt);
+ chld = JSONnew(ctx, jt);
if (jt->error)
return;
jt->elm[chld].kind = jt->elm[nxt].kind;
@@ -716,7 +722,7 @@ typedef struct {
} pattern;
static str
-JSONcompile(const char *expr, pattern terms[])
+JSONcompile(Client ctx, const char *expr, pattern terms[])
{
int t = 0;
const char *s, *beg;
@@ -748,7 +754,7 @@ JSONcompile(const char *expr, pattern te
for (beg = s; *s; s++)
if (*s == '.' || *s == '[' || *s == ',')
break;
- terms[t].name = GDKzalloc(s - beg + 1);
+ terms[t].name = ma_zalloc(ctx->alloc, s - beg + 1);
if (terms[t].name == NULL)
throw(MAL, "json.compile", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
terms[t].namelen = s - beg;
@@ -803,13 +809,13 @@ JSONcompile(const char *expr, pattern te
}
static str
-JSONgetValue(const JSON *jt, int idx)
+JSONgetValue(Client ctx, const JSON *jt, int idx)
{
str s;
if (jt->elm[idx].valuelen == 0)
- return GDKstrdup(str_nil);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]