5 This file is part of catta.
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.
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.
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
23 /** \file malloc.h Memory allocation */
25 #include <sys/types.h>
30 #include <catta/cdecl.h>
31 #include <catta/gccmacro.h>
35 /** Allocate some memory, just like the libc malloc() */
36 void *catta_malloc(size_t size) CATTA_GCC_ALLOC_SIZE(1);
38 /** Similar to catta_malloc() but set the memory to zero */
39 void *catta_malloc0(size_t size) CATTA_GCC_ALLOC_SIZE(1);
41 /** Free some memory */
42 void catta_free(void *p);
44 /** Similar to libc's realloc() */
45 void *catta_realloc(void *p, size_t size) CATTA_GCC_ALLOC_SIZE(2);
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);
53 /** Allocate n new structures of the specified type. */
54 #define catta_new(type, n) ((type*) catta_new_internal((n), sizeof(type)))
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);
62 /** Same as catta_new() but set the memory to zero */
63 #define catta_new0(type, n) ((type*) catta_new0_internal((n), sizeof(type)))
65 /** Just like libc's strdup() */
66 char *catta_strdup(const char *s);
68 /** Just like libc's strndup() */
69 char *catta_strndup(const char *s, size_t l);
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);
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 */
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);
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;
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);