]>
Commit | Line | Data |
---|---|---|
7b869986 | 1 | /* GNU Objective C Runtime Memory allocation functions |
99dee823 | 2 | Copyright (C) 1993-2021 Free Software Foundation, Inc. |
88e17b57 BE |
3 | Contributed by Kresten Krab Thorup |
4 | ||
38709cad | 5 | This file is part of GCC. |
88e17b57 | 6 | |
38709cad | 7 | GCC is free software; you can redistribute it and/or modify it |
88e17b57 | 8 | under the terms of the GNU General Public License as published by the |
748086b7 | 9 | Free Software Foundation; either version 3, or (at your option) any |
88e17b57 BE |
10 | later version. |
11 | ||
38709cad | 12 | GCC is distributed in the hope that it will be useful, but WITHOUT |
88e17b57 BE |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
748086b7 JJ |
17 | Under Section 7 of GPL version 3, you are granted additional |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
88e17b57 | 25 | |
575584a9 NP |
26 | /* This file includes the standard functions for memory allocation and |
27 | disposal. Users should use these functions in their ObjC programs | |
28 | so that they work properly with garbage collectors. */ | |
d1be5d82 | 29 | |
bc18535a NP |
30 | /* TODO: Turn these into macros or inline functions. */ |
31 | ||
6dead247 | 32 | #include "objc-private/common.h" |
7b869986 | 33 | #include "objc-private/error.h" |
88e17b57 | 34 | |
6dead247 NP |
35 | /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for |
36 | malloc, free, etc. on some platforms. It is unclear if we still | |
575584a9 | 37 | need it, but it can't hurt. */ |
88e17b57 BE |
38 | #define __USE_FIXED_PROTOTYPES__ |
39 | #include <stdlib.h> | |
6dead247 | 40 | |
718a8e53 | 41 | #include "objc/runtime.h" |
88e17b57 | 42 | |
d1be5d82 | 43 | #if OBJC_WITH_GC |
114bf3f1 | 44 | #include <gc/gc.h> |
88e17b57 BE |
45 | |
46 | void * | |
40165636 | 47 | objc_malloc (size_t size) |
88e17b57 | 48 | { |
d1be5d82 | 49 | void *res = (void *)(GC_malloc (size)); |
40165636 | 50 | if (! res) |
7b869986 | 51 | _objc_abort ("Virtual memory exhausted\n"); |
88e17b57 BE |
52 | return res; |
53 | } | |
54 | ||
55 | void * | |
40165636 | 56 | objc_atomic_malloc (size_t size) |
88e17b57 | 57 | { |
d1be5d82 | 58 | void *res = (void *)(GC_malloc_atomic (size)); |
40165636 | 59 | if (! res) |
7b869986 | 60 | _objc_abort ("Virtual memory exhausted\n"); |
88e17b57 BE |
61 | return res; |
62 | } | |
63 | ||
64 | void * | |
d1be5d82 NP |
65 | objc_realloc (void *mem, size_t size) |
66 | { | |
67 | void *res = (void *)(GC_realloc (mem, size)); | |
68 | if (! res) | |
69 | _objc_abort ("Virtual memory exhausted\n"); | |
70 | return res; | |
71 | } | |
72 | ||
73 | void * | |
74 | objc_calloc (size_t nelem, size_t size) | |
75 | { | |
76 | /* Note that GC_malloc returns cleared memory (see documentation) so | |
77 | there is no need to clear it. */ | |
7e268280 | 78 | void *res = (void *)(GC_malloc (nelem * size)); |
d1be5d82 NP |
79 | if (! res) |
80 | _objc_abort ("Virtual memory exhausted\n"); | |
81 | return res; | |
82 | } | |
83 | ||
84 | void | |
bc18535a | 85 | objc_free (void *mem __attribute__ ((__unused__))) |
d1be5d82 NP |
86 | { |
87 | return; | |
88 | } | |
89 | ||
90 | #else | |
91 | ||
92 | void * | |
93 | objc_malloc (size_t size) | |
88e17b57 | 94 | { |
d1be5d82 NP |
95 | void *res = (void *)(malloc (size)); |
96 | if (! res) | |
97 | _objc_abort ("Virtual memory exhausted\n"); | |
98 | return res; | |
99 | } | |
100 | ||
101 | void * | |
102 | objc_atomic_malloc (size_t size) | |
103 | { | |
104 | void *res = (void *)(malloc (size)); | |
40165636 | 105 | if (! res) |
7b869986 | 106 | _objc_abort ("Virtual memory exhausted\n"); |
88e17b57 BE |
107 | return res; |
108 | } | |
109 | ||
110 | void * | |
40165636 | 111 | objc_realloc (void *mem, size_t size) |
88e17b57 | 112 | { |
d1be5d82 | 113 | void *res = (void *)(realloc (mem, size)); |
40165636 | 114 | if (! res) |
7b869986 | 115 | _objc_abort ("Virtual memory exhausted\n"); |
88e17b57 BE |
116 | return res; |
117 | } | |
118 | ||
119 | void * | |
40165636 | 120 | objc_calloc (size_t nelem, size_t size) |
88e17b57 | 121 | { |
d1be5d82 | 122 | void *res = (void *)(calloc (nelem, size)); |
40165636 | 123 | if (! res) |
7b869986 | 124 | _objc_abort ("Virtual memory exhausted\n"); |
88e17b57 BE |
125 | return res; |
126 | } | |
127 | ||
128 | void | |
40165636 | 129 | objc_free (void *mem) |
88e17b57 | 130 | { |
d1be5d82 | 131 | free (mem); |
88e17b57 BE |
132 | } |
133 | ||
d1be5d82 | 134 | #endif /* !OBJC_WITH_GC */ |