From afa436b086a9f61239f57aff2d5a2afe1dffe994 Mon Sep 17 00:00:00 2001 From: Tom Yu Date: Thu, 9 Aug 2007 22:12:32 +0000 Subject: [PATCH] pull up r19632 from trunk r19632@cathode-dark-space: jaltman | 2007-06-22 15:03:57 -0400 ticket: 5584 1. Use the debug CRT versions of the memory allocation functions in util\perfstat.c. These functions allow associating file and line numbers with each allocation. 2. Perform a heap verification periodically to check for heap corruption in the debug build when using util\perfstat.c to manage memory allocations. 3. Change wcscmp() to _wcscmp() for compatibility in util\perfstat.h ticket: 5584 git-svn-id: svn://anonsvn.mit.edu/krb5/branches/krb5-1-6@19790 dc483132-0cff-0310-8789-dd5450dbe970 --- src/windows/identity/util/perfstat.c | 64 ++++++++++++++++++++++++---- src/windows/identity/util/perfstat.h | 2 +- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/windows/identity/util/perfstat.c b/src/windows/identity/util/perfstat.c index bcde1f229..aece7e273 100644 --- a/src/windows/identity/util/perfstat.c +++ b/src/windows/identity/util/perfstat.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -81,6 +82,16 @@ static int perf_ready = 0; static DWORD init_thread = 0; +#ifdef _DEBUG +/* */ +#define OPS_TILL_MEM_CHECK 1024 +static int ops_till_mem_check = OPS_TILL_MEM_CHECK; +#endif + +#define _PERF_BLOCK_TYPE(t) (_CLIENT_BLOCK | ((t) << 16)) +#define _RMEM_BLOCK _PERF_BLOCK_TYPE(0) +#define _PERF_BLOCK _PERF_BLOCK_TYPE(1) + static khm_int32 hash_stringA(const void * vs) { /* DJB algorithm */ @@ -104,7 +115,8 @@ static void perf_once(void) { InitializeCriticalSection(&cs_alloc); ZeroMemory(ht, sizeof(ht)); - next_alloc = malloc(sizeof(allocation) * ALLOCBLOCK); + next_alloc = _malloc_dbg(sizeof(allocation) * ALLOCBLOCK, _PERF_BLOCK, + __FILE__, __LINE__); assert(next_alloc); idx_next_alloc = 0; free_alloc = NULL; @@ -113,7 +125,8 @@ static void perf_once(void) { fn_hash.n = 13; fn_hash.hash = hash_stringA; fn_hash.comp = hash_string_compA; - fn_hash.bins = calloc(sizeof(hash_bin *), fn_hash.n); + fn_hash.bins = _calloc_dbg(fn_hash.n, sizeof(hash_bin *), + _PERF_BLOCK, __FILE__, __LINE__); perf_ready = 1; } else { @@ -134,7 +147,9 @@ static allocation * get_allocation(void) { LPOP(&free_alloc, &a); if (!a) { if (idx_next_alloc == ALLOCBLOCK) { - next_alloc = malloc(sizeof(allocation) * ALLOCBLOCK); + next_alloc = _malloc_dbg(sizeof(allocation) * ALLOCBLOCK, + _PERF_BLOCK, + __FILE__, __LINE__); assert(next_alloc); idx_next_alloc = 0; } @@ -208,7 +223,7 @@ perf_malloc(const char * file, int line, size_t s) { EnterCriticalSection(&cs_alloc); a = get_allocation(); - ptr = malloc(s); + ptr = _malloc_dbg(s, _RMEM_BLOCK, file, line); assert(ptr); /* TODO: handle this gracefully */ @@ -223,7 +238,8 @@ perf_malloc(const char * file, int line, size_t s) { &cblen))) fn_copy = NULL; else { - fn_copy = malloc(cblen + sizeof(char)); + fn_copy = _malloc_dbg(cblen + sizeof(char), _PERF_BLOCK, + __FILE__, __LINE__); if (fn_copy) { hash_bin * b; int hv; @@ -232,7 +248,8 @@ perf_malloc(const char * file, int line, size_t s) { hv = fn_hash.hash(fn_copy) % fn_hash.n; - b = malloc(sizeof(*b)); + b = _malloc_dbg(sizeof(*b), _PERF_BLOCK, + __FILE__, __LINE__); b->data = fn_copy; b->key = fn_copy; LINIT(b); @@ -252,6 +269,14 @@ perf_malloc(const char * file, int line, size_t s) { h = HASHPTR(ptr); LPUSH(&ht[h], a); + +#ifdef _DEBUG + if (-- ops_till_mem_check <= 0) { + assert(_CrtCheckMemory()); + ops_till_mem_check = OPS_TILL_MEM_CHECK; + } +#endif + LeaveCriticalSection(&cs_alloc); return ptr; @@ -269,7 +294,8 @@ perf_realloc(const char * file, int line, void * data, size_t s) { perf_once(); h = HASHPTR(data); - n_data = realloc(data, s); + n_data = _realloc_dbg(data, s, _RMEM_BLOCK, + __FILE__, __LINE__); assert(n_data); @@ -288,6 +314,14 @@ perf_realloc(const char * file, int line, void * data, size_t s) { h = HASHPTR(n_data); LPUSH(&ht[h], a); + +#ifdef _DEBUG + if (-- ops_till_mem_check <= 0) { + assert(_CrtCheckMemory()); + ops_till_mem_check = OPS_TILL_MEM_CHECK; + } +#endif + LeaveCriticalSection(&cs_alloc); return n_data; @@ -309,8 +343,20 @@ perf_free (void * b) { assert(a); - LDELETE(&ht[h], a); - LPUSH(&free_alloc, a); + if (a) { + LDELETE(&ht[h], a); + LPUSH(&free_alloc, a); + + _free_dbg(b, _RMEM_BLOCK); + } + +#ifdef _DEBUG + if (-- ops_till_mem_check <= 0) { + assert(_CrtCheckMemory()); + ops_till_mem_check = OPS_TILL_MEM_CHECK; + } +#endif + LeaveCriticalSection(&cs_alloc); } diff --git a/src/windows/identity/util/perfstat.h b/src/windows/identity/util/perfstat.h index 816a13681..0f356583c 100644 --- a/src/windows/identity/util/perfstat.h +++ b/src/windows/identity/util/perfstat.h @@ -44,7 +44,7 @@ #define PREALLOC(d,s) realloc(d,s) #define PFREE(p) free(p) #define PDUMP(f) ((void) 0) -#define PWCSDUP(s) wcsdup(s) +#define PWCSDUP(s) _wcsdup(s) #define PSTRDUP(s) strdup(s) #define PDESCTHREAD(n,c) #endif -- 2.26.2