Changeset: 25f4bdc319eb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/25f4bdc319eb
Modified Files:
        monetdb5/modules/atoms/json.c
Branch: Dec2025
Log Message:

Clean up json a little.


diffs (truncated from 706 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
@@ -72,7 +72,6 @@ typedef str json;
                        if (jt) {                                               
                                                        \
                                msg = jt->error;                                
                                                \
                                jt->error = NULL;                               
                                                \
-                               JSONfree(jt);                                   
                                                \
                        } else {                                                
                                                        \
                                msg = createException(MAL, "json.new", 
SQLSTATE(HY013) MAL_MALLOC_FAIL); \
                        }                                                       
                                                                \
@@ -83,7 +82,6 @@ typedef str json;
 int TYPE_json;
 
 /* Internal constructors. */
-static int jsonhint = 8;
 static JSON *JSONparse(allocator *ma, const char *j);
 
 static JSON *
@@ -94,12 +92,11 @@ JSONnewtree(allocator *ma)
        js = ma_zalloc(ma, sizeof(JSON));
        if (js == NULL)
                return NULL;
-       js->elm = ma_zalloc(ma, sizeof(JSONterm) * jsonhint);
+       js->elm = ma_zalloc(ma, sizeof(JSONterm) * 8);
        if (js->elm == NULL) {
-               //GDKfree(js);
                return NULL;
        }
-       js->size = jsonhint;
+       js->size = 8;
        return js;
 }
 
@@ -120,23 +117,10 @@ JSONnew(allocator *ma, JSON *js)
                js->elm = term;
                memset(term + js->size, 0, 8 * sizeof(JSONterm));
                js->size += 8;
-               if (jsonhint < js->size)
-                       jsonhint = js->size;
        }
        return js->free++;
 }
 
-/* Delete a JSON structure. */
-static void
-JSONfree(JSON *c)
-{
-       if (c == 0)
-               return;
-       freeException(c->error);
-       //GDKfree(c->elm);
-       //GDKfree(c);
-}
-
 static str
 JSONtoStorageString(JSON *jt, int idx, json *ret, size_t *out_size)
 {
@@ -145,10 +129,9 @@ JSONtoStorageString(JSON *jt, int idx, j
        str msg = MAL_SUCCEED;
 
        if (THRhighwater()) {
-               msg = createException(MAL, "json.new",
-                                                                       
SQLSTATE(42000)
-                                                                       "JSON 
object too complex to render into string.");
-               return msg;
+               throw(MAL, "json.new",
+                         SQLSTATE(42000)
+                         "JSON object too complex to render into string.");
        }
 
 
@@ -156,7 +139,7 @@ JSONtoStorageString(JSON *jt, int idx, j
        case JSON_OBJECT:
                *p++ = '{';
                *out_size += 1;
-               for(int i = jt->elm[idx].next; i != 0; i = jt->elm[i].next) {
+               for (int i = jt->elm[idx].next; i != 0; i = jt->elm[i].next) {
                        sz = 0;
                        if (i != jt->elm[idx].next) {
                                *p++ = ',';
@@ -175,7 +158,7 @@ JSONtoStorageString(JSON *jt, int idx, j
        case JSON_ARRAY:
                *p++ = '[';
                *out_size += 1;
-               for(int i = jt->elm[idx].next; i != 0; i = jt->elm[i].next) {
+               for (int i = jt->elm[idx].next; i != 0; i = jt->elm[i].next) {
                        sz = 0;
                        if (i != jt->elm[idx].next) {
                                *p++ = ',';
@@ -225,9 +208,10 @@ JSONtoStorageString(JSON *jt, int idx, j
                *out_size += 4;
                p += *out_size;
                break;
+       case JSON_BOOL:
+               /* not used */
        default:
-               msg = createException(MAL, "json.new", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
-               break;
+               MT_UNREACHABLE();
        }
 
        *p = 0;
@@ -270,7 +254,6 @@ JSONtoString(allocator *ma, str *s, size
 
        if (strNil(src)) {
                if (*s == NULL || *len < 4) {
-                       //GDKfree(*s);
                        *len = 4;
                        *s = ma_alloc(ma, 4);
                        if (*s == NULL)
@@ -304,7 +287,6 @@ JSONtoString(allocator *ma, str *s, size
        }
 
        if (cnt > (size_t) *len) {
-               // GDKfree(*s);
                *s = ma_alloc(ma, cnt);
                if (*s == NULL)
                        return -1;
@@ -348,7 +330,6 @@ JSONdumpInternal(allocator *ma, Client c
        BAT *bn = COLnew(0, TYPE_str, 0, TRANSIENT);
 
        if (!buffer || !bn) {
-               //GDKfree(buffer);
                BBPreclaim(bn);
                return NULL;
        }
@@ -364,7 +345,6 @@ JSONdumpInternal(allocator *ma, Client c
                        } while (datlen + depth * 4 + 512 > buflen);
                        char *newbuf = ma_realloc(ma, buffer, buflen, osz);
                        if (newbuf == NULL) {
-                               //GDKfree(buffer);
                                BBPreclaim(bn);
                                return NULL;
                        }
@@ -410,7 +390,6 @@ JSONdumpInternal(allocator *ma, Client c
                                buflen += 1024;
                                char *newbuf = ma_realloc(ma, buffer, buflen, 
osz);
                                if (newbuf == NULL) {
-                                       //GDKfree(buffer);
                                        BBPreclaim(bn);
                                        return NULL;
                                }
@@ -426,7 +405,6 @@ JSONdumpInternal(allocator *ma, Client c
                                } while (datlen + 10 + je->namelen > buflen);
                                char *newbuf = ma_realloc(ma, buffer, buflen, 
osz);
                                if (newbuf == NULL) {
-                                       //GDKfree(buffer);
                                        BBPreclaim(bn);
                                        return NULL;
                                }
@@ -443,7 +421,6 @@ JSONdumpInternal(allocator *ma, Client c
                                } while (datlen + 10 + je->valuelen > buflen);
                                char *newbuf = ma_realloc(ma, buffer, buflen, 
osz);
                                if (newbuf == NULL) {
-                                       //GDKfree(buffer);
                                        BBPreclaim(bn);
                                        return NULL;
                                }
@@ -454,11 +431,9 @@ JSONdumpInternal(allocator *ma, Client c
                }
                if (BUNappend(bn, buffer, false) != GDK_SUCCEED) {
                        BBPreclaim(bn);
-                       //GDKfree(buffer);
                        return NULL;
                }
        }
-       //GDKfree(buffer);
        return bn;
 }
 
@@ -475,7 +450,6 @@ JSONdump(Client ctx, MalBlkPtr mb, MalSt
 
        CHECK_JSON(jt);
        BAT *bn = JSONdumpInternal(ma, ctx, jt, 0);
-       JSONfree(jt);
        ma_close(ma, &ma_state);
        if (bn == NULL)
                throw(MAL, "json.dump", SQLSTATE(HY013) MAL_MALLOC_FAIL);
@@ -517,21 +491,23 @@ JSONstr2json_intern(allocator *ma, json 
        assert(ma);
        str msg = MAL_SUCCEED;
        json buf = *ret;
-       size_t ln = strlen(*j)+1;
+       size_t ln = strlen(*j) + 1;
        size_t out_size = 0;
+       JSON *jt = NULL;
+
+       if (buf == NULL || *len < ln) {
+               *len = ln;
+               buf = ma_alloc(ma, ln);
+               if (buf == NULL)
+                       throw(MAL, "json.new", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+       }
+
        allocator *ta = MT_thread_getallocator();
        allocator_state ta_state = {0};
-       if (ta != ma)
-               ta_state = ma_open(ta);
-
-       JSON *jt = NULL;
+       ta_state = ma_open(ta);
 
        if (strNil(*j)) {
-               if (!buf || *len < strLen(*j)) {
-                       *len = strLen(*j);
-                       buf = (json)ma_alloc(ma, *len);
-               }
-               buf = strcpy(buf, *j);
+               strcpy(buf, *j);
        } else {
                jt = JSONparse(ta, *j);
                if (jt == NULL || jt->error) {
@@ -543,29 +519,17 @@ JSONstr2json_intern(allocator *ma, json 
                        }
                        goto bailout;
                }
-
-               if (!buf || *len < ln) {
-                       *len = ln;
-                       buf = (json)ma_alloc(ma, ln);
-               }
-       }
-       if (buf == NULL) {
-               msg = createException(MAL, "json.new", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
-               goto bailout;
        }
 
        if (jt != NULL) {
                msg = JSONtoStorageString(jt, 0, &buf, &out_size);
                if (msg != MAL_SUCCEED) {
-                       //GDKfree(buf);
                        goto bailout;
                }
        }
        *ret = buf;
  bailout:
-       JSONfree(jt);
-       if (ta != ma)
-               ma_close(ta, &ta_state);
+       ma_close(ta, &ta_state);
        return msg;
 }
 
@@ -591,7 +555,6 @@ JSONisvalid(Client ctx, bit *ret, const 
                        throw(MAL, "json.isvalid", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                }
                *ret = jt->error == MAL_SUCCEED;
-               JSONfree(jt);
                ma_close(ta, &ta_state);
        }
        return MAL_SUCCEED;
@@ -867,7 +830,6 @@ JSONglue(allocator *ma, str res, str r, 
        str n;
 
        if (r == 0 || *r == 0) {
-               //GDKfree(r);
                return res;
        }
        len = strlen(r);
@@ -876,13 +838,9 @@ JSONglue(allocator *ma, str res, str r, 
        l = strlen(res);
        n = ma_alloc(ma, l + len + 3);
        if (n == NULL) {
-               //GDKfree(res);
-               //GDKfree(r);
                return NULL;
        }
        snprintf(n, l + len + 3, "%s%s%s", res, sep ? "," : "", r);
-       //GDKfree(res);
-       //GDKfree(r);
        return n;
 }
 
@@ -943,7 +901,6 @@ JSONmatch(allocator *ma, JSON *jt, int j
                                        r = JSONmatch(ma, jt, jt->elm[i].child, 
terms, ti + 1, terms[ti].index == INT_MAX);
                                }
                                if (r == (str) -1 || r == (str) -2) {
-                                       //GDKfree(res);
                                        return r;
                                }
                                res = JSONglue(ma, res, r, ',');
@@ -970,14 +927,11 @@ JSONmatch(allocator *ma, JSON *jt, int j
                                                r = JSONmatch(ma, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
                                        }
                                        if (r == (str) -1 || r == (str) -2) {
-                                               //GDKfree(res);
                                                return r;
                                        }
                                        if (accumulate) {
                                                res = JSONglue(ma, res, r, ',');
                                        } else {  // Keep the last matching 
value
-                                               //if (res)
-                                               //      GDKfree(res);
                                                res = r;
                                        }
                                }
@@ -985,7 +939,6 @@ JSONmatch(allocator *ma, JSON *jt, int j
                        } else if (terms[ti].token == ANY_STEP && 
jt->elm[i].child) {
                                r = JSONmatch(ma, jt, jt->elm[i].child, terms, 
ti, true);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to