]> gcc.gnu.org Git - gcc.git/blame - libobjc/memory.c
In libobjc/: 2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
[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
575584a9
NP
27/* This file includes the standard functions for memory allocation and
28 disposal. Users should use these functions in their ObjC programs
29 so that they work properly with garbage collectors. */
d1be5d82 30
bc18535a
NP
31/* TODO: Turn these into macros or inline functions. */
32
6dead247 33#include "objc-private/common.h"
7b869986 34#include "objc-private/error.h"
88e17b57 35
6dead247
NP
36/* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
37 malloc, free, etc. on some platforms. It is unclear if we still
575584a9 38 need it, but it can't hurt. */
88e17b57
BE
39#define __USE_FIXED_PROTOTYPES__
40#include <stdlib.h>
6dead247 41
718a8e53 42#include "objc/runtime.h"
88e17b57 43
d1be5d82
NP
44#if OBJC_WITH_GC
45#include <gc.h>
88e17b57
BE
46
47void *
40165636 48objc_malloc (size_t size)
88e17b57 49{
d1be5d82 50 void *res = (void *)(GC_malloc (size));
40165636 51 if (! res)
7b869986 52 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
53 return res;
54}
55
56void *
40165636 57objc_atomic_malloc (size_t size)
88e17b57 58{
d1be5d82 59 void *res = (void *)(GC_malloc_atomic (size));
40165636 60 if (! res)
7b869986 61 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
62 return res;
63}
64
65void *
d1be5d82
NP
66objc_realloc (void *mem, size_t size)
67{
68 void *res = (void *)(GC_realloc (mem, size));
69 if (! res)
70 _objc_abort ("Virtual memory exhausted\n");
71 return res;
72}
73
74void *
75objc_calloc (size_t nelem, size_t size)
76{
77 /* Note that GC_malloc returns cleared memory (see documentation) so
78 there is no need to clear it. */
7e268280 79 void *res = (void *)(GC_malloc (nelem * size));
d1be5d82
NP
80 if (! res)
81 _objc_abort ("Virtual memory exhausted\n");
82 return res;
83}
84
85void
bc18535a 86objc_free (void *mem __attribute__ ((__unused__)))
d1be5d82
NP
87{
88 return;
89}
90
91#else
92
93void *
94objc_malloc (size_t size)
88e17b57 95{
d1be5d82
NP
96 void *res = (void *)(malloc (size));
97 if (! res)
98 _objc_abort ("Virtual memory exhausted\n");
99 return res;
100}
101
102void *
103objc_atomic_malloc (size_t size)
104{
105 void *res = (void *)(malloc (size));
40165636 106 if (! res)
7b869986 107 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
108 return res;
109}
110
111void *
40165636 112objc_realloc (void *mem, size_t size)
88e17b57 113{
d1be5d82 114 void *res = (void *)(realloc (mem, size));
40165636 115 if (! res)
7b869986 116 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
117 return res;
118}
119
120void *
40165636 121objc_calloc (size_t nelem, size_t size)
88e17b57 122{
d1be5d82 123 void *res = (void *)(calloc (nelem, size));
40165636 124 if (! res)
7b869986 125 _objc_abort ("Virtual memory exhausted\n");
88e17b57
BE
126 return res;
127}
128
129void
40165636 130objc_free (void *mem)
88e17b57 131{
d1be5d82 132 free (mem);
88e17b57
BE
133}
134
d1be5d82
NP
135#endif /* !OBJC_WITH_GC */
136
137/* The rest of the file contains deprecated code. */
88e17b57
BE
138
139#if OBJC_WITH_GC
88e17b57 140
d1be5d82
NP
141void *
142objc_valloc (size_t size)
88e17b57 143{
d1be5d82
NP
144 void *res = (void *)(GC_malloc (size));
145 if (! res)
146 _objc_abort ("Virtual memory exhausted\n");
147 return res;
88e17b57
BE
148}
149
d1be5d82
NP
150#else
151
152void *
153objc_valloc (size_t size)
40165636 154{
d1be5d82
NP
155 void *res = (void *)(malloc (size));
156 if (! res)
157 _objc_abort ("Virtual memory exhausted\n");
158 return res;
40165636 159}
88e17b57 160
d1be5d82 161#endif /* !OBJC_WITH_GC */
88e17b57 162
575584a9
NP
163/* Hook functions for memory allocation and disposal. Deprecated and
164 currently unused. */
40165636
RB
165void *(*_objc_malloc) (size_t) = malloc;
166void *(*_objc_atomic_malloc) (size_t) = malloc;
167void *(*_objc_valloc) (size_t) = malloc;
168void *(*_objc_realloc) (void *, size_t) = realloc;
169void *(*_objc_calloc) (size_t, size_t) = calloc;
170void (*_objc_free) (void *) = free;
This page took 0.844147 seconds and 5 git commands to generate.