src/Objects.h
author David Anderson <dvander@alliedmods.net>
Wed Dec 26 17:18:15 2012 -0800 (2012-12-26)
changeset 242 b7d14a8b918e
parent 233 bbb498ccb4ac
child 247 7721042bdb67
permissions -rw-r--r--
Fix typo.
     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     MapKind_ReferenceType,
    42     MapKind_EnumType,
    43 
    44     // Represents basic data structures. When adding to this, don't forget
    45     // to update Zone::initialize(), or you'll get a NULL crash trying to
    46     // allocate, since these maps are cached.
    47     MapKind_FixedArray,
    48     MapKind_ByteArray,
    49     MapKind_Code,
    50     MapKind_Reference,
    51     MapKind_Module,
    52     MapKind_Function,
    53     MapKind_String,
    54     MapKind_Native,
    55     MapKind_FreeRegion,     // Special map denoting freed space.
    56     MapKind_EnvironmentSlotDescriptorArray,
    57     MapKind_Environment,
    58     MapKind_Descriptor,
    59     MapKind_Box,
    60 
    61     // These do not have canonical maps.
    62     MapKind_Array,
    63     MapKind_Struct,
    64 
    65     // These represent special Map subclasses that require special tracing.
    66     MapKinds_Special,
    67     MapKind_Meta,               // (default map class)
    68     MapKind_MetaArray,          // ArrayMap instances
    69     MapKind_MetaEnvironment,    // EnvironmentMap instances
    70     MapKind_MetaStruct,         // StructMap instances
    71     MapKinds_EndSpecial,
    72 
    73     MapKinds_Total
    74 };
    75 
    76 // All garbage collected objects must inherit from GCObject. Note that using
    77 // virtual functions is illegal, as the |map| field would be displaced.
    78 class Object
    79 {
    80     Barriered<Map> map_;
    81 
    82   public:
    83     void init(Map *map) {
    84         map_ = map;
    85     }
    86 
    87     Map *map() {
    88         return map_;
    89     }
    90     inline bool is(MapKind map);
    91 
    92     inline void write(Zone *zone, Object **ptr, Object *value);
    93 
    94     static inline size_t offsetOfMap() {
    95         // Clang doesn't accept offsetof here and it's not clear why...
    96         return reinterpret_cast<size_t>(&((Object *)NULL)->map_);
    97     }
    98 };
    99 
   100 // Maps describe the layout of GCObjects. Every GCObject requires a map.
   101 class Map : public Object
   102 {
   103     MapKind kind_;
   104 
   105   public:
   106     void setKind(MapKind kind) {
   107         kind_ = kind;
   108     }
   109     MapKind kind() {
   110         return kind_;
   111     }
   112 
   113     static inline Map *cast(Object *obj) {
   114         // Note that ArrayMap is a map, but a special one: since it is non-
   115         assert(obj->map()->kind() > MapKinds_Special &&
   116                obj->map()->kind() < MapKinds_EndSpecial);
   117         return reinterpret_cast<Map *>(obj);
   118     }
   119 };
   120 
   121 inline bool
   122 Object::is(MapKind kind)
   123 {
   124     return map()->kind() == kind;
   125 }
   126 
   127 }
   128 
   129 #endif //_include_jitcraft_objects_h_