Changeset: 1f5a6fd13a28 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1f5a6fd13a28
Modified Files:
        gdk/gdk_utils.c
        gdk/gdk_utils.h
        monetdb5/modules/mal/mal_io.c
        monetdb5/modules/mal/mal_io.h
        monetdb5/modules/mal/mal_io.mal
Branch: default
Log Message:

method to set a memory limit after which malloc fails for testing purposes


diffs (97 lines):

diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -310,6 +310,7 @@ int GDK_vm_trim = 1;
  * fall-back for other compilers. */
 #include "gdk_atomic.h"
 static volatile ATOMIC_TYPE GDK_mallocedbytes_estimate = 0;
+static volatile ATOMIC_TYPE GDK_mallocedbytes_limit = 0;
 static volatile ATOMIC_TYPE GDK_vm_cursize = 0;
 #ifdef GDK_VM_KEEPHISTO
 volatile ATOMIC_TYPE GDK_vm_nallocs[MAX_BIT] = { 0 };
@@ -1681,6 +1682,11 @@ GDKmalloc_prefixsize(size_t size)
        return s;
 }
 
+gdk_export void GDKsetmemorylimit(size_t nbytes) {
+       GDK_mallocedbytes_limit = nbytes;
+}
+
+
 /*
  * The emergency flag can be set to force a fatal error if needed.
  * Otherwise, the caller is able to deal with the lack of memory.
@@ -1698,6 +1704,13 @@ GDKmallocmax(size_t size, size_t *maxsiz
                GDKfatal("GDKmallocmax: called with size " SZFMT "", size);
 #endif
        }
+#ifndef NDEBUG
+       /* fail malloc for testing purposes depending on set limit */
+       if (GDK_mallocedbytes_limit > 0 &&
+                       (GDK_mallocedbytes_estimate + size + 
MALLOC_EXTRA_SPACE) > GDK_mallocedbytes_limit) {
+               return NULL;
+       }
+#endif
        size = (size + 7) & ~7; /* round up to a multiple of eight */
        s = GDKmalloc_prefixsize(size);
        if (s == NULL) {
@@ -1710,6 +1723,7 @@ GDKmallocmax(size_t size, size_t *maxsiz
                        }
                        GDKfatal("GDKmallocmax: failed for " SZFMT " bytes", 
size);
                } else {
+                       /* TODO why are we printing this on stderr? */
                        fprintf(stderr, "#GDKmallocmax: recovery ok. 
Continuing..\n");
                }
        }
diff --git a/gdk/gdk_utils.h b/gdk/gdk_utils.h
--- a/gdk/gdk_utils.h
+++ b/gdk/gdk_utils.h
@@ -80,6 +80,9 @@ gdk_export size_t _MT_pagesize;
 gdk_export void MT_init(void); /*  init the package. */
 gdk_export int GDKinit(opt *set, int setlen);
 
+/* used for testing only */
+gdk_export void GDKsetmemorylimit(size_t nbytes);
+
 /*
  * Upon closing the session, all persistent BATs should be saved and
  * the transient BATs should be removed.  The buffer pool manager
diff --git a/monetdb5/modules/mal/mal_io.c b/monetdb5/modules/mal/mal_io.c
--- a/monetdb5/modules/mal/mal_io.c
+++ b/monetdb5/modules/mal/mal_io.c
@@ -776,3 +776,11 @@ IOimport(void *ret, bat *bid, str *fnme)
        return MAL_SUCCEED;
 }
 
+
+
+str
+IOsetmemorylimit(void *res, lng *nbytes) {
+       (void) res;
+       GDKsetmemorylimit(*nbytes);
+       return MAL_SUCCEED;
+}
diff --git a/monetdb5/modules/mal/mal_io.h b/monetdb5/modules/mal/mal_io.h
--- a/monetdb5/modules/mal/mal_io.h
+++ b/monetdb5/modules/mal/mal_io.h
@@ -28,4 +28,7 @@ mal_export str IOimport(void *ret, bat *
 mal_export str io_stdin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
 mal_export str io_stdout(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
 mal_export str io_stderr(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
+
+mal_export str IOsetmemorylimit(void *res, lng *nbytes);
+
 #endif /* _PRINT_H_ */
diff --git a/monetdb5/modules/mal/mal_io.mal b/monetdb5/modules/mal/mal_io.mal
--- a/monetdb5/modules/mal/mal_io.mal
+++ b/monetdb5/modules/mal/mal_io.mal
@@ -60,3 +60,8 @@ address IOimport
 comment "Import a BAT from an ASCII dump. The tuples are appended to the
  first argument. Its signature must match the dump,
  else parsing errors will occur as an exception.";
+
+# used for testing malloc failures
+command setmemorylimit(nbytes:lng):void
+address IOsetmemorylimit
+comment "Set memory limit for testing";
\ No newline at end of file
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to