Lines Matching defs:*
0 /* $Id$ */
4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <unistd.h>
32 #include "malloc.h"
34 #ifndef va_copy
35 #ifdef __va_copy
36 #define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
37 #else
38 #define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
39 #endif
40 #endif
42 static const AvahiAllocator *allocator = NULL;
44 static void oom(void) AVAHI_GCC_NORETURN;
46 static void oom(void) {
48 static const char msg[] = "Out of memory, aborting ...\n";
49 const char *n = msg;
51 while (strlen(n) > 0) {
52 ssize_t r;
54 if ((r = write(2, n, strlen(n))) < 0)
55 break;
57 n += r;
60 abort();
63 /* Default implementation for avahi_malloc() */
64 static void* xmalloc(size_t size) {
65 void *p;
67 if (size == 0)
68 return NULL;
70 if (!(p = malloc(size)))
71 oom();
73 return p;
76 /* Default implementation for avahi_realloc() */
77 static void *xrealloc(void *p, size_t size) {
79 if (size == 0) {
80 free(p);
81 return NULL;
84 if (!(p = realloc(p, size)))
85 oom();
87 return p;
90 /* Default implementation for avahi_calloc() */
91 static void *xcalloc(size_t nmemb, size_t size) {
92 void *p;
94 if (size == 0 || nmemb == 0)
95 return NULL;
97 if (!(p = calloc(nmemb, size)))
98 oom();
100 return p;
103 void *avahi_malloc(size_t size) {
105 if (size <= 0)
106 return NULL;
108 if (!allocator)
109 return xmalloc(size);
111 assert(allocator->malloc);
112 return allocator->malloc(size);
115 void *avahi_malloc0(size_t size) {
116 void *p;
118 if (size <= 0)
119 return NULL;
121 if (!allocator)
122 return xcalloc(1, size);
124 if (allocator->calloc)
125 return allocator->calloc(1, size);
127 assert(allocator->malloc);
128 if ((p = allocator->malloc(size)))
129 memset(p, 0, size);
131 return p;
134 void avahi_free(void *p) {
136 if (!p)
137 return;
139 if (!allocator) {
140 free(p);
141 return;
144 assert(allocator->free);
145 allocator->free(p);
148 void *avahi_realloc(void *p, size_t size) {
150 if (size == 0) {
151 avahi_free(p);
152 return NULL;
155 if (!allocator)
156 return xrealloc(p, size);
158 assert(allocator->realloc);
159 return allocator->realloc(p, size);
162 char *avahi_strdup(const char *s) {
163 char *r;
164 size_t size;
166 if (!s)
167 return NULL;
169 size = strlen(s);
170 if (!(r = avahi_malloc(size+1)))
171 return NULL;
173 memcpy(r, s, size+1);
174 return r;
177 char *avahi_strndup(const char *s, size_t max) {
178 char *r;
179 size_t size;
180 const char *p;
182 if (!s)
183 return NULL;
185 for (p = s, size = 0;
186 size < max && *p;
187 p++, size++);
189 if (!(r = avahi_new(char, size+1)))
190 return NULL;
192 memcpy(r, s, size);
193 r[size] = 0;
194 return r;
197 /* Change the allocator */
198 void avahi_set_allocator(const AvahiAllocator *a) {
199 allocator = a;
202 char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
203 size_t len = 80;
204 char *buf;
206 assert(fmt);
208 if (!(buf = avahi_malloc(len)))
209 return NULL;
211 for (;;) {
212 int n;
213 char *nbuf;
214 va_list ap2;
216 va_copy (ap2, ap);
217 n = vsnprintf(buf, len, fmt, ap2);
218 va_end (ap2);
220 if (n >= 0 && n < (int) len)
221 return buf;
223 if (n >= 0)
224 len = n+1;
225 else
226 len *= 2;
228 if (!(nbuf = avahi_realloc(buf, len))) {
229 avahi_free(buf);
230 return NULL;
233 buf = nbuf;
237 char *avahi_strdup_printf(const char *fmt, ... ) {
238 char *s;
239 va_list ap;
241 assert(fmt);
243 va_start(ap, fmt);
244 s = avahi_strdup_vprintf(fmt, ap);
245 va_end(ap);
247 return s;
250 void *avahi_memdup(const void *s, size_t l) {
251 void *p;
252 assert(s);
254 if (!(p = avahi_malloc(l)))
255 return NULL;
257 memcpy(p, s, l);
258 return p;