jitcraft/Objects.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 10 02:11:13 2012 -0700 (2012-07-10)
changeset 68 b40b0e18a918
parent 66 cbba1997dc71
child 85 06a0015b6a5c
permissions -rw-r--r--
Added support for ISourcePawnEngine natives.
     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 
    39     // Represents basic data structures. When adding to this, don't forget
    40     // to update Zone::initialize(), or you'll get a NULL crash trying to
    41     // allocate, since these maps are cached.
    42     MapKind_FixedArray,
    43     MapKind_ByteArray,
    44     MapKind_Code,
    45     MapKind_Reference,
    46     MapKind_Module,
    47     MapKind_Function,
    48     MapKind_String,
    49     MapKind_Native,
    50     MapKind_FreeRegion,     // Special map denoting freed space.
    51 
    52     // Used to represent Arrays. This does not have a canonical map.
    53     MapKind_Array,
    54 
    55     // These represent special Map subclasses that require special tracing.
    56     MapKinds_Special,
    57     MapKind_Meta,           // (default map class)
    58     MapKind_MetaArray,      // ArrayMap instances
    59     MapKinds_EndSpecial,
    60 
    61     MapKinds_Total
    62 };
    63 
    64 // All garbage collected objects must inherit from GCObject. Note that using
    65 // virtual functions is illegal, as the |map| field would be displaced.
    66 class Object
    67 {
    68     Barriered<Map> map_;
    69 
    70   public:
    71     void init(Map *map) {
    72         map_ = map;
    73     }
    74 
    75     Map *map() {
    76         return map_;
    77     }
    78     inline bool is(MapKind map);
    79 
    80     inline void write(Zone *zone, Object **ptr, Object *value);
    81 
    82     static inline size_t offsetOfMap() {
    83         return offsetof(Object, map_);
    84     }
    85 };
    86 
    87 // Maps describe the layout of GCObjects. Every GCObject requires a map.
    88 class Map : public Object
    89 {
    90     MapKind kind_;
    91 
    92   public:
    93     void setKind(MapKind kind) {
    94         kind_ = kind;
    95     }
    96     MapKind kind() {
    97         return kind_;
    98     }
    99 
   100     static inline Map *cast(Object *obj) {
   101         // Note that ArrayMap is a map, but a special one: since it is non-
   102         assert(obj->map()->kind() > MapKinds_Special &&
   103                obj->map()->kind() < MapKinds_EndSpecial);
   104         return reinterpret_cast<Map *>(obj);
   105     }
   106 };
   107 
   108 inline bool
   109 Object::is(MapKind kind)
   110 {
   111     return map()->kind() == kind;
   112 }
   113 
   114 }
   115 
   116 #endif //_include_jitcraft_objects_h_