avahi  0.6.31
malloc.h
Go to the documentation of this file.
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 
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 
33 AVAHI_C_DECL_BEGIN
34 
36 void *avahi_malloc(size_t size) AVAHI_GCC_ALLOC_SIZE(1);
37 
39 void *avahi_malloc0(size_t size) AVAHI_GCC_ALLOC_SIZE(1);
40 
42 void avahi_free(void *p);
43 
45 void *avahi_realloc(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2);
46 
48 static 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 
54 #define avahi_new(type, n) ((type*) avahi_new_internal((n), sizeof(type)))
55 
57 static 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 
63 #define avahi_new0(type, n) ((type*) avahi_new0_internal((n), sizeof(type)))
64 
66 char *avahi_strdup(const char *s);
67 
69 char *avahi_strndup(const char *s, size_t l);
70 
72 void *avahi_memdup(const void *s, size_t l) AVAHI_GCC_ALLOC_SIZE(2);
73 
75 typedef 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);
81 
84 void avahi_set_allocator(const AvahiAllocator *a);
85 
87 char *avahi_strdup_printf(const char *fmt, ... ) AVAHI_GCC_PRINTF_ATTR12;
88 
91 char *avahi_strdup_vprintf(const char *fmt, va_list ap);
94 AVAHI_C_DECL_END
95 
96 #endif
char * avahi_strdup(const char *s)
Just like libc's strdup()
char * avahi_strndup(const char *s, size_t l)
Just like libc's strndup()
void * avahi_memdup(const void *s, size_t l) AVAHI_GCC_ALLOC_SIZE(2)
Duplicate the given memory block into a new one allocated with avahi_malloc()
void(* free)(void *p)
Definition: malloc.h:77
void * avahi_malloc(size_t size) AVAHI_GCC_ALLOC_SIZE(1)
Allocate some memory, just like the libc malloc()
struct AvahiAllocator AvahiAllocator
Wraps allocator functions.
char * avahi_strdup_printf(const char *fmt,...) AVAHI_GCC_PRINTF_ATTR12
Like sprintf() but store the result in a freshly allocated buffer.
void avahi_free(void *p)
Free some memory.
void * avahi_realloc(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2)
Similar to libc's realloc()
static void size_t k
Definition: malloc.h:48
void * avahi_malloc0(size_t size) AVAHI_GCC_ALLOC_SIZE(1)
Similar to avahi_malloc() but set the memory to zero.
Wraps allocator functions.
Definition: malloc.h:75
void avahi_set_allocator(const AvahiAllocator *a)
Change the allocator.