Changeset: f854d9b111a0 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f854d9b111a0
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_utils.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_client.h
Branch: resource_management
Log Message:

customize allocator constructor with blk size and clean up


diffs (273 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
@@ -486,6 +486,7 @@ BAT *canditer_slice2(const struct candit
 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);
 int closedir(DIR *dir);
+allocator *create_allocator(allocator *pa, size_t blk_size, size_t num_blks);
 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__));
@@ -585,7 +586,6 @@ struct dirent *readdir(DIR *dir);
 void rewinddir(DIR *dir);
 void *sa_alloc(allocator *sa, size_t sz);
 void sa_close(allocator *sa);
-allocator *sa_create(allocator *pa);
 void sa_destroy(allocator *sa);
 void sa_free(allocator *sa, void *);
 void sa_open(allocator *sa);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2527,11 +2527,11 @@ typedef struct allocator {
        size_t used;     /* memory used in last block */
        size_t usedmem;  /* total used memory */
        size_t blk_size; /* size of the last allocated block */
+       size_t dflt_blk_size; /* size of initial allocated block */
        size_t objects;  /* number of objects */
        size_t inuse;    /* number of objects in use*/
        size_t freelist_hits; /* number of object reuse*/
        void *freelist; /* list of freed objects */
-       void *freelist_blks;    /* list of freed blks */
        size_t frees;
 
        size_t tmp_used; /* keeps total of tmp allocated bytes */
@@ -2544,7 +2544,8 @@ gdk_export ValPtr VALcopy(allocator *va,
 gdk_export ValPtr VALinit(allocator *va, ValPtr d, int tpe, const void *s)
        __attribute__((__access__(write_only, 1)));
 
-gdk_export allocator *sa_create( allocator *pa );
+gdk_export allocator *create_allocator( allocator *pa, size_t blk_size,
+               size_t num_blks );
 gdk_export allocator *sa_reset( allocator *sa );
 gdk_export void *sa_alloc( allocator *sa,  size_t sz );
 gdk_export void *sa_zalloc( allocator *sa,  size_t sz );
@@ -2558,6 +2559,10 @@ gdk_export void sa_open( allocator *sa )
 gdk_export void sa_close( allocator *sa ); /* close temporary frame, reset to 
old state */
 gdk_export void sa_free( allocator *sa, void *);
 
+#define ALLOC_DEFAULT_NUM_BLOCKS 64
+#define ALLOC_DEFAULT_BLOCK_SIZE (128*1024)
+
+#define sa_create(pa)          create_allocator(pa, ALLOC_DEFAULT_BLOCK_SIZE, 
ALLOC_DEFAULT_NUM_BLOCKS)
 #define ma_create(pa)          sa_create(pa)
 #define ma_destroy(ma)         sa_destroy(ma)
 #define ma_alloc(ma, sz)       (void*)sa_alloc(ma, sz)
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -2106,7 +2106,7 @@ sa_free_obj(allocator *pa, void *obj, si
        //// find the block this object belongs to
        //for(i = 0; i < pa->nr; i++) {
        //      char * blk_start = (char *) pa->blks[i];
-       //      char * blk_end = blk_start + SA_BLOCK_SIZE;
+       //      char * blk_end = blk_start + SA_BLOCK_SIZE; // FIX if size 
doubles
        //      if ((obj_start >= blk_start) && (obj_end <= blk_end))
        //              break;
        //}
@@ -2120,55 +2120,6 @@ sa_free_obj(allocator *pa, void *obj, si
                pa->inuse -= 1;
 }
 
-//static void
-//sa_free_blk(allocator *sa, void *blk)
-//{
-//     // blks are maintained on the root allocator
-//     if (sa->pa == NULL) {
-//             size_t i;
-//
-//             for(i = 0; i < sa->nr; i++) {
-//                     if (sa->blks[i] == blk)
-//                             break;
-//             }
-//             assert (i < sa->nr);
-//
-//             GDKfree(blk);
-//             for (; i < sa->nr-1; i++)
-//                     sa->blks[i] = sa->blks[i+1];
-//             sa->nr--;
-//     }
-//     // free blks are maintained on the root allocator
-//     //if (pa->pa) {
-//     //      sa_free_blk(pa->pa, blk);
-//     //} else {
-//     //      size_t i;
-//
-//     //      for(i = 0; i < pa->nr; i++) {
-//     //              if (pa->blks[i] == blk)
-//     //                      break;
-//     //      }
-//     //      assert (i < pa->nr);
-//
-//     //      // size_t sz = GDKmallocated(blk);
-//     //      GDKfree(blk);
-//     //      for (; i < pa->nr-1; i++)
-//     //              pa->blks[i] = pa->blks[i+1];
-//     //      pa->nr--;
-//     //      //if (sz > (SA_BLOCK_SIZE)) {
-//     //      //      GDKfree(blk);
-//     //      //      for (; i < pa->nr-1; i++)
-//     //      //              pa->blks[i] = pa->blks[i+1];
-//     //      //      pa->nr--;
-//     //      //} else {
-//     //      //      freed_t *f = blk;
-//     //      //      f->sz = sz;
-//     //      //      f->n = pa->freelist_blks;
-//     //      //      pa->freelist_blks = f;
-//     //      //}
-//     //}
-//}
-//
 
 /*
  * Return first slot that will fit the size
@@ -2199,28 +2150,13 @@ sa_use_freed_obj(allocator *pa, size_t s
        return NULL;
 }
 
-static void *
-sa_use_freed_blk(allocator *pa, size_t sz)
-{
-       if (pa->pa)
-               return sa_use_freed_blk(pa->pa, sz);
-       if (pa->freelist_blks && (sz == SA_BLOCK_SIZE)) {
-               freed_t *f = pa->freelist_blks;
-               pa->freelist_blks = f->n;
-               return f;
-       }
-       return NULL;
-}
 
 static void *
 sa_use_freed(allocator *pa, size_t sz)
 {
-       if ((sz < SA_BLOCK_SIZE) && pa->freelist) {
+       if ((sz < pa->dflt_blk_size) && pa->freelist) {
                return sa_use_freed_obj(pa, sz);
        }
-       if ((sz == SA_BLOCK_SIZE) && pa->freelist_blks) {
-               return sa_use_freed_blk(pa, sz);
-       }
        return NULL;
 }
 
@@ -2235,13 +2171,12 @@ allocator *sa_reset(allocator *sa)
                sa->blks[i] = NULL;
        }
 
-       sa->freelist_blks = NULL;
        sa->frees = 0;
        sa->nr = 1;
        sa->used = 0;
        sa->freelist = NULL;
-       sa->usedmem = SA_BLOCK_SIZE;
-       sa->blk_size = SA_BLOCK_SIZE;
+       sa->usedmem = sa->dflt_blk_size;
+       sa->blk_size = sa->dflt_blk_size;
        sa->objects = 0;
        sa->inuse = 0;
        return sa;
@@ -2322,18 +2257,6 @@ static void *
                sa->nr ++;
                sa->used = sz;
                sa->usedmem += sa->blk_size;
-               //if (sz > SA_BLOCK_SIZE) {
-               //      // move odd size blks behind
-               //      sa->blks[sa->nr] = sa->blks[sa->nr-1];
-               //      sa->blks[sa->nr-1] = r;
-               //      sa->nr ++;
-               //      sa->usedmem += sz;
-               //} else {
-               //      sa->blks[sa->nr] = r;
-               //      sa->nr ++;
-               //      sa->used = sz;
-               //      sa->usedmem += SA_BLOCK_SIZE;
-               //}
        } else {
                r = sa->blks[sa->nr-1] + sa->used;
                sa->used += sz;
@@ -2361,19 +2284,19 @@ sa_alloc(allocator *sa, size_t sz)
 
 
 allocator *
-sa_create(allocator *pa)
+create_allocator(allocator *pa, size_t blk_size, size_t num_blks)
 {
        allocator *sa = (pa)? (allocator*)_sa_alloc_internal(pa, 
sizeof(allocator)) : (allocator*)GDKmalloc(sizeof(allocator));
        if (sa == NULL)
                return NULL;
-       sa->size = SA_NUM_BLOCKS;
+       sa->size = num_blks;
        sa->blks = (pa)? (char**)_sa_alloc_internal(pa, sizeof(char*) * 
sa->size) : (char**)GDKmalloc(sizeof(char*) * sa->size);
        if (sa->blks == NULL) {
                if (!pa)
                        GDKfree(sa);
                return NULL;
        }
-       sa->blks[0] = (pa)? (char*)_sa_alloc_internal(pa, SA_BLOCK_SIZE) : 
(char*)GDKmalloc(SA_BLOCK_SIZE);
+       sa->blks[0] = (pa)? (char*)_sa_alloc_internal(pa, blk_size) : 
(char*)GDKmalloc(blk_size);
        if (sa->blks[0] == NULL) {
                if (!pa) {
                        GDKfree(sa->blks);
@@ -2384,10 +2307,10 @@ sa_create(allocator *pa)
        eb_init(&sa->eb);
        sa->pa = pa;
        sa->nr = 1;
-       sa->usedmem = SA_BLOCK_SIZE;
-       sa->blk_size = SA_BLOCK_SIZE;
+       sa->usedmem = blk_size;
+       sa->blk_size = blk_size;
+       sa->dflt_blk_size = blk_size;
        sa->freelist = NULL;
-       sa->freelist_blks = NULL;
        sa->frees = 0;
        sa->used = 0;
        sa->objects = 0;
@@ -2412,11 +2335,6 @@ sa_zalloc( allocator *sa, size_t sz )
 void
 sa_destroy( allocator *sa )
 {
-       //if (sa->pa) {
-       //      sa_reset(sa);
-       //      sa_free_blk(sa->pa, sa->blks[0]);
-       //      return;
-       //}
        if (sa->pa == NULL) {
                // root allocator
                for (size_t i = 0; i < sa->nr; i++) {
@@ -2494,9 +2412,6 @@ sa_free(allocator *sa, void *obj)
        // double free check point
        assert(*((size_t *) ptr + 1) == CANARY_VALUE);
        sa->frees++;
-       if (sz < SA_BLOCK_SIZE) {
+       if (sz < sa->dflt_blk_size)
                sa_free_obj(sa, ptr, sz);
-       //} else {
-               // sa_free_blk(sa, ptr);
-       }
 }
diff --git a/monetdb5/mal/mal_client.c b/monetdb5/mal/mal_client.c
--- a/monetdb5/mal/mal_client.c
+++ b/monetdb5/mal/mal_client.c
@@ -283,7 +283,8 @@ MCinitClientRecord(Client c, oid user, b
        c->filetrans = false;
        c->handshake_options = NULL;
        c->query = NULL;
-       c->ta = ma_create(NULL);
+       c->alloc = ma_create(NULL);
+       c->ta = ma_create(c->alloc);
 
        char name[MT_NAME_LEN];
        snprintf(name, sizeof(name), "Client%d->s", (int) (c - mal_clients));
diff --git a/monetdb5/mal/mal_client.h b/monetdb5/mal/mal_client.h
--- a/monetdb5/mal/mal_client.h
+++ b/monetdb5/mal/mal_client.h
@@ -160,6 +160,7 @@ typedef struct CLIENT {
        MalStkPtr glb;                          /* global variable stack */
 
        allocator *ta;                          /* temporary allocator used 
during parsing and optimization */
+       allocator *alloc;                       /* main client allocator */
        /*
         * Here are pointers to scenario backends contexts.  For the time
         * being just SQL.  We need a pointer for each of them, since they
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to