From 6f1d9dd9f40b000f03c209207e98d8a4469594bb Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 27 Feb 2012 03:26:59 +0000 Subject: Add options to use mmap or electric fence to check memory accesses, and fix a few memory access violations. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1294008 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/gc.hpp | 134 +++++++++++++++++++++++++++-------- sca-cpp/trunk/kernel/mem-test.cpp | 4 +- sca-cpp/trunk/kernel/string-test.cpp | 2 +- sca-cpp/trunk/kernel/xml.hpp | 4 +- 4 files changed, 109 insertions(+), 35 deletions(-) (limited to 'sca-cpp/trunk/kernel') diff --git a/sca-cpp/trunk/kernel/gc.hpp b/sca-cpp/trunk/kernel/gc.hpp index 28ca80edc9..59947dd853 100644 --- a/sca-cpp/trunk/kernel/gc.hpp +++ b/sca-cpp/trunk/kernel/gc.hpp @@ -26,6 +26,10 @@ * Garbage collected memory management, using APR memory pools. */ +#ifdef WANT_MALLOC_MMAP +#include +#include +#endif #include #include #include @@ -111,6 +115,16 @@ public: T* ptr; }; +/** + * Initialize APR. + */ +class gc_apr_context_t { +public: + gc_apr_context_t() { + apr_initialize(); + } +} gc_apr_context; + /** * Garbage collected APR memory pool. */ @@ -161,20 +175,12 @@ apr_pool_t* pool(const gc_pool& pool) { * Destroy a memory pool. */ const bool destroy(const gc_pool& p) { + if (pool(p) == NULL) + return false; apr_pool_destroy(pool(p)); return true; } -/** - * Initialize APR. - */ -class gc_apr_context_t { -public: - gc_apr_context_t() { - apr_initialize(); - } -} gc_apr_context; - /** * Maintain a stack of memory pools. */ @@ -273,7 +279,7 @@ private: * register a cleanup callback for it. */ template apr_status_t gc_pool_cleanup(void* v) { - T* t = static_cast(v); + T* t = (T*)v; t->~T(); return APR_SUCCESS; } @@ -282,7 +288,7 @@ template T* gc_new(apr_pool_t* p) { void* gc_new_ptr = apr_palloc(p, sizeof(T)); assertOrFail(gc_new_ptr != NULL); apr_pool_cleanup_register(p, gc_new_ptr, gc_pool_cleanup, apr_pool_cleanup_null) ; - return static_cast(gc_new_ptr); + return (T*)(gc_new_ptr); } template T* gc_new(const gc_pool& p) { @@ -334,42 +340,112 @@ char* gc_cnew(size_t n) { /** * Pool based equivalent of the standard malloc function. */ -void* gc_malloc(size_t n) { - size_t* gc_malloc_ptr = static_cast(apr_palloc(gc_current_pool(), sizeof(size_t) + n)); - assertOrFail(gc_malloc_ptr != NULL); - *gc_malloc_ptr = n; - return gc_malloc_ptr + 1; +void* gc_pool_malloc(size_t n) { + size_t* ptr = static_cast(apr_palloc(gc_current_pool(), sizeof(size_t) + n)); + assertOrFail(ptr != NULL); + *ptr = n; + return ptr + 1; } /** * Pool based equivalent of the standard realloc function. */ -void* gc_realloc(void* ptr, size_t n) { +void* gc_pool_realloc(void* ptr, size_t n) { size_t size = *(static_cast(ptr) - 1); - size_t* gc_realloc_ptr = static_cast(apr_palloc(gc_current_pool(), sizeof(size_t) + n)); - assertOrFail(gc_realloc_ptr != NULL); - *gc_realloc_ptr = n; - memcpy(gc_realloc_ptr + 1, ptr, size < n? size : n); - return gc_realloc_ptr + 1; + size_t* rptr = static_cast(apr_palloc(gc_current_pool(), sizeof(size_t) + n)); + assertOrFail(rptr != NULL); + *rptr = n; + memcpy(rptr + 1, ptr, size < n? size : n); + return rptr + 1; } /** * Pool based equivalent of the standard free function. */ -void gc_free(unused void* ptr) { +void gc_pool_free(unused void* ptr) { // Memory allocated from a pool is freed when the pool is freed } /** * Pool based equivalent of the standard strdup function. */ -char* gc_strdup(const char* str) { - char* gc_strdup_ptr = static_cast(gc_malloc(strlen(str) + 1)); - assertOrFail(gc_strdup_ptr != NULL); - strcpy(gc_strdup_ptr, str); - return gc_strdup_ptr; +char* gc_pool_strdup(const char* str) { + char* dptr = static_cast(gc_pool_malloc(strlen(str) + 1)); + assertOrFail(dptr != NULL); + strcpy(dptr, str); + return dptr; +} + +#ifdef WANT_MALLOC_MMAP + +/** + * Mmap based memory allocation functions. + */ + +/** + * Mmap based equivalent of the standard malloc function. + */ +void* gc_mmap_malloc(size_t n, unused const void* caller) { + //printf("gc_mmap_malloc %d", n); + size_t* ptr = static_cast(mmap(NULL, sizeof(size_t) + n, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)); + assertOrFail(ptr != NULL); + *ptr = n; + //printf(" %p\n", ptr + 1); + return ptr + 1; +} + +/** + * Mmap based equivalent of the standard realloc function. + */ +void* gc_mmap_realloc(void* ptr, size_t n, const void* caller) { + if (ptr == NULL) + return gc_mmap_malloc(n, caller);; + //printf("gc_mmap_realloc %p %d", ptr, n); + size_t size = *(static_cast(ptr) - 1); + size_t* rptr = static_cast(mremap(static_cast(ptr) - 1, sizeof(size_t) + size, sizeof(size_t) + n, MREMAP_MAYMOVE, NULL)); + assertOrFail(rptr != NULL); + *rptr = n; + //printf(" %p\n", rptr + 1); + return rptr + 1; +} + +/** + * Mmap based equivalent of the standard free function. + */ +void gc_mmap_free(void* ptr, unused const void* caller) { + //printf("gc_mmap_free %p\n", ptr); + if (ptr == NULL) + return; + size_t size = *(static_cast(ptr) - 1); + munmap(static_cast(ptr) - 1, sizeof(size_t) + size); } +/** + * Mmap based equivalent of the standard memalign function. + */ +void* gc_mmap_memalign(unused size_t alignment, size_t n, unused const void* caller) { + //printf("gc_mmap_memalign %d %d\n", alignment, n); + return gc_mmap_malloc(n, caller); } +/** + * Install the mmap based memory allocation functions. + */ +void gc_mmap_init_hook(void) { + __malloc_hook = gc_mmap_malloc; + __realloc_hook = gc_mmap_realloc; + __free_hook = gc_mmap_free; + __memalign_hook = gc_mmap_memalign; +} + +#endif + +} + +#ifdef WANT_MALLOC_MMAP + +void (*__malloc_initialize_hook)(void) = tuscany::gc_mmap_init_hook; + +#endif + #endif /* tuscany_gc_hpp */ diff --git a/sca-cpp/trunk/kernel/mem-test.cpp b/sca-cpp/trunk/kernel/mem-test.cpp index b1164a5a36..e9a2f85ec6 100644 --- a/sca-cpp/trunk/kernel/mem-test.cpp +++ b/sca-cpp/trunk/kernel/mem-test.cpp @@ -103,7 +103,7 @@ struct poolAllocPerf { }; bool testPoolAllocPerf() { - const int count = 100000; + const int count = 10000; const lambda pl = poolAllocPerf(count); maxElements = 0; cout << "Memory pool alloc test " << (time(pl, 1, 1) / count) << " ms" << endl; @@ -139,7 +139,7 @@ struct stdAllocPerf { }; bool testStdAllocPerf() { - const int count = 100000; + const int count = 10000; const lambda sl = stdAllocPerf(count); maxElements = 0; cout << "Memory standard alloc test " << (time(sl, 1, 1) / count) << " ms" << endl; diff --git a/sca-cpp/trunk/kernel/string-test.cpp b/sca-cpp/trunk/kernel/string-test.cpp index f2f21f6ea6..e691d93dfb 100644 --- a/sca-cpp/trunk/kernel/string-test.cpp +++ b/sca-cpp/trunk/kernel/string-test.cpp @@ -153,7 +153,7 @@ bool testStringPerf() { memset(charBuffer, 'A', 16384); charBuffer[16384] = '\0'; - const int count = 100000; + const int count = 10000; { const lambda a16 = addStrings(16); cout << "string test " << time(a16, 5, count) << " ms" << endl; diff --git a/sca-cpp/trunk/kernel/xml.hpp b/sca-cpp/trunk/kernel/xml.hpp index 1f1c664c2d..b53093201d 100644 --- a/sca-cpp/trunk/kernel/xml.hpp +++ b/sca-cpp/trunk/kernel/xml.hpp @@ -51,12 +51,11 @@ class XMLParser { public: XMLParser() { debug("xml::XMLParser"); - xmlMemSetup(gc_free, gc_malloc, gc_realloc, gc_strdup); + xmlMemSetup(gc_pool_free, gc_pool_malloc, gc_pool_realloc, gc_pool_strdup); xmlInitParser(); } ~XMLParser() { - debug("xml::~XMLParser"); } } xmlParser; @@ -80,7 +79,6 @@ public: } ~XMLReader() { - debug("xml::~XMLReader"); if (!owner) return; xmlTextReaderClose(xml); -- cgit v1.2.3