src/Objects.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 15:00:08 2012 -0800 (2012-11-18)
changeset 214 e1a6deaa6ebe
parent 209 5cbec15fadfe
child 222 0cde1379b29f
permissions -rw-r--r--
Fix timeout handling in the test harness.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jitcraft_objects_h_
    20 #define _include_jitcraft_objects_h_
    21 
    22 #include "Utility.h"
    23 #include "Spaces.h"
    24 #include "Barriers.h"
    25 
    26 namespace ke {
    27 
    28 class Zone;
    29 
    30 // Garbage-collection objects require a header to describe their layout. This
    31 // header is called a "map". The different kinds of maps are enumerated below.
    32 class Map;
    33 
    34 enum MapKind
    35 {
    36     // Represents Type objects.
    37     MapKind_Type,
    38     MapKind_FunctionType,
    39     MapKind_StructType,
    40     MapKind_ArrayType,
    41 
    42     // Represents basic data structures. When adding to this, don't forget
    43     // to update Zone::initialize(), or you'll get a NULL crash trying to
    44     // allocate, since these maps are cached.
    45     MapKind_FixedArray,
    46     MapKind_ByteArray,
    47     MapKind_Code,
    48     MapKind_Reference,
    49     MapKind_Module,
    50     MapKind_Function,
    51     MapKind_String,
    52     MapKind_Native,
    53     MapKind_FreeRegion,     // Special map denoting freed space.
    54     MapKind_EnvironmentSlotDescriptorArray,
    55     MapKind_Environment,
    56     MapKind_Descriptor,
    57     MapKind_Box,
    58 
    59     // These do not have canonical maps.
    60     MapKind_Array,
    61     MapKind_Struct,
    62 
    63     // These represent special Map subclasses that require special tracing.
    64     MapKinds_Special,
    65     MapKind_Meta,               // (default map class)
    66     MapKind_MetaArray,          // ArrayMap instances
    67     MapKind_MetaEnvironment,    // EnvironmentMap instances
    68     MapKind_MetaStruct,         // StructMap instances
    69     MapKinds_EndSpecial,
    70 
    71     MapKinds_Total
    72 };
    73 
    74 // All garbage collected objects must inherit from GCObject. Note that using
    75 // virtual functions is illegal, as the |map| field would be displaced.
    76 class Object
    77 {
    78     Barriered<Map> map_;
    79 
    80   public:
    81     void init(Map *map) {
    82         map_ = map;
    83     }
    84 
    85     Map *map() {
    86         return map_;
    87     }
    88     inline bool is(MapKind map);
    89 
    90     inline void write(Zone *zone, Object **ptr, Object *value);
    91 
    92     static inline size_t offsetOfMap() {
    93         // Clang doesn't accept offsetof here and it's not clear why...
    94         return reinterpret_cast<size_t>(&((Object *)NULL)->map_);
    95     }
    96 };
    97 
    98 // Maps describe the layout of GCObjects. Every GCObject requires a map.
    99 class Map : public Object
   100 {
   101     MapKind kind_;
   102 
   103   public:
   104     void setKind(MapKind kind) {
   105         kind_ = kind;
   106     }
   107     MapKind kind() {
   108         return kind_;
   109     }
   110 
   111     static inline Map *cast(Object *obj) {
   112         // Note that ArrayMap is a map, but a special one: since it is non-
   113         assert(obj->map()->kind() > MapKinds_Special &&
   114                obj->map()->kind() < MapKinds_EndSpecial);
   115         return reinterpret_cast<Map *>(obj);
   116     }
   117 };
   118 
   119 inline bool
   120 Object::is(MapKind kind)
   121 {
   122     return map()->kind() == kind;
   123 }
   124 
   125 }
   126 
   127 #endif //_include_jitcraft_objects_h_