]> err.no Git - sope/blob - gnustep-objc/misc.c
improved rrule parser
[sope] / gnustep-objc / misc.c
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 #include "objc-api.h"
32
33 /*
34 ** Error handler function
35 ** NULL so that default is to just print to stderr
36 */
37 static objc_error_handler _objc_error_handler = NULL;
38
39 /* Trigger an objc error */
40 void
41 objc_error(id object, int code, const char* fmt, ...)
42 {
43   va_list ap;
44
45   va_start(ap, fmt);
46   objc_verror(object, code, fmt, ap);
47   va_end(ap);
48 }
49
50 /* Trigger an objc error */
51 void
52 objc_verror(id object, int code, const char* fmt, va_list ap)
53 {
54   BOOL result = NO;
55
56   /* Call the error handler if its there
57      Otherwise print to stderr */
58   if (_objc_error_handler)
59     result = (*_objc_error_handler)(object, code, fmt, ap);
60   else
61     vfprintf (stderr, fmt, ap);
62
63   /* Continue if the error handler says its ok
64      Otherwise abort the program */
65   if (result)
66     return;
67   else
68     abort();
69 }
70
71 /* Set the error handler */
72 objc_error_handler
73 objc_set_error_handler(objc_error_handler func)
74 {
75   objc_error_handler temp = _objc_error_handler;
76   _objc_error_handler = func;
77   return temp;
78 }
79
80 /*
81 ** Hook functions for memory allocation and disposal.
82 ** This makes it easy to substitute garbage collection systems
83 ** such as Boehm's GC by assigning these function pointers
84 ** to the GC's allocation routines.  By default these point
85 ** to the ANSI standard malloc, realloc, free, etc.
86 **
87 ** Users should call the normal objc routines above for
88 ** memory allocation and disposal within their programs.
89 */
90
91 #if OBJC_WITH_GC
92 #include "gc/gc.h"
93
94 static void *GC_calloc (size_t nelem, size_t size)
95 {
96   void* p = GC_malloc (nelem * size);
97   if (!p)
98     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n");
99
100   memset (p, 0, nelem * size);
101   return p;
102 }
103
104 static void noFree (void* p) {}
105
106 void *(*_objc_malloc)(size_t)          = GC_malloc;
107 void *(*_objc_atomic_malloc)(size_t)   = GC_malloc_atomic;
108 void *(*_objc_valloc)(size_t)          = GC_malloc;
109 void *(*_objc_realloc)(void *, size_t) = GC_realloc;
110 void *(*_objc_calloc)(size_t, size_t)  = GC_calloc;
111 void (*_objc_free)(void *)             = noFree;
112
113 #else
114
115 void *(*_objc_malloc)(size_t)          = malloc;
116 void *(*_objc_atomic_malloc)(size_t)   = malloc;
117 void *(*_objc_valloc)(size_t)          = malloc;
118 void *(*_objc_realloc)(void *, size_t) = realloc;
119 void *(*_objc_calloc)(size_t, size_t)  = calloc;
120 void (*_objc_free)(void *)             = free;
121
122
123 #endif