Skip to content
Newer
Older
100644 174 lines (147 sloc) 4.49 KB
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored Apr 28, 2010
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
3 *
4 * Copyright (C) 2010, Apple Inc. All rights reserved.
5 */
6
7 #ifndef __CLASS_H_
8 #define __CLASS_H_
9
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
13
8c55086 better ivar slot system for RubyObject + misc fixes/cleanup
Laurent Sansonetti authored May 18, 2010
14 #define RCLASS_IS_OBJECT_SUBCLASS (1<<1) /* class is a true RubyObject subclass */
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored Apr 28, 2010
15 #define RCLASS_IS_RUBY_CLASS (1<<2) /* class was created from Ruby */
16 #define RCLASS_IS_MODULE (1<<3) /* class represents a Ruby Module */
17 #define RCLASS_IS_SINGLETON (1<<4) /* class represents a singleton */
18 #define RCLASS_IS_FROZEN (1<<5) /* class is frozen */
19 #define RCLASS_IS_TAINTED (1<<6) /* class is tainted */
20 #define RCLASS_IS_INCLUDED (1<<10) /* module is included */
21 #define RCLASS_HAS_ROBJECT_ALLOC (1<<11) /* class uses the default RObject alloc */
22 #define RCLASS_SCOPE_PRIVATE (1<<12) /* class opened for private methods */
23 #define RCLASS_SCOPE_PROTECTED (1<<13) /* class opened for protected methods */
24 #define RCLASS_SCOPE_MOD_FUNC (1<<14) /* class opened for module_function methods */
25 #define RCLASS_KVO_CHECK_DONE (1<<15) /* class created by KVO and flags merged */
26
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored May 26, 2010
27 typedef struct rb_class_flags_cache {
28 Class klass;
29 unsigned long value;
30 struct rb_class_flags_cache *next;
31 } rb_class_flags_cache_t;
32
33 #define CACHE_SIZE 0x1000
34
35 extern rb_class_flags_cache_t *rb_class_flags;
36
37 static unsigned int
38 rb_class_flags_hash(Class k)
39 {
40 return ((unsigned long)k >> 2) & (CACHE_SIZE - 1);
41 }
42
43 static inline unsigned long
44 rb_class_get_mask(Class k)
45 {
46 rb_class_flags_cache_t *e = &rb_class_flags[rb_class_flags_hash(k)];
47 while (e != NULL) {
48 if (e->klass == k) {
49 return e->value;
50 }
51 e = e->next;
52 }
53 return 0;
54 }
55
56 static inline void
57 rb_class_set_mask(Class k, unsigned long mask)
58 {
59 rb_class_flags_cache_t *e = &rb_class_flags[rb_class_flags_hash(k)];
60 again:
61 if (e->klass == k) {
62 set_value:
63 e->value = mask;
64 return;
65 }
66 if (e->klass == 0) {
67 e->klass = k;
68 goto set_value;
69 }
70 if (e->next != NULL) {
71 e = e->next;
72 goto again;
73 }
74 rb_class_flags_cache_t *ne = (rb_class_flags_cache_t *)malloc(
75 sizeof(rb_class_flags_cache_t));
76 ne->klass = k;
77 ne->value = mask;
78 ne->next = NULL;
79 e->next = ne;
80 }
81
82 #define RCLASS_MASK_TYPE_SHIFT 16
83
84 static inline unsigned long
85 rb_class_get_flags(Class k)
86 {
87 return rb_class_get_mask(k) >> RCLASS_MASK_TYPE_SHIFT;
88 }
89
90 static inline void
91 rb_class_set_flags(Class k, unsigned long flags)
92 {
93 rb_class_set_mask(k, flags << RCLASS_MASK_TYPE_SHIFT);
94 }
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored Apr 28, 2010
95
96 #define RCLASS_VERSION(m) (rb_class_get_flags((Class)m))
97 #define RCLASS_SET_VERSION(m,f) (rb_class_set_flags((Class)m, (unsigned long)f))
98 #define RCLASS_SET_VERSION_FLAG(m,f) (RCLASS_SET_VERSION((Class)m, (RCLASS_VERSION(m) | f)))
99
100 #define RCLASS_RUBY(m) ((RCLASS_VERSION(m) & RCLASS_IS_RUBY_CLASS) == RCLASS_IS_RUBY_CLASS)
101 #define RCLASS_MODULE(m) ((RCLASS_VERSION(m) & RCLASS_IS_MODULE) == RCLASS_IS_MODULE)
102 #define RCLASS_SINGLETON(m) ((RCLASS_VERSION(m) & RCLASS_IS_SINGLETON) == RCLASS_IS_SINGLETON)
103
104 CFMutableDictionaryRef rb_class_ivar_dict(VALUE);
105 CFMutableDictionaryRef rb_class_ivar_dict_or_create(VALUE);
106 void rb_class_ivar_set_dict(VALUE, CFMutableDictionaryRef);
107
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored May 24, 2010
108 typedef enum {
109 SCOPE_DEFAULT = 0, // public for everything but Object
110 SCOPE_PUBLIC,
111 SCOPE_PRIVATE,
112 SCOPE_PROTECTED,
113 SCOPE_MODULE_FUNC,
114 } rb_vm_scope_t;
115
116 static inline void
117 rb_vm_check_if_module(VALUE mod)
118 {
119 switch (TYPE(mod)) {
120 case T_CLASS:
121 case T_MODULE:
122 break;
123
124 default:
125 rb_raise(rb_eTypeError, "%s is not a class/module",
126 RSTRING_PTR(rb_inspect(mod)));
127 }
128 }
129
130 static inline void
131 rb_vm_set_current_scope(VALUE mod, rb_vm_scope_t scope)
132 {
133 if (scope == SCOPE_DEFAULT) {
134 scope = mod == rb_cObject ? SCOPE_PRIVATE : SCOPE_PUBLIC;
135 }
136 long v = RCLASS_VERSION(mod);
137 switch (scope) {
138 case SCOPE_PUBLIC:
139 v &= ~RCLASS_SCOPE_PRIVATE;
140 v &= ~RCLASS_SCOPE_PROTECTED;
141 v &= ~RCLASS_SCOPE_MOD_FUNC;
142 break;
143
144 case SCOPE_PRIVATE:
145 v |= RCLASS_SCOPE_PRIVATE;
146 v &= ~RCLASS_SCOPE_PROTECTED;
147 v &= ~RCLASS_SCOPE_MOD_FUNC;
148 break;
149
150 case SCOPE_PROTECTED:
151 v &= ~RCLASS_SCOPE_PRIVATE;
152 v |= RCLASS_SCOPE_PROTECTED;
153 v &= ~RCLASS_SCOPE_MOD_FUNC;
154 break;
155
156 case SCOPE_MODULE_FUNC:
157 v &= ~RCLASS_SCOPE_PRIVATE;
158 v &= ~RCLASS_SCOPE_PROTECTED;
159 v |= RCLASS_SCOPE_MOD_FUNC;
160 break;
161
162 case SCOPE_DEFAULT:
163 abort(); // handled earlier
164 }
165
166 RCLASS_SET_VERSION(mod, v);
167 }
168
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored Apr 28, 2010
169 #if defined(__cplusplus)
170 } // extern "C"
171 #endif
172
173 #endif // __CLASS_H_
Something went wrong with that request. Please try again.