Support an extensive memory debugging process, if you provide some extra files

This commit is contained in:
K. Lange 2022-05-21 20:43:53 +09:00
parent 27278cfa68
commit 78e85ce873
3 changed files with 62 additions and 3 deletions

View File

@ -78,3 +78,15 @@ extern void krk_markObject(KrkObj * object);
*/ */
extern void krk_markTable(KrkTable * table); extern void krk_markTable(KrkTable * table);
/**
* @brief Assume ownership of @p size bytes at @p ptr
*
* Future memory allocation operations with this ptr must be
* through @c krk_reallocate - @p size will be added to the
* VM allocation count, and if extensively memory debugging
* is enabled then the pointer will be marked as owned.
*
* @param ptr Pointer to take ownership of
* @param size Size of data at @p ptr
*/
extern void krk_gcTakeBytes(const void * ptr, size_t size);

View File

@ -5,7 +5,26 @@
#include <kuroko/table.h> #include <kuroko/table.h>
#include <kuroko/util.h> #include <kuroko/util.h>
#if defined(KRK_EXTENSIVE_MEMORY_DEBUGGING)
/**
* Extensive memory debugging tracks every allocation
* in a table, but we don't want to use our table...
*/
#include "../../toaruos/lib/list.c"
#include "../../toaruos/lib/hashmap.c"
static hashmap_t * _debug_hash = NULL;
#endif
void krk_gcTakeBytes(const void * ptr, size_t size) {
#if defined(KRK_EXTENSIVE_MEMORY_DEBUGGING)
hashmap_set(_debug_hash, (void*)ptr, (void*)(uintptr_t)(size));
#endif
vm.bytesAllocated += size;
}
void * krk_reallocate(void * ptr, size_t old, size_t new) { void * krk_reallocate(void * ptr, size_t old, size_t new) {
vm.bytesAllocated -= old; vm.bytesAllocated -= old;
vm.bytesAllocated += new; vm.bytesAllocated += new;
@ -20,12 +39,40 @@ void * krk_reallocate(void * ptr, size_t old, size_t new) {
} }
} }
void * out;
if (new == 0) { if (new == 0) {
free(ptr); free(ptr);
return NULL; out = NULL;
} else {
out = realloc(ptr, new);
} }
return realloc(ptr, new); #if defined(KRK_EXTENSIVE_MEMORY_DEBUGGING)
if (!_debug_hash) {
_debug_hash = hashmap_create_int(1000);
}
if (ptr != NULL) {
if (!hashmap_has(_debug_hash, ptr)) {
fprintf(stderr, "Invalid reallocation of %p from %zu to %zu\n", ptr, old, new);
abort();
}
uintptr_t t = (uintptr_t)hashmap_get(_debug_hash, ptr);
if (t != old) {
fprintf(stderr, "Invalid reallocation of %p from %zu - should be %zu - to %zu\n", ptr, old, t, new);
abort();
}
}
if (ptr) {
hashmap_remove(_debug_hash, ptr);
}
if (out) {
hashmap_set(_debug_hash, out, (void*)(uintptr_t)new);
}
#endif
return out;
} }
static void freeObject(KrkObj * object) { static void freeObject(KrkObj * object) {

View File

@ -212,7 +212,7 @@ KrkString * krk_takeString(char * chars, size_t length) {
} }
/* Part of taking ownership of this string is that we track its memory usage */ /* Part of taking ownership of this string is that we track its memory usage */
vm.bytesAllocated += length + 1; krk_gcTakeBytes(chars, length + 1);
KrkString * result = allocateString(chars, length, hash); KrkString * result = allocateString(chars, length, hash);
return result; return result;
} }