2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * @brief Memory management
42 /** @brief Allocate and zero out
43 * @param n Number of bytes to allocate
44 * @return Pointer to allocated memory, or 0
46 static void *malloc_and_zero(size_t n) {
47 void *ptr = malloc(n);
49 if(ptr) memset(ptr, 0, n);
54 static void *(*do_malloc)(size_t) = GC_malloc;
55 static void *(*do_realloc)(void *, size_t) = GC_realloc;
56 static void *(*do_malloc_atomic)(size_t) = GC_malloc_atomic;
57 static void (*do_free)(void *) = GC_free;
59 static void *(*do_malloc)(size_t) = malloc_and_zero;
60 static void *(*do_realloc)(void *, size_t) = realloc;
61 static void *(*do_malloc_atomic)(size_t) = malloc;
62 static void (*do_free)(void *) = free;
65 /** @brief Initialize memory management
67 * Must be called by all programs that use garbage collection. Define
68 * @c ${DISORDER_GC} to @c no to suppress use of the collector
69 * (e.g. for debugging purposes).
75 if(((e = getenv("DISORDER_GC")) && !strcmp(e, "no"))) {
76 do_malloc = malloc_and_zero;
77 do_malloc_atomic = malloc;
82 assert(GC_all_interior_pointers);
87 /** @brief Allocate memory
88 * @param n Bytes to allocate
89 * @return Pointer to allocated memory
91 * Terminates the process on error. The allocated memory is always
94 void *xmalloc(size_t n) {
97 if(!(ptr = do_malloc(n)) && n)
98 fatal(errno, "error allocating memory");
102 /** @brief Reallocate memory
103 * @param ptr Block to reallocated
104 * @param n Bytes to allocate
105 * @return Pointer to allocated memory
107 * Terminates the process on error. It is NOT guaranteed that any
108 * additional memory allocated is 0-filled.
110 void *xrealloc(void *ptr, size_t n) {
111 if(!(ptr = do_realloc(ptr, n)) && n)
112 fatal(errno, "error allocating memory");
116 /** @brief Allocate memory
117 * @param count Number of objects to allocate
118 * @param size Size of one object
119 * @return Pointer to allocated memory
121 * Terminates the process on error. The allocated memory is always
124 void *xcalloc(size_t count, size_t size) {
125 if(count > SIZE_MAX / size)
126 fatal(0, "excessively large calloc");
127 return xmalloc(count * size);
130 /** @brief Allocate memory
131 * @param n Bytes to allocate
132 * @return Pointer to allocated memory
134 * Terminates the process on error. The allocated memory is not
135 * guaranteed to be 0-filled and is not suitable for storing pointers
138 void *xmalloc_noptr(size_t n) {
141 if(!(ptr = do_malloc_atomic(n)) && n)
142 fatal(errno, "error allocating memory");
146 /** @brief Reallocate memory
147 * @param ptr Block to reallocated
148 * @param n Bytes to allocate
149 * @return Pointer to allocated memory
151 * Terminates the processf on error. It is NOT guaranteed that any
152 * additional memory allocated is 0-filled. The block must have been
153 * allocated with xmalloc_noptr() (or xrealloc_noptr()) initially.
155 void *xrealloc_noptr(void *ptr, size_t n) {
157 return xmalloc_noptr(n);
158 if(!(ptr = do_realloc(ptr, n)) && n)
159 fatal(errno, "error allocating memory");
163 /** @brief Duplicate a string
164 * @param s String to copy
165 * @return New copy of string
167 * This uses the equivalent of xmalloc_noptr() to allocate the new string.
169 char *xstrdup(const char *s) {
172 if(!(t = do_malloc_atomic(strlen(s) + 1)))
173 fatal(errno, "error allocating memory");
177 /** @brief Duplicate a prefix of a string
178 * @param s String to copy
179 * @param n Prefix of string to copy
180 * @return New copy of string
182 * This uses the equivalent of xmalloc_noptr() to allocate the new string.
183 * @p n must not exceed the length of the string.
185 char *xstrndup(const char *s, size_t n) {
188 if(!(t = do_malloc_atomic(n + 1)))
189 fatal(errno, "error allocating memory");
195 /** @brief Free memory
196 * @param ptr Block to free or 0
198 void xfree(void *ptr) {