blob: ab7f568ebc00dc57d7df43a5d5877f1e04d6c599 [file] [log] [blame]
Evgeny Eltsin4e0ae762010-11-22 17:09:461/* GNU Objective C Runtime Miscellaneous
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3, or (at your option) any
11later version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT
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
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/>. */
26
27
28#define __USE_FIXED_PROTOTYPES__
29#include <stdlib.h>
30#include "objc/runtime.h"
31
32/*
33** Error handler function
34** NULL so that default is to just print to stderr
35*/
36static objc_error_handler _objc_error_handler = NULL;
37
38/* Trigger an objc error */
39void
40objc_error (id object, int code, const char *fmt, ...)
41{
42 va_list ap;
43
44 va_start (ap, fmt);
45 objc_verror (object, code, fmt, ap);
46 va_end (ap);
47}
48
49/* Trigger an objc error */
50void
51objc_verror (id object, int code, const char *fmt, va_list ap)
52{
53 BOOL result = NO;
54
55 /* Call the error handler if its there
56 Otherwise print to stderr */
57 if (_objc_error_handler)
58 result = (*_objc_error_handler) (object, code, fmt, ap);
59 else
60 vfprintf (stderr, fmt, ap);
61
62 /* Continue if the error handler says its ok
63 Otherwise abort the program */
64 if (result)
65 return;
66 else
67 abort ();
68}
69
70/* Set the error handler */
71objc_error_handler
72objc_set_error_handler (objc_error_handler func)
73{
74 objc_error_handler temp = _objc_error_handler;
75 _objc_error_handler = func;
76 return temp;
77}
78
79/*
80** Standard functions for memory allocation and disposal.
81** Users should use these functions in their ObjC programs so
82** that they work properly with garbage collectors as well as
83** can take advantage of the exception/error handling available.
84*/
85
86void *
87objc_malloc (size_t size)
88{
89 void *res = (void *) (*_objc_malloc) (size);
90 if (! res)
91 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
92 return res;
93}
94
95void *
96objc_atomic_malloc (size_t size)
97{
98 void *res = (void *) (*_objc_atomic_malloc) (size);
99 if (! res)
100 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
101 return res;
102}
103
104void *
105objc_valloc (size_t size)
106{
107 void *res = (void *) (*_objc_valloc) (size);
108 if (! res)
109 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
110 return res;
111}
112
113void *
114objc_realloc (void *mem, size_t size)
115{
116 void *res = (void *) (*_objc_realloc) (mem, size);
117 if (! res)
118 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
119 return res;
120}
121
122void *
123objc_calloc (size_t nelem, size_t size)
124{
125 void *res = (void *) (*_objc_calloc) (nelem, size);
126 if (! res)
127 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
128 return res;
129}
130
131void
132objc_free (void *mem)
133{
134 (*_objc_free) (mem);
135}
136
137/*
138** Hook functions for memory allocation and disposal.
139** This makes it easy to substitute garbage collection systems
140** such as Boehm's GC by assigning these function pointers
141** to the GC's allocation routines. By default these point
142** to the ANSI standard malloc, realloc, free, etc.
143**
144** Users should call the normal objc routines above for
145** memory allocation and disposal within their programs.
146*/
147
148#if OBJC_WITH_GC
149#include <gc.h>
150
151static void *
152GC_calloc (size_t nelem, size_t size)
153{
154 void *p = GC_malloc (nelem * size);
155 if (! p)
156 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n");
157
158 memset (p, 0, nelem * size);
159 return p;
160}
161
162static void
163noFree (void *p)
164{
165}
166
167void *(*_objc_malloc) (size_t) = GC_malloc;
168void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic;
169void *(*_objc_valloc) (size_t) = GC_malloc;
170void *(*_objc_realloc) (void *, size_t) = GC_realloc;
171void *(*_objc_calloc) (size_t, size_t) = GC_calloc;
172void (*_objc_free) (void *) = noFree;
173
174#else /* !OBJC_WITH_GC */
175
176void *(*_objc_malloc) (size_t) = malloc;
177void *(*_objc_atomic_malloc) (size_t) = malloc;
178void *(*_objc_valloc) (size_t) = malloc;
179void *(*_objc_realloc) (void *, size_t) = realloc;
180void *(*_objc_calloc) (size_t, size_t) = calloc;
181void (*_objc_free) (void *) = free;
182
183
184#endif /* !OBJC_WITH_GC */