7 This file is part of avahi.
9 avahi is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 avahi is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17 Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with avahi; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 /** \file malloc.h Memory allocation */
27 #include <sys/types.h>
32 #include <avahi-common/cdecl.h>
33 #include <avahi-common/gccmacro.h>
37 /** Allocate some memory, just like the libc malloc() */
38 void *avahi_malloc(size_t size);
40 /** Similar to avahi_malloc() but set the memory to zero */
41 void *avahi_malloc0(size_t size);
43 /** Free some memory */
44 void avahi_free(void *p);
46 /** Similar to libc's realloc() */
47 void *avahi_realloc(void *p, size_t size);
49 /** Internal helper for avahi_new() */
50 static inline void* avahi_new_internal(unsigned n, size_t k) {
51 assert(n < INT_MAX/k);
52 return avahi_malloc(n*k);
55 /** Allocate n new structures of the specified type. */
56 #define avahi_new(type, n) ((type*) avahi_new_internal((n), sizeof(type)))
58 /** Internal helper for avahi_new0() */
59 static inline void* avahi_new0_internal(unsigned n, size_t k) {
60 assert(n < INT_MAX/k);
61 return avahi_malloc0(n*k);
64 /** Same as avahi_new() but set the memory to zero */
65 #define avahi_new0(type, n) ((type*) avahi_new0_internal((n), sizeof(type)))
67 /** Just like libc's strdup() */
68 char *avahi_strdup(const char *s);
70 /** Just like libc's strndup() */
71 char *avahi_strndup(const char *s, size_t l);
73 /** Duplicate the given memory block into a new one allocated with avahi_malloc() */
74 void *avahi_memdup(const void *s, size_t l);
76 /** Wraps allocator functions */
77 typedef struct AvahiAllocator {
78 void* (*malloc)(size_t size);
79 void (*free)(void *p);
80 void* (*realloc)(void *p, size_t size);
81 void* (*calloc)(size_t nmemb, size_t size); /**< May be NULL */
84 /** Change the allocator. May be NULL to return to default (libc)
85 * allocators. The structure is not copied! */
86 void avahi_set_allocator(const AvahiAllocator *a);
88 /** Like sprintf() but store the result in a freshly allocated buffer. Free this with avahi_free() */
89 char *avahi_strdup_printf(const char *fmt, ... ) AVAHI_GCC_PRINTF_ATTR12;
91 /** Same as avahi_strdup_printf() but take a va_list instead of varargs */
92 char *avahi_strdup_vprintf(const char *fmt, va_list ap);