]> git.meshlink.io Git - catta/blob - include/catta/malloc.h
rename everything avahi to catta
[catta] / include / catta / malloc.h
1 #ifndef foomallochfoo
2 #define foomallochfoo
3
4 /***
5   This file is part of catta.
6
7   catta 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   catta 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 catta; 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 <catta/cdecl.h>
31 #include <catta/gccmacro.h>
32
33 CATTA_C_DECL_BEGIN
34
35 /** Allocate some memory, just like the libc malloc() */
36 void *catta_malloc(size_t size) CATTA_GCC_ALLOC_SIZE(1);
37
38 /** Similar to catta_malloc() but set the memory to zero */
39 void *catta_malloc0(size_t size) CATTA_GCC_ALLOC_SIZE(1);
40
41 /** Free some memory */
42 void catta_free(void *p);
43
44 /** Similar to libc's realloc() */
45 void *catta_realloc(void *p, size_t size) CATTA_GCC_ALLOC_SIZE(2);
46
47 /** Internal helper for catta_new() */
48 static inline void* CATTA_GCC_ALLOC_SIZE2(1,2) catta_new_internal(unsigned n, size_t k) {
49     assert(n < INT_MAX/k);
50     return catta_malloc(n*k);
51 }
52
53 /** Allocate n new structures of the specified type. */
54 #define catta_new(type, n) ((type*) catta_new_internal((n), sizeof(type)))
55
56 /** Internal helper for catta_new0() */
57 static inline void* CATTA_GCC_ALLOC_SIZE2(1,2) catta_new0_internal(unsigned n, size_t k) {
58     assert(n < INT_MAX/k);
59     return catta_malloc0(n*k);
60 }
61
62 /** Same as catta_new() but set the memory to zero */
63 #define catta_new0(type, n) ((type*) catta_new0_internal((n), sizeof(type)))
64
65 /** Just like libc's strdup() */
66 char *catta_strdup(const char *s);
67
68 /** Just like libc's strndup() */
69 char *catta_strndup(const char *s, size_t l);
70
71 /** Duplicate the given memory block into a new one allocated with catta_malloc() */
72 void *catta_memdup(const void *s, size_t l) CATTA_GCC_ALLOC_SIZE(2);
73
74 /** Wraps allocator functions */
75 typedef struct CattaAllocator {
76     void* (*malloc)(size_t size) CATTA_GCC_ALLOC_SIZE(1);
77     void (*free)(void *p);
78     void* (*realloc)(void *p, size_t size) CATTA_GCC_ALLOC_SIZE(2);
79     void* (*calloc)(size_t nmemb, size_t size) CATTA_GCC_ALLOC_SIZE2(1,2);   /**< May be NULL */
80 } CattaAllocator;
81
82 /** Change the allocator. May be NULL to return to default (libc)
83  * allocators. The structure is not copied! */
84 void catta_set_allocator(const CattaAllocator *a);
85
86 /** Like sprintf() but store the result in a freshly allocated buffer. Free this with catta_free() */
87 char *catta_strdup_printf(const char *fmt, ... ) CATTA_GCC_PRINTF_ATTR12;
88
89 /** \cond fulldocs */
90 /** Same as catta_strdup_printf() but take a va_list instead of varargs */
91 char *catta_strdup_vprintf(const char *fmt, va_list ap);
92 /** \endcond */
93
94 CATTA_C_DECL_END
95
96 #endif