1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-memory.c D-BUS memory handling
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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
24 #include "dbus-memory.h"
25 #include "dbus-internals.h"
30 * @defgroup DBusMemory Memory Allocation
32 * @brief dbus_malloc(), dbus_free(), etc.
34 * Functions and macros related to allocating and releasing
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*.
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
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.
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
68 * @typedef DBusFreeFunction
70 * The type of a function which frees a block of memory.
72 * @param memory the memory to free
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;
81 #ifdef DBUS_BUILD_TESTS
83 initialize_malloc_debug (void)
87 if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
89 fail_counts = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
90 _dbus_set_fail_alloc_counter (fail_counts);
93 if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
94 fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
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().
107 * @param bytes number of bytes to allocate
108 * @return allocated memory, or #NULL if the allocation fails.
111 dbus_malloc (size_t bytes)
113 #ifdef DBUS_BUILD_TESTS
114 initialize_malloc_debug ();
116 if (_dbus_decrement_fail_alloc_counter ())
118 if (fail_counts != -1)
119 _dbus_set_fail_alloc_counter (fail_counts);
125 if (bytes == 0) /* some system mallocs handle this, some don't */
128 else if (fail_size != 0 && bytes > fail_size)
132 return malloc (bytes);
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().
141 * @param bytes number of bytes to allocate
142 * @return allocated memory, or #NULL if the allocation fails.
145 dbus_malloc0 (size_t bytes)
147 #ifdef DBUS_BUILD_TESTS
148 initialize_malloc_debug ();
150 if (_dbus_decrement_fail_alloc_counter ())
152 if (fail_counts != -1)
153 _dbus_set_fail_alloc_counter (fail_counts);
162 else if (fail_size != 0 && bytes > fail_size)
166 return calloc (bytes, 1);
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.
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.
180 dbus_realloc (void *memory,
183 #ifdef DBUS_BUILD_TESTS
184 initialize_malloc_debug ();
186 if (_dbus_decrement_fail_alloc_counter ())
188 if (fail_counts != -1)
189 _dbus_set_fail_alloc_counter (fail_counts);
195 if (bytes == 0) /* guarantee this is safe */
201 else if (fail_size != 0 && bytes > fail_size)
206 return realloc (memory, bytes);
211 * Frees a block of memory previously allocated by dbus_malloc() or
212 * dbus_malloc0(). If passed #NULL, does nothing.
214 * @param memory block to be freed
217 dbus_free (void *memory)
219 if (memory) /* we guarantee it's safe to free (NULL) */