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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* $Id$ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include "malloc.h"
#ifndef va_copy
#ifdef __va_copy
#define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
#else
#define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
#endif
#endif
static const AvahiAllocator *allocator = NULL;
static void oom(void) AVAHI_GCC_NORETURN;
static void oom(void) {
static const char msg[] = "Out of memory, aborting ...\n";
const char *n = msg;
while (strlen(n) > 0) {
ssize_t r;
if ((r = write(2, n, strlen(n))) < 0)
break;
n += r;
}
abort();
}
/* Default implementation for avahi_malloc() */
static void* xmalloc(size_t size) {
void *p;
if (size == 0)
return NULL;
if (!(p = malloc(size)))
oom();
return p;
}
/* Default implementation for avahi_realloc() */
static void *xrealloc(void *p, size_t size) {
if (size == 0) {
free(p);
return NULL;
}
if (!(p = realloc(p, size)))
oom();
return p;
}
/* Default implementation for avahi_calloc() */
static void *xcalloc(size_t nmemb, size_t size) {
void *p;
if (size == 0 || nmemb == 0)
return NULL;
if (!(p = calloc(nmemb, size)))
oom();
return p;
}
void *avahi_malloc(size_t size) {
if (size <= 0)
return NULL;
if (!allocator)
return xmalloc(size);
assert(allocator->malloc);
return allocator->malloc(size);
}
void *avahi_malloc0(size_t size) {
void *p;
if (size <= 0)
return NULL;
if (!allocator)
return xcalloc(1, size);
if (allocator->calloc)
return allocator->calloc(1, size);
assert(allocator->malloc);
if ((p = allocator->malloc(size)))
memset(p, 0, size);
return p;
}
void avahi_free(void *p) {
if (!p)
return;
if (!allocator) {
free(p);
return;
}
assert(allocator->free);
allocator->free(p);
}
void *avahi_realloc(void *p, size_t size) {
if (size == 0) {
avahi_free(p);
return NULL;
}
if (!allocator)
return xrealloc(p, size);
assert(allocator->realloc);
return allocator->realloc(p, size);
}
char *avahi_strdup(const char *s) {
char *r;
size_t size;
if (!s)
return NULL;
size = strlen(s);
if (!(r = avahi_malloc(size+1)))
return NULL;
memcpy(r, s, size+1);
return r;
}
char *avahi_strndup(const char *s, size_t max) {
char *r;
size_t size;
const char *p;
if (!s)
return NULL;
for (p = s, size = 0;
size < max && *p;
p++, size++);
if (!(r = avahi_new(char, size+1)))
return NULL;
memcpy(r, s, size);
r[size] = 0;
return r;
}
/* Change the allocator */
void avahi_set_allocator(const AvahiAllocator *a) {
allocator = a;
}
char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
size_t len = 80;
char *buf;
assert(fmt);
if (!(buf = avahi_malloc(len)))
return NULL;
for (;;) {
int n;
char *nbuf;
va_list ap2;
va_copy (ap2, ap);
n = vsnprintf(buf, len, fmt, ap2);
va_end (ap2);
if (n >= 0 && n < (int) len)
return buf;
if (n >= 0)
len = n+1;
else
len *= 2;
if (!(nbuf = avahi_realloc(buf, len))) {
avahi_free(buf);
return NULL;
}
buf = nbuf;
}
}
char *avahi_strdup_printf(const char *fmt, ... ) {
char *s;
va_list ap;
assert(fmt);
va_start(ap, fmt);
s = avahi_strdup_vprintf(fmt, ap);
va_end(ap);
return s;
}
void *avahi_memdup(const void *s, size_t l) {
void *p;
assert(s);
if (!(p = avahi_malloc(l)))
return NULL;
memcpy(p, s, l);
return p;
}
|