summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2024-03-14 21:32:33 +0100
committerAldrik Ramaekers <aldrikboy@gmail.com>2024-03-14 21:32:33 +0100
commit313e98335e47e2321a8e59e757b991d3e300c3ef (patch)
tree34a6820297a74f55cabc05768da77d2f3e40cb61
parentd6924cc6ffb57b06a70403b4f91ace6e88324d26 (diff)
mem leak lib
-rw-r--r--build_win32.bat6
-rw-r--r--mem/LICENSE.txt21
-rw-r--r--mem/mem.cpp80
-rw-r--r--mem/mem.h11
-rw-r--r--src/array.cpp1
-rw-r--r--src/export.cpp1
-rw-r--r--src/memory_bucket.cpp1
-rw-r--r--src/search.cpp2
-rw-r--r--src/unix/main_unix.cpp1
-rw-r--r--src/windows/main_windows.cpp1
10 files changed, 121 insertions, 4 deletions
diff --git a/build_win32.bat b/build_win32.bat
index f0bd716..55acb82 100644
--- a/build_win32.bat
+++ b/build_win32.bat
@@ -2,9 +2,9 @@ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary
@set OUT_DIR=bin\\debug
@set OUT_EXE=text-search
@set INCLUDES=/I..\.. /I..\..\backends
-@set SOURCES=imgui/imgui*.cpp src/*.cpp imfiledialog/*.cpp src/windows/*.cpp imgui/backends/imgui_impl_win32.cpp src/widgets/*.cpp
+@set SOURCES=imgui/imgui*.cpp src/*.cpp imfiledialog/*.cpp src/windows/*.cpp imgui/backends/imgui_impl_win32.cpp src/widgets/*.cpp mem/mem.cpp
@set LIBS=opengl32.lib Advapi32.lib Shell32.lib Ole32.lib User32.lib Pathcch.lib bin/debug/icon.res
-@set FLAGS=
+@set FLAGS=/DTS_DEBUG
windres misc/icon.rc -O coff -o bin/debug/icon.res
if "%1"=="-a" (
@@ -17,6 +17,6 @@ if "%1"=="-release" (
)
mkdir %OUT_DIR%
-cl /std:c++17 /nologo %FLAGS% /W3 /Zi /MD /EHsc /Isrc/windows /external:W0 /external:Iimgui /external:Iimgui/backends /Isrc /utf-8 %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fd%OUT_DIR%/vc140.pdb /Fo%OUT_DIR%/ /link %LIBS%
+cl /std:c++17 /nologo %FLAGS% /W3 /Zi /MD /EHsc /Isrc/windows /external:W0 /external:Iimgui /external:Imem /external:Iimgui/backends /Isrc /utf-8 %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fd%OUT_DIR%/vc140.pdb /Fo%OUT_DIR%/ /link %LIBS%
if "%1"=="-r" call "bin/debug/text-search.exe"
if "%1"=="-d" call devenv "bin/debug/text-search.exe"
diff --git a/mem/LICENSE.txt b/mem/LICENSE.txt
new file mode 100644
index 0000000..af4ad2e
--- /dev/null
+++ b/mem/LICENSE.txt
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2015-2018 Bibhas Bhattacharya
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/mem/mem.cpp b/mem/mem.cpp
new file mode 100644
index 0000000..0442d4e
--- /dev/null
+++ b/mem/mem.cpp
@@ -0,0 +1,80 @@
+#define _CRT_SECURE_NO_WARNINGS
+
+#include <stdlib.h>
+#include <stdio.h>
+#pragma warning(disable : 4996)
+void *debug_malloc(size_t size, const char* file, int line) {
+ void *p = malloc(size);
+
+ if (p == NULL) {
+ return NULL;
+ }
+
+ char buff[256];
+
+ sprintf(buff, "%p.mem", p);
+
+ FILE *f = fopen(buff, "w");
+
+ fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size);
+ fclose(f);
+
+ return p;
+}
+
+void *debug_calloc(size_t count, size_t size, const char* file, int line) {
+ void *p = calloc(count, size);
+
+ if (p == NULL) {
+ return NULL;
+ }
+
+ char buff[256];
+
+ sprintf(buff, "%p.mem", p);
+
+ FILE *f = fopen(buff, "w");
+
+ fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line,
+ count * size);
+ fclose(f);
+
+ return p;
+}
+
+void *debug_realloc(void *ptr, size_t size, const char* file, int line) {
+ void *p = realloc(ptr, size);
+
+ if (p == NULL) {
+ return NULL;
+ }
+
+ char buff[256];
+ //Delete the old pointer record
+ sprintf(buff, "%p.mem", ptr);
+ if (unlink(buff) < 0) {
+ printf("Double free: %p File: %s Line: %d\n", ptr, file, line);
+ }
+
+ //Create the new pointer record
+ sprintf(buff, "%p.mem", p);
+
+ FILE *f = fopen(buff, "w");
+
+ fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size);
+ fclose(f);
+
+ return p;
+}
+
+void debug_free(void *p, const char* file, int line) {
+ char buff[256];
+
+ sprintf(buff, "%p.mem", p);
+ if (unlink(buff) < 0) {
+ printf("Double free: %p File: %s Line: %d\n", p, file, line);
+ }
+
+ free(p);
+}
+
diff --git a/mem/mem.h b/mem/mem.h
new file mode 100644
index 0000000..cc6597b
--- /dev/null
+++ b/mem/mem.h
@@ -0,0 +1,11 @@
+void *debug_malloc(size_t size, const char* file, int line);
+void *debug_calloc(size_t count, size_t size, const char* file, int line);
+void *debug_realloc(void *ptr, size_t size, const char* file, int line);
+void debug_free(void *p, const char* file, int line);
+
+#ifdef TS_DEBUG
+#define malloc(s) debug_malloc(s, __FILE__, __LINE__)
+#define calloc(c, s) debug_calloc(c, s, __FILE__, __LINE__)
+#define realloc(p, s) debug_realloc(p, s, __FILE__, __LINE__)
+#define free(p) debug_free(p, __FILE__, __LINE__)
+#endif
diff --git a/src/array.cpp b/src/array.cpp
index 32eb4f3..929a96c 100644
--- a/src/array.cpp
+++ b/src/array.cpp
@@ -1,5 +1,6 @@
#include "array.h"
#include "config.h"
+#include "mem.h"
#include <stdlib.h>
#include <cstring>
diff --git a/src/export.cpp b/src/export.cpp
index 4e316c4..207a41a 100644
--- a/src/export.cpp
+++ b/src/export.cpp
@@ -1,6 +1,7 @@
#include "export.h"
#include "array.h"
#include "config.h"
+#include "mem.h"
#include <stdio.h>
#ifndef _WIN32
diff --git a/src/memory_bucket.cpp b/src/memory_bucket.cpp
index d846bf1..b465a5f 100644
--- a/src/memory_bucket.cpp
+++ b/src/memory_bucket.cpp
@@ -1,5 +1,6 @@
#include "memory_bucket.h"
#include "config.h"
+#include "mem.h"
#include <stdlib.h>
ts_memory_bucket ts_memory_bucket_init(uint32_t bucket_size)
diff --git a/src/search.cpp b/src/search.cpp
index 9c08487..dfb96b2 100644
--- a/src/search.cpp
+++ b/src/search.cpp
@@ -1,7 +1,7 @@
#include "search.h"
#include "platform.h"
#include "config.h"
-
+#include "mem.h"
#include <stdio.h>
ts_search_result *current_search_result = nullptr;
diff --git a/src/unix/main_unix.cpp b/src/unix/main_unix.cpp
index 9a73740..dc8985f 100644
--- a/src/unix/main_unix.cpp
+++ b/src/unix/main_unix.cpp
@@ -7,6 +7,7 @@
#include "mutex.h"
#include "array.h"
#include "memory_bucket.h"
+#include "mem.h"
#include "image.h"
#include "config.h"
#include <stdio.h>
diff --git a/src/windows/main_windows.cpp b/src/windows/main_windows.cpp
index 45082ca..2426159 100644
--- a/src/windows/main_windows.cpp
+++ b/src/windows/main_windows.cpp
@@ -8,6 +8,7 @@
#include "platform.h"
#include "mutex.h"
#include "array.h"
+#include "mem.h"
#include "memory_bucket.h"
#include "image.h"
#include "config.h"