]> gcc.gnu.org Git - gcc.git/blame - libobjc/memory.c
archive.c: Do not include objc/objc.h.
[gcc.git] / libobjc / memory.c
CommitLineData
7b869986
NP
1/* GNU Objective C Runtime Memory allocation functions
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009, 2010
40165636 3 Free Software Foundation, Inc.
88e17b57
BE
4 Contributed by Kresten Krab Thorup
5
38709cad 6This file is part of GCC.
88e17b57 7
38709cad 8GCC is free software; you can redistribute it and/or modify it
88e17b57 9under the terms of the GNU General Public License as published by the
748086b7 10Free Software Foundation; either version 3, or (at your option) any
88e17b57
BE
11later version.
12
38709cad 13GCC is distributed in the hope that it will be useful, but WITHOUT
88e17b57
BE
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
748086b7
JJ
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. */
88e17b57 26
d1be5d82
NP
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
bc18535a
NP
33/* TODO: Turn these into macros or inline functions. */
34
6dead247 35#include "objc-private/common.h"
7b869986 36#include "objc-private/error.h"
88e17b57 37
6dead247
NP
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*/
88e17b57
BE
42#define __USE_FIXED_PROTOTYPES__
43#include <stdlib.h>
6dead247 44
718a8e53 45#include "objc/runtime.h"
88e17b57 46
d1be5d82
NP
47#if OBJC_WITH_GC
48#include <gc.h>
88e17b57
BE
49
50void *
40165636 51objc_malloc (size_t size)
88e17b57 52{
d1be5d82 53 void *res = (void *)(GC_malloc (size));
40165636 54 if (! res)
7b869986 55 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
56 return res;
57}
58
59void *
40165636 60objc_atomic_malloc (size_t size)
88e17b57 61{
d1be5d82 62 void *res = (void *)(GC_malloc_atomic (size));
40165636 63 if (! res)
7b869986 64 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
65 return res;
66}
67
68void *
d1be5d82
NP
69objc_realloc (void *mem, size_t size)
70{
71 void *res = (void *)(GC_realloc (mem, size));
72 if (! res)
73 _objc_abort ("Virtual memory exhausted\n");
74 return res;
75}
76
77void *
78objc_calloc (size_t nelem, size_t size)
79{
80 /* Note that GC_malloc returns cleared memory (see documentation) so
81 there is no need to clear it. */
7e268280 82 void *res = (void *)(GC_malloc (nelem * size));
d1be5d82
NP
83 if (! res)
84 _objc_abort ("Virtual memory exhausted\n");
85 return res;
86}
87
88void
bc18535a 89objc_free (void *mem __attribute__ ((__unused__)))
d1be5d82
NP
90{
91 return;
92}
93
94#else
95
96void *
97objc_malloc (size_t size)
88e17b57 98{
d1be5d82
NP
99 void *res = (void *)(malloc (size));
100 if (! res)
101 _objc_abort ("Virtual memory exhausted\n");
102 return res;
103}
104
105void *
106objc_atomic_malloc (size_t size)
107{
108 void *res = (void *)(malloc (size));
40165636 109 if (! res)
7b869986 110 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
111 return res;
112}
113
114void *
40165636 115objc_realloc (void *mem, size_t size)
88e17b57 116{
d1be5d82 117 void *res = (void *)(realloc (mem, size));
40165636 118 if (! res)
7b869986 119 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
120 return res;
121}
122
123void *
40165636 124objc_calloc (size_t nelem, size_t size)
88e17b57 125{
d1be5d82 126 void *res = (void *)(calloc (nelem, size));
40165636 127 if (! res)
7b869986 128 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
129 return res;
130}
131
132void
40165636 133objc_free (void *mem)
88e17b57 134{
d1be5d82 135 free (mem);
88e17b57
BE
136}
137
d1be5d82
NP
138#endif /* !OBJC_WITH_GC */
139
140/* The rest of the file contains deprecated code. */
88e17b57
BE
141
142#if OBJC_WITH_GC
88e17b57 143
d1be5d82
NP
144void *
145objc_valloc (size_t size)
88e17b57 146{
d1be5d82
NP
147 void *res = (void *)(GC_malloc (size));
148 if (! res)
149 _objc_abort ("Virtual memory exhausted\n");
150 return res;
88e17b57
BE
151}
152
d1be5d82
NP
153#else
154
155void *
156objc_valloc (size_t size)
40165636 157{
d1be5d82
NP
158 void *res = (void *)(malloc (size));
159 if (! res)
160 _objc_abort ("Virtual memory exhausted\n");
161 return res;
40165636 162}
88e17b57 163
d1be5d82 164#endif /* !OBJC_WITH_GC */
88e17b57 165
d1be5d82
NP
166/*
167 Hook functions for memory allocation and disposal. Deprecated
168 and currently unused.
169*/
88e17b57 170
40165636
RB
171void *(*_objc_malloc) (size_t) = malloc;
172void *(*_objc_atomic_malloc) (size_t) = malloc;
173void *(*_objc_valloc) (size_t) = malloc;
174void *(*_objc_realloc) (void *, size_t) = realloc;
175void *(*_objc_calloc) (size_t, size_t) = calloc;
176void (*_objc_free) (void *) = free;
This page took 0.900307 seconds and 5 git commands to generate.