Changeset: 4be57592a03e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/4be57592a03e
Modified Files:
        clients/Tests/exports.stable.out
        monetdb5/modules/mal/manifold.c
        monetdb5/modules/mal/pcre.c
        monetdb5/modules/mal/remote.c
        monetdb5/modules/mal/tablet.c
        monetdb5/modules/mal/tablet.h
        sql/backends/monet5/sql_result.c
Branch: Dec2025
Log Message:

Use thread-local allocators.


diffs (truncated from 765 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -819,7 +819,7 @@ BUN SQLload_file(Client cntxt, Tablet *a
 str TABLETcollect(BAT **bats, Tablet *as);
 str TABLETcreate_bats(Tablet *as, BUN est);
 void TABLETdestroy_format(Tablet *as);
-int TABLEToutput_file(allocator *ma, Tablet *as, BAT *order, stream *s, 
bstream *in);
+int TABLEToutput_file(Tablet *as, BAT *order, stream *s, bstream *in);
 int TRACEtable(Client cntxt, BAT **r);
 int TYPE_xml;
 int UTF8_strlen(const char *s);
diff --git a/monetdb5/modules/mal/manifold.c b/monetdb5/modules/mal/manifold.c
--- a/monetdb5/modules/mal/manifold.c
+++ b/monetdb5/modules/mal/manifold.c
@@ -138,7 +138,7 @@ typedef struct {
 // Only the last error message is returned, the value of
 // an erroneous call depends on the operator itself.
 static str
-MANIFOLDjob(allocator *ma, MULTItask *mut)
+MANIFOLDjob(MULTItask *mut)
 {
        int i;
        char **args;
@@ -149,9 +149,14 @@ MANIFOLDjob(allocator *ma, MULTItask *mu
        if (olimit == 0)
                return msg;                             /* nothing to do */
 
-       args = (char **) ma_zalloc(ma, sizeof(char *) * mut->pci->argc);
-       if (args == NULL)
+       allocator *ta = MT_thread_getallocator();
+       allocator_state ta_state = ma_open(ta);
+
+       args = (char **) ma_zalloc(ta, sizeof(char *) * mut->pci->argc);
+       if (args == NULL) {
+               ma_close(ta, &ta_state);
                throw(MAL, "mal.manifold", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+       }
 
        // the mod.fcn arguments are ignored from the call
        for (i = mut->pci->retc + 2; i < mut->pci->argc; i++) {
@@ -195,7 +200,7 @@ MANIFOLDjob(allocator *ma, MULTItask *mu
        //if (ATOMextern(mut->args[0].type) && y)
        //      GDKfree(y);
   bunins_failed:
-       //GDKfree(args);
+       ma_close(ta, &ta_state);
        return msg;
 }
 
@@ -305,9 +310,14 @@ MANIFOLDevaluate(Client cntxt, MalBlkPtr
                throw(MAL, "mal.manifold", "Illegal manifold function call");
        }
 
-       mat = (MULTIarg *) ma_zalloc(mb->ma, sizeof(MULTIarg) * pci->argc);
-       if (mat == NULL)
+       allocator *ta = MT_thread_getallocator();
+       allocator_state ta_state = ma_open(ta);
+
+       mat = (MULTIarg *) ma_zalloc(ta, sizeof(MULTIarg) * pci->argc);
+       if (mat == NULL) {
+               ma_close(ta, &ta_state);
                throw(MAL, "mal.manifold", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+       }
 
        // mr-job structure preparation
        MULTItask mut = {
@@ -386,7 +396,7 @@ MANIFOLDevaluate(Client cntxt, MalBlkPtr
        }
        */
        mut.pci->fcn = fcn;
-       msg = MANIFOLDjob(mb->ma, &mut);
+       msg = MANIFOLDjob(&mut);
        //freeInstruction(mut.pci);
 
   wrapup:
@@ -407,7 +417,7 @@ MANIFOLDevaluate(Client cntxt, MalBlkPtr
                *getArgReference_bat(stk, pci, 0) = mat[0].b->batCacheid;
                BBPkeepref(mat[0].b);
        }
-       //GDKfree(mat);
+       ma_close(ta, &ta_state);
        return msg;
 }
 
diff --git a/monetdb5/modules/mal/pcre.c b/monetdb5/modules/mal/pcre.c
--- a/monetdb5/modules/mal/pcre.c
+++ b/monetdb5/modules/mal/pcre.c
@@ -435,10 +435,9 @@ pcre_replace(allocator *ma, str *res, co
 }
 
 static str
-pcre_replace_bat(allocator *ma, BAT **res, BAT *origin_strs, const char 
*pattern,
+pcre_replace_bat(BAT **res, BAT *origin_strs, const char *pattern,
                                 const char *replacement, const char *flags, 
bool global)
 {
-       (void) ma;
 #ifdef HAVE_LIBPCRE
        int err = 0;
        PCRE2_UCHAR *tmpres;
@@ -452,6 +451,8 @@ pcre_replace_bat(allocator *ma, BAT **re
        PCRE2_SIZE len_replacement = (PCRE2_SIZE) strlen(replacement);
        PCRE2_SPTR origin_str;
        PCRE2_SIZE max_dest_size = 0, init_size = 0;
+       allocator *ta = MT_thread_getallocator();
+       allocator_state ta_state = ma_open(ta);
 
        while (*flags) {
                switch (*flags) {
@@ -471,6 +472,7 @@ pcre_replace_bat(allocator *ma, BAT **re
                        compile_options |= PCRE2_EXTENDED;
                        break;
                default:
+                       ma_close(ta, &ta_state);
                        throw(MAL, global ? "batpcre.replace" : 
"batpcre.replace_first",
                                  ILLEGAL_ARGUMENT ": unsupported flag 
character '%c'\n",
                                  *flags);
@@ -485,6 +487,7 @@ pcre_replace_bat(allocator *ma, BAT **re
        if (pcre_code == NULL) {
                PCRE2_UCHAR errbuf[256];
                pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+               ma_close(ta, &ta_state);
                throw(MAL, global ? "pcre.replace" : "pcre.replace_first",
                          OPERATION_FAILED
                          ": pcre compile of pattern (%s) failed at %d 
with\n'%s'.\n",
@@ -493,6 +496,7 @@ pcre_replace_bat(allocator *ma, BAT **re
        match_data = pcre2_match_data_create_from_pattern(pcre_code, NULL);
        if (match_data == NULL) {
                pcre2_code_free(pcre_code);
+               ma_close(ta, &ta_state);
                throw(MAL, "regexp.rematch", MAL_MALLOC_FAIL);
        }
 
@@ -502,19 +506,20 @@ pcre_replace_bat(allocator *ma, BAT **re
        /* the buffer for all destination strings is allocated only once,
         * and extended when needed */
        init_size = max_dest_size = 64*1024;
-       tmpres = ma_alloc(ma, max_dest_size);
+       tmpres = ma_alloc(ta, max_dest_size);
        if (tmpbat == NULL || tmpres == NULL) {
                pcre2_match_data_free(match_data);
                pcre2_code_free(pcre_code);
                BBPreclaim(tmpbat);
                //GDKfree(tmpres);
+               ma_close(ta, &ta_state);
                throw(MAL, global ? "batpcre.replace" : "batpcre.replace_first",
                          SQLSTATE(HY013) MAL_MALLOC_FAIL);
        }
        BATiter origin_strsi = bat_iterator(origin_strs);
        BATloop(origin_strs, p, q) {
                origin_str = BUNtvar(origin_strsi, p);
-               tmpres = single_replace(ma, pcre_code, match_data, origin_str,
+               tmpres = single_replace(ta, pcre_code, match_data, origin_str,
                                                                (PCRE2_SIZE) 
strlen((char *) origin_str), exec_options,
                                                                (PCRE2_SPTR) 
replacement, len_replacement,
                                                                tmpres, 
&max_dest_size);
@@ -524,6 +529,7 @@ pcre_replace_bat(allocator *ma, BAT **re
                        pcre2_code_free(pcre_code);
                        //GDKfree(tmpres);
                        BBPreclaim(tmpbat);
+                       ma_close(ta, &ta_state);
                        throw(MAL, global ? "batpcre.replace" : 
"batpcre.replace_first",
                                  SQLSTATE(HY013) MAL_MALLOC_FAIL);
                }
@@ -536,6 +542,7 @@ pcre_replace_bat(allocator *ma, BAT **re
        pcre2_match_data_free(match_data);
        pcre2_code_free(pcre_code);
        //GDKfree(tmpres);
+       ma_close(ta, &ta_state);
        *res = tmpbat;
        return MAL_SUCCEED;
 #else
@@ -804,13 +811,13 @@ static str
 PCREreplace_bat_wrap(Client ctx, bat *res, const bat *bid, const char *const 
*pat,
                                         const char *const *repl, const char 
*const *flags)
 {
-       allocator *ma = ctx->curprg->def->ma;
        BAT *b, *bn = NULL;
        str msg;
+       (void) ctx;
        if ((b = BATdescriptor(*bid)) == NULL)
                throw(MAL, "batpcre.replace", SQLSTATE(HY002) 
RUNTIME_OBJECT_MISSING);
 
-       msg = pcre_replace_bat(ma, &bn, b, *pat, *repl, *flags, true);
+       msg = pcre_replace_bat(&bn, b, *pat, *repl, *flags, true);
        if (msg == MAL_SUCCEED) {
                *res = bn->batCacheid;
                BBPkeepref(bn);
@@ -823,13 +830,13 @@ static str
 PCREreplacefirst_bat_wrap(Client ctx, bat *res, const bat *bid, const char 
*const *pat,
                                                  const char *const *repl, 
const char *const *flags)
 {
-       allocator *ma = ctx->curprg->def->ma;
        BAT *b, *bn = NULL;
        str msg;
+       (void) ctx;
        if ((b = BATdescriptor(*bid)) == NULL)
                throw(MAL, "batpcre.replace_first", SQLSTATE(HY002) 
RUNTIME_OBJECT_MISSING);
 
-       msg = pcre_replace_bat(ma, &bn, b, *pat, *repl, *flags, false);
+       msg = pcre_replace_bat(&bn, b, *pat, *repl, *flags, false);
        if (msg == MAL_SUCCEED) {
                *res = bn->batCacheid;
                BBPkeepref(bn);
@@ -855,8 +862,7 @@ PCREimatch(Client ctx, bit *ret, const c
 static str
 PCREpatindex(Client ctx, int *ret, const char *const *pat, const char *const 
*val)
 {
-       allocator *ma = ctx->curprg->def->ma;
-       (void) ma;
+       (void) ctx;
 #ifdef HAVE_LIBPCRE
        pcre2_code *re;
        pcre2_match_data *match_data;
@@ -870,8 +876,13 @@ PCREpatindex(Client ctx, int *ret, const
                return MAL_SUCCEED;
        }
 
-       if ((msg = pat2pcre(ma, &ppat, *pat)) != MAL_SUCCEED)
+       allocator *ta = MT_thread_getallocator();
+       allocator_state ta_state = ma_open(ta);
+
+       if ((msg = pat2pcre(ta, &ppat, *pat)) != MAL_SUCCEED) {
+               ma_close(ta, &ta_state);
                return msg;
+       }
        re = pcre2_compile((PCRE2_SPTR) ppat, PCRE2_ZERO_TERMINATED, PCRE2_UTF 
| PCRE2_NO_UTF_CHECK | PCRE2_MULTILINE, &errcode, &errpos, NULL);
        if (re == NULL) {
                PCRE2_UCHAR errbuf[256];
@@ -880,8 +891,10 @@ PCREpatindex(Client ctx, int *ret, const
                                                          "compilation of 
regular expression (%s) failed at %d with %s",
                                                          ppat, (int) errpos, 
(char *) errbuf);
                //GDKfree(ppat);
+               ma_close(ta, &ta_state);
                return msg;
        }
+       ma_close(ta, &ta_state);
        //GDKfree(ppat);
        match_data = pcre2_match_data_create_from_pattern(re, NULL);
        if (match_data == NULL) {
@@ -966,7 +979,7 @@ choose_like_path(bool *use_re, bool *use
 }
 
 static str
-PCRElike_imp(allocator *ma, bit *ret, const char *const *s, const char *const 
*pat,
+PCRElike_imp(bit *ret, const char *const *s, const char *const *pat,
                         const char *const *esc, const bit *isens)
 {
        str res = MAL_SUCCEED;
@@ -989,11 +1002,14 @@ PCRElike_imp(allocator *ma, bit *ret, co
                        *ret = *isens ? GDKstrcasecmp(*s, *pat) == 0
                                : strcmp(*s, *pat) == 0;
                } else {
-                       if (!(re = mnre_create(ma, *pat, *isens, (unsigned 
char) **esc)))
+                       allocator *ta = MT_thread_getallocator();
+                       allocator_state ta_state = ma_open(ta);
+                       if (!(re = mnre_create(ta, *pat, *isens, (unsigned 
char) **esc)))
                                res = createException(MAL, "pcre.like4",
                                                                          
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                        else
                                *ret = mnre_match(*s, re);
+                       ma_close(ta, &ta_state);
                }
        }
 
@@ -1006,8 +1022,8 @@ static str
 PCRElike(Client ctx, bit *ret, const char *const *s, const char *const *pat,
                 const char *const *esc, const bit *isens)
 {
-       allocator *ma = ctx->curprg->def->ma;
-       return PCRElike_imp(ma, ret, s, pat, esc, isens);
+       (void) ctx;
+       return PCRElike_imp(ret, s, pat, esc, isens);
 }
 
 static str
@@ -1092,7 +1108,7 @@ BATPCRElike_imp(Client cntxt, MalBlkPtr 
        struct RE *mnre_simple = NULL;
        BATiter bi = { 0 }, pi;
 
-       allocator *ma = cntxt->curprg->def->ma;
+       (void) cntxt;
        if (input_is_a_bat) {
                bat *bid = getArgReference_bat(stk, pci, 1);
                if (!(b = BATdescriptor(*bid))) {
@@ -1142,16 +1158,20 @@ BATPCRElike_imp(Client cntxt, MalBlkPtr 
                        if (empty) {
                                ret[p] = bit_nil;
                        } else {
-                               if ((msg = mnre_like_build(ma, &mnre_simple, 
np, isensitive,
+                               allocator *ta = MT_thread_getallocator();
+                               allocator_state ta_state = ma_open(ta);
+                               if ((msg = mnre_like_build(ta, &mnre_simple, 
np, isensitive,
                                                                                
 use_strcmp,
                                                                                
 (unsigned char) **esc)) != MAL_SUCCEED) {
                                        bat_iterator_end(&pi);
                                        if (b)
                                                bat_iterator_end(&bi);
+                                       ma_close(ta, &ta_state);
                                        goto bailout;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to