diff options
Diffstat (limited to 'C/7zAlloc.c')
-rw-r--r-- | C/7zAlloc.c | 69 |
1 files changed, 39 insertions, 30 deletions
diff --git a/C/7zAlloc.c b/C/7zAlloc.c index c924a52..2f0659a 100644 --- a/C/7zAlloc.c +++ b/C/7zAlloc.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* 7zAlloc.c -- Allocation functions | 1 | /* 7zAlloc.c -- Allocation functions for 7z processing |
2 | 2017-04-03 : Igor Pavlov : Public domain */ | 2 | 2023-03-04 : Igor Pavlov : Public domain */ |
3 | 3 | ||
4 | #include "Precomp.h" | 4 | #include "Precomp.h" |
5 | 5 | ||
@@ -7,74 +7,83 @@ | |||
7 | 7 | ||
8 | #include "7zAlloc.h" | 8 | #include "7zAlloc.h" |
9 | 9 | ||
10 | /* #define _SZ_ALLOC_DEBUG */ | 10 | /* #define SZ_ALLOC_DEBUG */ |
11 | /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ | 11 | /* use SZ_ALLOC_DEBUG to debug alloc/free operations */ |
12 | 12 | ||
13 | #ifdef _SZ_ALLOC_DEBUG | 13 | #ifdef SZ_ALLOC_DEBUG |
14 | 14 | ||
15 | /* | ||
15 | #ifdef _WIN32 | 16 | #ifdef _WIN32 |
16 | #include <windows.h> | 17 | #include "7zWindows.h" |
17 | #endif | 18 | #endif |
19 | */ | ||
18 | 20 | ||
19 | #include <stdio.h> | 21 | #include <stdio.h> |
20 | int g_allocCount = 0; | 22 | static int g_allocCount = 0; |
21 | int g_allocCountTemp = 0; | 23 | static int g_allocCountTemp = 0; |
22 | 24 | ||
25 | static void Print_Alloc(const char *s, size_t size, int *counter) | ||
26 | { | ||
27 | const unsigned size2 = (unsigned)size; | ||
28 | fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2); | ||
29 | (*counter)++; | ||
30 | } | ||
31 | static void Print_Free(const char *s, int *counter) | ||
32 | { | ||
33 | (*counter)--; | ||
34 | fprintf(stderr, "\n%s count = %10d", s, *counter); | ||
35 | } | ||
23 | #endif | 36 | #endif |
24 | 37 | ||
25 | void *SzAlloc(ISzAllocPtr p, size_t size) | 38 | void *SzAlloc(ISzAllocPtr p, size_t size) |
26 | { | 39 | { |
27 | UNUSED_VAR(p); | 40 | UNUSED_VAR(p) |
28 | if (size == 0) | 41 | if (size == 0) |
29 | return 0; | 42 | return 0; |
30 | #ifdef _SZ_ALLOC_DEBUG | 43 | #ifdef SZ_ALLOC_DEBUG |
31 | fprintf(stderr, "\nAlloc %10u bytes; count = %10d", (unsigned)size, g_allocCount); | 44 | Print_Alloc("Alloc", size, &g_allocCount); |
32 | g_allocCount++; | ||
33 | #endif | 45 | #endif |
34 | return malloc(size); | 46 | return malloc(size); |
35 | } | 47 | } |
36 | 48 | ||
37 | void SzFree(ISzAllocPtr p, void *address) | 49 | void SzFree(ISzAllocPtr p, void *address) |
38 | { | 50 | { |
39 | UNUSED_VAR(p); | 51 | UNUSED_VAR(p) |
40 | #ifdef _SZ_ALLOC_DEBUG | 52 | #ifdef SZ_ALLOC_DEBUG |
41 | if (address != 0) | 53 | if (address) |
42 | { | 54 | Print_Free("Free ", &g_allocCount); |
43 | g_allocCount--; | ||
44 | fprintf(stderr, "\nFree; count = %10d", g_allocCount); | ||
45 | } | ||
46 | #endif | 55 | #endif |
47 | free(address); | 56 | free(address); |
48 | } | 57 | } |
49 | 58 | ||
50 | void *SzAllocTemp(ISzAllocPtr p, size_t size) | 59 | void *SzAllocTemp(ISzAllocPtr p, size_t size) |
51 | { | 60 | { |
52 | UNUSED_VAR(p); | 61 | UNUSED_VAR(p) |
53 | if (size == 0) | 62 | if (size == 0) |
54 | return 0; | 63 | return 0; |
55 | #ifdef _SZ_ALLOC_DEBUG | 64 | #ifdef SZ_ALLOC_DEBUG |
56 | fprintf(stderr, "\nAlloc_temp %10u bytes; count = %10d", (unsigned)size, g_allocCountTemp); | 65 | Print_Alloc("Alloc_temp", size, &g_allocCountTemp); |
57 | g_allocCountTemp++; | 66 | /* |
58 | #ifdef _WIN32 | 67 | #ifdef _WIN32 |
59 | return HeapAlloc(GetProcessHeap(), 0, size); | 68 | return HeapAlloc(GetProcessHeap(), 0, size); |
60 | #endif | 69 | #endif |
70 | */ | ||
61 | #endif | 71 | #endif |
62 | return malloc(size); | 72 | return malloc(size); |
63 | } | 73 | } |
64 | 74 | ||
65 | void SzFreeTemp(ISzAllocPtr p, void *address) | 75 | void SzFreeTemp(ISzAllocPtr p, void *address) |
66 | { | 76 | { |
67 | UNUSED_VAR(p); | 77 | UNUSED_VAR(p) |
68 | #ifdef _SZ_ALLOC_DEBUG | 78 | #ifdef SZ_ALLOC_DEBUG |
69 | if (address != 0) | 79 | if (address) |
70 | { | 80 | Print_Free("Free_temp ", &g_allocCountTemp); |
71 | g_allocCountTemp--; | 81 | /* |
72 | fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp); | ||
73 | } | ||
74 | #ifdef _WIN32 | 82 | #ifdef _WIN32 |
75 | HeapFree(GetProcessHeap(), 0, address); | 83 | HeapFree(GetProcessHeap(), 0, address); |
76 | return; | 84 | return; |
77 | #endif | 85 | #endif |
86 | */ | ||
78 | #endif | 87 | #endif |
79 | free(address); | 88 | free(address); |
80 | } | 89 | } |