summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunyan He <junyan.he@intel.com>2016-04-23 12:12:26 +0800
committerJunyan He <junyan.he@intel.com>2016-04-23 12:12:26 +0800
commitd8941d72b6d4476ff2fed76ac03b801044610405 (patch)
tree9b4ea2e9430733f9e038a23bb11f09194eada181
parentaa29042f882eb4d7356a7e2f51275998ae95eb27 (diff)
alloc
-rw-r--r--libclapi/cl_alloc.c25
-rw-r--r--libclapi/cl_alloc.h2
2 files changed, 18 insertions, 9 deletions
diff --git a/libclapi/cl_alloc.c b/libclapi/cl_alloc.c
index ab2ba544..8978ba46 100644
--- a/libclapi/cl_alloc.c
+++ b/libclapi/cl_alloc.c
@@ -26,6 +26,7 @@
#ifdef CL_ALLOC_DEBUG
static pthread_mutex_t cl_alloc_log_lock;
+#define MAX_ALLOC_LOG_NUM 1024*1024
static unsigned int cl_alloc_log_num;
typedef struct _cl_alloc_log_item {
@@ -36,8 +37,9 @@ typedef struct _cl_alloc_log_item {
} _cl_alloc_log_item;
typedef struct _cl_alloc_log_item* cl_alloc_log_item;
-static cl_alloc_log_item* cl_alloc_log_map[8];
-static int cl_alloc_log_map_size[8];
+#define ALLOC_LOG_BUCKET_SZ 128
+static cl_alloc_log_item* cl_alloc_log_map[ALLOC_LOG_BUCKET_SZ];
+static int cl_alloc_log_map_size[ALLOC_LOG_BUCKET_SZ];
LOCAL void cl_alloc_debug_init(void)
{
@@ -48,7 +50,7 @@ LOCAL void cl_alloc_debug_init(void)
pthread_mutex_init(&cl_alloc_log_lock, NULL);
- for (i = 0; i < 8; i++) {
+ for (i = 0; i < ALLOC_LOG_BUCKET_SZ; i++) {
cl_alloc_log_map_size[i] = 128;
cl_alloc_log_map[i] = malloc(cl_alloc_log_map_size[i]*sizeof(cl_alloc_log_item));
memset(cl_alloc_log_map[i], 0, cl_alloc_log_map_size[i]*sizeof(cl_alloc_log_item));
@@ -64,9 +66,16 @@ static void insert_alloc_log_item(void* ptr, size_t sz, char* file, int line)
cl_long slot;
int i;
+ if (cl_alloc_log_num > MAX_ALLOC_LOG_NUM) {
+ // To many alloc without free. We consider already leaks a lot.
+ cl_alloc_report_unfreed();
+ assert(0);
+ }
+
+
slot = (cl_long)ptr;
- slot = (slot>>5) & 0x07;
- assert(slot < 8);
+ slot = (slot>>5) & 0x07f;
+ assert(slot < ALLOC_LOG_BUCKET_SZ);
cl_alloc_log_item it = malloc(sizeof(_cl_alloc_log_item));
assert(it);
@@ -101,8 +110,8 @@ static void delete_alloc_log_item(void* ptr, char* file, int line)
int i;
slot = (cl_long)ptr;
- slot = (slot>>5) & 0x07;
- assert(slot < 8);
+ slot = (slot>>5) & 0x07f;
+ assert(slot < ALLOC_LOG_BUCKET_SZ);
pthread_mutex_lock(&cl_alloc_log_lock);
for (i = 0; i < cl_alloc_log_map_size[slot]; i++) {
@@ -184,7 +193,7 @@ void cl_alloc_report_unfreed(void)
printf("-------------------------------------------------------------------\n");
num = 0;
- for (slot = 0; slot < 8; slot++) {
+ for (slot = 0; slot < ALLOC_LOG_BUCKET_SZ; slot++) {
for (i = 0; i < cl_alloc_log_map_size[slot]; i++) {
if (cl_alloc_log_map[slot][i]) {
printf("Leak point at file:%s, line: %d, ptr is %p, alloc size is %ld\n",
diff --git a/libclapi/cl_alloc.h b/libclapi/cl_alloc.h
index 4980d0fc..ec716ee9 100644
--- a/libclapi/cl_alloc.h
+++ b/libclapi/cl_alloc.h
@@ -22,7 +22,7 @@
#include <stdlib.h>
-//#define CL_ALLOC_DEBUG 1
+#define CL_ALLOC_DEBUG 1
#ifdef CL_ALLOC_DEBUG
/* Return a valid pointer for the requested memory block size */