struct isc_mem {
unsigned int magic;
unsigned int flags;
+ unsigned int debugging;
isc_mutex_t lock;
bool checkfree;
struct stats stats[STATS_BUCKETS + 1];
*/
#if !ISC_MEM_TRACKLINES
-#define ADD_TRACE(a, b, c, d, e)
-#define DELETE_TRACE(a, b, c, d, e)
+#define ADD_TRACE(mctx, ptr, size, file, line)
+#define DELETE_TRACE(mctx, ptr, size, file, line)
#define ISC_MEMFUNC_SCOPE
#else /* if !ISC_MEM_TRACKLINES */
#define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
-#define SHOULD_TRACE_OR_RECORD(ptr) \
- ((isc_mem_debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
+#define SHOULD_TRACE_OR_RECORD(mctx, ptr) \
+ (((mctx)->debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
-#define ADD_TRACE(a, b, c, d, e) \
- if (SHOULD_TRACE_OR_RECORD(b)) { \
- add_trace_entry(a, b, c, d, e); \
+#define ADD_TRACE(mctx, ptr, size, file, line) \
+ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
+ add_trace_entry(mctx, ptr, size, file, line); \
}
-#define DELETE_TRACE(a, b, c, d, e) \
- if (SHOULD_TRACE_OR_RECORD(b)) { \
- delete_trace_entry(a, b, c, d, e); \
+#define DELETE_TRACE(mctx, ptr, size, file, line) \
+ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
+ delete_trace_entry(mctx, ptr, size, file, line); \
}
static void
MCTXLOCK(mctx);
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n",
ptr, size, file, line, mctx);
}
MCTXLOCK(mctx);
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n",
ptr, size, file, line, mctx);
}
}
static void
-mem_create(isc_mem_t **ctxp, unsigned int flags) {
+mem_create(isc_mem_t **ctxp, unsigned int debugging, unsigned int flags) {
isc_mem_t *ctx = NULL;
REQUIRE(ctxp != NULL && *ctxp == NULL);
*ctx = (isc_mem_t){
.magic = MEM_MAGIC,
+ .debugging = debugging,
.flags = flags,
.checkfree = true,
};
ISC_LIST_INIT(ctx->pools);
#if ISC_MEM_TRACKLINES
- if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
+ if ((ctx->debugging & ISC_MEM_DEBUGRECORD) != 0) {
unsigned int i;
ctx->debuglist =
if (isc_refcount_decrement(&ctx->references) == 1) {
isc_refcount_destroy(&ctx->references);
#if ISC_MEM_TRACKLINES
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((ctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy mctx %p file %s line %u\n",
ctx, file, line);
}
*ctxp = NULL;
#if ISC_MEM_TRACKLINES
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((ctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file,
line);
}
(void)atomic_compare_exchange_strong(&ctx->maxinuse, &maxinuse,
inuse);
- if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) {
+ if ((ctx->debugging & ISC_MEM_DEBUGUSAGE) != 0) {
fprintf(stderr, "maxinuse = %lu\n",
(unsigned long)inuse);
}
};
#if ISC_MEM_TRACKLINES
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create pool %p file %s line %u mctx %p\n",
mpctx, file, line, mctx);
}
mctx = mpctx->mctx;
#if ISC_MEM_TRACKLINES
- if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+ if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n",
mpctx, file, line, mctx);
}
void
isc__mem_create(isc_mem_t **mctxp FLARG) {
- mem_create(mctxp, isc_mem_defaultflags);
+ mem_create(mctxp, isc_mem_debugging, isc_mem_defaultflags);
#if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp,
ISC_RUN_TEST_IMPL(isc_mem_noflags) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
- char buf[4096], *p, *q;
+ char buf[4096], *p;
FILE *f;
void *ptr;
UNUSED(state);
- isc_mem_create(&mctx2);
isc_mem_debugging = 0;
+ isc_mem_create(&mctx2);
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
+ isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_stdio_close(f);
memset(buf, 0, sizeof(buf));
buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n');
- assert_non_null(p);
- assert_in_range(p, 0, buf + sizeof(buf) - 3);
- p += 2;
- q = strchr(p, '\n');
- assert_non_null(q);
- *q = '\0';
- assert_string_equal(p, "None.");
-
- isc_mem_debugging = ISC_MEM_DEBUGRECORD;
+ assert_null(p);
}
/* test mem with record flag */
UNUSED(state);
+ isc_mem_debugging = ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE;
isc_mem_create(&mctx2);
- isc_mem_debugging = ISC_MEM_DEBUGTRACE;
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
+ isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_stdio_close(f);
memset(buf, 0, sizeof(buf));
buf[sizeof(buf) - 1] = 0;
- assert_memory_equal(buf, "add ", 4);
+ assert_memory_equal(buf, "create ", 6);
p = strchr(buf, '\n');
assert_non_null(p);
+
+ assert_memory_equal(p + 1, "add ", 4);
+ p = strchr(p + 1, '\n');
+ assert_non_null(p);
p = strchr(p + 1, '\n');
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
p = strchr(p + 1, '\n');
assert_non_null(p);
assert_memory_equal(p + 1, "del ", 4);
-
- isc_mem_debugging = ISC_MEM_DEBUGRECORD;
}
#endif /* if ISC_MEM_TRACKLINES */
ISC_TEST_ENTRY(isc_mem_zeroget)
ISC_TEST_ENTRY(isc_mem_reget)
-#if !defined(__SANITIZE_THREAD__)
-ISC_TEST_ENTRY(isc_mem_benchmark)
-#endif /* __SANITIZE_THREAD__ */
#if ISC_MEM_TRACKLINES
ISC_TEST_ENTRY(isc_mem_noflags)
ISC_TEST_ENTRY(isc_mem_recordflag)
*/
ISC_TEST_ENTRY(isc_mem_traceflag)
#endif /* if ISC_MEM_TRACKLINES */
+#if !defined(__SANITIZE_THREAD__)
+ISC_TEST_ENTRY(isc_mem_benchmark)
+#endif /* __SANITIZE_THREAD__ */
ISC_TEST_LIST_END