f07ee916c770fb37d38a0dd84ebffbb872a634f2
[gnulib.git] / lib / safe-alloc.h
1 /*
2  * safe-alloc.h: 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 __GNUC_PREREQ
30 # if defined __GNUC__ && defined __GNUC_MINOR__
31 #  define __GNUC_PREREQ(maj, min)                                       \
32   ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
33 # else
34 #  define __GNUC_PREREQ(maj, min) 0
35 # endif
36 #endif
37
38 # ifndef ATTRIBUTE_RETURN_CHECK
39 #  if __GNUC_PREREQ (3, 4)
40 #   define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
41 #  else
42 #   define ATTRIBUTE_RETURN_CHECK
43 #  endif
44 # endif
45
46 /* Don't call these directly - use the macros below */
47 int
48 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
49   ATTRIBUTE_RETURN_CHECK;
50
51 int
52 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
53   ATTRIBUTE_RETURN_CHECK;
54
55 /**
56  * ALLOC:
57  * @ptr: pointer to hold address of allocated memory
58  *
59  * Allocate sizeof(*ptr) bytes of memory and store
60  * the address of allocated memory in 'ptr'. Fill the
61  * newly allocated memory with zeros.
62  *
63  * Return -1 on failure to allocate, zero on success
64  */
65 # define ALLOC(ptr)                                     \
66   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), 1, 1)
67
68 /**
69  * ALLOC_N:
70  * @ptr: pointer to hold address of allocated memory
71  * @count: number of elements to allocate
72  *
73  * Allocate an array of 'count' elements, each sizeof(*ptr)
74  * bytes long and store the address of allocated memory in
75  * 'ptr'. Fill the newly allocated memory with zeros.
76  *
77  * Return -1 on failure, 0 on success
78  */
79 # define ALLOC_N(ptr, count)                                    \
80   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 1)
81
82 /**
83  * ALLOC_N_UNINITIALIZED:
84  * @ptr: pointer to hold address of allocated memory
85  * @count: number of elements to allocate
86  *
87  * Allocate an array of 'count' elements, each sizeof(*ptr)
88  * bytes long and store the address of allocated memory in
89  * 'ptr'. Do not initialize the new memory at all.
90  *
91  * Return -1 on failure to allocate, zero on success
92  */
93 # define ALLOC_N_UNINITIALIZED(ptr, count)                      \
94   safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 0)
95
96 /**
97  * REALLOC_N:
98  * @ptr: pointer to hold address of allocated memory
99  * @count: number of elements to allocate
100  *
101  * Re-allocate an array of 'count' elements, each sizeof(*ptr)
102  * bytes long and store the address of allocated memory in
103  * 'ptr'. Fill the newly allocated memory with zeros
104  *
105  * Return -1 on failure to reallocate, zero on success
106  */
107 # define REALLOC_N(ptr, count)                                  \
108   safe_alloc_realloc_n (&(ptr), sizeof(*(ptr)), (count))
109
110 /**
111  * FREE:
112  * @ptr: pointer holding address to be freed
113  *
114  * Free the memory stored in 'ptr' and update to point
115  * to NULL.
116  */
117 # define FREE(ptr)                              \
118   do                                            \
119     {                                           \
120       free (ptr);                               \
121       (ptr) = NULL;                             \
122     }                                           \
123   while(0)
124
125 #endif /* SAFE_ALLOC_H_ */