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
34 static const AvahiAllocator *allocator = NULL;
36 static void oom(void) AVAHI_GCC_NORETURN;
38 static void oom(void) {
40 static const char msg[] = "Out of memory, aborting ...\n";
43 while (strlen(n) > 0) {
46 if ((r = write(2, n, strlen(n))) < 0)
55 /* Default implementation for avahi_malloc() */
56 static void* xmalloc(size_t size) {
62 if (!(p = malloc(size)))
68 /* Default implementation for avahi_realloc() */
69 static void *xrealloc(void *p, size_t size) {
76 if (!(p = realloc(p, size)))
82 /* Default implementation for avahi_calloc() */
83 static void *xcalloc(size_t nmemb, size_t size) {
86 if (size == 0 || nmemb == 0)
89 if (!(p = calloc(nmemb, size)))
95 void *avahi_malloc(size_t size) {
101 return xmalloc(size);
103 assert(allocator->malloc);
104 return allocator->malloc(size);
107 void *avahi_malloc0(size_t size) {
114 return xcalloc(1, size);
116 if (allocator->calloc)
117 return allocator->calloc(1, size);
119 assert(allocator->malloc);
120 if ((p = allocator->malloc(size)))
126 void avahi_free(void *p) {
136 assert(allocator->free);
140 void *avahi_realloc(void *p, size_t size) {
148 return xrealloc(p, size);
150 assert(allocator->realloc);
151 return allocator->realloc(p, size);
154 char *avahi_strdup(const char *s) {
162 if (!(r = avahi_malloc(size+1)))
165 memcpy(r, s, size+1);
169 char *avahi_strndup(const char *s, size_t max) {
177 for (p = s, size = 0;
181 if (!(r = avahi_new(char, size+1)))
189 /* Change the allocator */
190 void avahi_set_allocator(const AvahiAllocator *a) {
194 char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
200 if (!(buf = avahi_malloc(len)))
210 n = vsnprintf(buf, len, fmt, ap2);
214 if (n >= 0 && n < (int) len)
222 if (!(nbuf = avahi_realloc(buf, len))) {
231 char *avahi_strdup_printf(const char *fmt, ... ) {
238 s = avahi_strdup_vprintf(fmt, ap);
244 void *avahi_memdup(const void *s, size_t l) {
248 if (!(p = avahi_malloc(l)))