Changeset: accff0db71ec for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/accff0db71ec
Modified Files:
gdk/gdk_utils.c
Branch: resource_management
Log Message:
Rearrange code (i.e. move) to make debug output (esp. -d[1<<26]) more useful.
diffs (truncated from 1027 to 300 lines):
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1651,360 +1651,6 @@ GDKlibversion(void)
return GDK_VERSION;
}
-inline size_t
-GDKmem_cursize(void)
-{
- /* RAM/swapmem that Monet is really using now */
- return (size_t) ATOMIC_GET(&GDK_mallocedbytes_estimate);
-}
-
-inline size_t
-GDKvm_cursize(void)
-{
- /* current Monet VM address space usage */
- return (size_t) ATOMIC_GET(&GDK_vm_cursize) + GDKmem_cursize();
-}
-
-#define heapinc(_memdelta) \
- ATOMIC_ADD(&GDK_mallocedbytes_estimate, _memdelta)
-#ifndef NDEBUG
-#define heapdec(_memdelta)
\
- do { \
- ATOMIC_BASE_TYPE old = ATOMIC_SUB(&GDK_mallocedbytes_estimate,
_memdelta); \
- assert(old >= (ATOMIC_BASE_TYPE) _memdelta); \
- } while (0)
-#else
-#define heapdec(_memdelta) \
- ATOMIC_SUB(&GDK_mallocedbytes_estimate, _memdelta)
-#endif
-
-#define meminc(vmdelta)
\
- ATOMIC_ADD(&GDK_vm_cursize, SEG_SIZE(vmdelta))
-#ifndef NDEBUG
-#define memdec(vmdelta)
\
- do { \
- ssize_t diff = SEG_SIZE(vmdelta); \
- ATOMIC_BASE_TYPE old = ATOMIC_SUB(&GDK_vm_cursize, diff); \
- assert(old >= (ATOMIC_BASE_TYPE) diff); \
- } while (0)
-#else
-#define memdec(vmdelta)
\
- ATOMIC_SUB(&GDK_vm_cursize, SEG_SIZE(vmdelta))
-#endif
-
-/* Memory allocation
- *
- * The functions GDKmalloc, GDKzalloc, GDKrealloc, GDKstrdup, and
- * GDKfree are used throughout to allocate and free memory. These
- * functions are almost directly mapped onto the system
- * malloc/realloc/free functions, but they give us some extra
- * debugging hooks.
- *
- * When allocating memory, we allocate a bit more than was asked for.
- * The extra space is added onto the front of the memory area that is
- * returned, and in debug builds also some at the end. The area in
- * front is used to store the actual size of the allocated area. The
- * most important use is to be able to keep statistics on how much
- * memory is being used. In debug builds, the size is also used to
- * make sure that we don't write outside of the allocated arena. This
- * is also where the extra space at the end comes in.
- */
-
-/* we allocate extra space and return a pointer offset by this amount */
-#define MALLOC_EXTRA_SPACE (2 * SIZEOF_VOID_P)
-
-#if defined(NDEBUG) || defined(SANITIZER)
-#define DEBUG_SPACE 0
-#else
-#define DEBUG_SPACE 16
-#endif
-
-/* malloc smaller than this aren't subject to the GDK_vm_maxsize test */
-#define SMALL_MALLOC 256
-
-static void *
-GDKmalloc_internal(size_t size, bool clear)
-{
- void *s;
- size_t nsize;
-
- assert(size != 0);
-#ifndef SIZE_CHECK_IN_HEAPS_ONLY
- if (size > SMALL_MALLOC &&
- GDKvm_cursize() + size >= GDK_vm_maxsize &&
- !MT_thread_override_limits()) {
- GDKerror("allocating too much memory\n");
- return NULL;
- }
-#endif
-
- /* pad to multiple of eight bytes and add some extra space to
- * write real size in front; when debugging, also allocate
- * extra space for check bytes */
- nsize = (size + 7) & ~7;
- if (clear)
- s = calloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE, 1);
- else
- s = malloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
- if (s == NULL) {
- GDKsyserror("malloc failed; memory requested: %zu, memory in
use: %zu, virtual memory in use: %zu\n", size, GDKmem_cursize(),
GDKvm_cursize());;
- return NULL;
- }
- s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
-
- heapinc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
-
- /* just before the pointer that we return, write how much we
- * asked of malloc */
- ((size_t *) s)[-1] = nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE;
-#if !defined(NDEBUG) && !defined(SANITIZER)
- /* just before that, write how much was asked of us */
- ((size_t *) s)[-2] = size;
- /* write pattern to help find out-of-bounds writes */
- memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
-#endif
- return s;
-}
-
-#undef GDKmalloc
-void *
-GDKmalloc(size_t size)
-{
- void *s;
-
- if ((s = GDKmalloc_internal(size, false)) == NULL)
- return NULL;
-#if !defined(NDEBUG) && !defined(SANITIZER)
- /* write a pattern to help make sure all data is properly
- * initialized by the caller */
- DEADBEEFCHK memset(s, '\xBD', size);
-#endif
- return s;
-}
-
-#undef GDKzalloc
-void *
-GDKzalloc(size_t size)
-{
- return GDKmalloc_internal(size, true);
-}
-
-#undef GDKstrdup
-char *
-GDKstrdup(const char *s)
-{
- size_t size;
- char *p;
-
- if (s == NULL)
- return NULL;
- size = strlen(s) + 1;
-
- if ((p = GDKmalloc_internal(size, false)) == NULL)
- return NULL;
- memcpy(p, s, size); /* including terminating NULL byte */
- return p;
-}
-
-#undef GDKstrndup
-char *
-GDKstrndup(const char *s, size_t size)
-{
- char *p;
-
- if (s == NULL)
- return NULL;
- if ((p = GDKmalloc_internal(size + 1, false)) == NULL)
- return NULL;
- if (size > 0)
- memcpy(p, s, size);
- p[size] = '\0'; /* make sure it's NULL terminated */
- return p;
-}
-
-#undef GDKfree
-void
-GDKfree(void *s)
-{
- size_t asize;
-
- if (s == NULL)
- return;
-
- asize = ((size_t *) s)[-1]; /* how much allocated last */
-
-#if !defined(NDEBUG) && !defined(SANITIZER)
- size_t *p = s;
- assert((asize & 2) == 0); /* check against duplicate free */
- size_t size = p[-2];
- assert(((size + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
- /* check for out-of-bounds writes */
- for (size_t i = size; i < asize - MALLOC_EXTRA_SPACE; i++)
- assert(((char *) s)[i] == '\xBD');
- p[-1] |= 2; /* indicate area is freed */
-
- /* overwrite memory that is to be freed with a pattern that
- * will help us recognize access to already freed memory in
- * the debugger */
- DEADBEEFCHK memset(s, '\xDB', asize - MALLOC_EXTRA_SPACE);
-#endif
-
- free((char *) s - MALLOC_EXTRA_SPACE);
- heapdec((ssize_t) asize);
-}
-
-#undef GDKrealloc
-void *
-GDKrealloc(void *s, size_t size)
-{
- size_t nsize, asize;
-#if !defined(NDEBUG) && !defined(SANITIZER)
- size_t osize;
-#endif
- size_t *os = s;
-
- assert(size != 0);
-
- if (s == NULL)
- return GDKmalloc(size);
-
- nsize = (size + 7) & ~7;
- asize = os[-1]; /* how much allocated last */
-
-#ifndef SIZE_CHECK_IN_HEAPS_ONLY
- if (size > SMALL_MALLOC &&
- nsize > asize &&
- GDKvm_cursize() + nsize - asize >= GDK_vm_maxsize &&
- !MT_thread_override_limits()) {
- GDKerror("allocating too much memory\n");
- return NULL;
- }
-#endif
-#if !defined(NDEBUG) && !defined(SANITIZER)
- assert((asize & 2) == 0); /* check against duplicate free */
- /* check for out-of-bounds writes */
- osize = os[-2]; /* how much asked for last */
- assert(((osize + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
- for (size_t i = osize; i < asize - MALLOC_EXTRA_SPACE; i++)
- assert(((char *) s)[i] == '\xBD');
- /* if shrinking, write debug pattern into to-be-freed memory */
- DEADBEEFCHK if (size < osize)
- memset((char *) s + size, '\xDB', osize - size);
- os[-1] |= 2; /* indicate area is freed */
-#endif
- s = realloc((char *) s - MALLOC_EXTRA_SPACE,
- nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
- if (s == NULL) {
-#if !defined(NDEBUG) && !defined(SANITIZER)
- os[-1] &= ~2; /* not freed after all */
- assert(os[-1] == asize);
- assert(os[-2] == osize);
-#endif
- GDKsyserror("realloc failed; memory requested: %zu, memory in
use: %zu, virtual memory in use: %zu\n", size, GDKmem_cursize(),
GDKvm_cursize());;
- return NULL;
- }
- s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
- /* just before the pointer that we return, write how much we
- * asked of malloc */
- ((size_t *) s)[-1] = nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE;
-#if !defined(NDEBUG) && !defined(SANITIZER)
- /* just before that, write how much was asked of us */
- ((size_t *) s)[-2] = size;
- /* if growing, initialize new memory with debug pattern */
- DEADBEEFCHK if (size > osize)
- memset((char *) s + osize, '\xBD', size - osize);
- /* write pattern to help find out-of-bounds writes */
- memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
-#endif
-
- heapinc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
- heapdec((ssize_t) asize);
-
- return s;
-}
-
-/* return how much memory was allocated; the argument must be a value
- * returned by GDKmalloc, GDKzalloc, GDKrealloc, GDKstrdup, or
- * GDKstrndup */
-size_t
-GDKmallocated(const void *s)
-{
- return ((const size_t *) s)[-1]; /* how much allocated last */
-}
-
-/*
- * @- virtual memory
- * allocations affect only the logical VM resources.
- */
-#undef GDKmmap
-void *
-GDKmmap(const char *path, int mode, size_t len)
-{
- void *ret;
-
-#ifndef SIZE_CHECK_IN_HEAPS_ONLY
- if (GDKvm_cursize() + len >= GDK_vm_maxsize &&
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]