修复内存检测的bug
This commit is contained in:
@@ -104,4 +104,10 @@ class LatchRecipe(ConanFile):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def package_info(self):
|
def package_info(self):
|
||||||
self.cpp_info.libs = ["latch"]
|
self.cpp_info.components["latch"].libs = ["latch"]
|
||||||
|
self.cpp_info.components["latch"].set_property("pkg_config_name", "latch")
|
||||||
|
|
||||||
|
self.cpp_info.components["latch_debug"].libs = ["latch_debug"]
|
||||||
|
self.cpp_info.components["latch_debug"].set_property(
|
||||||
|
"pkg_config_name", "latch_debug"
|
||||||
|
)
|
||||||
|
|||||||
@@ -4,20 +4,19 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static lt_memory_debug_info_list_t LT_MEMORY_DEBUG_INFO_LIST = {
|
static lt_memory_debug_info_list_t LT_MEMORY_DEBUG_INFO_LIST = {NULL, NULL, 0};
|
||||||
NULL, NULL, 0};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 内存调试信息添加
|
* @brief 内存调试信息添加
|
||||||
* @param file 文件名
|
* @param file 文件名
|
||||||
* @param line 行号
|
* @param line 行号
|
||||||
* @param ptr 内存地址
|
* @param ptr 内存地址
|
||||||
* @param size 内存大小
|
* @param size 内存大小
|
||||||
*/
|
*/
|
||||||
void lt_memory_debug_info_add(const char *file,
|
void lt_memory_debug_info_add(const char *file,
|
||||||
int line,
|
int line,
|
||||||
void *ptr,
|
void *ptr,
|
||||||
size_t size) {
|
size_t size) {
|
||||||
lt_memory_debug_info_t *info =
|
lt_memory_debug_info_t *info =
|
||||||
(lt_memory_debug_info_t *)malloc(sizeof(lt_memory_debug_info_t));
|
(lt_memory_debug_info_t *)malloc(sizeof(lt_memory_debug_info_t));
|
||||||
info->file = (char *)malloc(strlen(file) + 1);
|
info->file = (char *)malloc(strlen(file) + 1);
|
||||||
@@ -28,11 +27,10 @@ void lt_memory_debug_info_add(const char *file,
|
|||||||
info->next = NULL;
|
info->next = NULL;
|
||||||
if (LT_MEMORY_DEBUG_INFO_LIST.head == NULL) {
|
if (LT_MEMORY_DEBUG_INFO_LIST.head == NULL) {
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.head = info;
|
LT_MEMORY_DEBUG_INFO_LIST.head = info;
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
|
|
||||||
} else {
|
} else {
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.tail->next = info;
|
LT_MEMORY_DEBUG_INFO_LIST.tail->next = info;
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
|
|
||||||
}
|
}
|
||||||
|
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.size++;
|
LT_MEMORY_DEBUG_INFO_LIST.size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,32 +53,35 @@ void lt_memory_debug_print_info() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 内存调试信息移除指定内存记录
|
* @brief 内存调试信息移除指定内存记录
|
||||||
*/
|
*/
|
||||||
bool lt_memory_debug_info_remove_prt(void *ptr) {
|
bool lt_memory_debug_info_remove_prt(void *ptr) {
|
||||||
lt_memory_debug_info_t *i = LT_MEMORY_DEBUG_INFO_LIST.head;
|
lt_memory_debug_info_t *i = LT_MEMORY_DEBUG_INFO_LIST.head;
|
||||||
lt_memory_debug_info_t *pre = NULL;
|
lt_memory_debug_info_t *pre = NULL;
|
||||||
|
// 找到该内存记录
|
||||||
while (i != NULL) {
|
while (i != NULL) {
|
||||||
if (i->ptr == ptr) {
|
if (i->ptr == ptr) {
|
||||||
if (pre != NULL)
|
if (pre != NULL)
|
||||||
pre->next = i->next;
|
pre->next = i->next;
|
||||||
else
|
else
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.head = i->next;
|
LT_MEMORY_DEBUG_INFO_LIST.head = i->next;
|
||||||
free(i->file);
|
|
||||||
free(i);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pre = i;
|
pre = i;
|
||||||
i = i->next;
|
i = i->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (i == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
free(i->file);
|
||||||
|
free(i);
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.size--;
|
LT_MEMORY_DEBUG_INFO_LIST.size--;
|
||||||
|
|
||||||
if (LT_MEMORY_DEBUG_INFO_LIST.size == 0) {
|
if (LT_MEMORY_DEBUG_INFO_LIST.size == 0) {
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.head = NULL;
|
LT_MEMORY_DEBUG_INFO_LIST.head = NULL;
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.tail = NULL;
|
LT_MEMORY_DEBUG_INFO_LIST.tail = NULL;
|
||||||
} else if (i == NULL) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
LT_MEMORY_DEBUG_INFO_LIST.tail = pre;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static void *(*__lt_malloc)(size_t size) = malloc;
|
static void *(*__lt_malloc)(size_t size) = malloc;
|
||||||
static void *(*__lt_calloc)(size_t nmemb, size_t size) = calloc;
|
static void *(*__lt_calloc)(size_t nmemb, size_t size) = calloc;
|
||||||
static void *(*__lt_realloc)(void *ptr, size_t size) = realloc;
|
static void *(*__lt_realloc)(void *ptr, size_t size) = realloc;
|
||||||
static void (*__lt_free)(void *ptr) = free;
|
static void (*__lt_free)(void *ptr) = free;
|
||||||
|
|
||||||
#ifndef LATCH_DEBUG
|
#ifndef LATCH_DEBUG
|
||||||
void *lt_malloc_direct(size_t size, char *file, int line) {
|
void *lt_malloc_direct(size_t size, char *file, int line) {
|
||||||
@@ -44,8 +44,7 @@ void lt_free_direct(void *ptr, char *file, int line) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void lt_memory_set_functions(void *(*malloc_func)(size_t size),
|
void lt_memory_set_functions(void *(*malloc_func)(size_t size),
|
||||||
void *(*calloc_func)(size_t nmemb,
|
void *(*calloc_func)(size_t nmemb, size_t size),
|
||||||
size_t size),
|
|
||||||
void *(*realloc_func)(void *ptr, size_t size),
|
void *(*realloc_func)(void *ptr, size_t size),
|
||||||
void (*free_func)(void *ptr)) {
|
void (*free_func)(void *ptr)) {
|
||||||
__lt_malloc = malloc_func;
|
__lt_malloc = malloc_func;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ add_executable(
|
|||||||
)
|
)
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
${PROJECT_NAME}_string
|
${PROJECT_NAME}_string
|
||||||
latch
|
latch_debug
|
||||||
gtest::gtest
|
gtest::gtest
|
||||||
)
|
)
|
||||||
gtest_discover_tests(${PROJECT_NAME}_string)
|
gtest_discover_tests(${PROJECT_NAME}_string)
|
||||||
Reference in New Issue
Block a user