Changeset: a10d7f1cfc89 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a10d7f1cfc89
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_system.c
        gdk/gdk_system_private.h
        gdk/gdk_utils.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_instruction.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_cat.c
        sql/backends/monet5/sql_execute.c
        sql/backends/monet5/sql_scenario.c
        sql/backends/monet5/sql_upgrades.c
        sql/server/sql_mvc.c
        sql/server/sqlparse.c
        sql/storage/store.c
        tools/monetdbe/monetdbe.c
        tools/mserver/shutdowntest.c
Branch: Dec2025
Log Message:

Cleanup of allocator.
We don't really need a parent allocator since it was only used to
allocate full blocks.  We may as well allocate them directly.
Properly count usedmem.  This means we don't need to reset it in
ma_close.
In resizeMalBlk, use correct allocator for resizing.


diffs (truncated from 709 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
@@ -479,7 +479,7 @@ BAT *canditer_slice(const struct candite
 BAT *canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN 
hi2);
 BAT *canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, 
oid hi2);
 BAT *canditer_sliceval(const struct canditer *ci, oid lo, oid hi);
-allocator *create_allocator(allocator *pa, const char *, bool use_lock);
+allocator *create_allocator(const char *, bool use_lock);
 char *ctime_r(const time_t *restrict, char *restrict);
 date date_add_day(date dt, int days) __attribute__((__const__));
 date date_add_month(date dt, int months) __attribute__((__const__));
@@ -567,7 +567,6 @@ void ma_close(const allocator_state *);
 void ma_destroy(allocator *sa);
 void ma_free(allocator *sa, void *);
 exception_buffer *ma_get_eb(allocator *sa) __attribute__((__pure__));
-allocator *ma_get_parent(const allocator *sa);
 int ma_info(allocator *sa, char *buf, size_t buflen, const char *pref);
 const char *ma_name(allocator *sa);
 allocator_state ma_open(allocator *sa);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -71,7 +71,6 @@ typedef struct allocator allocator;
 typedef struct {
        size_t nr;
        size_t used;
-       size_t usedmem;  /* total used memory */
        size_t objects;
        size_t inuse;
        size_t tmp_used;
@@ -1755,8 +1754,7 @@ gdk_export ValPtr VALcopy(allocator *va,
 gdk_export ValPtr VALinit(allocator *va, ValPtr d, int tpe, const void *s)
        __attribute__((__access__(write_only, 2)));
 
-gdk_export allocator *create_allocator(allocator *pa, const char *, bool 
use_lock);
-gdk_export allocator *ma_get_parent(const allocator *sa);
+gdk_export allocator *create_allocator(const char *, bool use_lock);
 gdk_export bool ma_tmp_active(const allocator *sa);
 gdk_export void ma_reset(allocator *sa);
 gdk_export void *ma_alloc(allocator *sa,  size_t sz);
@@ -1857,16 +1855,15 @@ gdk_export int ma_info(allocator *sa, ch
                          _sa, ma_name(_sa), strlen(_s1), strlen(_s2), _res); \
                _res;                                                   \
        })
-#define create_allocator(sa, nm, lk)                                   \
-       ({                                                              \
-               allocator *_sa = (sa);                                  \
-               const char *_nm = (nm);                                 \
-               bool _lk = (lk);                                        \
-               allocator *_res = create_allocator(_sa, _nm, _lk);      \
-               TRC_DEBUG(ALLOC,                                        \
-                         "create_allocator(%p(%s)) -> %p(%s)\n",       \
-                         _sa, ma_name(_sa), _res, ma_name(_res));      \
-               _res;                                                   \
+#define create_allocator(nm, lk)                               \
+       ({                                                      \
+               const char *_nm = (nm);                         \
+               bool _lk = (lk);                                \
+               allocator *_res = create_allocator(_nm, _lk);   \
+               TRC_DEBUG(ALLOC,                                \
+                         "create_allocator() -> %p(%s)\n",     \
+                         _res, ma_name(_res));                 \
+               _res;                                           \
        })
 #define ma_open(sa)                                                    \
        ({                                                              \
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -418,7 +418,7 @@ MT_thread_init(void)
        }
        InitializeCriticalSection(&winthread_cs);
 #endif
-       mainthread.ma = create_allocator(NULL, mainthread.threadname, false);
+       mainthread.ma = create_allocator(mainthread.threadname, false);
        if (mainthread.ma == NULL) {
                GDKerror("Creating thread-local allocator failed");
                return false;
@@ -462,7 +462,7 @@ MT_thread_register(void)
                .semawait = ATOMIC_PTR_VAR_INIT(NULL),
        };
        snprintf(self->threadname, sizeof(self->threadname), "foreign %zu", 
self->tid);
-       self->ma = create_allocator(NULL, self->threadname, false);
+       self->ma = create_allocator(self->threadname, false);
        if (self->ma == NULL) {
                free(self);
                return false;
@@ -971,7 +971,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
                .exited = ATOMIC_VAR_INIT(0),
                .working = ATOMIC_PTR_VAR_INIT(NULL),
                .semawait = ATOMIC_PTR_VAR_INIT(NULL),
-               .ma = create_allocator(NULL, threadname, false),
+               .ma = create_allocator(threadname, false),
        };
        if (self->ma == NULL) {
                free(self);
diff --git a/gdk/gdk_system_private.h b/gdk/gdk_system_private.h
--- a/gdk/gdk_system_private.h
+++ b/gdk/gdk_system_private.h
@@ -49,7 +49,6 @@ struct freebats *MT_thread_getfreebats(v
        __attribute__((__visibility__("hidden")));
 
 struct allocator {
-       struct allocator *pa;
        size_t size;     /* size of the allocator in terms of blocks */
        size_t nr;       /* number of blocks allocated */
        void **blks;
@@ -63,7 +62,6 @@ struct allocator {
        size_t frees;
        size_t tmp_used; /* counter for temp usage */
 
-       int refcount;
        exception_buffer eb;
        MT_Lock lock;    /* lock for thread-safe allocations */
        bool use_lock;
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -2071,13 +2071,11 @@ ma_free_obj(allocator *sa, void *obj, si
 static void
 ma_free_blk_memory(allocator *sa, void *blk)
 {
-       if (!sa->pa) {
-               // all blks are GDKmalloc
-               size_t sz = GDKmallocated(blk) - (MALLOC_EXTRA_SPACE + 
DEBUG_SPACE);
-               assert(sz > 0);
-               GDKfree(blk);
-               sa->usedmem -= sz;
-       }
+       // all blks are GDKmalloc
+       size_t sz = GDKmallocated(blk) - (MALLOC_EXTRA_SPACE + DEBUG_SPACE);
+       assert(sz > 0);
+       GDKfree(blk);
+       sa->usedmem -= sz;
 }
 
 
@@ -2086,12 +2084,7 @@ ma_free_blk(allocator *sa, void *blk)
 {
        size_t i = ma_get_blk_idx(sa, blk, 0);
        if (i < sa->nr) {
-               if (sa->pa) {
-                       COND_LOCK_ALLOCATOR(sa->pa);
-                       ma_free_blk(sa->pa, blk);
-                       COND_UNLOCK_ALLOCATOR(sa->pa);
-               } else
-                       ma_free_blk_memory(sa, blk);
+               ma_free_blk_memory(sa, blk);
                // compact
                for (; i < sa->nr-1; i++)
                        sa->blks[i] = sa->blks[i+1];
@@ -2111,7 +2104,7 @@ ma_use_freed_obj(allocator *sa, size_t s
        int MAX_ITERATIONS = 100;
        COND_LOCK_ALLOCATOR(sa);
        freed_t *curr = sa->freelist;
-       while(curr && (cntr < MAX_ITERATIONS)) {
+       while (curr && (cntr < MAX_ITERATIONS)) {
                if (sz <= curr->sz) {
                        if (prev) {
                                prev->n = curr->n;
@@ -2153,26 +2146,13 @@ ma_reallocated(allocator *sa)
        return sa->blks != (void **) sa->first_blk;
 }
 
-static inline bool
-ma_has_dependencies(allocator *sa)
-{
-       return (sa->refcount > 0) && !sa->pa;
-}
-
-
 static inline void
 _ma_free_blks(allocator *sa, size_t start_idx)
 {
        for (size_t i = start_idx; i < sa->nr; i++) {
                void *blk = sa->blks[i];
                if (blk) {
-                       if (sa->pa) {
-                               COND_LOCK_ALLOCATOR(sa->pa);
-                               ma_free_blk(sa->pa, blk);
-                               COND_UNLOCK_ALLOCATOR(sa->pa);
-                       } else {
-                               ma_free_blk_memory(sa, blk);
-                       }
+                       ma_free_blk_memory(sa, blk);
                }
        }
        sa->nr = start_idx;
@@ -2187,13 +2167,6 @@ void
 ma_reset(allocator *sa)
 {
        COND_LOCK_ALLOCATOR(sa);
-       assert(!ma_has_dependencies(sa));
-       if (ma_has_dependencies(sa)) {
-               COND_UNLOCK_ALLOCATOR(sa);
-               if (sa->eb.enabled)
-                       eb_error(&sa->eb, "reset failed, allocator has 
dependencies", 1000);
-               return;
-       }
        // 1st block is where we live, free the rest
        _ma_free_blks(sa, 1);
 
@@ -2204,10 +2177,8 @@ ma_reset(allocator *sa)
        if (ma_reallocated(sa)) {
                void **old_blks = sa->blks;
                sa->blks = (void **) sa->first_blk;
-               if (!sa->pa) {
-                       GDKfree(old_blks);
-                       sa->usedmem -= sizeof(void *) * sa->size;
-               }
+               GDKfree(old_blks);
+               sa->usedmem -= sizeof(void *) * sa->size;
        }
 
        sa->size = MA_NUM_BLOCKS;
@@ -2216,15 +2187,12 @@ ma_reset(allocator *sa)
        sa->frees = 0;
        sa->nr = 1;
        sa->freelist = NULL;
-       sa->usedmem = MA_BLOCK_SIZE;
        sa->objects = 0;
        sa->inuse = 0;
        sa->tmp_used = 0;
        COND_UNLOCK_ALLOCATOR(sa);
 }
 
-static void * _ma_alloc_internal(allocator* sa, size_t sz);
-
 #undef ma_realloc
 void *
 ma_realloc(allocator *sa, void *p, size_t sz, size_t oldsz)
@@ -2262,24 +2230,19 @@ ma_double_num_blks(allocator *sa)
        void **tmp;
        size_t osz = sa->size;
        sa->size *= 2;
-       if (sa->pa)
-               tmp = _ma_alloc_internal(sa->pa, sizeof(void *) * sa->size);
-       else {
-               size_t bytes = sizeof(void *) * sa->size;
-               tmp = GDKmalloc(bytes);
-               sa->usedmem += bytes;
-       }
-       if (tmp) {
-               size_t bytes = sizeof(void *) * osz;
-               memcpy(tmp, sa->blks, bytes);
-               if (!sa->pa && ma_reallocated(sa)) {
-                       GDKfree(sa->blks);
-                       sa->usedmem -= bytes;
-               }
-       } else {
+       size_t bytes = sizeof(void *) * sa->size;
+       tmp = GDKmalloc(bytes);
+       if (tmp == NULL) {
                sa->size /= 2; /* undo */
                return -1;
        }
+       sa->usedmem += bytes;
+       bytes = sizeof(void *) * osz;
+       memcpy(tmp, sa->blks, bytes);
+       if (ma_reallocated(sa)) {
+               GDKfree(sa->blks);
+               sa->usedmem -= bytes;
+       }
        sa->blks = tmp;
        return 0;
 }
@@ -2300,11 +2263,7 @@ static void *
                if (sz > blk_size) {
                        blk_size = sz;
                }
-               if (sa->pa) {
-                       r = _ma_alloc_internal(sa->pa, blk_size);
-               } else {
-                       r = GDKmalloc(blk_size);
-               }
+               r = GDKmalloc(blk_size);
 
                if (r == NULL) {
                        COND_UNLOCK_ALLOCATOR(sa);
@@ -2317,8 +2276,7 @@ static void *
                        COND_UNLOCK_ALLOCATOR(sa);
                        if (sa->eb.enabled)
                                eb_error(&sa->eb, "out of memory", 1000);
-                       if (!sa->pa)
-                               GDKfree(r);
+                       GDKfree(r);
                        return NULL;
                }
                if (sz >= MA_BLOCK_SIZE && sa->nr > 1 && !ma_tmp_active(sa)) {
@@ -2356,11 +2314,10 @@ ma_alloc(allocator *sa, size_t sz)
 
 #undef create_allocator
 allocator *
-create_allocator(allocator *pa, const char *name, bool use_lock)
+create_allocator(const char *name, bool use_lock)
 {
-//     assert(pa == NULL || pa->use_lock);
        // allocator lives in the 1st blk
-       char *first_blk = (pa) ? _ma_alloc_internal(pa, MA_BLOCK_SIZE) : 
GDKmalloc(MA_BLOCK_SIZE);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to