2 * memory.c: safer memory allocation
4 * Copyright (C) 2008-2016 Daniel P. Berrange
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
28 /* Don't call these directly - use the macros below */
29 int mem_alloc_n(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
30 int mem_realloc_n(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
35 * @ptr: pointer to hold address of allocated memory
37 * Allocate sizeof(*ptr) bytes of memory and store
38 * the address of allocated memory in 'ptr'. Fill the
39 * newly allocated memory with zeros.
41 * Returns -1 on failure, 0 on success
43 #define ALLOC(ptr) mem_alloc_n(&(ptr), sizeof(*(ptr)), 1)
47 * @ptr: pointer to hold address of allocated memory
48 * @count: number of elements to allocate
50 * Allocate an array of 'count' elements, each sizeof(*ptr)
51 * bytes long and store the address of allocated memory in
52 * 'ptr'. Fill the newly allocated memory with zeros.
54 * Returns -1 on failure, 0 on success
56 #define ALLOC_N(ptr, count) mem_alloc_n(&(ptr), sizeof(*(ptr)), (count))
60 * @ptr: pointer to hold address of allocated memory
61 * @count: number of elements to allocate
63 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
64 * bytes long and store the address of allocated memory in
65 * 'ptr'. Fill the newly allocated memory with zeros
67 * Returns -1 on failure, 0 on success
69 #define REALLOC_N(ptr, count) mem_realloc_n(&(ptr), sizeof(*(ptr)), (count))
73 * @ptr: pointer holding address to be freed
75 * Free the memory stored in 'ptr' and update to point
84 #endif /* __VIR_MEMORY_H_ */