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/runtime.h"
33 ** Error handler function
34 ** NULL so that default is to just print to stderr
36 static objc_error_handler _objc_error_handler
= NULL
;
38 /* Trigger an objc error */
40 objc_error (id object
, int code
, const char *fmt
, ...)
45 objc_verror (object
, code
, fmt
, ap
);
49 /* Trigger an objc error */
51 objc_verror (id object
, int code
, const char *fmt
, va_list ap
)
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
);
60 vfprintf (stderr
, fmt
, ap
);
62 /* Continue if the error handler says its ok
63 Otherwise abort the program */
70 /* Set the error handler */
72 objc_set_error_handler (objc_error_handler func
)
74 objc_error_handler temp
= _objc_error_handler
;
75 _objc_error_handler
= func
;
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.
87 objc_malloc (size_t size
)
89 void *res
= (void *) (*_objc_malloc
) (size
);
91 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted\n");
96 objc_atomic_malloc (size_t size
)
98 void *res
= (void *) (*_objc_atomic_malloc
) (size
);
100 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted\n");
105 objc_valloc (size_t size
)
107 void *res
= (void *) (*_objc_valloc
) (size
);
109 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted\n");
114 objc_realloc (void *mem
, size_t size
)
116 void *res
= (void *) (*_objc_realloc
) (mem
, size
);
118 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted\n");
123 objc_calloc (size_t nelem
, size_t size
)
125 void *res
= (void *) (*_objc_calloc
) (nelem
, size
);
127 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted\n");
132 objc_free (void *mem
)
138 ** Hook functions for memory allocation and disposal.
139 ** This makes it easy to substitute garbage collection systems
140 ** such as Boehm's GC by assigning these function pointers
141 ** to the GC's allocation routines. By default these point
142 ** to the ANSI standard malloc, realloc, free, etc.
144 ** Users should call the normal objc routines above for
145 ** memory allocation and disposal within their programs.
152 GC_calloc (size_t nelem
, size_t size
)
154 void *p
= GC_malloc (nelem
* size
);
156 objc_error (nil
, OBJC_ERR_MEMORY
, "Virtual memory exhausted!\n");
158 memset (p
, 0, nelem
* size
);
167 void *(*_objc_malloc
) (size_t) = GC_malloc
;
168 void *(*_objc_atomic_malloc
) (size_t) = GC_malloc_atomic
;
169 void *(*_objc_valloc
) (size_t) = GC_malloc
;
170 void *(*_objc_realloc
) (void *, size_t) = GC_realloc
;
171 void *(*_objc_calloc
) (size_t, size_t) = GC_calloc
;
172 void (*_objc_free
) (void *) = noFree
;
174 #else /* !OBJC_WITH_GC */
176 void *(*_objc_malloc
) (size_t) = malloc
;
177 void *(*_objc_atomic_malloc
) (size_t) = malloc
;
178 void *(*_objc_valloc
) (size_t) = malloc
;
179 void *(*_objc_realloc
) (void *, size_t) = realloc
;
180 void *(*_objc_calloc
) (size_t, size_t) = calloc
;
181 void (*_objc_free
) (void *) = free
;
184 #endif /* !OBJC_WITH_GC */