1 /* 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
6 This file is part of GCC.
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
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
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.
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/>. */
28 #define __USE_FIXED_PROTOTYPES__
30 #include "objc/objc.h"
31 #include "objc/objc-api.h"
32 #include "objc-private/runtime.h"
35 ** Error handler function
36 ** NULL so that default is to just print to stderr
38 static objc_error_handler _objc_error_handler = NULL;
40 /* Trigger an objc error */
42 objc_error (id object, int code, const char *fmt, ...)
47 objc_verror (object, code, fmt, ap);
51 /* Trigger an objc error */
53 objc_verror (id object, int code, const char *fmt, va_list ap)
57 /* Call the error handler if its there
58 Otherwise print to stderr */
59 if (_objc_error_handler)
60 result = (*_objc_error_handler) (object, code, fmt, ap);
62 vfprintf (stderr, fmt, ap);
64 /* Continue if the error handler says its ok
65 Otherwise abort the program */
72 /* Set the error handler */
74 objc_set_error_handler (objc_error_handler func)
76 objc_error_handler temp = _objc_error_handler;
77 _objc_error_handler = func;
82 ** Standard functions for memory allocation and disposal.
83 ** Users should use these functions in their ObjC programs so
84 ** that they work properly with garbage collectors as well as
85 ** can take advantage of the exception/error handling available.
89 objc_malloc (size_t size)
91 void *res = (void *) (*_objc_malloc) (size);
93 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
98 objc_atomic_malloc (size_t size)
100 void *res = (void *) (*_objc_atomic_malloc) (size);
102 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
107 objc_valloc (size_t size)
109 void *res = (void *) (*_objc_valloc) (size);
111 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
116 objc_realloc (void *mem, size_t size)
118 void *res = (void *) (*_objc_realloc) (mem, size);
120 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
125 objc_calloc (size_t nelem, size_t size)
127 void *res = (void *) (*_objc_calloc) (nelem, size);
129 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
134 objc_free (void *mem)
140 ** Hook functions for memory allocation and disposal.
141 ** This makes it easy to substitute garbage collection systems
142 ** such as Boehm's GC by assigning these function pointers
143 ** to the GC's allocation routines. By default these point
144 ** to the ANSI standard malloc, realloc, free, etc.
146 ** Users should call the normal objc routines above for
147 ** memory allocation and disposal within their programs.
154 GC_calloc (size_t nelem, size_t size)
156 void *p = GC_malloc (nelem * size);
158 objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n");
160 memset (p, 0, nelem * size);
169 void *(*_objc_malloc) (size_t) = GC_malloc;
170 void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic;
171 void *(*_objc_valloc) (size_t) = GC_malloc;
172 void *(*_objc_realloc) (void *, size_t) = GC_realloc;
173 void *(*_objc_calloc) (size_t, size_t) = GC_calloc;
174 void (*_objc_free) (void *) = noFree;
176 #else /* !OBJC_WITH_GC */
178 void *(*_objc_malloc) (size_t) = malloc;
179 void *(*_objc_atomic_malloc) (size_t) = malloc;
180 void *(*_objc_valloc) (size_t) = malloc;
181 void *(*_objc_realloc) (void *, size_t) = realloc;
182 void *(*_objc_calloc) (size_t, size_t) = calloc;
183 void (*_objc_free) (void *) = free;
186 #endif /* !OBJC_WITH_GC */