Changeset: 6b8937a70447 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/6b8937a70447
Modified Files:
        sql/storage/bat/bat_storage.c
        sql/storage/bat/bat_table.c
        sql/storage/store.c
        sql/test/miscellaneous/Tests/All
Branch: iso
Log Message:

Merged with Jul2021


diffs (truncated from 911 to 300 lines):

diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -526,7 +526,6 @@ gdk_return
 BATextend(BAT *b, BUN newcap)
 {
        size_t theap_size;
-       gdk_return rc = GDK_SUCCEED;
 
        assert(newcap <= BUN_MAX);
        BATcheck(b, GDK_FAIL);
@@ -554,21 +553,9 @@ BATextend(BAT *b, BUN newcap)
        if (b->theap->base) {
                TRC_DEBUG(HEAP, "HEAPgrow in BATextend %s %zu %zu\n",
                          b->theap->filename, b->theap->size, theap_size);
-               MT_lock_set(&b->theaplock);
-               if (ATOMIC_GET(&b->theap->refs) == 1) {
-                       rc = HEAPextend(b->theap, theap_size, b->batRestricted 
== BAT_READ);
-               } else {
-                       MT_lock_unset(&b->theaplock);
-                       Heap *h = HEAPgrow(b->theap, theap_size);
-                       if (h == NULL)
-                               return GDK_FAIL;
-                       MT_lock_set(&b->theaplock);
-                       HEAPdecref(b->theap, false);
-                       b->theap = h;
-               }
-               MT_lock_unset(&b->theaplock);
+               return HEAPgrow(&b->theaplock, &b->theap, theap_size, 
b->batRestricted == BAT_READ);
        }
-       return rc;
+       return GDK_SUCCEED;
 }
 
 
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -147,25 +147,10 @@ insert_string_bat(BAT *b, BAT *n, struct
                                (var_t) 1 << 17;
                        MT_thread_setalgorithm("copy vheap, copy heap");
                        if (b->tvheap->size < ni.vh->free) {
-                               MT_lock_set(&b->theaplock);
-                               if (ATOMIC_GET(&b->tvheap->refs) == 1) {
-                                       if (HEAPextend(b->tvheap, ni.vh->free, 
force) != GDK_SUCCEED) {
-                                               MT_lock_unset(&b->theaplock);
-                                               bat_iterator_end(&ni);
-                                               return GDK_FAIL;
-                                       }
-                               } else {
-                                       MT_lock_unset(&b->theaplock);
-                                       Heap *h = HEAPgrow(b->tvheap, 
ni.vh->free);
-                                       if (h == NULL) {
-                                               bat_iterator_end(&ni);
-                                               return GDK_FAIL;
-                                       }
-                                       MT_lock_set(&b->theaplock);
-                                       HEAPdecref(b->tvheap, false);
-                                       b->tvheap = h;
+                               if (HEAPgrow(&b->theaplock, &b->tvheap, 
ni.vh->free, force) != GDK_SUCCEED) {
+                                       bat_iterator_end(&ni);
+                                       return GDK_FAIL;
                                }
-                               MT_lock_unset(&b->theaplock);
                        }
                        memcpy(b->tvheap->base, ni.vh->base, ni.vh->free);
                        b->tvheap->free = ni.vh->free;
@@ -210,25 +195,10 @@ insert_string_bat(BAT *b, BAT *n, struct
                                toff = (toff + GDK_VARALIGN - 1) & 
~(GDK_VARALIGN - 1);
                                /* if in "force" mode, the heap may be
                                 * shared when memory mapped */
-                               MT_lock_set(&b->theaplock);
-                               if (ATOMIC_GET(&b->tvheap->refs) == 1) {
-                                       if (HEAPextend(b->tvheap, toff + 
ni.vh->size, force) != GDK_SUCCEED) {
-                                               MT_lock_unset(&b->theaplock);
-                                               bat_iterator_end(&ni);
-                                               return GDK_FAIL;
-                                       }
-                               } else {
-                                       MT_lock_unset(&b->theaplock);
-                                       Heap *h = HEAPgrow(b->tvheap, toff + 
ni.vh->size);
-                                       if (h == NULL) {
-                                               bat_iterator_end(&ni);
-                                               return GDK_FAIL;
-                                       }
-                                       MT_lock_set(&b->theaplock);
-                                       HEAPdecref(b->tvheap, false);
-                                       b->tvheap = h;
+                               if (HEAPgrow(&b->theaplock, &b->tvheap, toff + 
ni.vh->size, force) != GDK_SUCCEED) {
+                                       bat_iterator_end(&ni);
+                                       return GDK_FAIL;
                                }
-                               MT_lock_unset(&b->theaplock);
                                MT_thread_setalgorithm("append vheap");
                                memcpy(b->tvheap->base + toff, ni.vh->base, 
ni.vh->free);
                                b->tvheap->free = toff + ni.vh->free;
diff --git a/gdk/gdk_heap.c b/gdk/gdk_heap.c
--- a/gdk/gdk_heap.c
+++ b/gdk/gdk_heap.c
@@ -86,38 +86,49 @@ decompose_filename(str nme)
        return ext;
 }
 
-Heap *
-HEAPgrow(const Heap *old, size_t size)
+gdk_return
+HEAPgrow(MT_Lock *lock, Heap **hp, size_t size, bool mayshare)
 {
-       assert(size >= old->free);
-       assert(old->storage == STORE_MEM || old->storage == STORE_MMAP);
+       Heap *new;
 
-       TRC_DEBUG(HEAP, "Growing heap %s %zu", old->filename, size);
-       Heap *new = GDKmalloc(sizeof(Heap));
-       if (new == NULL)
-               return NULL;
-       *new = (Heap) {
-               .farmid = old->farmid,
-               .hashash = old->hashash,
-               .cleanhash = old->cleanhash,
-               .dirty = true,
-               .remove = old->remove,
-               .parentid = old->parentid,
-               .wasempty = old->wasempty,
-       };
-       memcpy(new->filename, old->filename, sizeof(new->filename));
-       if (HEAPalloc(new, size, 1, 1) != GDK_SUCCEED) {
-               GDKfree(new);
-               return NULL;
+       MT_lock_set(lock);
+       if (ATOMIC_GET(&(*hp)->refs) == 1) {
+               gdk_return rc = HEAPextend((*hp), size, mayshare);
+               MT_lock_unset(lock);
+               return rc;
        }
-       ATOMIC_INIT(&new->refs, 1);
-       assert(new->storage == STORE_MEM || new->storage == STORE_MMAP);
-       new->free = old->free;
-       if (old->free > 0 &&
-           (new->storage == STORE_MEM || old->storage == STORE_MEM))
-               memcpy(new->base, old->base, old->free);
-       /* else both are STORE_MMAP and refer to the same file */
-       return new;
+       new = GDKmalloc(sizeof(Heap));
+       if (new != NULL) {
+               Heap *old = *hp;
+               *new = (Heap) {
+                       .farmid = old->farmid,
+                       .hashash = old->hashash,
+                       .dirty = true,
+                       .remove = old->remove,
+                       .parentid = old->parentid,
+                       .wasempty = old->wasempty,
+               };
+               memcpy(new->filename, old->filename, sizeof(new->filename));
+               if (HEAPalloc(new, size, 1, 1) == GDK_SUCCEED) {
+                       ATOMIC_INIT(&new->refs, 1);
+                       new->free = old->free;
+                       new->cleanhash = old->cleanhash;
+                       if (old->free > 0 &&
+                           (new->storage == STORE_MEM || old->storage == 
STORE_MEM))
+                               memcpy(new->base, old->base, old->free);
+                       /* else both are STORE_MMAP and refer to the
+                        * same file and so we don't need to copy */
+
+                       /* replace old heap with new */
+                       HEAPdecref(*hp, false);
+                       *hp = new;
+               } else {
+                       GDKfree(new);
+                       new = NULL;
+               }
+       }
+       MT_lock_unset(lock);
+       return new ? GDK_SUCCEED : GDK_FAIL;
 }
 
 /*
@@ -1107,22 +1118,10 @@ HEAP_malloc(BAT *b, size_t nbytes)
 
                /* Increase the size of the heap. */
                TRC_DEBUG(HEAP, "HEAPextend in HEAP_malloc %s %zu %zu\n", 
heap->filename, heap->size, newsize);
-               MT_lock_set(&b->theaplock);
-               if (ATOMIC_GET(&heap->refs) == 1) {
-                       if (HEAPextend(heap, newsize, false) != GDK_SUCCEED) {
-                               MT_lock_unset(&b->theaplock);
-                               return 0;
-                       }
-               } else {
-                       MT_lock_unset(&b->theaplock);
-                       Heap *new = HEAPgrow(heap, newsize);
-                       if (new == NULL)
-                               return 0;
-                       MT_lock_set(&b->theaplock);
-                       HEAPdecref(heap, false);
-                       b->tvheap = heap = new;
+               if (HEAPgrow(&b->theaplock, &b->tvheap, newsize, false) != 
GDK_SUCCEED) {
+                       return 0;
                }
-               MT_lock_unset(&b->theaplock);
+               heap = b->tvheap;
                heap->free = newsize;
                hheader = HEAP_index(heap, 0, HEADER);
 
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -199,7 +199,7 @@ gdk_return HEAPdelete(Heap *h, const cha
        __attribute__((__visibility__("hidden")));
 void HEAPfree(Heap *h, bool remove)
        __attribute__((__visibility__("hidden")));
-Heap *HEAPgrow(const Heap *old, size_t size)
+gdk_return HEAPgrow(MT_Lock *lock, Heap **old, size_t size, bool mayshare)
        __attribute__((__visibility__("hidden")));
 gdk_return HEAPload(Heap *h, const char *nme, const char *ext, bool trunc)
        __attribute__((__warn_unused_result__))
diff --git a/gdk/gdk_string.c b/gdk/gdk_string.c
--- a/gdk/gdk_string.c
+++ b/gdk/gdk_string.c
@@ -263,31 +263,20 @@ strPut(BAT *b, var_t *dst, const void *V
 
        if (h->free == 0) {
                if (h->size < 64 * 1024) {
-                       MT_lock_set(&b->theaplock);
-                       if (ATOMIC_GET(&h->refs) == 1) {
-                               if (HEAPextend(h, 64 * 1024, true) != 
GDK_SUCCEED) {
-                                       MT_lock_unset(&b->theaplock);
-                                       return 0;
-                               }
-                       } else {
-                               MT_lock_unset(&b->theaplock);
-                               Heap *new = HEAPgrow(h, 64 * 1024);
-                               if (new == NULL)
-                                       return 0;
-                               MT_lock_set(&b->theaplock);
-                               HEAPdecref(h, false);
-                               b->tvheap = h = new;
+                       if (HEAPgrow(&b->theaplock, &b->tvheap, 64 * 1024, 
true) != GDK_SUCCEED) {
+                               return 0;
                        }
-                       MT_lock_unset(&b->theaplock);
+                       h = b->tvheap;
                }
                h->free = GDK_STRHASHTABLE * sizeof(stridx_t);
                h->dirty = true;
+#ifdef NDEBUG
                memset(h->base, 0, h->free);
+#else
+               /* fill should solve initialization problems within valgrind */
+               memset(h->base, 0, h->size);
+#endif
                h->hashash = false;
-#ifndef NDEBUG
-               /* fill should solve initialization problems within valgrind */
-               memset(h->base + h->free, 0, h->size - h->free);
-#endif
        }
 
        off = strHash(v);
@@ -326,10 +315,12 @@ strPut(BAT *b, var_t *dst, const void *V
        /* check that string is correctly encoded UTF-8; there was no
         * need to do this earlier: if the string was found above, it
         * must have gone through here in the past */
+#ifndef NDEBUG
        if (checkUTF8(v) != GDK_SUCCEED) {
                GDKerror("incorrectly encoded UTF-8\n");
                return 0;
        }
+#endif
 
        pad = GDK_VARALIGN - (h->free & (GDK_VARALIGN - 1));
        if (GDK_ELIMBASE(h->free + pad) == 0) { /* i.e. h->free+pad < 
GDK_ELIMLIMIT */
@@ -366,22 +357,10 @@ strPut(BAT *b, var_t *dst, const void *V
                        return 0;
                }
                TRC_DEBUG(HEAP, "HEAPextend in strPut %s %zu %zu\n", 
h->filename, h->size, newsize);
-               MT_lock_set(&b->theaplock);
-               if (ATOMIC_GET(&h->refs) == 1) {
-                       if (HEAPextend(h, newsize, true) != GDK_SUCCEED) {
-                               MT_lock_unset(&b->theaplock);
-                               return 0;
-                       }
-               } else {
-                       MT_lock_unset(&b->theaplock);
-                       Heap *new = HEAPgrow(h, newsize);
-                       if (new == NULL)
-                               return 0;
-                       MT_lock_set(&b->theaplock);
-                       HEAPdecref(h, false);
-                       b->tvheap = h = new;
+               if (HEAPgrow(&b->theaplock, &b->tvheap, newsize, true) != 
GDK_SUCCEED) {
+                       return 0;
                }
-               MT_lock_unset(&b->theaplock);
+               h = b->tvheap;
 
                /* make bucket point into the new heap */
                bucket = ((stridx_t *) h->base) + off;
diff --git a/sql/storage/bat/bat_storage.c b/sql/storage/bat/bat_storage.c
--- a/sql/storage/bat/bat_storage.c
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to