| 1 | /* GNU Objective C Runtime Miscellaneous
|
|---|
| 2 | Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
|
|---|
| 3 | Contributed by Kresten Krab Thorup
|
|---|
| 4 |
|
|---|
| 5 | This file is part of GNU CC.
|
|---|
| 6 |
|
|---|
| 7 | GNU CC is free software; you can redistribute it and/or modify it
|
|---|
| 8 | under the terms of the GNU General Public License as published by the
|
|---|
| 9 | Free Software Foundation; either version 2, or (at your option) any
|
|---|
| 10 | later version.
|
|---|
| 11 |
|
|---|
| 12 | GNU CC is distributed in the hope that it will be useful, but WITHOUT
|
|---|
| 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|---|
| 15 | for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with GNU CC; see the file COPYING. If not, write to the Free
|
|---|
| 19 | Software Foundation, 59 Temple Place - Suite 330,
|
|---|
| 20 | Boston, MA 02111-1307, USA. */
|
|---|
| 21 |
|
|---|
| 22 | /* As a special exception, if you link this library with files compiled with
|
|---|
| 23 | GCC to produce an executable, this does not cause the resulting executable
|
|---|
| 24 | to be covered by the GNU General Public License. This exception does not
|
|---|
| 25 | however invalidate any other reasons why the executable file might be
|
|---|
| 26 | covered by the GNU General Public License. */
|
|---|
| 27 |
|
|---|
| 28 | #define __USE_FIXED_PROTOTYPES__
|
|---|
| 29 | #include <stdlib.h>
|
|---|
| 30 | #include "runtime.h"
|
|---|
| 31 |
|
|---|
| 32 | /*
|
|---|
| 33 | ** Error handler function
|
|---|
| 34 | ** NULL so that default is to just print to stderr
|
|---|
| 35 | */
|
|---|
| 36 | static objc_error_handler _objc_error_handler = NULL;
|
|---|
| 37 |
|
|---|
| 38 | /* Trigger an objc error */
|
|---|
| 39 | void
|
|---|
| 40 | objc_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 */
|
|---|
| 50 | void
|
|---|
| 51 | objc_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 */
|
|---|
| 71 | objc_error_handler
|
|---|
| 72 | objc_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 |
|
|---|
| 86 | void *
|
|---|
| 87 | objc_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 |
|
|---|
| 95 | void *
|
|---|
| 96 | objc_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");
|
|---|
|
|---|