Changeset: f1217572396a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f1217572396a
Removed Files:
        sql/common/exception_buffer.c
        sql/common/sql_mem.c
        sql/include/exception_buffer.h
Modified Files:
        MonetDB.spec
        debian/monetdb5-sql-dev.install
        gdk/gdk.h
        gdk/gdk_utils.c
        sql/backends/monet5/CMakeLists.txt
        sql/common/CMakeLists.txt
        sql/include/CMakeLists.txt
        sql/include/sql_mem.h
Branch: no_type_bat
Log Message:

moved allocator and exception_buffer into gdk.h/gdk_utils.c


diffs (truncated from 864 to 300 lines):

diff --git a/MonetDB.spec b/MonetDB.spec
--- a/MonetDB.spec
+++ b/MonetDB.spec
@@ -631,7 +631,6 @@ This package contains files needed to de
 
 %files SQL-server5-devel
 %defattr(-,root,root)
-%{_includedir}/monetdb/exception_buffer.h
 %{_includedir}/monetdb/opt_backend.h
 %{_includedir}/monetdb/rel_*.h
 %{_includedir}/monetdb/sql*.h
diff --git a/debian/monetdb5-sql-dev.install b/debian/monetdb5-sql-dev.install
--- a/debian/monetdb5-sql-dev.install
+++ b/debian/monetdb5-sql-dev.install
@@ -1,4 +1,3 @@
-debian/tmp/usr/include/monetdb/exception_buffer.h usr/include/monetdb
 debian/tmp/usr/include/monetdb/opt_backend.h usr/include/monetdb
 debian/tmp/usr/include/monetdb/rel_*.h usr/include/monetdb
 debian/tmp/usr/include/monetdb/sql*.h usr/include/monetdb
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2464,4 +2464,106 @@ gdk_export gdk_return gdk_add_callback(c
                *argv[], int interval);
 gdk_export gdk_return gdk_remove_callback(char *, gdk_callback_func *f);
 
+
+#include <setjmp.h>
+
+typedef struct exception_buffer {
+#ifdef HAVE_SIGLONGJMP
+       sigjmp_buf state;
+#else
+       jmp_buf state;
+#endif
+       int code;
+       char *msg;
+       int enabled;
+} exception_buffer;
+
+extern exception_buffer *eb_init(exception_buffer *eb);
+
+/* != 0 on when we return to the savepoint */
+#ifdef HAVE_SIGLONGJMP
+#define eb_savepoint(eb) ((eb)->enabled = 1, sigsetjmp((eb)->state, 0))
+#else
+#define eb_savepoint(eb) ((eb)->enabled = 1, setjmp((eb)->state))
+#endif
+extern _Noreturn void eb_error(exception_buffer *eb, char *msg, int val);
+
+typedef struct allocator {
+       struct allocator *pa;
+       size_t size;
+       size_t nr;
+       char **blks;
+       size_t used;    /* memory used in last block */
+       size_t usedmem; /* used memory */
+       void *freelist; /* list of freed blocks */
+       exception_buffer eb;
+} allocator;
+
+gdk_export allocator *sa_create( allocator *pa );
+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 );
+extern void *sa_realloc( allocator *sa,  void *ptr, size_t sz, size_t osz );
+extern void sa_destroy( allocator *sa );
+extern char *sa_strndup( allocator *sa, const char *s, size_t l);
+gdk_export char *sa_strdup( allocator *sa, const char *s);
+extern char *sa_strconcat( allocator *sa, const char *s1, const char *s2);
+extern size_t sa_size( allocator *sa );
+
+#if !defined(NDEBUG) && !defined(__COVERITY__) && defined(__GNUC__)
+#define sa_alloc(sa, sz)                                       \
+       ({                                                      \
+               allocator *_sa = (sa);                  \
+               size_t _sz = (sz);                              \
+               void *_res = sa_alloc(_sa, _sz);                \
+               TRC_DEBUG(ALLOC,                                \
+                               "sa_alloc(%p,%zu) -> %p\n",     \
+                               _sa, _sz, _res);        \
+               _res;                                           \
+       })
+#define sa_zalloc(sa, sz)                                      \
+       ({                                                      \
+               allocator *_sa = (sa);                  \
+               size_t _sz = (sz);                              \
+               void *_res = sa_zalloc(_sa, _sz);               \
+               TRC_DEBUG(ALLOC,                                \
+                               "sa_zalloc(%p,%zu) -> %p\n",    \
+                               _sa, _sz, _res);        \
+               _res;                                           \
+       })
+#define sa_realloc(sa, ptr, sz, osz)                                   \
+       ({                                                              \
+               allocator *_sa = (sa);                          \
+               void *_ptr = (ptr);                                     \
+               size_t _sz = (sz);                                      \
+               size_t _osz = (osz);                                    \
+               void *_res = sa_realloc(_sa, _ptr, _sz, _osz);          \
+               TRC_DEBUG(ALLOC,                                \
+                               "sa_realloc(%p,%p,%zu,%zu) -> %p\n",    \
+                               _sa, _ptr, _sz, _osz, _res);            \
+               _res;                                                   \
+       })
+#define sa_strdup(sa, s)                                       \
+       ({                                                      \
+               allocator *_sa = (sa);                  \
+               const char *_s = (s);                           \
+               char *_res = sa_strdup(_sa, _s);                \
+               TRC_DEBUG(ALLOC,                                \
+                               "sa_strdup(%p,len=%zu) -> %p\n",        \
+                               _sa, strlen(_s), _res); \
+               _res;                                           \
+       })
+#define sa_strndup(sa, s, l)                                   \
+       ({                                                      \
+               allocator *_sa = (sa);                  \
+               const char *_s = (s);                           \
+               size_t _l = (l);                                \
+               char *_res = sa_strndup(_sa, _s, _l);           \
+               TRC_DEBUG(ALLOC,                                \
+                               "sa_strndup(%p,len=%zu) -> %p\n",       \
+                               _sa, _l, _res);         \
+               _res;                                           \
+       })
+#endif
+
 #endif /* _GDK_H_ */
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -2088,3 +2088,252 @@ GDKprintinfo(void)
        for (struct prinfocb *p = prinfocb; p; p = p->next)
                (*p->func)();
 }
+
+exception_buffer *
+eb_init(exception_buffer *eb)
+{
+       if (eb) {
+               eb->enabled = 0;
+               eb->code = 0;
+               eb->msg = NULL;
+       }
+       return eb;
+}
+
+void
+eb_error( exception_buffer *eb, char *msg, int val )
+{
+       eb->code = val;
+       eb->msg = msg;
+       eb->enabled = 0;                        /* not any longer... */
+#ifdef HAVE_SIGLONGJMP
+       siglongjmp(eb->state, eb->code);
+#else
+       longjmp(eb->state, eb->code);
+#endif
+}
+
+#define SA_BLOCK (64*1024)
+
+typedef struct freed_t {
+       struct freed_t *n;
+       size_t sz;
+} freed_t;
+
+static void
+sa_destroy_freelist( freed_t *f )
+{
+       while(f) {
+               freed_t *n = f->n;
+               GDKfree(f);
+               f = n;
+       }
+}
+
+static void
+sa_free(allocator *pa, void *blk)
+{
+       assert(!pa->pa);
+       size_t i;
+
+       for(i = 0; i < pa->nr; i++) {
+               if (pa->blks[i] == blk)
+                       break;
+       }
+       assert (i < pa->nr);
+       for (; i < pa->nr-1; i++)
+               pa->blks[i] = pa->blks[i+1];
+       pa->nr--;
+
+       size_t sz = GDKmallocated(blk);
+       if (sz > (SA_BLOCK + 32)) {
+               GDKfree(blk);
+       } else {
+               freed_t *f = blk;
+               f->n = pa->freelist;
+
+               pa->freelist = f;
+       }
+}
+
+static void *
+sa_use_freed(allocator *pa, size_t sz)
+{
+       (void)sz;
+
+       freed_t *f = pa->freelist;
+       pa->freelist = f->n;
+       return f;
+}
+
+allocator *
+sa_create(allocator *pa)
+{
+       allocator *sa = (pa)?(allocator*)sa_alloc(pa, 
sizeof(allocator)):(allocator*)GDKmalloc(sizeof(allocator));
+       if (sa == NULL)
+               return NULL;
+       eb_init(&sa->eb);
+       sa->pa = pa;
+       sa->size = 64;
+       sa->nr = 1;
+       sa->blks = pa?(char**)sa_alloc(pa, sizeof(char*) * 
sa->size):(char**)GDKmalloc(sizeof(char*) * sa->size);
+       sa->freelist = NULL;
+       if (sa->blks == NULL) {
+               if (!pa)
+                       GDKfree(sa);
+               return NULL;
+       }
+       sa->blks[0] = pa?(char*)sa_alloc(pa, 
SA_BLOCK):(char*)GDKmalloc(SA_BLOCK);
+       sa->usedmem = SA_BLOCK;
+       if (sa->blks[0] == NULL) {
+               if (!pa)
+                       GDKfree(sa->blks);
+               if (!pa)
+                       GDKfree(sa);
+               return NULL;
+       }
+       sa->used = 0;
+       return sa;
+}
+
+allocator *sa_reset( allocator *sa )
+{
+       size_t i ;
+
+       for (i = 1; i<sa->nr; i++) {
+               if (!sa->pa)
+                       GDKfree(sa->blks[i]);
+               else
+                       sa_free(sa->pa, sa->blks[i]);
+       }
+       sa->nr = 1;
+       sa->used = 0;
+       sa->usedmem = SA_BLOCK;
+       return sa;
+}
+
+#undef sa_realloc
+#undef sa_alloc
+void *
+sa_realloc( allocator *sa, void *p, size_t sz, size_t oldsz )
+{
+       void *r = sa_alloc(sa, sz);
+
+       if (r)
+               memcpy(r, p, oldsz);
+       return r;
+}
+
+#define round16(sz) ((sz+15)&~15)
+void *
+sa_alloc( allocator *sa, size_t sz )
+{
+       char *r;
+       sz = round16(sz);
+       if (sz > (SA_BLOCK-sa->used)) {
+               if (sa->pa)
+                       r = (char*)sa_alloc(sa->pa, (sz > SA_BLOCK ? sz : 
SA_BLOCK));
+           else if (sz <= SA_BLOCK && sa->freelist) {
+                       r = sa_use_freed(sa, SA_BLOCK);
+               } else
+                       r = GDKmalloc(sz > SA_BLOCK ? sz : SA_BLOCK);
+               if (r == NULL) {
+                       if (sa->eb.enabled)
+                               eb_error(&sa->eb, "out of memory", 1000);
+                       return NULL;
+               }
+               if (sa->nr >= sa->size) {
+                       char **tmp;
+                       size_t osz = sa->size;
+                       sa->size *=2;
+                       if (sa->pa)
+                               tmp = (char**)sa_realloc(sa->pa, sa->blks, 
sizeof(char*) * sa->size, sizeof(char*) * osz);
+                       else
+                               tmp = GDKrealloc(sa->blks, sizeof(char*) * 
sa->size);
+                       if (tmp == NULL) {
+                               sa->size /= 2; /* undo */
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to