2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
36 #define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
40 static const AvahiAllocator *allocator = NULL;
42 static void oom(void) AVAHI_GCC_NORETURN;
44 static void oom(void) {
46 static const char msg[] = "Out of memory, aborting ...\n";
49 while (strlen(n) > 0) {
52 if ((r = write(2, n, strlen(n))) < 0)
61 /* Default implementation for avahi_malloc() */
62 static void* xmalloc(size_t size) {
68 if (!(p = malloc(size)))
74 /* Default implementation for avahi_realloc() */
75 static void *xrealloc(void *p, size_t size) {
82 if (!(p = realloc(p, size)))
88 /* Default implementation for avahi_calloc() */
89 static void *xcalloc(size_t nmemb, size_t size) {
92 if (size == 0 || nmemb == 0)
95 if (!(p = calloc(nmemb, size)))
101 void *avahi_malloc(size_t size) {
107 return xmalloc(size);
109 assert(allocator->malloc);
110 return allocator->malloc(size);
113 void *avahi_malloc0(size_t size) {
120 return xcalloc(1, size);
122 if (allocator->calloc)
123 return allocator->calloc(1, size);
125 assert(allocator->malloc);
126 if ((p = allocator->malloc(size)))
132 void avahi_free(void *p) {
142 assert(allocator->free);
146 void *avahi_realloc(void *p, size_t size) {
154 return xrealloc(p, size);
156 assert(allocator->realloc);
157 return allocator->realloc(p, size);
160 char *avahi_strdup(const char *s) {
168 if (!(r = avahi_malloc(size+1)))
171 memcpy(r, s, size+1);
175 char *avahi_strndup(const char *s, size_t max) {
183 for (p = s, size = 0;
187 if (!(r = avahi_new(char, size+1)))
195 /* Change the allocator */
196 void avahi_set_allocator(const AvahiAllocator *a) {
200 char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
206 if (!(buf = avahi_malloc(len)))
215 n = vsnprintf(buf, len, fmt, ap2);
218 if (n >= 0 && n < (int) len)
226 if (!(nbuf = avahi_realloc(buf, len))) {
235 char *avahi_strdup_printf(const char *fmt, ... ) {
242 s = avahi_strdup_vprintf(fmt, ap);
248 void *avahi_memdup(const void *s, size_t l) {
252 if (!(p = avahi_malloc(l)))