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