]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/memory.c
objc.h: Updated comments.
[thirdparty/gcc.git] / libobjc / memory.c
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
5
6 This file is part of GCC.
7
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.
12
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.
17
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.
21
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/>. */
26
27 /*
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.
31 */
32
33 /* TODO: Turn these into macros or inline functions. */
34
35 #include "objc-private/common.h"
36 #include "objc-private/error.h"
37
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.
41 */
42 #define __USE_FIXED_PROTOTYPES__
43 #include <stdlib.h>
44
45 #include "objc/objc.h"
46 #include "objc/objc-api.h"
47
48 #if OBJC_WITH_GC
49 #include <gc.h>
50
51 void *
52 objc_malloc (size_t size)
53 {
54 void *res = (void *)(GC_malloc (size));
55 if (! res)
56 _objc_abort ("Virtual memory exhausted\n");
57 return res;
58 }
59
60 void *
61 objc_atomic_malloc (size_t size)
62 {
63 void *res = (void *)(GC_malloc_atomic (size));
64 if (! res)
65 _objc_abort ("Virtual memory exhausted\n");
66 return res;
67 }
68
69 void *
70 objc_realloc (void *mem, size_t size)
71 {
72 void *res = (void *)(GC_realloc (mem, size));
73 if (! res)
74 _objc_abort ("Virtual memory exhausted\n");
75 return res;
76 }
77
78 void *
79 objc_calloc (size_t nelem, size_t size)
80 {
81 /* Note that GC_malloc returns cleared memory (see documentation) so
82 there is no need to clear it. */
83 void *res = (void *)(GC_malloc (nelem * size));
84 if (! res)
85 _objc_abort ("Virtual memory exhausted\n");
86 return res;
87 }
88
89 void
90 objc_free (void *mem __attribute__ ((__unused__)))
91 {
92 return;
93 }
94
95 #else
96
97 void *
98 objc_malloc (size_t size)
99 {
100 void *res = (void *)(malloc (size));
101 if (! res)
102 _objc_abort ("Virtual memory exhausted\n");
103 return res;
104 }
105
106 void *
107 objc_atomic_malloc (size_t size)
108 {
109 void *res = (void *)(malloc (size));
110 if (! res)
111 _objc_abort ("Virtual memory exhausted\n");
112 return res;
113 }
114
115 void *
116 objc_realloc (void *mem, size_t size)
117 {
118 void *res = (void *)(realloc (mem, size));
119 if (! res)
120 _objc_abort ("Virtual memory exhausted\n");
121 return res;
122 }
123
124 void *
125 objc_calloc (size_t nelem, size_t size)
126 {
127 void *res = (void *)(calloc (nelem, size));
128 if (! res)
129 _objc_abort ("Virtual memory exhausted\n");
130 return res;
131 }
132
133 void
134 objc_free (void *mem)
135 {
136 free (mem);
137 }
138
139 #endif /* !OBJC_WITH_GC */
140
141 /* The rest of the file contains deprecated code. */
142
143 #if OBJC_WITH_GC
144
145 void *
146 objc_valloc (size_t size)
147 {
148 void *res = (void *)(GC_malloc (size));
149 if (! res)
150 _objc_abort ("Virtual memory exhausted\n");
151 return res;
152 }
153
154 #else
155
156 void *
157 objc_valloc (size_t size)
158 {
159 void *res = (void *)(malloc (size));
160 if (! res)
161 _objc_abort ("Virtual memory exhausted\n");
162 return res;
163 }
164
165 #endif /* !OBJC_WITH_GC */
166
167 /*
168 Hook functions for memory allocation and disposal. Deprecated
169 and currently unused.
170 */
171
172 void *(*_objc_malloc) (size_t) = malloc;
173 void *(*_objc_atomic_malloc) (size_t) = malloc;
174 void *(*_objc_valloc) (size_t) = malloc;
175 void *(*_objc_realloc) (void *, size_t) = realloc;
176 void *(*_objc_calloc) (size_t, size_t) = calloc;
177 void (*_objc_free) (void *) = free;