1#ifndef foomallochfoo 2#define foomallochfoo 3 4/* $Id$ */ 5 6/*** 7 This file is part of avahi. 8 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. 13 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. 18 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 22 USA. 23***/ 24 25/** \file malloc.h Memory allocation */ 26 27#include <sys/types.h> 28#include <stdarg.h> 29#include <limits.h> 30#include <assert.h> 31 32#include <avahi-common/cdecl.h> 33#include <avahi-common/gccmacro.h> 34 35AVAHI_C_DECL_BEGIN 36 37/** Allocate some memory, just like the libc malloc() */ 38void *avahi_malloc(size_t size) AVAHI_GCC_ALLOC_SIZE(1); 39 40/** Similar to avahi_malloc() but set the memory to zero */ 41void *avahi_malloc0(size_t size) AVAHI_GCC_ALLOC_SIZE(1); 42 43/** Free some memory */ 44void avahi_free(void *p); 45 46/** Similar to libc's realloc() */ 47void *avahi_realloc(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2); 48 49/** Internal helper for avahi_new() */ 50static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new_internal(unsigned n, size_t k) { 51 assert(n < INT_MAX/k); 52 return avahi_malloc(n*k); 53} 54 55/** Allocate n new structures of the specified type. */ 56#define avahi_new(type, n) ((type*) avahi_new_internal((n), sizeof(type))) 57 58/** Internal helper for avahi_new0() */ 59static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new0_internal(unsigned n, size_t k) { 60 assert(n < INT_MAX/k); 61 return avahi_malloc0(n*k); 62} 63 64/** Same as avahi_new() but set the memory to zero */ 65#define avahi_new0(type, n) ((type*) avahi_new0_internal((n), sizeof(type))) 66 67/** Just like libc's strdup() */ 68char *avahi_strdup(const char *s); 69 70/** Just like libc's strndup() */ 71char *avahi_strndup(const char *s, size_t l); 72 73/** Duplicate the given memory block into a new one allocated with avahi_malloc() */ 74void *avahi_memdup(const void *s, size_t l) AVAHI_GCC_ALLOC_SIZE(2); 75 76/** Wraps allocator functions */ 77typedef struct AvahiAllocator { 78 void* (*malloc)(size_t size) AVAHI_GCC_ALLOC_SIZE(1); 79 void (*free)(void *p); 80 void* (*realloc)(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2); 81 void* (*calloc)(size_t nmemb, size_t size) AVAHI_GCC_ALLOC_SIZE2(1,2); /**< May be NULL */ 82} AvahiAllocator; 83 84/** Change the allocator. May be NULL to return to default (libc) 85 * allocators. The structure is not copied! */ 86void avahi_set_allocator(const AvahiAllocator *a); 87 88/** Like sprintf() but store the result in a freshly allocated buffer. Free this with avahi_free() */ 89char *avahi_strdup_printf(const char *fmt, ... ) AVAHI_GCC_PRINTF_ATTR12; 90 91/** \cond fulldocs */ 92/** Same as avahi_strdup_printf() but take a va_list instead of varargs */ 93char *avahi_strdup_vprintf(const char *fmt, va_list ap); 94/** \endcond */ 95 96AVAHI_C_DECL_END 97 98#endif 99