1 /* GNU Objective C Runtime Memory allocation functions
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009, 2010
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/>. */
27 #include "objc-private/common.h"
28 #include "objc-private/error.h"
30 /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
31 malloc, free, etc. on some platforms. It is unclear if we still
32 need it, but it can't hurt.
34 #define __USE_FIXED_PROTOTYPES__
37 #include "objc/objc.h"
38 #include "objc/objc-api.h"
39 #include "objc-private/runtime.h"
42 Standard functions for memory allocation and disposal. Users should
43 use these functions in their ObjC programs so that they work
44 properly with garbage collectors as well as can take advantage of
45 the exception/error handling available.
49 objc_malloc (size_t size)
51 void *res = (void *) (*_objc_malloc) (size);
53 _objc_abort ("Virtual memory exhausted\n");
58 objc_atomic_malloc (size_t size)
60 void *res = (void *) (*_objc_atomic_malloc) (size);
62 _objc_abort ("Virtual memory exhausted\n");
67 objc_valloc (size_t size)
69 void *res = (void *) (*_objc_valloc) (size);
71 _objc_abort ("Virtual memory exhausted\n");
76 objc_realloc (void *mem, size_t size)
78 void *res = (void *) (*_objc_realloc) (mem, size);
80 _objc_abort ("Virtual memory exhausted\n");
85 objc_calloc (size_t nelem, size_t size)
87 void *res = (void *) (*_objc_calloc) (nelem, size);
89 _objc_abort ("Virtual memory exhausted\n");
100 Hook functions for memory allocation and disposal. This makes it
101 easy to substitute garbage collection systems such as Boehm's GC by
102 assigning these function pointers to the GC's allocation routines.
103 By default these point to the ANSI standard malloc, realloc, free,
106 Users should call the normal objc routines above for memory
107 allocation and disposal within their programs.
113 /* FIXME: The following sounds pointless because the GC_malloc
114 documentation says that it returns memory that is already zeroed!
117 GC_calloc (size_t nelem, size_t size)
119 void *p = GC_malloc (nelem * size);
121 _objc_abort ("Virtual memory exhausted!\n");
123 memset (p, 0, nelem * size);
132 void *(*_objc_malloc) (size_t) = GC_malloc;
133 void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic;
134 void *(*_objc_valloc) (size_t) = GC_malloc;
135 void *(*_objc_realloc) (void *, size_t) = GC_realloc;
136 void *(*_objc_calloc) (size_t, size_t) = GC_calloc;
137 void (*_objc_free) (void *) = noFree;
139 #else /* !OBJC_WITH_GC */
141 void *(*_objc_malloc) (size_t) = malloc;
142 void *(*_objc_atomic_malloc) (size_t) = malloc;
143 void *(*_objc_valloc) (size_t) = malloc;
144 void *(*_objc_realloc) (void *, size_t) = realloc;
145 void *(*_objc_calloc) (size_t, size_t) = calloc;
146 void (*_objc_free) (void *) = free;
149 #endif /* !OBJC_WITH_GC */