2 * safe-alloc.h: safer memory allocation
4 * Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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
22 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
25 # define SAFE_ALLOC_H_
30 # if defined __GNUC__ && defined __GNUC_MINOR__
31 # define __GNUC_PREREQ(maj, min) \
32 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
34 # define __GNUC_PREREQ(maj, min) 0
38 # ifndef ATTRIBUTE_RETURN_CHECK
39 # if __GNUC_PREREQ (3, 4)
40 # define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
42 # define ATTRIBUTE_RETURN_CHECK
46 /* Don't call these directly - use the macros below */
48 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
49 ATTRIBUTE_RETURN_CHECK;
52 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
53 ATTRIBUTE_RETURN_CHECK;
57 * @ptr: pointer to hold address of allocated memory
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.
63 * Return -1 on failure to allocate, zero on success
66 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), 1, 1)
70 * @ptr: pointer to hold address of allocated memory
71 * @count: number of elements to allocate
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.
77 * Return -1 on failure, 0 on success
79 # define ALLOC_N(ptr, count) \
80 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 1)
83 * ALLOC_N_UNINITIALIZED:
84 * @ptr: pointer to hold address of allocated memory
85 * @count: number of elements to allocate
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.
91 * Return -1 on failure to allocate, zero on success
93 # define ALLOC_N_UNINITIALIZED(ptr, count) \
94 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 0)
98 * @ptr: pointer to hold address of allocated memory
99 * @count: number of elements to allocate
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
105 * Return -1 on failure to reallocate, zero on success
107 # define REALLOC_N(ptr, count) \
108 safe_alloc_realloc_n (&(ptr), sizeof(*(ptr)), (count))
112 * @ptr: pointer holding address to be freed
114 * Free the memory stored in 'ptr' and update to point
125 #endif /* SAFE_ALLOC_H_ */