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