@@ -37,6 +37,7 @@ but the pointer type will always be `VALUE` (figure 1).
3737Here is the definition of `VALUE`:
3838
3939▼ `VALUE`
40+
4041<pre class="longlist">
4142 71 typedef unsigned long VALUE;
4243
@@ -73,6 +74,7 @@ something like the following.
7374Let's look at the definition of a few object structures.
7475
7576▼ Examples of object structure
77+
7678<pre class="longlist">
7779 /* structure for ordinary objects */
7880 295 struct RObject {
@@ -110,10 +112,11 @@ general.
110112
111113First, as `VALUE` is defined as `unsigned long`, it must be cast before
112114being used. That's why `Rxxxx()` macros have been made for each object
113- structure. For example, for `struct RString` there is `RSTRING()`, for `struct
114- RArray` there is `RARRAY()`, etc... These macros are used like this:
115+ structure. For example, for `struct RString` there is `RSTRING()`, for
116+ `struct RArray` there is `RARRAY()`, etc... These macros are used like this:
115117
116118<pre class="emlist">
119+
117120VALUE str = ....;
118121VALUE arr = ....;
119122RSTRING(str)->len; /* ((struct RString*)str)->len */
@@ -132,6 +135,7 @@ information shared by all object structures. The definition of `struct RBasic`
132135is the following:
133136
134137▼ `struct RBasic`
138+
135139<pre class="longlist">
136140 290 struct RBasic {
137141 291 unsigned long flags;
@@ -169,13 +173,13 @@ reserved word.
169173
170174h4. About structure types
171175
172- I said that the type of structure is stored in the `flags` member of `struct
173- Basic`. But why do we have to store the type of structure? It's to be able to
174- handle all different types of structure via `VALUE`. If you cast a pointer to
175- a structure to `VALUE`, as the type information does not remain, the compiler
176- won't be able to help. Therefore we have to manage the type ourselves. That's
177- the consequence of being able to handle all the structure types in a unified
178- way.
176+ I said that the type of structure is stored in the `flags` member of
177+ `struct Basic`. But why do we have to store the type of structure? It's to be
178+ able to handle all different types of structure via `VALUE`. If you cast a
179+ pointer to a structure to `VALUE`, as the type information does not remain,
180+ the compiler won't be able to help. Therefore we have to manage the type
181+ ourselves. That's the consequence of being able to handle all the structure
182+ types in a unified way.
179183
180184OK, but the used structure is defined by the class so why are the structure
181185type and class are stored separately? Being able to find the structure type
@@ -243,6 +247,7 @@ Then, let's see in practice the `INT2FIX()` macro that converts from a C `int`
243247to a `Fixnum`, and confirm that `Fixnum` are directly embedded in `VALUE`.
244248
245249▼ `INT2FIX`
250+
246251<pre class="longlist">
247252 123 #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG))
248253 122 #define FIXNUM_FLAG 0x01
@@ -277,6 +282,7 @@ why symbols were necessary. First, let's start with the `ID` type used inside
277282`ruby`. It's like this:
278283
279284▼ `ID`
285+
280286<pre class="longlist">
281287 72 typedef unsigned long ID;
282288
@@ -310,6 +316,7 @@ why `Symbol`, like `Fixnum`, was made stored in `VALUE`. Let's look at the
310316`ID2SYM()` macro converting `ID` to `Symbol` object.
311317
312318▼ `ID2SYM`
319+
313320<pre class="longlist">
314321 158 #define SYMBOL_FLAG 0x0e
315322 160 #define ID2SYM(x) ((VALUE)(((long)(x))<<8|SYMBOL_FLAG))
@@ -326,6 +333,7 @@ any other `VALUE`. Quite a clever trick.
326333Finally, let's see the reverse conversion of `ID2SYM()`, `SYM2ID()`.
327334
328335▼ `SYM2ID()`
336+
329337<pre class="longlist">
330338 161 #define SYM2ID(x) RSHIFT((long)x,8)
331339
@@ -342,6 +350,7 @@ values. `nil` is an object used to denote that there is no object. Their
342350values at the C level are defined like this:
343351
344352▼ `true false nil`
353+
345354<pre class="longlist">
346355 164 #define Qfalse 0 /* Ruby's false */
347356 165 #define Qtrue 2 /* Ruby's true */
@@ -362,6 +371,7 @@ For `Qnil`, there is a macro dedicated to check if a `VALUE` is `Qnil` or not,
362371`NIL_P()`.
363372
364373▼ `NIL_P()`
374+
365375<pre>
366376 170 #define NIL_P(v) ((VALUE)(v) == Qnil)
367377
@@ -378,6 +388,7 @@ However, in C, `nil` (`Qnil`) is true. That's why in C a Ruby-style macro,
378388`RTEST()`, has been created.
379389
380390▼ `RTEST()`
391+
381392<pre class="longlist">
382393 169 #define RTEST(v) (((VALUE)(v) & ~Qnil) != 0)
383394
@@ -398,6 +409,7 @@ not have the fun answer I was expecting...
398409h4. `Qundef`
399410
400411▼ `Qundef`
412+
401413<pre class="longlist">
402414 167 #define Qundef 6 /* undefined value for placeholder */
403415
@@ -425,6 +437,7 @@ content. That's why modules also use the `struct RClass` structure, and are
425437differentiated by the `T_MODULE` structure flag.
426438
427439▼ `struct RClass`
440+
428441<pre class="longlist">
429442 300 struct RClass {
430443 301 struct RBasic basic;
@@ -443,21 +456,21 @@ names to objects. In the case of `m_tbl`, it keeps the
443456correspondence between the name (`ID`) of the methods possessed by this class
444457and the methods entity itself.
445458
446- The fourth member `super` keeps, like its name suggests, the superclass. As it's a
447- `VALUE`, it's (a pointer to) the class object of the superclass. In Ruby there
448- is only one class that has no superclass (the root class): `Object`.
459+ The fourth member `super` keeps, like its name suggests, the superclass. As
460+ it's a `VALUE`, it's (a pointer to) the class object of the superclass. In Ruby
461+ there is only one class that has no superclass (the root class): `Object`.
449462
450463However I already said that all `Object` methods are defined in the `Kernel`
451464module, `Object` just includes it. As modules are functionally similar to
452- multiple inheritance, it may seem having just `super` is problematic, but but
465+ multiple inheritance, it may seem having just `super` is problematic, but
453466in `ruby` some clever changes are made to make it look like single
454467inheritance. The details of this process will be explained in the fourth
455468chapter "Classes and modules".
456469
457- Because of this, `super` of the structure of `Object` points to `struct
458- RClass` of the `Kernel` object. Only the `super` of Kernel is NULL. So
459- contrary to what I said, if `super` is NULL, this `RClass` is the `Kernel`
460- object (figure 6).
470+ Because of this, `super` of the structure of `Object` points to `struct RClass`
471+ of the `Kernel` object. Only the `super` of Kernel is NULL. So contrary to
472+ what I said, if `super` is NULL, this `RClass` is the `Kernel` object (figure
473+ 6).
461474
462475!images/ch_object_classtree.png(Class tree at the C level)!
463476
@@ -472,6 +485,7 @@ must not be defined.
472485The sequential search process in `m_tbl` is done by `search_method()`.
473486
474487▼ `search_method()`
488+
475489<pre class="longlist">
476490 256 static NODE*
477491 257 search_method(klass, id, origin)
@@ -523,6 +537,7 @@ all right but how is it in practice? Let's look at the function
523537`rb_ivar_set()` that puts an object in an instance variable.
524538
525539▼ `rb_ivar_set()`
540+
526541<pre class="longlist">
527542 /* write val in the id instance of obj */
528543 984 VALUE
@@ -579,6 +594,7 @@ Well, let's go back to `rb_ivar_set()`. It seems only the treatments of
579594the basis that their second member is `iv_tbl`. Let's confirm it in practice.
580595
581596▼ Structures whose second member is `iv_tbl`
597+
582598<pre class="longlist">
583599 /* TYPE(val) == T_OBJECT */
584600 295 struct RObject {
@@ -636,6 +652,7 @@ For objects for which the structure used is not `T_OBJECT`, `T_MODULE`, or
636652`T_CLASS`, what happens when modifying an instance variable?
637653
638654▼ `rb_ivar_set()` in the case there is no `iv_tbl`
655+
639656<pre class="longlist">
6406571000 default:
6416581001 generic_ivar_set(obj, id, val);
@@ -658,6 +675,7 @@ global `st_table`, `generic_iv_table` (figure 7) for these associations.
658675Let's see this in practice.
659676
660677▼ `generic_ivar_set()`
678+
661679<pre class="longlist">
662680 801 static st_table *generic_iv_tbl;
663681
@@ -730,8 +748,8 @@ of course a bit check is way faster than searching a `struct st_table`.
730748h3. Gaps in structures
731749
732750Now you should understand how the instance variables are stored, but why are
733- there structures without `iv_tbl`? Why is there no `iv_tbl` in `struct
734- RString` or `struct RArray`? Couldn't `iv_tbl` be part of `RBasic`?
751+ there structures without `iv_tbl`? Why is there no `iv_tbl` in
752+ `struct RString` or `struct RArray`? Couldn't `iv_tbl` be part of `RBasic`?
735753
736754Well, this could have been done, but there are good reasons why it was not. As
737755a matter of fact, this problem is deeply linked to the way `ruby` manages
@@ -750,12 +768,12 @@ possible to regroup structures of similar size is desirable. The details about
750768
751769Generally the most used structure is `struct RString`. After that, in programs
752770there are `struct RArray` (array), `RHash` (hash), `RObject` (user defined
753- object), etc. However, this `struct RObject` only uses the space of `struct
754- RBasic` + 1 pointer. On the other hand, `struct RString`, `RArray` and `RHash`
755- take the space of `struct RBasic` + 3 pointers. In other words, when putting a
756- `struct RObject` in the shared entity, the space for 2 pointers is useless.
757- And beyond that, if `RString` had 4 pointers, `RObject` would use less that
758- half the size of the shared entity. As you would expect, it's wasteful.
771+ object), etc. However, this `struct RObject` only uses the space of
772+ `struct RBasic` + 1 pointer. On the other hand, `struct RString`, `RArray` and
773+ `RHash` take the space of `struct RBasic` + 3 pointers. In other words, when
774+ putting a `struct RObject` in the shared entity, the space for 2 pointers is
775+ useless. And beyond that, if `RString` had 4 pointers, `RObject` would use less
776+ that half the size of the shared entity. As you would expect, it's wasteful.
759777
760778So the received merit for `iv_tbl` is more or less saving memory and speeding
761779up. Furthermore we do not know if it is used often or not. In the facts,
@@ -773,6 +791,7 @@ We saw the `rb_ivar_set()` function that sets variables, so let's see quickly
773791how to get them.
774792
775793▼ `rb_ivar_get()`
794+
776795<pre class="longlist">
777796 960 VALUE
778797 961 rb_ivar_get(obj, id)
@@ -812,7 +831,7 @@ The structure is strictly the same.
812831`st_lookup()` finds the relation, it returns true, so the whole `if` can be
813832read as "If the instance variable has been set, return its value".
814833
815- ( C) If no correspondence could be found, in other words if we read an
834+ ( C) If no correspondence could be found, in other words if we read an
816835instance variable that has not been set, we first leave the `if` then the
817836`switch`. `rb_warning()` will then issue a warning and `nil` will be returned.
818837That's because you can read instance variables that have not been set in Ruby.
@@ -841,6 +860,7 @@ h3. `struct RString`
841860its subclasses.
842861
843862▼ `struct RString`
863+
844864<pre class="longlist">
845865 314 struct RString {
846866 315 struct RBasic basic;
@@ -953,6 +973,7 @@ h3. `struct RArray`
953973`Array`.
954974
955975▼ `struct RArray`
976+
956977<pre class="longlist">
957978 324 struct RArray {
958979 325 struct RBasic basic;
@@ -967,11 +988,11 @@ h3. `struct RArray`
967988(ruby.h)
968989</pre>
969990
970- Except for the type of `ptr`, this structure is almost the same as `struct
971- RString`. `ptr` points to the content of the array, and `len` is its length.
972- `aux` is exactly the same as in `struct RString`. `aux.capa` is the "real"
973- length of the memory pointed by `ptr`, and if `ptr` is shared, `aux.shared`
974- stores the shared original array object.
991+ Except for the type of `ptr`, this structure is almost the same as
992+ `struct RString`. `ptr` points to the content of the array, and `len` is its
993+ length. `aux` is exactly the same as in `struct RString`. `aux.capa` is the
994+ "real" length of the memory pointed by `ptr`, and if `ptr` is shared,
995+ `aux.shared` stores the shared original array object.
975996
976997From this structure, it's clear that Ruby's `Array` is an array and not a
977998list. So when the number of elements changes in a big way, a `realloc()` must
@@ -1003,6 +1024,7 @@ h3. `struct RRegexp`
10031024It's the structure for the instances of the regular expression class `Regexp`.
10041025
10051026▼ `struct RRegexp`
1027+
10061028<pre class="longlist">
10071029 334 struct RRegexp {
10081030 335 struct RBasic basic;
@@ -1027,6 +1049,7 @@ h3. `struct RHash`
10271049`struct RHash` is the structure for Ruby's `Hash` objects.
10281050
10291051▼ `struct RHash`
1052+
10301053<pre class="longlist">
10311054 341 struct RHash {
10321055 342 struct RBasic basic;
@@ -1050,6 +1073,7 @@ h3. `struct RFile`
10501073its subclasses.
10511074
10521075▼ `struct RFile`
1076+
10531077<pre class="longlist">
10541078 348 struct RFile {
10551079 349 struct RBasic basic;
@@ -1060,6 +1084,7 @@ its subclasses.
10601084</pre>
10611085
10621086▼ `OpenFile`
1087+
10631088<pre class="longlist">
10641089 19 typedef struct OpenFile {
10651090 20 FILE *f; /* stdio ptr for read/write */
@@ -1090,6 +1115,7 @@ for managing a pointer to a user defined structure" has been created on
10901115`ruby`'s side to manage this. This structure is `struct RData`.
10911116
10921117▼ `struct RData`
1118+
10931119<pre class="longlist">
10941120 353 struct RData {
10951121 354 struct RBasic basic;
0 commit comments