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
36 #define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
38 #define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
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";
51 while (strlen(n) > 0) {
54 if ((r = write(2, n, strlen(n))) < 0)
63 /* Default implementation for avahi_malloc() */
64 static void* xmalloc(size_t size) {
70 if (!(p = malloc(size)))
76 /* Default implementation for avahi_realloc() */
77 static void *xrealloc(void *p, size_t size) {
84 if (!(p = realloc(p, size)))
90 /* Default implementation for avahi_calloc() */
91 static void *xcalloc(size_t nmemb, size_t size) {
94 if (size == 0 || nmemb == 0)
97 if (!(p = calloc(nmemb, size)))
103 void *avahi_malloc(size_t size) {
109 return xmalloc(size);
111 assert(allocator->malloc);
112 return allocator->malloc(size);
115 void *avahi_malloc0(size_t size) {
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)))
134 void avahi_free(void *p) {
144 assert(allocator->free);
148 void *avahi_realloc(void *p, size_t size) {
156 return xrealloc(p, size);
158 assert(allocator->realloc);
159 return allocator->realloc(p, size);
162 char *avahi_strdup(const char *s) {
170 if (!(r = avahi_malloc(size+1)))
173 memcpy(r, s, size+1);
177 char *avahi_strndup(const char *s, size_t max) {
185 for (p = s, size = 0;
189 if (!(r = avahi_new(char, size+1)))
197 /* Change the allocator */
198 void avahi_set_allocator(const AvahiAllocator *a) {
202 char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
208 if (!(buf = avahi_malloc(len)))
217 n = vsnprintf(buf, len, fmt, ap2);
220 if (n >= 0 && n < (int) len)
228 if (!(nbuf = avahi_realloc(buf, len))) {
237 char *avahi_strdup_printf(const char *fmt, ... ) {
244 s = avahi_strdup_vprintf(fmt, ap);
250 void *avahi_memdup(const void *s, size_t l) {
254 if (!(p = avahi_malloc(l)))