OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / libobjc / memory.c
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
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 #include "objc-private/common.h"
28 #include "objc-private/error.h"
29
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.
33 */
34 #define __USE_FIXED_PROTOTYPES__
35 #include <stdlib.h>
36
37 #include "objc/objc.h"
38 #include "objc/objc-api.h"
39 #include "objc-private/runtime.h"
40
41 /*
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.
46 */
47
48 void *
49 objc_malloc (size_t size)
50 {
51   void *res = (void *) (*_objc_malloc) (size);
52   if (! res)
53     _objc_abort ("Virtual memory exhausted\n");
54   return res;
55 }
56
57 void *
58 objc_atomic_malloc (size_t size)
59 {
60   void *res = (void *) (*_objc_atomic_malloc) (size);
61   if (! res)
62     _objc_abort ("Virtual memory exhausted\n");
63   return res;
64 }
65
66 void *
67 objc_valloc (size_t size)
68 {
69   void *res = (void *) (*_objc_valloc) (size);
70   if (! res)
71     _objc_abort ("Virtual memory exhausted\n");
72   return res;
73 }
74
75 void *
76 objc_realloc (void *mem, size_t size)
77 {
78   void *res = (void *) (*_objc_realloc) (mem, size);
79   if (! res)
80     _objc_abort ("Virtual memory exhausted\n");
81   return res;
82 }
83
84 void *
85 objc_calloc (size_t nelem, size_t size)
86 {
87   void *res = (void *) (*_objc_calloc) (nelem, size);
88   if (! res)
89     _objc_abort ("Virtual memory exhausted\n");
90   return res;
91 }
92
93 void
94 objc_free (void *mem)
95 {
96   (*_objc_free) (mem);
97 }
98
99 /*
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,
104   etc.
105
106   Users should call the normal objc routines above for memory
107   allocation and disposal within their programs.
108 */
109
110 #if OBJC_WITH_GC
111 #include <gc.h>
112
113 /* FIXME: The following sounds pointless because the GC_malloc
114    documentation says that it returns memory that is already zeroed!
115 */
116 static void *
117 GC_calloc (size_t nelem, size_t size)
118 {
119   void *p = GC_malloc (nelem * size);
120   if (! p)
121     _objc_abort ("Virtual memory exhausted!\n");
122
123   memset (p, 0, nelem * size);
124   return p;
125 }
126
127 static void
128 noFree (void *p)
129 {
130 }
131
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;
138
139 #else   /* !OBJC_WITH_GC */
140
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;
147
148
149 #endif  /* !OBJC_WITH_GC */