UNPKG

3.28 kBtext/x-cView Raw
1/*
2Copyright (c) 2009-2013 Roger Light <roger@atchoo.org>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
133. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
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
49static unsigned long memcount = 0;
50static unsigned long max_memcount = 0;
51#endif
52
53void *_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
67void _mosquitto_free(void *mem)
68{
69#ifdef REAL_WITH_MEMORY_TRACKING
70 memcount -= malloc_usable_size(mem);
71#endif
72 free(mem);
73}
74
75void *_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
90unsigned long _mosquitto_memory_used(void)
91{
92 return memcount;
93}
94
95unsigned long _mosquitto_max_memory_used(void)
96{
97 return max_memcount;
98}
99#endif
100
101void *_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
121char *_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