OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / libobjc / misc.c
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
5
6 This file is part of GCC.
7
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
11 later version.
12
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
16 for more details.
17
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.
21
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/>.  */
26
27
28 #define __USE_FIXED_PROTOTYPES__
29 #include <stdlib.h>
30 #include "objc/objc.h"
31 #include "objc/objc-api.h"
32 #include "objc-private/runtime.h"
33
34 /*
35 ** Error handler function
36 ** NULL so that default is to just print to stderr
37 */
38 static objc_error_handler _objc_error_handler = NULL;
39
40 /* Trigger an objc error */
41 void
42 objc_error (id object, int code, const char *fmt, ...)
43 {
44   va_list ap;
45
46   va_start (ap, fmt);
47   objc_verror (object, code, fmt, ap);
48   va_end (ap);
49 }
50
51 /* Trigger an objc error */
52 void
53 objc_verror (id object, int code, const char *fmt, va_list ap)
54 {
55   BOOL result = NO;
56
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);
61   else
62     vfprintf (stderr, fmt, ap);
63
64   /* Continue if the error handler says its ok
65      Otherwise abort the program */
66   if (result)
67     return;
68   else
69     abort ();
70 }
71
72 /* Set the error handler */
73 objc_error_handler
74 objc_set_error_handler (objc_error_handler func)
75 {
76   objc_error_handler temp = _objc_error_handler;
77   _objc_error_handler = func;
78   return temp;
79 }
80
81 /*
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.
86 */
87
88 void *
89 objc_malloc (size_t size)
90 {
91   void *res = (void *) (*_objc_malloc) (size);
92   if (! res)
93     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
94   return res;
95 }
96
97 void *
98 objc_atomic_malloc (size_t size)
99 {
100   void *res = (void *) (*_objc_atomic_malloc) (size);
101   if (! res)
102     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
103   return res;
104 }
105
106 void *
107 objc_valloc (size_t size)
108 {
109   void *res = (void *) (*_objc_valloc) (size);
110   if (! res)
111     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
112   return res;
113 }
114
115 void *
116 objc_realloc (void *mem, size_t size)
117 {
118   void *res = (void *) (*_objc_realloc) (mem, size);
119   if (! res)
120     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
121   return res;
122 }
123
124 void *
125 objc_calloc (size_t nelem, size_t size)
126 {
127   void *res = (void *) (*_objc_calloc) (nelem, size);
128   if (! res)
129     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
130   return res;
131 }
132
133 void
134 objc_free (void *mem)
135 {
136   (*_objc_free) (mem);
137 }
138
139 /*
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.
145 **
146 ** Users should call the normal objc routines above for
147 ** memory allocation and disposal within their programs.
148 */
149
150 #if OBJC_WITH_GC
151 #include <gc.h>
152
153 static void *
154 GC_calloc (size_t nelem, size_t size)
155 {
156   void *p = GC_malloc (nelem * size);
157   if (! p)
158     objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n");
159
160   memset (p, 0, nelem * size);
161   return p;
162 }
163
164 static void
165 noFree (void *p)
166 {
167 }
168
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;
175
176 #else   /* !OBJC_WITH_GC */
177
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;
184
185
186 #endif  /* !OBJC_WITH_GC */