1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-memory.c D-BUS memory handling
4 * Copyright (C) 2002 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"
28 * @defgroup DBusMemory Memory Allocation
30 * @brief dbus_malloc(), dbus_free(), etc.
32 * Functions and macros related to allocating and releasing
41 * Safe macro for using dbus_malloc(). Accepts the type
42 * to allocate and the number of type instances to
43 * allocate as arguments, and returns a memory block
44 * cast to the desired type, instead of as a void*.
46 * @param type type name to allocate
47 * @param count number of instances in the allocated array
48 * @returns the new memory block or #NULL on failure
54 * Safe macro for using dbus_malloc0(). Accepts the type
55 * to allocate and the number of type instances to
56 * allocate as arguments, and returns a memory block
57 * cast to the desired type, instead of as a void*.
58 * The allocated array is initialized to all-bits-zero.
60 * @param type type name to allocate
61 * @param count number of instances in the allocated array
62 * @returns the new memory block or #NULL on failure
66 * @typedef DBusFreeFunction
68 * The type of a function which frees a block of memory.
70 * @param memory the memory to free
74 * Allocates the given number of bytes, as with standard
75 * malloc(). Guaranteed to return #NULL if bytes is zero
76 * on all platforms. Returns #NULL if the allocation fails.
77 * The memory must be released with dbus_free().
79 * @param bytes number of bytes to allocate
80 * @return allocated memory, or #NULL if the allocation fails.
83 dbus_malloc (size_t bytes)
85 if (bytes == 0) /* some system mallocs handle this, some don't */
88 return malloc (bytes);
92 * Allocates the given number of bytes, as with standard malloc(), but
93 * all bytes are initialized to zero as with calloc(). Guaranteed to
94 * return #NULL if bytes is zero on all platforms. Returns #NULL if the
95 * allocation fails. The memory must be released with dbus_free().
97 * @param bytes number of bytes to allocate
98 * @return allocated memory, or #NULL if the allocation fails.
101 dbus_malloc0 (size_t bytes)
106 return calloc (bytes, 1);
110 * Resizes a block of memory previously allocated by dbus_malloc() or
111 * dbus_malloc0(). Guaranteed to free the memory and return #NULL if bytes
112 * is zero on all platforms. Returns #NULL if the resize fails.
113 * If the resize fails, the memory is not freed.
115 * @param memory block to be resized
116 * @param bytes new size of the memory block
117 * @return allocated memory, or #NULL if the resize fails.
120 dbus_realloc (void *memory,
123 if (bytes == 0) /* guarantee this is safe */
130 return realloc (memory, bytes);
135 * Frees a block of memory previously allocated by dbus_malloc() or
136 * dbus_malloc0(). If passed #NULL, does nothing.
138 * @param memory block to be freed
141 dbus_free (void *memory)
143 if (memory) /* we guarantee it's safe to free (NULL) */