Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libobjc / memory.c
blob458d2566fcbe68ea453707d1050d0a17992e6e58
1 /* GNU Objective C Runtime Memory allocation functions
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
11 later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
28 This file includes the standard functions for memory allocation and
29 disposal. Users should use these functions in their ObjC programs
30 so that they work properly with garbage collectors.
33 /* TODO: Turn these into macros or inline functions. */
35 #include "objc-private/common.h"
36 #include "objc-private/error.h"
38 /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
39 malloc, free, etc. on some platforms. It is unclear if we still
40 need it, but it can't hurt.
42 #define __USE_FIXED_PROTOTYPES__
43 #include <stdlib.h>
45 #include "objc/runtime.h"
47 #if OBJC_WITH_GC
48 #include <gc.h>
50 void *
51 objc_malloc (size_t size)
53 void *res = (void *)(GC_malloc (size));
54 if (! res)
55 _objc_abort ("Virtual memory exhausted\n");
56 return res;
59 void *
60 objc_atomic_malloc (size_t size)
62 void *res = (void *)(GC_malloc_atomic (size));
63 if (! res)
64 _objc_abort ("Virtual memory exhausted\n");
65 return res;
68 void *
69 objc_realloc (void *mem, size_t size)
71 void *res = (void *)(GC_realloc (mem, size));
72 if (! res)
73 _objc_abort ("Virtual memory exhausted\n");
74 return res;
77 void *
78 objc_calloc (size_t nelem, size_t size)
80 /* Note that GC_malloc returns cleared memory (see documentation) so
81 there is no need to clear it. */
82 void *res = (void *)(GC_malloc (nelem * size));
83 if (! res)
84 _objc_abort ("Virtual memory exhausted\n");
85 return res;
88 void
89 objc_free (void *mem __attribute__ ((__unused__)))
91 return;
94 #else
96 void *
97 objc_malloc (size_t size)
99 void *res = (void *)(malloc (size));
100 if (! res)
101 _objc_abort ("Virtual memory exhausted\n");
102 return res;
105 void *
106 objc_atomic_malloc (size_t size)
108 void *res = (void *)(malloc (size));
109 if (! res)
110 _objc_abort ("Virtual memory exhausted\n");
111 return res;
114 void *
115 objc_realloc (void *mem, size_t size)
117 void *res = (void *)(realloc (mem, size));
118 if (! res)
119 _objc_abort ("Virtual memory exhausted\n");
120 return res;
123 void *
124 objc_calloc (size_t nelem, size_t size)
126 void *res = (void *)(calloc (nelem, size));
127 if (! res)
128 _objc_abort ("Virtual memory exhausted\n");
129 return res;
132 void
133 objc_free (void *mem)
135 free (mem);
138 #endif /* !OBJC_WITH_GC */
140 /* The rest of the file contains deprecated code. */
142 #if OBJC_WITH_GC
144 void *
145 objc_valloc (size_t size)
147 void *res = (void *)(GC_malloc (size));
148 if (! res)
149 _objc_abort ("Virtual memory exhausted\n");
150 return res;
153 #else
155 void *
156 objc_valloc (size_t size)
158 void *res = (void *)(malloc (size));
159 if (! res)
160 _objc_abort ("Virtual memory exhausted\n");
161 return res;
164 #endif /* !OBJC_WITH_GC */
167 Hook functions for memory allocation and disposal. Deprecated
168 and currently unused.
171 void *(*_objc_malloc) (size_t) = malloc;
172 void *(*_objc_atomic_malloc) (size_t) = malloc;
173 void *(*_objc_valloc) (size_t) = malloc;
174 void *(*_objc_realloc) (void *, size_t) = realloc;
175 void *(*_objc_calloc) (size_t, size_t) = calloc;
176 void (*_objc_free) (void *) = free;