Ruby: include/ruby/internal/newobj.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

newobj.h
Go to the documentation of this file.
1#ifndef RBIMPL_NEWOBJ_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_NEWOBJ_H
23#include "ruby/internal/cast.h"
24#include "ruby/internal/core/rbasic.h"
25#include "ruby/internal/dllexport.h"
26#include "ruby/internal/fl_type.h"
27#include "ruby/internal/special_consts.h"
28#include "ruby/internal/value.h"
29#include "ruby/assert.h"
30
31#define RB_NEWOBJ(obj,type) type *(obj) = RBIMPL_CAST((type *)rb_newobj())
32#define RB_NEWOBJ_OF(obj,type,klass,flags) type *(obj) = RBIMPL_CAST((type *)rb_newobj_of(klass, flags))
33
34#define NEWOBJ RB_NEWOBJ
35#define NEWOBJ_OF RB_NEWOBJ_OF /* core has special NEWOBJ_OF() in internal.h */
36#define OBJSETUP rb_obj_setup /* use NEWOBJ_OF instead of NEWOBJ()+OBJSETUP() */
37#define CLONESETUP rb_clone_setup
38#define DUPSETUP rb_dup_setup
39
40 RBIMPL_SYMBOL_EXPORT_BEGIN()
41 VALUE rb_newobj(void);
42 VALUE rb_newobj_of(VALUE, VALUE);
43 VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type);
44 VALUE rb_obj_class(VALUE);
45 VALUE rb_singleton_class_clone(VALUE);
46void rb_singleton_class_attached(VALUE,VALUE);
47void rb_copy_generic_ivar(VALUE,VALUE);
48 RBIMPL_SYMBOL_EXPORT_END()
49
50static inline void
51rb_clone_setup(VALUE clone, VALUE obj)
52{
53 RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(obj));
54 RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(clone));
55
56 const VALUE flags = RUBY_FL_PROMOTED0 | RUBY_FL_PROMOTED1 | RUBY_FL_FINALIZE;
57 rb_obj_setup(clone, rb_singleton_class_clone(obj),
58 RB_FL_TEST_RAW(obj, ~flags));
59 rb_singleton_class_attached(RBASIC_CLASS(clone), clone);
60 if (RB_FL_TEST(obj, RUBY_FL_EXIVAR)) rb_copy_generic_ivar(clone, obj);
61}
62
63static inline void
64rb_dup_setup(VALUE dup, VALUE obj)
65{
66 RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(obj));
67 RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(dup));
68
69 rb_obj_setup(dup, rb_obj_class(obj), RB_FL_TEST_RAW(obj, RUBY_FL_DUPPED));
70 if (RB_FL_TEST(obj, RUBY_FL_EXIVAR)) rb_copy_generic_ivar(dup, obj);
71}
72
73#endif /* RBIMPL_NEWOBJ_H */
assert.h
RBIMPL_ASSERT_OR_ASSUME
#define RBIMPL_ASSERT_OR_ASSUME(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition: assert.h:229
cast.h
Defines RBIMPL_CAST.
dllexport.h
Tewaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
fl_type.h
Defines enum ruby_fl_type.
RUBY_FL_DUPPED
@ RUBY_FL_DUPPED
Definition: fl_type.h:207
RUBY_FL_PROMOTED1
@ RUBY_FL_PROMOTED1
Definition: fl_type.h:165
RUBY_FL_PROMOTED0
@ RUBY_FL_PROMOTED0
Definition: fl_type.h:164
RUBY_FL_EXIVAR
@ RUBY_FL_EXIVAR
Definition: fl_type.h:172
RUBY_FL_FINALIZE
@ RUBY_FL_FINALIZE
Definition: fl_type.h:167
rb_singleton_class_clone
VALUE rb_singleton_class_clone(VALUE)
Definition: class.c:459
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE, VALUE)
Attach a object to a singleton class.
Definition: class.c:530
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:245
rb_obj_setup
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common (RBasic) fields in obj.
Definition: object.c:126
rb_copy_generic_ivar
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1638
rb_newobj
VALUE rb_newobj(void)
Definition: gc.c:2365
rb_newobj_of
VALUE rb_newobj_of(VALUE, VALUE)
Definition: gc.c:2371
rbasic.h
Defines struct RBasic.
RBASIC_CLASS
#define RBASIC_CLASS
Definition: rbasic.h:35
special_consts.h
Defines enum ruby_special_consts.
value.h
Defines VALUE and ID.
VALUE
unsigned long VALUE
Definition: value.h:38