summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeffrey Altman <jaltman@secure-endpoints.com>2007-06-22 19:03:57 +0000
committerJeffrey Altman <jaltman@secure-endpoints.com>2007-06-22 19:03:57 +0000
commitfe7af6bf3b0ca903c72c22d66a38aaef92d72520 (patch)
tree378ccc20b33f3737bfb3aaf5ea5d18ab0c6c9e77
parent2357ca5bb8e58d3b3a093a409a636fc111b0d3fd (diff)
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/trunk@19632 dc483132-0cff-0310-8789-dd5450dbe970
-rw-r--r--src/windows/identity/util/perfstat.c64
-rw-r--r--src/windows/identity/util/perfstat.h2
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<windows.h>
#include<utils.h>
+#include<crtdbg.h>
#include<malloc.h>
#include<stdio.h>
#include<strsafe.h>
@@ -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