1*e4b17023SJohn Marino /* GNU Objective C Runtime Memory allocation functions
2*e4b17023SJohn Marino Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009, 2010
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino Contributed by Kresten Krab Thorup
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it
9*e4b17023SJohn Marino under the terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino Free Software Foundation; either version 3, or (at your option) any
11*e4b17023SJohn Marino later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT
14*e4b17023SJohn Marino ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /* This file includes the standard functions for memory allocation and
28*e4b17023SJohn Marino disposal. Users should use these functions in their ObjC programs
29*e4b17023SJohn Marino so that they work properly with garbage collectors. */
30*e4b17023SJohn Marino
31*e4b17023SJohn Marino /* TODO: Turn these into macros or inline functions. */
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino #include "objc-private/common.h"
34*e4b17023SJohn Marino #include "objc-private/error.h"
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
37*e4b17023SJohn Marino malloc, free, etc. on some platforms. It is unclear if we still
38*e4b17023SJohn Marino need it, but it can't hurt. */
39*e4b17023SJohn Marino #define __USE_FIXED_PROTOTYPES__
40*e4b17023SJohn Marino #include <stdlib.h>
41*e4b17023SJohn Marino
42*e4b17023SJohn Marino #include "objc/runtime.h"
43*e4b17023SJohn Marino
44*e4b17023SJohn Marino #if OBJC_WITH_GC
45*e4b17023SJohn Marino #include <gc.h>
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino void *
objc_malloc(size_t size)48*e4b17023SJohn Marino objc_malloc (size_t size)
49*e4b17023SJohn Marino {
50*e4b17023SJohn Marino void *res = (void *)(GC_malloc (size));
51*e4b17023SJohn Marino if (! res)
52*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
53*e4b17023SJohn Marino return res;
54*e4b17023SJohn Marino }
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino void *
objc_atomic_malloc(size_t size)57*e4b17023SJohn Marino objc_atomic_malloc (size_t size)
58*e4b17023SJohn Marino {
59*e4b17023SJohn Marino void *res = (void *)(GC_malloc_atomic (size));
60*e4b17023SJohn Marino if (! res)
61*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
62*e4b17023SJohn Marino return res;
63*e4b17023SJohn Marino }
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino void *
objc_realloc(void * mem,size_t size)66*e4b17023SJohn Marino objc_realloc (void *mem, size_t size)
67*e4b17023SJohn Marino {
68*e4b17023SJohn Marino void *res = (void *)(GC_realloc (mem, size));
69*e4b17023SJohn Marino if (! res)
70*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
71*e4b17023SJohn Marino return res;
72*e4b17023SJohn Marino }
73*e4b17023SJohn Marino
74*e4b17023SJohn Marino void *
objc_calloc(size_t nelem,size_t size)75*e4b17023SJohn Marino objc_calloc (size_t nelem, size_t size)
76*e4b17023SJohn Marino {
77*e4b17023SJohn Marino /* Note that GC_malloc returns cleared memory (see documentation) so
78*e4b17023SJohn Marino there is no need to clear it. */
79*e4b17023SJohn Marino void *res = (void *)(GC_malloc (nelem * size));
80*e4b17023SJohn Marino if (! res)
81*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
82*e4b17023SJohn Marino return res;
83*e4b17023SJohn Marino }
84*e4b17023SJohn Marino
85*e4b17023SJohn Marino void
objc_free(void * mem)86*e4b17023SJohn Marino objc_free (void *mem __attribute__ ((__unused__)))
87*e4b17023SJohn Marino {
88*e4b17023SJohn Marino return;
89*e4b17023SJohn Marino }
90*e4b17023SJohn Marino
91*e4b17023SJohn Marino #else
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino void *
objc_malloc(size_t size)94*e4b17023SJohn Marino objc_malloc (size_t size)
95*e4b17023SJohn Marino {
96*e4b17023SJohn Marino void *res = (void *)(malloc (size));
97*e4b17023SJohn Marino if (! res)
98*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
99*e4b17023SJohn Marino return res;
100*e4b17023SJohn Marino }
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino void *
objc_atomic_malloc(size_t size)103*e4b17023SJohn Marino objc_atomic_malloc (size_t size)
104*e4b17023SJohn Marino {
105*e4b17023SJohn Marino void *res = (void *)(malloc (size));
106*e4b17023SJohn Marino if (! res)
107*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
108*e4b17023SJohn Marino return res;
109*e4b17023SJohn Marino }
110*e4b17023SJohn Marino
111*e4b17023SJohn Marino void *
objc_realloc(void * mem,size_t size)112*e4b17023SJohn Marino objc_realloc (void *mem, size_t size)
113*e4b17023SJohn Marino {
114*e4b17023SJohn Marino void *res = (void *)(realloc (mem, size));
115*e4b17023SJohn Marino if (! res)
116*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
117*e4b17023SJohn Marino return res;
118*e4b17023SJohn Marino }
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino void *
objc_calloc(size_t nelem,size_t size)121*e4b17023SJohn Marino objc_calloc (size_t nelem, size_t size)
122*e4b17023SJohn Marino {
123*e4b17023SJohn Marino void *res = (void *)(calloc (nelem, size));
124*e4b17023SJohn Marino if (! res)
125*e4b17023SJohn Marino _objc_abort ("Virtual memory exhausted\n");
126*e4b17023SJohn Marino return res;
127*e4b17023SJohn Marino }
128*e4b17023SJohn Marino
129*e4b17023SJohn Marino void
objc_free(void * mem)130*e4b17023SJohn Marino objc_free (void *mem)
131*e4b17023SJohn Marino {
132*e4b17023SJohn Marino free (mem);
133*e4b17023SJohn Marino }
134*e4b17023SJohn Marino
135*e4b17023SJohn Marino #endif /* !OBJC_WITH_GC */
136