X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-common%2Fmalloc.c;h=52e0962df25a25607f221435d2f16593b5fd8222;hb=141a46406d1bbbf6676299b41f4c90ce1004eb71;hp=19e1e590014a56c2e8b2aa56dcb043ebf1b7289b;hpb=e63a65b3955b173a3e8d6b78c6377a518a9922d6;p=catta diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c index 19e1e59..52e0962 100644 --- a/avahi-common/malloc.c +++ b/avahi-common/malloc.c @@ -27,18 +27,78 @@ #include #include #include +#include #include "malloc.h" 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 +111,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); @@ -79,8 +139,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); @@ -104,16 +169,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); - - if (size > max) - size = max; + for (p = s, size = 0; + size < max && *p; + p++, size++); - if (!(r = avahi_malloc(size+1))) + if (!(r = avahi_new(char, size+1))) return NULL; memcpy(r, s, size); @@ -127,7 +192,7 @@ 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); @@ -138,8 +203,13 @@ char *avahi_strdup_vprintf(const char *fmt, va_list ap) { for (;;) { int n; char *nbuf; + va_list ap2; + + va_copy (ap2, ap); - n = vsnprintf(buf, len, fmt, ap); + n = vsnprintf(buf, len, fmt, ap2); + + va_end (ap2); if (n >= 0 && n < (int) len) return buf; @@ -161,6 +231,8 @@ 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); @@ -169,3 +241,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; +}