]>
Commit | Line | Data |
---|---|---|
8a7d0ecc | 1 | /* GNU Objective C Runtime class related functions |
fbd26352 | 2 | Copyright (C) 1993-2019 Free Software Foundation, Inc. |
8a7d0ecc | 3 | Contributed by Kresten Krab Thorup |
4 | ||
a622d84f | 5 | This file is part of GCC. |
8a7d0ecc | 6 | |
a622d84f | 7 | GCC is free software; you can redistribute it and/or modify it under the |
8a7d0ecc | 8 | terms of the GNU General Public License as published by the Free Software |
6bc9506f | 9 | Foundation; either version 3, or (at your option) any later version. |
8a7d0ecc | 10 | |
a622d84f | 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
8a7d0ecc | 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
14 | details. | |
15 | ||
6bc9506f | 16 | Under Section 7 of GPL version 3, you are granted additional |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | You should have received a copy of the GNU General Public License and | |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
8a7d0ecc | 24 | |
e58aa1bc | 25 | #include "objc-private/common.h" |
f75aa158 | 26 | #include "objc/runtime.h" |
27 | #include "objc/thr.h" /* Required by objc-private/runtime.h. */ | |
28 | #include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */ | |
29 | #include "objc-private/runtime.h" /* the kitchen sink */ | |
8a7d0ecc | 30 | |
f75aa158 | 31 | #include <string.h> /* For memcpy() */ |
3c744362 | 32 | |
8a7d0ecc | 33 | #if OBJC_WITH_GC |
baf71228 | 34 | # include <gc/gc.h> |
35 | # include <gc/gc_typed.h> | |
8a7d0ecc | 36 | #endif |
37 | ||
3c744362 | 38 | /* FIXME: The semantics of extraBytes are not really clear. */ |
39 | inline | |
8a7d0ecc | 40 | id |
3c744362 | 41 | class_createInstance (Class class, size_t extraBytes) |
8a7d0ecc | 42 | { |
43 | id new = nil; | |
44 | ||
45 | #if OBJC_WITH_GC | |
61776355 | 46 | if (CLS_ISCLASS (class)) |
3c744362 | 47 | new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes, |
48 | (GC_descr)class->gc_object_type); | |
8a7d0ecc | 49 | #else |
61776355 | 50 | if (CLS_ISCLASS (class)) |
3c744362 | 51 | new = (id) objc_calloc (class->instance_size + extraBytes, 1); |
8a7d0ecc | 52 | #endif |
53 | ||
61776355 | 54 | if (new != nil) |
8a7d0ecc | 55 | { |
3c744362 | 56 | /* There is no need to zero the memory, since both |
57 | GC_malloc_explicitly_typed and objc_calloc return zeroed | |
58 | memory. */ | |
8a7d0ecc | 59 | new->class_pointer = class; |
60 | } | |
3c744362 | 61 | |
62 | /* TODO: Invoke C++ constructors on all appropriate C++ instance | |
63 | variables of the new object. */ | |
64 | ||
8a7d0ecc | 65 | return new; |
66 | } | |
67 | ||
3c744362 | 68 | /* Traditional GNU Objective-C Runtime API. */ |
8a7d0ecc | 69 | id |
3c744362 | 70 | object_copy (id object, size_t extraBytes) |
8a7d0ecc | 71 | { |
61776355 | 72 | if ((object != nil) && CLS_ISCLASS (object->class_pointer)) |
3c744362 | 73 | { |
74 | /* TODO: How should it work with C++ constructors ? */ | |
75 | id copy = class_createInstance (object->class_pointer, extraBytes); | |
76 | memcpy (copy, object, object->class_pointer->instance_size + extraBytes); | |
77 | return copy; | |
78 | } | |
8a7d0ecc | 79 | else |
80 | return nil; | |
81 | } | |
82 | ||
83 | id | |
61776355 | 84 | object_dispose (id object) |
8a7d0ecc | 85 | { |
61776355 | 86 | if ((object != nil) && CLS_ISCLASS (object->class_pointer)) |
8a7d0ecc | 87 | { |
3c744362 | 88 | /* TODO: Invoke C++ destructors on all appropriate C++ instance |
89 | variables. But what happens with the garbage collector ? | |
90 | Would object_dispose() be ever called in that case ? */ | |
91 | ||
92 | objc_free (object); | |
8a7d0ecc | 93 | } |
94 | return nil; | |
95 | } | |
96 | ||
631bc351 | 97 | const char * |
98 | object_getClassName (id object) | |
99 | { | |
100 | if (object != nil) | |
101 | return object->class_pointer->name; | |
102 | else | |
103 | return "Nil"; | |
104 | } | |
105 | ||
106 | Class | |
107 | object_setClass (id object, Class class_) | |
108 | { | |
109 | if (object == nil) | |
110 | return Nil; | |
111 | else | |
112 | { | |
113 | Class old_class = object->class_pointer; | |
114 | ||
115 | object->class_pointer = class_; | |
116 | return old_class; | |
117 | } | |
118 | } |