From f671c582bd62f65ee036f505d75841c15a5ecec3 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 14 May 2015 19:22:11 +0100 Subject: Memento improvements. Firstly, when displaying a list of nested blocks, don't suppress outputting a block just because it contains a pointer to itself. Various valgrind fixes from the gs version of memento. Experimental C++ operators. See writeup in memento.h comments for how to integrate. --- source/fitz/memento.c | 60 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) (limited to 'source/fitz/memento.c') diff --git a/source/fitz/memento.c b/source/fitz/memento.c index a8708c33..abf7274c 100644 --- a/source/fitz/memento.c +++ b/source/fitz/memento.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2001-2013 Artifex Software, Inc. +/* Copyright (C) 2009-2015 Artifex Software, Inc. All Rights Reserved. This software is provided AS-IS with no warranty, either express or @@ -44,6 +44,10 @@ int atexit(void (*)(void)); #include #endif +#ifdef MEMENTO + +#ifndef MEMENTO_CPP_EXTRAS_ONLY + #ifdef MEMENTO_ANDROID #include @@ -117,8 +121,6 @@ char *getenv(const char *); #define MEMENTO_MAXPATTERN 0 #endif -#ifdef MEMENTO - #ifdef MEMENTO_GS_HACKS #include "valgrind.h" #else @@ -577,6 +579,7 @@ int Memento_listBlocksNested(void) count = 0; size = 0; for (b = globals.used.head; b; b = b->next) { + VALGRIND_MAKE_MEM_DEFINED(b, sizeof(*b)); size += b->rawsize; count++; } @@ -631,6 +634,10 @@ int Memento_listBlocksNested(void) if (child->flags & Memento_Flag_HasParent) continue; + /* Not interested in pointers to ourself! */ + if (child == b) + continue; + /* We're also assuming acyclicness here. If this is one of * our parents, ignore it. */ parent = b->parent; @@ -656,6 +663,14 @@ int Memento_listBlocksNested(void) fprintf(stderr, " Total size of blocks = %d\n", size); MEMENTO_UNDERLYING_FREE(blocks); + + /* Now put the blocks back for valgrind */ + for (b = globals.used.head; b;) { + Memento_BlkHeader *next = b->next; + VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(*b)); + b = next; + } + return 0; } @@ -957,9 +972,9 @@ void *Memento_label(void *ptr, const char *label) if (ptr == NULL) return NULL; block = MEMBLK_FROMBLK(ptr); - VALGRIND_MAKE_MEM_DEFINED(block, sizeof(*block)); + VALGRIND_MAKE_MEM_DEFINED(&block->label, sizeof(block->label)); block->label = label; - VALGRIND_MAKE_MEM_UNDEFINED(block, sizeof(*block)); + VALGRIND_MAKE_MEM_UNDEFINED(&block->label, sizeof(block->label)); return ptr; } @@ -1057,6 +1072,7 @@ static int checkBlock(Memento_BlkHeader *memblk, const char *action) /* Failure! */ fprintf(stderr, "Attempt to %s block ", action); showBlock(memblk, 32); + fprintf(stderr, "\n"); Memento_breakpoint(); return 1; } else if (data.preCorrupt || data.postCorrupt) { @@ -1099,7 +1115,6 @@ void Memento_free(void *blk) if (memblk->flags & Memento_Flag_BreakOnFree) Memento_breakpoint(); - VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk)); globals.alloc -= memblk->rawsize; globals.numFrees++; @@ -1460,6 +1475,39 @@ size_t Memento_setMax(size_t max) globals.maxMemory = max; return max; } +#endif /* MEMENTO_CPP_EXTRAS_ONLY */ + +#ifdef __cplusplus +/* Dumb overrides for the new and delete operators */ + +void *operator new(size_t size) +{ + if (size == 0) + size = 1; + return malloc(size); +} + +void operator delete(void *pointer) +{ + return free(pointer); +} + +/* Some C++ systems (apparently) don't provide new[] or delete[] + * operators. Provide a way to cope with this */ +#ifndef MEMENTO_CPP_NO_ARRAY_CONSTRUCTORS +void *operator new[](size_t size) +{ + if (size == 0) + size = 1; + return malloc(size); +} + +void operator delete[](void *pointer) +{ + return free(pointer); +} +#endif /* MEMENTO_CPP_NO_ARRAY_CONSTRUCTORS */ +#endif /* __cplusplus */ #else -- cgit v1.2.3