X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-common%2Fmalloc.c;h=23b13a939d0fb536d3a377677b00e81edb47ad48;hb=9c0f9c65093cfa53d45f9b68782321eb8063a032;hp=19e1e590014a56c2e8b2aa56dcb043ebf1b7289b;hpb=e63a65b3955b173a3e8d6b78c6377a518a9922d6;p=catta diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c index 19e1e59..23b13a9 100644 --- a/avahi-common/malloc.c +++ b/avahi-common/malloc.c @@ -1,18 +1,16 @@ -/* $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 @@ -27,18 +25,86 @@ #include #include #include +#include #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 malloc(size); + return xmalloc(size); assert(allocator->malloc); return allocator->malloc(size); @@ -51,7 +117,7 @@ void *avahi_malloc0(size_t size) { return NULL; if (!allocator) - return calloc(1, size); + return xcalloc(1, size); if (allocator->calloc) return allocator->calloc(1, size); @@ -67,7 +133,7 @@ void avahi_free(void *p) { if (!p) return; - + if (!allocator) { free(p); return; @@ -79,8 +145,13 @@ void avahi_free(void *p) { void *avahi_realloc(void *p, size_t size) { + if (size == 0) { + avahi_free(p); + return NULL; + } + if (!allocator) - return realloc(p, size); + return xrealloc(p, size); assert(allocator->realloc); return allocator->realloc(p, size); @@ -89,7 +160,7 @@ void *avahi_realloc(void *p, size_t size) { char *avahi_strdup(const char *s) { char *r; size_t size; - + if (!s) return NULL; @@ -104,16 +175,16 @@ char *avahi_strdup(const char *s) { char *avahi_strndup(const char *s, size_t max) { char *r; size_t size; - + const char *p; + if (!s) return NULL; - size = strlen(s); + for (p = s, size = 0; + size < max && *p; + p++, size++); - if (size > max) - size = max; - - if (!(r = avahi_malloc(size+1))) + if (!(r = avahi_new(char, size+1))) return NULL; memcpy(r, s, size); @@ -127,9 +198,9 @@ void avahi_set_allocator(const AvahiAllocator *a) { } char *avahi_strdup_vprintf(const char *fmt, va_list ap) { - size_t len = 100; + size_t len = 80; char *buf; - + assert(fmt); if (!(buf = avahi_malloc(len))) @@ -138,8 +209,11 @@ char *avahi_strdup_vprintf(const char *fmt, va_list ap) { for (;;) { int n; char *nbuf; - - n = vsnprintf(buf, len, fmt, ap); + va_list ap2; + + va_copy (ap2, ap); + n = vsnprintf(buf, len, fmt, ap2); + va_end (ap2); if (n >= 0 && n < (int) len) return buf; @@ -161,7 +235,9 @@ char *avahi_strdup_vprintf(const char *fmt, va_list ap) { 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); @@ -169,3 +245,13 @@ char *avahi_strdup_printf(const char *fmt, ... ) { 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; +}