Bump to 1.14.1
[platform/upstream/augeas.git] / src / memory.h
1 /*
2  * memory.c: safer memory allocation
3  *
4  * Copyright (C) 2008-2016 Daniel P. Berrange
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
23 #ifndef MEMORY_H_
24 #define MEMORY_H_
25
26 #include "internal.h"
27
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;
31
32
33 /**
34  * ALLOC:
35  * @ptr: pointer to hold address of allocated memory
36  *
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.
40  *
41  * Returns -1 on failure, 0 on success
42  */
43 #define ALLOC(ptr) mem_alloc_n(&(ptr), sizeof(*(ptr)), 1)
44
45 /**
46  * ALLOC_N:
47  * @ptr: pointer to hold address of allocated memory
48  * @count: number of elements to allocate
49  *
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.
53  *
54  * Returns -1 on failure, 0 on success
55  */
56 #define ALLOC_N(ptr, count) mem_alloc_n(&(ptr), sizeof(*(ptr)), (count))
57
58 /**
59  * REALLOC_N:
60  * @ptr: pointer to hold address of allocated memory
61  * @count: number of elements to allocate
62  *
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
66  *
67  * Returns -1 on failure, 0 on success
68  */
69 #define REALLOC_N(ptr, count) mem_realloc_n(&(ptr), sizeof(*(ptr)), (count))
70
71 /**
72  * FREE:
73  * @ptr: pointer holding address to be freed
74  *
75  * Free the memory stored in 'ptr' and update to point
76  * to NULL.
77  */
78 #define FREE(ptr)                               \
79   do {                                          \
80     free(ptr);                                  \
81     (ptr) = NULL;                               \
82   } while(0)
83
84 #endif /* __VIR_MEMORY_H_ */