src/ObjectVisitor.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 261 219b49bf4a03
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of SourcePawn.
[email protected]
     6
 *
[email protected]
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
dvan[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#ifndef _include_jc_object_visitor_h_
[email protected]
    20
#define _include_jc_object_visitor_h_
[email protected]
    21
[email protected]
    22
#include "Objects.h"
[email protected]
    23
#include "Types.h"
[email protected]
    24
#include "FixedArray.h"
[email protected]
    25
#include "Code.h"
[email protected]
    26
#include "Reference.h"
[email protected]
    27
#include "Modules.h"
[email protected]
    28
#include "Functions.h"
[email protected]
    29
#include "Array.h"
[email protected]
    30
#include "Strings.h"
[email protected]
    31
#include "Environments.h"
[email protected]
    32
#include "Structures.h"
[email protected]
    33
#include "Box.h"
[email protected]
    34
#include "Packages.h"
[email protected]
    35
#include "List.h"
[email protected]
    36
[email protected]
    37
namespace ke {
[email protected]
    38
[email protected]
    39
// An ObjectVisitor is used for traversing object references in the heap. Since
[email protected]
    40
// this pattern must be lightning fast, we avoid virtual dispatch by forcing
[email protected]
    41
// visitors to compose themselves with the visiting logic.
[email protected]
    42
template <typename T>
[email protected]
    43
class ObjectVisitor : public T
[email protected]
    44
{
[email protected]
    45
  private:
[email protected]
    46
    void visitField(Object *obj, size_t offset) {
[email protected]
    47
        this->visit(reinterpret_cast<Object **>(Address(obj) + offset));
[email protected]
    48
    }
[email protected]
    49
[email protected]
    50
    size_t iterateType(Object *obj) {
[email protected]
    51
        visitField(obj, Type::offsetOfRoot());
[email protected]
    52
        return sizeof(Type);
[email protected]
    53
    }
[email protected]
    54
[email protected]
    55
    size_t iterateEnumType(Object *obj) {
[email protected]
    56
        visitField(obj, EnumType::offsetOfName());
[email protected]
    57
        return sizeof(EnumType);
[email protected]
    58
    }
[email protected]
    59
[email protected]
    60
    size_t iterateReferenceType(Object *obj) {
[email protected]
    61
        iterateType(obj);
[email protected]
    62
        visitField(obj, ReferenceType::offsetOfContained());
[email protected]
    63
        return sizeof(ReferenceType);
[email protected]
    64
    }
[email protected]
    65
[email protected]
    66
    size_t iterateArrayType(Object *obj) {
[email protected]
    67
        iterateType(obj);
[email protected]
    68
        visitField(obj, ArrayType::offsetOfContained());
[email protected]
    69
        visitField(obj, ArrayType::offsetOfNewMap());
[email protected]
    70
        return sizeof(ArrayType);
[email protected]
    71
    }
[email protected]
    72
[email protected]
    73
    size_t iterateStructType(Object *obj) {
[email protected]
    74
        iterateType(obj);
[email protected]
    75
        visitField(obj, StructType::offsetOfFields());
[email protected]
    76
        visitField(obj, StructType::offsetOfNewMap());
[email protected]
    77
        visitField(obj, StructType::offsetOfName());
[email protected]
    78
        return sizeof(StructType);
[email protected]
    79
    }
[email protected]
    80
[email protected]
    81
    size_t iterateFunctionType(Object *obj) {
[email protected]
    82
        iterateType(obj);
[email protected]
    83
        visitField(obj, FunctionType::offsetOfReturnType());
[email protected]
    84
        visitField(obj, FunctionType::offsetOfParameters());      
[email protected]
    85
        visitField(obj, FunctionType::offsetOfDefaults());
[email protected]
    86
        visitField(obj, FunctionType::offsetOfName());
[email protected]
    87
        return sizeof(FunctionType);
[email protected]
    88
    }
[email protected]
    89
[email protected]
    90
    size_t iterateFixedArray(FixedArray *array) {
[email protected]
    91
        unsigned length = array->length();
[email protected]
    92
        Object **ptr = reinterpret_cast<Object **>(array + 1);
[email protected]
    93
        for (unsigned i = 0; i < length; i++)
[email protected]
    94
            this->visit(ptr + i);
[email protected]
    95
        return array->objSize();
[email protected]
    96
    }
[email protected]
    97
[email protected]
    98
    size_t iterateByteArray(ByteArray *array) {
[email protected]
    99
        return array->objSize();
[email protected]
   100
    }
[email protected]
   101
[email protected]
   102
    size_t iterateCode(Object *obj) {
[email protected]
   103
        visitField(obj, Code::offsetOfModule());
[email protected]
   104
        visitField(obj, Code::offsetOfBytecode());
[email protected]
   105
        visitField(obj, Code::offsetOfLocals());
[email protected]
   106
        visitField(obj, Code::offsetOfObjects());
[email protected]
   107
        visitField(obj, Code::offsetOfSafepoints());
[email protected]
   108
        visitField(obj, Code::offsetOfSourceMap());
[email protected]
   109
        visitField(obj, Code::offsetOfStrings());
[email protected]
   110
        return sizeof(Code);
[email protected]
   111
    }
[email protected]
   112
[email protected]
   113
    size_t iterateReference(Object *obj) {
[email protected]
   114
        visitField(obj, Reference::offsetOfBase());
[email protected]
   115
        return sizeof(Reference);
[email protected]
   116
    }
[email protected]
   117
[email protected]
   118
    void visitImportable(Object *obj) {
[email protected]
   119
        visitField(obj, Package::offsetOfName());
[email protected]
   120
        visitField(obj, Package::offsetOfParent());
[email protected]
   121
        visitField(obj, Package::offsetOfPath());
[email protected]
   122
    }
[email protected]
   123
[email protected]
   124
    size_t iterateModule(Object *obj) {
[email protected]
   125
        visitImportable(obj);
[email protected]
   126
        visitField(obj, Module::offsetOfGlobals());
[email protected]
   127
        visitField(obj, Module::offsetOfCode());
[email protected]
   128
        visitField(obj, Module::offsetOfImports());
[email protected]
   129
        return sizeof(Module);
[email protected]
   130
    }
[email protected]
   131
[email protected]
   132
    size_t iteratePackage(Object *obj) {
[email protected]
   133
        visitImportable(obj);
[email protected]
   134
        visitField(obj, Package::offsetOfContents());
[email protected]
   135
        return sizeof(Package);
[email protected]
   136
    }
[email protected]
   137
[email protected]
   138
    size_t iterateFunction(Object *obj) {
[email protected]
   139
        visitField(obj, Function::offsetOfCode());
[email protected]
   140
        visitField(obj, Function::offsetOfType());
[email protected]
   141
        visitField(obj, Function::offsetOfName());
[email protected]
   142
        return sizeof(Function);
[email protected]
   143
    }
[email protected]
   144
[email protected]
   145
    size_t iterateDescriptor(Object *obj) {
[email protected]
   146
        visitField(obj, Descriptor::offsetOfName());
[email protected]
   147
        visitField(obj, Descriptor::offsetOfType());
[email protected]
   148
        return sizeof(Descriptor);
[email protected]
   149
    }
[email protected]
   150
[email protected]
   151
    size_t iterateNative(Object *obj) {
[email protected]
   152
        visitField(obj, Native::offsetOfType());
[email protected]
   153
        return sizeof(Native);
[email protected]
   154
    }
[email protected]
   155
[email protected]
   156
    size_t iterateString(Object *obj) {
[email protected]
   157
        return String::cast(obj)->objSize();
[email protected]
   158
    }
[email protected]
   159
[email protected]
   160
    size_t iterateArray(Array *array, ArrayMap *map) {
[email protected]
   161
        if (!map->type()->hasInlineElements())
[email protected]
   162
            visitField(array, Array::offsetOfElements());
[email protected]
   163
        return array->objSize(map);
[email protected]
   164
    }
[email protected]
   165
[email protected]
   166
    size_t iterateDependentArray(Object *array) {
[email protected]
   167
        visitField(array, DependentArray::offsetOfRoot());
[email protected]
   168
        return sizeof(DependentArray);
[email protected]
   169
    }
[email protected]
   170
[email protected]
   171
    size_t iterateBox(Box *obj) {
[email protected]
   172
        visitField(obj, Box::offsetOfType());
[email protected]
   173
        if (obj->type()->isTraceable())
[email protected]
   174
            visitField(obj, Box::offsetOfObj());
[email protected]
   175
        return sizeof(Box);
[email protected]
   176
    }
[email protected]
   177
[email protected]
   178
    size_t iterateArrayMap(ArrayMap *map) {
[email protected]
   179
        visitField(map, ArrayMap::offsetOfType());
[email protected]
   180
        return sizeof(ArrayMap);
[email protected]
   181
    }
[email protected]
   182
[email protected]
   183
    size_t iterateEnvironmentSlotDescriptorArray(EnvironmentSlotDescriptorArray *env) {
[email protected]
   184
        unsigned length = env->length();
[email protected]
   185
        EnvironmentSlotDescriptor *desc = env->slots();
[email protected]
   186
        for (unsigned i = 0; i < length; i++)
[email protected]
   187
            this->visit(desc->type.addr());
[email protected]
   188
        return env->objSize();
[email protected]
   189
    }
[email protected]
   190
[email protected]
   191
    size_t iterateEnvironmentMap(EnvironmentMap *map) {
[email protected]
   192
        visitField(map, EnvironmentMap::offsetOfSlots());
[email protected]
   193
        return map->objSize();
[email protected]
   194
    }
[email protected]
   195
[email protected]
   196
    size_t iterateStructMap(StructMap *map) {
[email protected]
   197
        visitField(map, StructMap::offsetOfType());
[email protected]
   198
        visitField(map, StructMap::offsetOfComposed());
[email protected]
   199
        return map->objSize();
[email protected]
   200
    }
[email protected]
   201
[email protected]
   202
    size_t iterateEnvironment(Environment *env) {
[email protected]
   203
        EnvironmentMap *map = env->map();
[email protected]
   204
        const unsigned *offsets = map->traceOffsets();
[email protected]
   205
        unsigned noffsets = map->traceOffsetCount();
[email protected]
   206
        for (unsigned i = 0; i < noffsets; i++)
[email protected]
   207
            visitField(env, Environment::offsetOfSlotOffset(offsets[i]));
[email protected]
   208
        return env->objSize();
[email protected]
   209
    }
[email protected]
   210
[email protected]
   211
    size_t iterateStruct(Struct *obj) {
[email protected]
   212
        StructMap *map = obj->map();
[email protected]
   213
        const unsigned *offsets = map->traceOffsets();
[email protected]
   214
        unsigned noffsets = map->traceOffsetCount();
[email protected]
   215
        for (unsigned i = 0; i < noffsets; i++)
[email protected]
   216
            visitField(obj, Struct::offsetOfSlotOffset(offsets[i]));
[email protected]
   217
        return Struct::SizeFor(map);
[email protected]
   218
    }
[email protected]
   219
[email protected]
   220
    size_t iterateList(Object *obj) {
[email protected]
   221
        visitField(obj, List<Object>::offsetOfArray());
[email protected]
   222
        return sizeof(List<Object>);
[email protected]
   223
    }
[email protected]
   224
[email protected]
   225
  private:
[email protected]
   226
    size_t iterateObject(Object *obj) {
[email protected]
   227
        visitField(obj, Object::offsetOfMap());
[email protected]
   228
        return iterate(obj, obj->map());
[email protected]
   229
    }
[email protected]
   230
[email protected]
   231
    size_t iterateObject(Object *obj, Map *map) {
[email protected]
   232
        switch (map->kind()) {
[email protected]
   233
          case MapKind_Type:
[email protected]
   234
            return iterateType(obj);
[email protected]
   235
[email protected]
   236
          case MapKind_ReferenceType:
[email protected]
   237
            return iterateReferenceType(obj);
[email protected]
   238
[email protected]
   239
          case MapKind_ArrayType:
[email protected]
   240
            return iterateArrayType(obj);
[email protected]
   241
[email protected]
   242
          case MapKind_EnumType:
[email protected]
   243
            return iterateEnumType(obj);
[email protected]
   244
[email protected]
   245
          case MapKind_FunctionType:
[email protected]
   246
            return iterateFunctionType(obj);
[email protected]
   247
[email protected]
   248
          case MapKind_StructType:
[email protected]
   249
            return iterateStructType(obj);
[email protected]
   250
[email protected]
   251
          case MapKind_FixedArray:
[email protected]
   252
            return iterateFixedArray(FixedArray::cast(obj));
[email protected]
   253
[email protected]
   254
          case MapKind_ByteArray:
[email protected]
   255
            return iterateByteArray(ByteArray::cast(obj));
[email protected]
   256
[email protected]
   257
          case MapKind_Code:
[email protected]
   258
            return iterateCode(obj);
[email protected]
   259
[email protected]
   260
          case MapKind_Reference:
[email protected]
   261
            return iterateReference(obj);
[email protected]
   262
[email protected]
   263
          case MapKind_Module:
[email protected]
   264
            return iterateModule(obj);
[email protected]
   265
[email protected]
   266
          case MapKind_Package:
[email protected]
   267
            return iteratePackage(obj);
[email protected]
   268
[email protected]
   269
          case MapKind_Function:
[email protected]
   270
            return iterateFunction(obj);
[email protected]
   271
[email protected]
   272
          case MapKind_Descriptor:
[email protected]
   273
            return iterateDescriptor(obj);
[email protected]
   274
dva[email protected]
   275
          case MapKind_String:
[email protected]
   276
            return iterateString(obj);
[email protected]
   277
[email protected]
   278
          case MapKind_Array:
[email protected]
   279
            return iterateArray(Array::cast(obj), ArrayMap::cast(map));
[email protected]
   280
[email protected]
   281
          case MapKind_DependentArray:
[email protected]
   282
            return iterateDependentArray(obj);
[email protected]
   283
dvander[email protected]
   284
          case MapKind_Box:
[email protected]
   285
            return iterateBox(Box::cast(obj));
[email protected]
   286
[email protected]
   287
          case MapKind_Struct:
[email protected]
   288
            return iterateStruct(Struct::cast(obj));
[email protected]
   289
[email protected]
   290
          case MapKind_List:
[email protected]
   291
            return iterateList(obj);
[email protected]
   292
[email protected]
   293
          case MapKind_EnvironmentSlotDescriptorArray:
[email protected]
   294
            return iterateEnvironmentSlotDescriptorArray(EnvironmentSlotDescriptorArray::cast(obj));
[email protected]
   295
[email protected]
   296
          case MapKind_Environment:
[email protected]
   297
            return iterateEnvironment(Environment::cast(obj));
[email protected]
   298
[email protected]
   299
          case MapKind_Native:
[email protected]
   300
            return iterateNative(obj);
[email protected]
   301
[email protected]
   302
          case MapKind_MetaArray:
[email protected]
   303
            return iterateArrayMap(ArrayMap::cast(obj));
[email protected]
   304
[email protected]
   305
          case MapKind_MetaEnvironment:
[email protected]
   306
            return iterateEnvironmentMap(EnvironmentMap::cast(obj));
[email protected]
   307
[email protected]
   308
          case MapKind_MetaStruct:
[email protected]
   309
            return iterateStructMap(StructMap::cast(obj));
[email protected]
   310
[email protected]
   311
          default:
[email protected]
   312
            assert(map->kind() == MapKind_Meta);
[email protected]
   313
            return sizeof(Map);
[email protected]
   314
        }
[email protected]
   315
    }
[email protected]
   316
[email protected]
   317
  public:
[email protected]
   318
    size_t iterate(Object *obj) {
[email protected]
   319
        return Align(iterateObject(obj), kObjectAlignment);
[email protected]
   320
    }
[email protected]
   321
[email protected]
   322
    size_t iterate(Object *obj, Map *map) {
[email protected]
   323
        return Align(iterateObject(obj, map), kObjectAlignment);
[email protected]
   324
    }
[email protected]
   325
};
[email protected]
   326
[email protected]
   327
static inline size_t
[email protected]
   328
SizeOfObjectRaw(Object *obj, Map *map)
[email protected]
   329
{
[email protected]
   330
    switch (map->kind()) {
[email protected]
   331
      case MapKind_Type:
[email protected]
   332
        return sizeof(Type);
[email protected]
   333
[email protected]
   334
      case MapKind_ReferenceType:
[email protected]
   335
        return sizeof(ReferenceType);
[email protected]
   336
[email protected]
   337
      case MapKind_ArrayType:
[email protected]
   338
        return sizeof(ArrayType);
[email protected]
   339
[email protected]
   340
      case MapKind_EnumType:
[email protected]
   341
        return sizeof(EnumType);
[email protected]
   342
[email protected]
   343
      case MapKind_FunctionType:
[email protected]
   344
        return sizeof(FunctionType);
[email protected]
   345
[email protected]
   346
      case MapKind_StructType:
[email protected]
   347
        return sizeof(StructType);
[email protected]
   348
[email protected]
   349
      case MapKind_FixedArray:
[email protected]
   350
        return FixedArray::cast(obj)->objSize();
[email protected]
   351
[email protected]
   352
      case MapKind_ByteArray:
[email protected]
   353
        return ByteArray::cast(obj)->objSize();
[email protected]
   354
[email protected]
   355
      case MapKind_Code:
[email protected]
   356
        return sizeof(Code);
[email protected]
   357
[email protected]
   358
      case MapKind_Reference:
[email protected]
   359
        return sizeof(Reference);
[email protected]
   360
[email protected]
   361
      case MapKind_Module:
[email protected]
   362
        return sizeof(Module);
[email protected]
   363
[email protected]
   364
      case MapKind_Package:
[email protected]
   365
        return sizeof(Package);
[email protected]
   366
[email protected]
   367
      case MapKind_Descriptor:
[email protected]
   368
        return sizeof(Descriptor);
[email protected]
   369
[email protected]
   370
      case MapKind_Function:
[email protected]
   371
        return sizeof(Function);
[email protected]
   372
[email protected]
   373
      case MapKind_String:
[email protected]
   374
        return String::cast(obj)->objSize();
[email protected]
   375
[email protected]
   376
      case MapKind_Native:
[email protected]
   377
        return sizeof(Native);
[email protected]
   378
[email protected]
   379
      case MapKind_Array:
[email protected]
   380
        return Array::cast(obj)->objSize(ArrayMap::cast(map));
[email protected]
   381
[email protected]
   382
       case MapKind_DependentArray:
[email protected]
   383
        return sizeof(DependentArray);
[email protected]
   384
[email protected]
   385
      case MapKind_Box:
[email protected]
   386
        return sizeof(Box);
[email protected]
   387
[email protected]
   388
      case MapKind_List:
[email protected]
   389
        return sizeof(List<Object>);
[email protected]
   390
[email protected]
   391
      case MapKind_Struct:
[email protected]
   392
        return Struct::SizeFor(StructMap::cast(map));
[email protected]
   393
[email protected]
   394
      case MapKind_EnvironmentSlotDescriptorArray:
[email protected]
   395
        return EnvironmentSlotDescriptorArray::cast(obj)->objSize();
[email protected]
   396
[email protected]
   397
      case MapKind_Environment:
[email protected]
   398
        return Environment::cast(obj)->objSize();
[email protected]
   399
[email protected]
   400
      case MapKind_MetaArray:
[email protected]
   401
        return sizeof(ArrayMap);
[email protected]
   402
[email protected]
   403
      case MapKind_MetaEnvironment:
[email protected]
   404
        return EnvironmentMap::cast(obj)->objSize();
[email protected]
   405
[email protected]
   406
      case MapKind_MetaStruct:
[email protected]
   407
        return StructMap::cast(obj)->objSize();
[email protected]
   408
[email protected]
   409
      case MapKind_FreeRegion:
[email protected]
   410
        return *reinterpret_cast<size_t *>(reinterpret_cast<Address>(obj + 1));
[email protected]
   411
[email protected]
   412
      default:
[email protected]
   413
        assert(map->kind() == MapKind_Meta);
[email protected]
   414
        return sizeof(Map);
[email protected]
   415
    }
[email protected]
   416
}
[email protected]
   417
[email protected]
   418
static inline size_t
[email protected]
   419
SizeOfObject(Object *obj, Map *map)
[email protected]
   420
{
[email protected]
   421
    return Align(SizeOfObjectRaw(obj, map), kObjectAlignment);
[email protected]
   422
}
[email protected]
   423
[email protected]
   424
class ObjectSizeEvaluator
[email protected]
   425
{
[email protected]
   426
  public:
[email protected]
   427
    size_t operator()(Object *obj, Map *map) {
[email protected]
   428
        return SizeOfObject(obj, map);
[email protected]
   429
    }
[email protected]
   430
};
[email protected]
   431
[email protected]
   432
}
[email protected]
   433
[email protected]
   434
#endif // _include_jc_object_visitor_h_