Changeset: 1377b20aad9d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/1377b20aad9d
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk_system_private.h
        gdk/gdk_utils.c
        gdk/gdk_utils.h
        monetdb5/optimizer/opt_aliases.c
Branch: Dec2025
Log Message:

Slight code rearrangement; use void * instead of char *; unexport GDKmallocated.


diffs (truncated from 1440 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
@@ -304,7 +304,6 @@ ATOMIC_FLAG GDKlocklistlock;
 ATOMIC_TYPE GDKlocksleepcnt;
 void GDKlockstatistics(int);
 void *GDKmalloc(size_t size) __attribute__((__malloc__)) 
__attribute__((__malloc__(GDKfree, 1))) __attribute__((__alloc_size__(1))) 
__attribute__((__warn_unused_result__));
-size_t GDKmallocated(const void *s);
 size_t GDKmem_cursize(void);
 gdk_return GDKmergeidx(BAT *b, BAT **a, int n_ar);
 void *GDKmmap(const char *path, int mode, size_t len) 
__attribute__((__warn_unused_result__));
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
@@ -52,8 +52,8 @@ struct allocator {
        struct allocator *pa;
        size_t size;     /* size of the allocator in terms of blocks */
        size_t nr;       /* number of blocks allocated */
-       char **blks;
-       char *first_blk;
+       void **blks;
+       void *first_blk;
        size_t used;     /* memory used in last block */
        size_t usedmem;  /* total used memory */
        size_t objects;  /* number of objects */
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1685,686 +1685,6 @@ GDKprintinforegister(void (*func)(void))
 #define DEBUG_SPACE    16
 #endif
 
-#define MA_NUM_BLOCKS 64
-#define MA_BLOCK_SIZE (128*1024)
-#define MA_HEADER_SIZE (2*(sizeof(size_t)))
-#define CANARY_VALUE ((size_t)0xDEADBEEFDEADBEEF)
-#define round16(sz) ((sz+15)&~15)
-#define round_block_size(sz) ((sz + (MA_BLOCK_SIZE - 1))&~(MA_BLOCK_SIZE - 1))
-
-#define COND_LOCK_ALLOCATOR(a)                 \
-       bool __alloc_locked = false;            \
-       if ((a)->use_lock) {                    \
-               MT_lock_set(&(a)->lock);        \
-               __alloc_locked = true;          \
-       }
-
-#define COND_UNLOCK_ALLOCATOR(a)               \
-       if (__alloc_locked) {                   \
-               MT_lock_unset(&(a)->lock);      \
-       }
-
-
-typedef struct freed_t {
-       size_t sz;
-       struct freed_t *n;
-} freed_t;
-
-
-static inline size_t
-ma_get_blk_idx(allocator *sa, void *blk, size_t offset)
-{
-       for (size_t i = offset; i < sa->nr; i++) {
-               if (sa->blks[i] == blk)
-                       return i;
-       }
-       assert(0 && "allocator block not found");
-       if (sa->eb.enabled) {
-               eb_error(&sa->eb, "allocator block not found", 1000);
-       }
-       return sa->nr;
-}
-
-
-static void
-ma_free_obj(allocator *sa, void *obj, size_t sz)
-{
-       //size_t i;
-
-       //char *obj_start = (char *) obj;
-       //char *obj_end = obj_start + sz;
-
-       //// find the block this object belongs to
-       //for(i = 0; i < sa->nr; i++) {
-       //      char * blk_start = (char *) sa->blks[i];
-       //      char * blk_end = blk_start + MA_BLOCK_SIZE;
-       //      if ((obj_start >= blk_start) && (obj_end <= blk_end))
-       //              break;
-       //}
-       //assert (i < sa->nr);
-       freed_t *f = obj;
-       f->sz = sz;
-       f->n = sa->freelist;
-       sa->freelist = f;
-       if (sa->inuse > 0)
-               sa->inuse -= 1;
-}
-
-/*
- * Put regular blks of size MA_BLOCK_SIZE on freelist_blks
- * all others are GDKfree
- */
-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);
-               if (sz == MA_BLOCK_SIZE) {
-                       freed_t *f = blk;
-                       f->sz = sz;
-                       f->n = sa->freelist_blks;
-                       sa->freelist_blks = f;
-               } else {
-                       GDKfree(blk);
-                       sa->usedmem -= sz;
-               }
-       }
-}
-
-
-static void
-ma_free_blk(allocator *sa, void *blk)
-{
-       size_t i = ma_get_blk_idx(sa, blk, 0);
-       if (i < sa->nr) {
-               if (sa->pa)
-                       ma_free_blk(sa->pa, blk);
-               else
-                       ma_free_blk_memory(sa, blk);
-               // compact
-               for (; i < sa->nr-1; i++)
-                       sa->blks[i] = sa->blks[i+1];
-               sa->nr--;
-       }
-}
-
-
-/*
- * Return first slot that will fit the size
- */
-static void *
-ma_use_freed_obj(allocator *sa, size_t sz)
-{
-       freed_t *prev = NULL;
-       int cntr = 0;
-       int MAX_ITERATIONS = 100;
-       COND_LOCK_ALLOCATOR(sa);
-       freed_t *curr = sa->freelist;
-       while(curr && (cntr < MAX_ITERATIONS)) {
-               if (sz <= curr->sz) {
-                       if (prev) {
-                               prev->n = curr->n;
-                       } else {
-                               sa->freelist = curr->n;
-                       }
-                       sa->free_obj_hits += 1;
-                       sa->inuse += 1;
-                       COND_UNLOCK_ALLOCATOR(sa);
-                       return curr;
-               } else {
-                       prev = curr;
-                       curr = curr->n;
-               }
-               cntr += 1;
-       }
-       COND_UNLOCK_ALLOCATOR(sa);
-       return NULL;
-}
-
-static int ma_double_num_blks(allocator *sa);
-
-
-/*
- * Free blocks are maintained at top level
- */
-static void *
-ma_use_freed_blk(allocator *sa, size_t sz)
-{
-       if (sa->pa)
-               return ma_use_freed_blk(sa->pa, sz);
-       COND_LOCK_ALLOCATOR(sa);
-       if (sa->freelist_blks && (sz == MA_BLOCK_SIZE)) {
-               if (sa->nr >= sa->size && ma_double_num_blks(sa) < 0) {
-                       COND_UNLOCK_ALLOCATOR(sa);
-                       if (sa->eb.enabled)
-                               eb_error(&sa->eb, "out of memory", 1000);
-                       return NULL;
-               }
-               freed_t *f = sa->freelist_blks;
-               sa->freelist_blks = f->n;
-               sa->used = MA_BLOCK_SIZE;
-               sa->blks[sa->nr] = (char*)f;
-               sa->nr ++;
-               sa->free_blk_hits += 1;
-               COND_UNLOCK_ALLOCATOR(sa);
-               return f;
-       }
-       COND_UNLOCK_ALLOCATOR(sa);
-       return NULL;
-}
-
-
-static void *
-ma_use_freed(allocator *sa, size_t sz)
-{
-       if (sz < MA_BLOCK_SIZE) {
-               return ma_use_freed_obj(sa, sz);
-       }
-       if (sz == MA_BLOCK_SIZE) {
-               return ma_use_freed_blk(sa, sz);
-       }
-       return NULL;
-}
-
-static inline bool
-ma_reallocated(allocator *sa)
-{
-       return sa->blks != (char **)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++) {
-               char *blk = sa->blks[i];
-               if (blk) {
-                       if (sa->pa) {
-                               ma_free_blk(sa->pa, blk);
-                       } else {
-                               ma_free_blk_memory(sa, blk);
-                       }
-               }
-       }
-       sa->nr = start_idx;
-}
-
-
-/*
- * Reset allocator to initial state
- */
-#undef ma_reset
-allocator *
-ma_reset(allocator *sa)
-{
-       COND_LOCK_ALLOCATOR(sa);
-       assert(!ma_has_dependencies(sa));
-       if (ma_has_dependencies(sa)) {
-               if (sa->eb.enabled)
-                       eb_error(&sa->eb, "reset failed, allocator has 
dependencies", 1000);
-               return sa;
-       }
-       // 1st block is where we live, free the rest
-       _ma_free_blks(sa, 1);
-
-       // compute start offset
-       size_t offset = round16(sizeof(char*) * MA_NUM_BLOCKS) +
-               round16(sizeof(allocator));
-       // If reallocated, we need to restore original layout
-       if (ma_reallocated(sa)) {
-               char **old_blks = sa->blks;
-               sa->blks = (char **)sa->first_blk;
-               if (!sa->pa) {
-                   GDKfree(old_blks);
-                   sa->usedmem -= sizeof(char*) * sa->size;
-               }
-       }
-
-       sa->size = MA_NUM_BLOCKS;
-       sa->blks[0] = sa->first_blk;
-       sa->used = offset;
-       sa->frees = 0;
-       sa->nr = 1;
-       // reset freelist only i.e. leave freelist_blks alone as
-       // it may have blocks we can re-use
-       sa->freelist = NULL;
-       sa->usedmem = MA_BLOCK_SIZE;
-       sa->objects = 0;
-       sa->inuse = 0;
-       sa->tmp_used = 0;
-       COND_UNLOCK_ALLOCATOR(sa);
-       return 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)
-{
-       void *r = ma_alloc(sa, sz);
-
-       if (r)
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to