jitcraft/Objects.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 02 23:37:12 2012 -0700 (2012-07-02)
changeset 28 a9665857cfd5
child 52 22b67b4cf067
permissions -rw-r--r--
Added MacOS, gcc, and clang support. Preliminary crappy autoconf scripts of course.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jitcraft_objects_h_
    19 #define _include_jitcraft_objects_h_
    20 
    21 #include "Utility.h"
    22 #include "Spaces.h"
    23 #include "Barriers.h"
    24 
    25 namespace jc {
    26 
    27 class Zone;
    28 
    29 // Garbage-collection objects require a header to describe their layout. This
    30 // header is called a "map". The different kinds of maps are enumerated below.
    31 class Map;
    32 
    33 enum MapKind
    34 {
    35     // Represents Type objects.
    36     MapKind_Type,
    37 
    38     // Represents basic data structures. When adding to this, don't forget
    39     // to update Zone::initialize(), or you'll get a NULL crash trying to
    40     // allocate, since these maps are cached.
    41     MapKind_FixedArray,
    42     MapKind_ByteArray,
    43     MapKind_Code,
    44     MapKind_Reference,
    45     MapKind_Module,
    46     MapKind_Function,
    47 
    48     // Used to represent Arrays. This does not have a canonical map.
    49     MapKind_Array,
    50 
    51     // These represent special Map subclasses that require special tracing.
    52     MapKinds_Special,
    53     MapKind_Meta,           // (default map class)
    54     MapKind_MetaArray,      // ArrayMap instances
    55     MapKinds_EndSpecial,
    56 
    57     MapKinds_Total
    58 };
    59 
    60 typedef void * VoidPtr;
    61 
    62 // All garbage collected objects must inherit from GCObject. Note that using
    63 // virtual functions is illegal, as the |map| field would be displaced.
    64 class Object
    65 {
    66     Barriered<Map> map_;
    67 
    68   public:
    69     void init(Map *map) {
    70         map_ = map;
    71     }
    72 
    73     Map *map() {
    74         return map_;
    75     }
    76     inline bool is(MapKind map);
    77 
    78     inline void write(Zone *zone, Object **ptr, Object *value);
    79 };
    80 
    81 // Maps describe the layout of GCObjects. Every GCObject requires a map.
    82 class Map : public Object
    83 {
    84     MapKind kind_;
    85 
    86   public:
    87     void setKind(MapKind kind) {
    88         kind_ = kind;
    89     }
    90     MapKind kind() {
    91         return kind_;
    92     }
    93 
    94     static inline Map *cast(Object *obj) {
    95         // Note that ArrayMap is a map, but a special one: since it is non-
    96         assert(obj->map()->kind() > MapKinds_Special &&
    97                obj->map()->kind() < MapKinds_EndSpecial);
    98         return reinterpret_cast<Map *>(obj);
    99     }
   100 };
   101 
   102 inline bool
   103 Object::is(MapKind kind)
   104 {
   105     return map()->kind() == kind;
   106 }
   107 
   108 }
   109 
   110 #endif //_include_jitcraft_objects_h_