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_
29 # ifndef ATTRIBUTE_RETURN_CHECK
30 # if __GNUC_PREREQ (3, 4)
31 # define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
33 # define ATTRIBUTE_RETURN_CHECK
37 /* Don't call these directly - use the macros below */
39 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
40 ATTRIBUTE_RETURN_CHECK;
43 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
44 ATTRIBUTE_RETURN_CHECK;
48 * @ptr: pointer to hold address of allocated memory
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.
54 * Return -1 on failure to allocate, zero on success
57 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), 1, 1)
61 * @ptr: pointer to hold address of allocated memory
62 * @count: number of elements to allocate
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.
68 * Return -1 on failure, 0 on success
70 # define ALLOC_N(ptr, count) \
71 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 1)
74 * ALLOC_N_UNINITIALIZED:
75 * @ptr: pointer to hold address of allocated memory
76 * @count: number of elements to allocate
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.
82 * Return -1 on failure to allocate, zero on success
84 # define ALLOC_N_UNINITIALIZED(ptr, count) \
85 safe_alloc_alloc_n (&(ptr), sizeof(*(ptr)), (count), 0)
89 * @ptr: pointer to hold address of allocated memory
90 * @count: number of elements to allocate
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
96 * Return -1 on failure to reallocate, zero on success
98 # define REALLOC_N(ptr, count) \
99 safe_alloc_realloc_n (&(ptr), sizeof(*(ptr)), (count))
103 * @ptr: pointer holding address to be freed
105 * Free the memory stored in 'ptr' and update to point
116 #endif /* SAFE_ALLOC_H_ */