1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | #include "config.h"
|
31 |
|
32 | #include <stdlib.h>
|
33 | #include <string.h>
|
34 |
|
35 | #include "memory_mosq.h"
|
36 |
|
37 | #ifdef REAL_WITH_MEMORY_TRACKING
|
38 | # if defined(__APPLE__)
|
39 | # include <malloc/malloc.h>
|
40 | # define malloc_usable_size malloc_size
|
41 | # elif defined(__FreeBSD__)
|
42 | # include <malloc_np.h>
|
43 | # else
|
44 | # include <malloc.h>
|
45 | # endif
|
46 | #endif
|
47 |
|
48 | #ifdef REAL_WITH_MEMORY_TRACKING
|
49 | static unsigned long memcount = 0;
|
50 | static unsigned long max_memcount = 0;
|
51 | #endif
|
52 |
|
53 | void *_mosquitto_calloc(size_t nmemb, size_t size)
|
54 | {
|
55 | void *mem = calloc(nmemb, size);
|
56 |
|
57 | #ifdef REAL_WITH_MEMORY_TRACKING
|
58 | memcount += malloc_usable_size(mem);
|
59 | if(memcount > max_memcount){
|
60 | max_memcount = memcount;
|
61 | }
|
62 | #endif
|
63 |
|
64 | return mem;
|
65 | }
|
66 |
|
67 | void _mosquitto_free(void *mem)
|
68 | {
|
69 | #ifdef REAL_WITH_MEMORY_TRACKING
|
70 | memcount -= malloc_usable_size(mem);
|
71 | #endif
|
72 | free(mem);
|
73 | }
|
74 |
|
75 | void *_mosquitto_malloc(size_t size)
|
76 | {
|
77 | void *mem = malloc(size);
|
78 |
|
79 | #ifdef REAL_WITH_MEMORY_TRACKING
|
80 | memcount += malloc_usable_size(mem);
|
81 | if(memcount > max_memcount){
|
82 | max_memcount = memcount;
|
83 | }
|
84 | #endif
|
85 |
|
86 | return mem;
|
87 | }
|
88 |
|
89 | #ifdef REAL_WITH_MEMORY_TRACKING
|
90 | unsigned long _mosquitto_memory_used(void)
|
91 | {
|
92 | return memcount;
|
93 | }
|
94 |
|
95 | unsigned long _mosquitto_max_memory_used(void)
|
96 | {
|
97 | return max_memcount;
|
98 | }
|
99 | #endif
|
100 |
|
101 | void *_mosquitto_realloc(void *ptr, size_t size)
|
102 | {
|
103 | void *mem;
|
104 | #ifdef REAL_WITH_MEMORY_TRACKING
|
105 | if(ptr){
|
106 | memcount -= malloc_usable_size(ptr);
|
107 | }
|
108 | #endif
|
109 | mem = realloc(ptr, size);
|
110 |
|
111 | #ifdef REAL_WITH_MEMORY_TRACKING
|
112 | memcount += malloc_usable_size(mem);
|
113 | if(memcount > max_memcount){
|
114 | max_memcount = memcount;
|
115 | }
|
116 | #endif
|
117 |
|
118 | return mem;
|
119 | }
|
120 |
|
121 | char *_mosquitto_strdup(const char *s)
|
122 | {
|
123 | char *str = strdup(s);
|
124 |
|
125 | #ifdef REAL_WITH_MEMORY_TRACKING
|
126 | memcount += malloc_usable_size(str);
|
127 | if(memcount > max_memcount){
|
128 | max_memcount = memcount;
|
129 | }
|
130 | #endif
|
131 |
|
132 | return str;
|
133 | }
|
134 |
|