[FIX] build. (remove memory leaks) 41/19941/2
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 23 Apr 2014 08:52:45 +0000 (12:52 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 25 Apr 2014 12:11:39 +0000 (16:11 +0400)
fix for:
    5450910a130e257ba698906eec12abd73bcc9090
    Idfe22b4ac76a8a1c23101361d6ce2a1758e1c05c

Change-Id: Ib8074401dd39b47f922d70b77f50f2e60aef70c6
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/Makefile
daemon/main.c
daemon/malloc_debug.c [new file with mode: 0644]
daemon/malloc_debug.h [new file with mode: 0644]

index de606d2..63e5f37 100644 (file)
@@ -2,7 +2,8 @@ CC :=gcc
 DEBUG_CPPFLAGS =                               \
                -DDEBUG                                 \
                -DUSE_LOG_ONCE                  \
-#              -DMALLOC_DEBUG_ON               \#
+#              -DMALLOC_DEBUG_LEVEL=1          \#
+#              -DMALLOC_DEBUG_LEVEL=2          \#
 #              -DDEB_PRINTBUF                  \#
 #              -DPARSE_DEBUG_ON                \#
 #              -DTHREAD_SAMPLING_DEBUG \#
index 2b073ac..571765f 100644 (file)
@@ -452,7 +452,7 @@ int main()
        close_system_file_descriptors();
 
        //DO NOT USE THIS FUNCTION FOR RELEASE IT IS TOO SLOW
-#ifdef MALLOC_DEBUG_ON
+#ifdef MALLOC_DEBUG_LEVEL
        msg_swap_free_all_data(&prof_session.user_space_inst);
 #endif
 
diff --git a/daemon/malloc_debug.c b/daemon/malloc_debug.c
new file mode 100644 (file)
index 0000000..ec00c13
--- /dev/null
@@ -0,0 +1,220 @@
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "debug.h"
+#include "malloc_debug.h"
+
+#ifdef MALLOC_DEBUG_LEVEL
+
+#undef calloc
+#undef malloc
+#undef free
+
+#if MALLOC_DEBUG_LEVEL == 2
+#define logi LOGI
+#define loge LOGE
+#else
+#define logi(...)
+#define loge(...)
+#endif
+
+struct mlist_t *files_list;
+
+int list_append(struct mlist_t **to, struct mlist_t *from)
+{
+       struct mlist_t *p = NULL;
+
+       if (*to == NULL) {
+               //empty list
+               *to = from;
+       } else {
+               p = *to;
+               *to = from;
+               from->next = (void *)p;
+               p->prev = (void *)from;
+       }
+
+       return 0;
+}
+
+void list_rm_el(struct mlist_t **list, struct mlist_t *element)
+{
+       struct mlist_t *prev = element->prev;
+       struct mlist_t *next = element->next;
+
+       if (element != NULL) {
+               if (prev != NULL)
+                       //prev != null, next == null
+                       //prev != null, next != null
+                       prev->next = next;
+               else
+                       //prev == null, next == null
+                       //prev == null, next != null
+                       *list = next;
+
+               if (next != NULL)
+                       next->prev = prev;
+       }
+
+       free(element);
+}
+
+struct mlist_t *find_malloc(struct mlist_t *file_list, void *addr)
+{
+       struct mlist_t *el = file_list->addr;
+
+       while (el != NULL) {
+               if (el->addr == addr)
+                       return el;
+               el = el->next;
+       }
+
+       return NULL;
+}
+
+struct mlist_t *find_list(const char *name)
+{
+       struct mlist_t *el = files_list;
+
+       while (el != NULL) {
+               if (strcmp(el->info, name) == 0)
+                       return el;
+               el = el->next;
+       }
+       el = malloc(sizeof(struct mlist_t));
+       memset(el, 0, sizeof(struct mlist_t));
+       el->info = name;
+       list_append(&files_list, el);
+
+       return el;
+}
+
+void print_file_malloc_list(struct mlist_t *file, int only_count)
+{
+       struct mlist_t *el = NULL;
+       int count = 0;
+
+       LOGI(" -> malloc list for file (%s)\n", file->info);
+       el = file->addr;
+       if (el == NULL)
+               LOGI("list is empty\n");
+       while (el != NULL) {
+               count++;
+               if (only_count == 0)
+                       LOGI("  %04d) 0x%lX <%s>\n", el->line, el->addr,
+                            el->info);
+               el = el->next;
+       }
+
+       if (only_count == 1)
+               LOGI("  malloc count = %d\n", count);
+}
+
+void print_malloc_list(char *file_name, int only_count)
+{
+       struct mlist_t *file = NULL;
+       LOGI("BEGIN--------------------------------------------------------\n");
+       if (file_name == NULL) {
+               file = files_list;
+               while (file != NULL) {
+                       print_file_malloc_list(file, only_count);
+                       file = file->next;
+               }
+       } else {
+               struct mlist_t *file = find_list(file_name);
+               print_file_malloc_list(file, only_count);
+       }
+       LOGI("END----------------------------------------------------------\n");
+}
+
+int rm_malloc(struct mlist_t *file_list, void *addr)
+{
+       struct mlist_t *el = find_malloc(file_list, addr);
+
+       if (el != NULL) {
+               list_rm_el(&(file_list->addr), el);
+               return 1;
+       } else {
+               return 0;
+       }
+}
+
+void add_malloc(struct mlist_t **list, void *addr, int line,
+               const char *file_name, const char *func_name, size_t size)
+{
+       struct mlist_t *el = malloc(sizeof(*el));
+
+       el->addr = addr;
+       el->line = line;
+       el->info = func_name;
+       el->size = size;
+       el->next = NULL;
+       el->prev = NULL;
+
+       list_append(list, el);
+
+}
+
+void *malloc_call_d(int line, const char *file_name, const char *func_name,
+                   size_t size)
+{
+       logi("malloc>\n");
+       void *addr = malloc(size);
+       logi("malloc> 0x%0lX (%d:%s '%s')\n", addr, line, func_name, file_name);
+       struct mlist_t *file_el = find_list(file_name);
+
+       add_malloc(&(file_el->addr), addr, line, file_name, func_name, size);
+
+       return addr;
+}
+
+void *calloc_call_d(int line, const char *file_name, const char *func_name,
+                   size_t n, size_t size)
+{
+
+       logi("calloc>\n");
+       void *addr = calloc(n, size);
+       logi("calloc> 0x%0lX (%d:%s '%s')\n", addr, line, func_name, file_name);
+       struct mlist_t *file_el = find_list(file_name);
+
+       add_malloc(&(file_el->addr), addr, line, file_name, func_name,
+                  size * n);
+
+       return addr;
+}
+
+void free_call_d(int line, const char *file_name, const char *function,
+                void *addr)
+{
+
+       struct mlist_t *list = find_list(file_name);
+       if (!rm_malloc(list, addr)) {
+               list = files_list;
+               while (list != NULL) {
+                       if (rm_malloc(list, addr)) {
+                               free(addr);
+                               return;
+                       }
+                       list = list->next;
+               }
+
+       } else {
+               logi("free addr 0x%08lX (%d:%s '%s')\n",
+                    addr, line, function, file_name);
+               free(addr);
+               return;
+       }
+       LOGW("cannot free element!!! 0x%08lX (%d:%s '%s')\n",
+            addr, line, function, file_name);
+
+}
+
+//redefine functions
+#define malloc(size) malloc_call_d( __LINE__ , __FILE__, __FUNCTION__, size)
+#define calloc(num, size) calloc_call_d( __LINE__ , __FILE__, __FUNCTION__, num, size)
+#define free(addr) free_call_d(__LINE__, __FILE__, __func__, addr)
+
+#else
+
+#endif /* MALLOC_DEBUG_LEVEL */
diff --git a/daemon/malloc_debug.h b/daemon/malloc_debug.h
new file mode 100644 (file)
index 0000000..5b1be17
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef __MALLOC_DEBUG__
+#define __MALLOC_DEBUG__
+
+#include <stdint.h>
+
+#ifdef MALLOC_DEBUG_LEVEL
+struct mlist_t {
+       void *next;
+       void *prev;
+       void *addr;
+       int line;
+       char *info;
+       uint32_t size;
+};
+
+void print_malloc_list(char *file_name, int only_count);
+
+#define malloc(size) malloc_call_d( __LINE__ , __FILE__, __FUNCTION__, size)
+#define calloc(num, size) calloc_call_d( __LINE__ , __FILE__, __FUNCTION__, num, size)
+#define free(addr) free_call_d(__LINE__, __FILE__, __func__, addr)
+
+#else /* MALLOC_DEBUG_LEVEL */
+
+#define print_malloc_list(...) do{}while(0)
+
+#endif /* MALLOC_DEBUG_LEVEL */
+
+#endif /* __MALLOC_DEBUG__ */