New module 'safe-alloc'.
[gnulib.git] / lib / safe-alloc.h
1 /*
2  * memory.c: safer memory allocation
3  *
4  * Copyright (C) 2009 Free Software Foundation, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  */
21
22 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
23
24 #ifndef SAFE_ALLOC_H_
25 # define SAFE_ALLOC_H_
26
27 # include <stdlib.h>
28
29 # ifndef ATTRIBUTE_RETURN_CHECK
30 #  if __GNUC_PREREQ (3, 4)
31 #   define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
32 #  else
33 #   define ATTRIBUTE_RETURN_CHECK
34 #  endif
35 # endif
36
37 /* Don't call these directly - use the macros below */
38 int
39 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
40   ATTRIBUTE_RETURN_CHECK;
41
42 int
43 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
44   ATTRIBUTE_RETURN_CHECK;
45
46 /**
47  * ALLOC:
48  * @ptr: pointer to hold address of allocated memory
49  *
50  * Allocate sizeof(*ptr) bytes of memory and store
51  * the address of allocated memory in 'ptr'. Fill the
52  * newly allocated memory with zeros.
53  *
54  * Return -1 on failure to allocate, zero on success
55  */
56 # define ALLOC(ptr)                                     \
57   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), 1, 1)
58
59 /**
60  * ALLOC_N:
61  * @ptr: pointer to hold address of allocated memory
62  * @count: number of elements to allocate
63  *
64  * Allocate an array of 'count' elements, each sizeof(*ptr)
65  * bytes long and store the address of allocated memory in
66  * 'ptr'. Fill the newly allocated memory with zeros.
67  *
68  * Return -1 on failure, 0 on success
69  */
70 # define ALLOC_N(ptr, count)                                    \
71   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 1)
72
73 /**
74  * ALLOC_N_UNINITIALIZED:
75  * @ptr: pointer to hold address of allocated memory
76  * @count: number of elements to allocate
77  *
78  * Allocate an array of 'count' elements, each sizeof(*ptr)
79  * bytes long and store the address of allocated memory in
80  * 'ptr'. Do not initialize the new memory at all.
81  *
82  * Return -1 on failure to allocate, zero on success
83  */
84 # define ALLOC_N_UNINITIALIZED(ptr, count)                      \
85   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 0)
86
87 /**
88  * REALLOC_N:
89  * @ptr: pointer to hold address of allocated memory
90  * @count: number of elements to allocate
91  *
92  * Re-allocate an array of 'count' elements, each sizeof(*ptr)
93  * bytes long and store the address of allocated memory in
94  * 'ptr'. Fill the newly allocated memory with zeros
95  *
96  * Return -1 on failure to reallocate, zero on success
97  */
98 # define REALLOC_N(ptr, count)                                  \
99   safe_alloc_realloc_n (&(ptr), sizeof(*(ptr)), (count))
100
101 /**
102  * FREE:
103  * @ptr: pointer holding address to be freed
104  *
105  * Free the memory stored in 'ptr' and update to point
106  * to NULL.
107  */
108 # define FREE(ptr)                              \
109   do                                            \
110     {                                           \
111       free(ptr);                                \
112       (ptr) = NULL;                             \
113     }                                           \
114   while(0)
115
116 #endif /* SAFE_ALLOC_H_ */