Changeset: 886f92f5f5c9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/886f92f5f5c9
Added Files:
        sql/backends/monet5/UDF/capi/Tests/capi17.test
Modified Files:
        sql/backends/monet5/UDF/capi/Tests/All
        sql/backends/monet5/UDF/capi/capi.c
        sql/backends/monet5/UDF/capi/cheader.h
        sql/backends/monet5/UDF/capi/cheader.text.h
Branch: Sep2022
Log Message:

cleaning up more 'cudf' incorrect allocations/frees


diffs (truncated from 413 to 300 lines):

diff --git a/sql/backends/monet5/UDF/capi/Tests/All 
b/sql/backends/monet5/UDF/capi/Tests/All
--- a/sql/backends/monet5/UDF/capi/Tests/All
+++ b/sql/backends/monet5/UDF/capi/Tests/All
@@ -16,5 +16,5 @@ NOT_WIN32?capi13
 NOT_WIN32?capi14
 NOT_WIN32?capi15
 NOT_WIN32?capi16
-#NOT_WIN32?capi17 no oids
+NOT_WIN32?capi17
 NOT_WIN32?capi18
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi17.test 
b/sql/backends/monet5/UDF/capi/Tests/capi17.test
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi17.test
@@ -0,0 +1,75 @@
+statement ok
+create or replace function my_test(stub string)
+returns string
+language c {
+    result->initialize(result, stub.count);
+    for (int i = 0; i < stub.count; i++) {
+        result->data[i] = malloc(4);
+        result->data[i][0] = 'a';
+        result->data[i][1] = 'b';
+        result->data[i][2] = 'c';
+        result->data[i][3] = '\0';
+    }
+}
+
+query T rowsort
+select my_test('');
+----
+abc
+
+statement ok
+create or replace function my_test(stub int)
+returns string
+language c {
+    result->initialize(result, stub.count);
+    for (int i = 0; i < stub.count; i++) {
+        result->data[i] = malloc(4);
+        result->data[i][0] = 'a';
+        result->data[i][1] = 'b';
+        result->data[i][2] = 'c';
+        result->data[i][3] = '\0';
+    }
+}
+
+query T rowsort
+select my_test(1);
+----
+abc
+
+statement ok
+create or replace aggregate my_test2(stub string)
+returns string
+language c {
+    result->initialize(result, aggr_group.count);
+    for (int i = 0; i < aggr_group.count; i++) {
+        result->data[i] = malloc(4);
+        result->data[i][0] = 'a';
+        result->data[i][1] = 'b';
+        result->data[i][2] = 'c';
+        result->data[i][3] = '\0';
+    }
+}
+
+query T rowsort
+select my_test2('') from sys._tables;
+----
+abc
+
+statement ok
+create or replace aggregate my_test2(stub string)
+returns string
+language c {
+    result->initialize(result, aggr_group.count);
+    for (int i = 0; i < aggr_group.count; i++) {
+        result->data[i] = malloc(4);
+        result->data[i][0] = 'a';
+        result->data[i][1] = 'b';
+        result->data[i][2] = 'c';
+        result->data[i][3] = '\0';
+    }
+};
+
+query T rowsort
+select count(*) > 1 from (select my_test2('') from sys._tables group by id) as 
c
+----
+True
diff --git a/sql/backends/monet5/UDF/capi/capi.c 
b/sql/backends/monet5/UDF/capi/capi.c
--- a/sql/backends/monet5/UDF/capi/capi.c
+++ b/sql/backends/monet5/UDF/capi/capi.c
@@ -201,30 +201,6 @@ static void wrapped_GDK_free(void* ptr) 
        return;
 }
 
-static void *wrapped_GDK_malloc_nojump(size_t size)
-{
-       if (size == 0)
-               return NULL;
-       void *ptr = GDKmalloc(size + sizeof(allocated_region));
-       if (!ptr) {
-               return NULL;
-       }
-       return add_allocated_region(ptr);
-}
-
-static void *wrapped_GDK_zalloc_nojump(size_t size)
-{
-       if (size == 0)
-               return NULL;
-       void *ptr = GDKzalloc(size + sizeof(allocated_region));
-       if (!ptr) {
-               return NULL;
-       }
-       /*return add_allocated_region(ptr); we GDKfree this already in the 
CUDFeval, so no need to keep it in the
-        * allocated_regions */
-       return ptr;
-}
-
 #define GENERATE_NUMERIC_IS_NULL(type, tpename) \
        static int tpename##_is_null(type value) { return 
is_##tpename##_nil(value); }
 
@@ -303,12 +279,13 @@ static void blob_initialize(struct cudf_
                        size_t it = 0;                                          
           \
                        tpe val = b->tseqbase;                                  
           \
                        /* bat is dense, materialize it */                      
           \
-                       bat_data->data = wrapped_GDK_malloc_nojump(             
           \
+                       bat_data->data = GDKmalloc(                        \
                                bat_data->count * 
sizeof(bat_data->null_value));               \
                        if (!bat_data->data) {                                  
           \
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);      \
                                goto wrapup;                                    
               \
                        }                                                       
           \
+                       bat_data->alloced = true;                               
                                                   \
                        for (it = 0; it < bat_data->count; it++) {              
           \
                                bat_data->data[it] = val++;                     
               \
                        }                                                       
           \
@@ -325,12 +302,13 @@ static void blob_initialize(struct cudf_
                        }                                                       
           \
                } else {                                                        
       \
                        /* cannot mprotect bat region, copy data */             
           \
-                       bat_data->data = wrapped_GDK_malloc_nojump(             
           \
+                       bat_data->data = GDKmalloc(                        \
                                bat_data->count * 
sizeof(bat_data->null_value));               \
                        if (bat_data->count > 0 && !bat_data->data) {           
           \
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);      \
                                goto wrapup;                                    
               \
                        }                                                       
           \
+                       bat_data->alloced = true;                               
                                                   \
                        memcpy(bat_data->data, Tloc(b, 0),                      
           \
                                bat_data->count * 
sizeof(bat_data->null_value));                \
                }                                                               
       \
@@ -367,6 +345,8 @@ const char *ldflags_pragma = "#pragma LD
 #define JIT_COMPILER_NAME "cc"
 #define JIT_CPP_COMPILER_NAME "c++"
 
+static bool isAlloced(int type, void *struct_ptr);
+static bool isValloced(int type, void *struct_ptr);
 static size_t GetTypeCount(int type, void *struct_ptr);
 static void *GetTypeData(int type, void *struct_ptr);
 static void *GetTypeBat(int type, void *struct_ptr);
@@ -1073,12 +1053,14 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
                        j = 0;
 
                        // check if we can mprotect the varheap
                        // if we can't mprotect, copy the strings instead
                        assert(input_bats[index]->tvheap);
                        can_mprotect_varheap = 
can_mprotect_region(input_bats[index]->tvheap->base);
+                       bat_data->valloced = !can_mprotect_varheap;
 
                        li = bat_iterator(input_bats[index]);
                        BATloop(input_bats[index], p, q)
@@ -1090,7 +1072,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                        if (can_mprotect_varheap) {
                                                bat_data->data[j] = t;
                                        } else {
-                                               bat_data->data[j] = 
wrapped_GDK_malloc_nojump(strlen(t) + 1);
+                                               bat_data->data[j] = 
GDKmalloc(strlen(t) + 1);
                                                if (!bat_data->data[j]) {
                                                        bat_iterator_end(&li);
                                                        msg = 
createException(MAL, "cudf.eval", MAL_MALLOC_FAIL);
@@ -1124,6 +1106,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
 
                        baseptr = (date *)Tloc(input_bats[index], 0);
                        for (j = 0; j < bat_data->count; j++) {
@@ -1140,6 +1123,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
 
                        baseptr = (daytime *)Tloc(input_bats[index], 0);
                        for (j = 0; j < bat_data->count; j++) {
@@ -1156,6 +1140,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
 
                        baseptr = (timestamp *)Tloc(input_bats[index], 0);
                        for (j = 0; j < bat_data->count; j++) {
@@ -1175,12 +1160,14 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
                        j = 0;
 
                        // check if we can mprotect the varheap
                        // if we can't mprotect, copy the strings instead
                        assert(input_bats[index]->tvheap);
                        can_mprotect_varheap = 
can_mprotect_region(input_bats[index]->tvheap->base);
+                       bat_data->valloced = !can_mprotect_varheap;
 
                        li = bat_iterator(input_bats[index]);
                        BATloop(input_bats[index], p, q)
@@ -1194,7 +1181,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                        if (can_mprotect_varheap) {
                                                bat_data->data[j].data = 
&t->data[0];
                                        } else if (t->nitems > 0) {
-                                               bat_data->data[j].data = 
wrapped_GDK_malloc_nojump(t->nitems);
+                                               bat_data->data[j].data = 
GDKmalloc(t->nitems);
                                                if (!bat_data->data[j].data) {
                                                        bat_iterator_end(&li);
                                                        msg = 
createException(MAL, "cudf.eval", MAL_MALLOC_FAIL);
@@ -1235,6 +1222,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
                        }
+                       bat_data->alloced = true;
                        j = 0;
 
                        li = bat_iterator(input_bats[index]);
@@ -1261,6 +1249,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                j++;
                        }
                        bat_iterator_end(&li);
+                       bat_data->valloced = true;
                }
                input_size = BATcount(input_bats[index]) > input_size
                                                 ? BATcount(input_bats[index])
@@ -1274,7 +1263,8 @@ static str CUDFeval(Client cntxt, MalBlk
                bat_data->count = input_size;
                bat_data->null_value = oid_nil;
                bat_data->data =
-                       wrapped_GDK_zalloc_nojump(bat_data->count * 
sizeof(bat_data->null_value));
+                       GDKzalloc(bat_data->count * 
sizeof(bat_data->null_value));
+               bat_data->alloced = true;
                if (!bat_data->data) {
                                msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                                goto wrapup;
@@ -1642,36 +1632,34 @@ wrapup:
        }
        // input data
        if (inputs) {
-               for (i = 0; i < (size_t)input_count + extra_inputs; i++) {
+               for (i = 0; i < input_count + extra_inputs; i++) {
                        if (inputs[i]) {
-                               if (isaBatType(getArgType(mb, pci, i))) {
-                                       bat_type = getBatType(getArgType(mb, 
pci, i));
+                               int arg = i + pci->retc + ARG_OFFSET;
+                               bat_type = getArgType(mb, pci, arg);
+                               if (isaBatType(bat_type)) {
+                                       bat_type = getBatType(bat_type);
                                }
-                               if (bat_type == TYPE_str || bat_type == 
TYPE_date ||
-                                   bat_type == TYPE_daytime ||
-                                   bat_type == TYPE_timestamp || bat_type == 
TYPE_blob) {
-                                       // have to free input data
-                                       void *data = GetTypeData(bat_type, 
inputs[i]);
-                                       if (data) {
-                                               GDKfree(data);
-                                       }
-                               } else if (bat_type != TYPE_bit && bat_type != 
TYPE_bte &&
-                                                  bat_type != TYPE_sht && 
bat_type != TYPE_int &&
-                                                  bat_type != TYPE_oid && 
bat_type != TYPE_lng &&
-                                                  bat_type != TYPE_flt && 
bat_type != TYPE_dbl) {
-                                       // this type was converted to 
individually malloced
-                                       // strings
-                                       // we have to free all the individual 
strings
+                               if (i == input_count) /* non grouped aggr case 
*/
+                                       bat_type = TYPE_oid;
+                               if (bat_type < 0)
+                                       continue;
+                               if (isAlloced(bat_type, inputs[i])) {
                                        char **data = (char 
**)GetTypeData(bat_type, inputs[i]);
-                                       size_t count = GetTypeCount(bat_type, 
inputs[i]);
-                                       for (j = 0; j < count; j++) {
-                                               if (data[j]) {
-                                                       GDKfree(data[j]);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to