2003-02-16 Anders Carlsson <andersca@codefactory.se>
[platform/upstream/dbus.git] / dbus / dbus-memory.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-memory.c  D-BUS memory handling
3  *
4  * Copyright (C) 2002, 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-memory.h"
25 #include "dbus-internals.h"
26 #include <stdlib.h>
27
28
29 /**
30  * @defgroup DBusMemory Memory Allocation
31  * @ingroup  DBus
32  * @brief dbus_malloc(), dbus_free(), etc.
33  *
34  * Functions and macros related to allocating and releasing
35  * blocks of memory.
36  *
37  * @{
38  */
39
40 /**
41  * @def dbus_new
42  *
43  * Safe macro for using dbus_malloc(). Accepts the type
44  * to allocate and the number of type instances to
45  * allocate as arguments, and returns a memory block
46  * cast to the desired type, instead of as a void*.
47  *
48  * @param type type name to allocate
49  * @param count number of instances in the allocated array
50  * @returns the new memory block or #NULL on failure
51  */
52
53 /**
54  * @def dbus_new0
55  *
56  * Safe macro for using dbus_malloc0(). Accepts the type
57  * to allocate and the number of type instances to
58  * allocate as arguments, and returns a memory block
59  * cast to the desired type, instead of as a void*.
60  * The allocated array is initialized to all-bits-zero.
61  *
62  * @param type type name to allocate
63  * @param count number of instances in the allocated array
64  * @returns the new memory block or #NULL on failure
65  */
66
67 /**
68  * @typedef DBusFreeFunction
69  *
70  * The type of a function which frees a block of memory.
71  *
72  * @param memory the memory to free
73  */
74
75 #ifdef DBUS_BUILD_TESTS
76 static dbus_bool_t inited = FALSE;
77 static int fail_counts = -1;
78 static size_t fail_size = 0;
79 #endif
80
81 #ifdef DBUS_BUILD_TESTS
82 static void
83 initialize_malloc_debug (void)
84 {
85   if (!inited)
86     {
87       if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
88         {
89           fail_counts = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
90           _dbus_set_fail_alloc_counter (fail_counts);
91         }
92       
93       if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
94         fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
95       
96       inited = TRUE;
97     }
98 }
99 #endif
100
101 /**
102  * Allocates the given number of bytes, as with standard
103  * malloc(). Guaranteed to return #NULL if bytes is zero
104  * on all platforms. Returns #NULL if the allocation fails.
105  * The memory must be released with dbus_free().
106  *
107  * @param bytes number of bytes to allocate
108  * @return allocated memory, or #NULL if the allocation fails.
109  */
110 void*
111 dbus_malloc (size_t bytes)
112 {
113 #ifdef DBUS_BUILD_TESTS
114   initialize_malloc_debug ();
115   
116   if (_dbus_decrement_fail_alloc_counter ())
117     {
118       if (fail_counts != -1)
119         _dbus_set_fail_alloc_counter (fail_counts);
120       
121       return NULL;
122     }
123 #endif
124   
125   if (bytes == 0) /* some system mallocs handle this, some don't */
126     return NULL;
127 #if DBUS_BUILD_TESTS
128   else if (fail_size != 0 && bytes > fail_size)
129     return NULL;
130 #endif
131   else
132     return malloc (bytes);
133 }
134
135 /**
136  * Allocates the given number of bytes, as with standard malloc(), but
137  * all bytes are initialized to zero as with calloc(). Guaranteed to
138  * return #NULL if bytes is zero on all platforms. Returns #NULL if the
139  * allocation fails.  The memory must be released with dbus_free().
140  *
141  * @param bytes number of bytes to allocate
142  * @return allocated memory, or #NULL if the allocation fails.
143  */
144 void*
145 dbus_malloc0 (size_t bytes)
146 {
147 #ifdef DBUS_BUILD_TESTS
148   initialize_malloc_debug ();
149   
150   if (_dbus_decrement_fail_alloc_counter ())
151     {
152       if (fail_counts != -1)
153         _dbus_set_fail_alloc_counter (fail_counts);
154       
155       return NULL;
156     }
157 #endif
158
159   if (bytes == 0)
160     return NULL;
161 #if DBUS_BUILD_TESTS
162   else if (fail_size != 0 && bytes > fail_size)
163     return NULL;
164 #endif
165   else
166     return calloc (bytes, 1);
167 }
168
169 /**
170  * Resizes a block of memory previously allocated by dbus_malloc() or
171  * dbus_malloc0(). Guaranteed to free the memory and return #NULL if bytes
172  * is zero on all platforms. Returns #NULL if the resize fails.
173  * If the resize fails, the memory is not freed.
174  *
175  * @param memory block to be resized
176  * @param bytes new size of the memory block
177  * @return allocated memory, or #NULL if the resize fails.
178  */
179 void*
180 dbus_realloc (void  *memory,
181               size_t bytes)
182 {
183 #ifdef DBUS_BUILD_TESTS
184   initialize_malloc_debug ();
185   
186   if (_dbus_decrement_fail_alloc_counter ())
187     {
188       if (fail_counts != -1)
189         _dbus_set_fail_alloc_counter (fail_counts);
190       
191       return NULL;
192     }
193 #endif
194   
195   if (bytes == 0) /* guarantee this is safe */
196     {
197       dbus_free (memory);
198       return NULL;
199     }
200 #if DBUS_BUILD_TESTS
201   else if (fail_size != 0 && bytes > fail_size)
202     return NULL;
203 #endif
204   else
205     {
206       return realloc (memory, bytes);
207     }
208 }
209
210 /**
211  * Frees a block of memory previously allocated by dbus_malloc() or
212  * dbus_malloc0(). If passed #NULL, does nothing.
213  * 
214  * @param memory block to be freed
215  */
216 void
217 dbus_free (void  *memory)
218 {
219   if (memory) /* we guarantee it's safe to free (NULL) */
220     free (memory);
221 }
222
223 /** @} */