Clean up object size handling so the GC has any hope of working. Also, clean up shell.
authorDavid Anderson <dvander@alliedmods.net>
Sun Aug 05 01:45:37 2012 -0700 (2012-08-05)
changeset 161b3f0aaee735a
parent 160 12807399b953
child 162 c9fa93ce2f1f
Clean up object size handling so the GC has any hope of working. Also, clean up shell.
src/Environments.h
src/FixedArray.h
src/ObjectVisitor.h
src/Strings.h
src/Structures.h
src/main.cpp
     1.1 --- a/src/Environments.h	Sun Aug 05 01:28:12 2012 -0700
     1.2 +++ b/src/Environments.h	Sun Aug 05 01:45:37 2012 -0700
     1.3 @@ -44,7 +44,7 @@
     1.4      static Environment *New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
     1.5  
     1.6      inline EnvironmentMap *map();
     1.7 -    inline size_t size();
     1.8 +    inline size_t objSize();
     1.9  
    1.10      float getFloat(unsigned slot) {
    1.11          return *pointerToSlot<float>(slot);
    1.12 @@ -99,9 +99,8 @@
    1.13      unsigned length() {
    1.14          return length_;
    1.15      }
    1.16 -    unsigned size() {
    1.17 -        return Align(sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray),
    1.18 -                     kObjectAlignment);
    1.19 +    unsigned objSize() {
    1.20 +        return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray);
    1.21      }
    1.22  
    1.23      static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
    1.24 @@ -128,8 +127,8 @@
    1.25      const unsigned *traceOffsets() {
    1.26          return reinterpret_cast<unsigned *>(this + 1);
    1.27      }
    1.28 -    size_t size() {
    1.29 -        return Align(sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
    1.30 +    size_t objSize() {
    1.31 +        return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount();
    1.32      }
    1.33      EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
    1.34          return slots_->at(slot);
    1.35 @@ -148,9 +147,9 @@
    1.36  };
    1.37  
    1.38  size_t
    1.39 -Environment::size()
    1.40 +Environment::objSize()
    1.41  {
    1.42 -    return Align(sizeof(Environment) + map()->structureSize(), kObjectAlignment);
    1.43 +    return sizeof(Environment) + map()->structureSize();
    1.44  }
    1.45  
    1.46  EnvironmentMap *
     2.1 --- a/src/FixedArray.h	Sun Aug 05 01:28:12 2012 -0700
     2.2 +++ b/src/FixedArray.h	Sun Aug 05 01:45:37 2012 -0700
     2.3 @@ -1,20 +1,20 @@
     2.4 -/* vim: set ts=4 sw=4 tw=99 et
     2.5 +/* vim: set ts=4 sw=4 tw=99 et:
     2.6   *
     2.7   * Copyright (C) 2012 David Anderson
     2.8   *
     2.9 - * This file is part of JITCraft.
    2.10 + * This file is part of SourcePawn.
    2.11   *
    2.12 - * JITCraft is free software: you can redistribute it and/or modify it under
    2.13 + * SourcePawn is free software: you can redistribute it and/or modify it under
    2.14   * the terms of the GNU General Public License as published by the Free
    2.15   * Software Foundation, either version 3 of the License, or (at your option)
    2.16   * any later version.
    2.17   * 
    2.18 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    2.19 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    2.20   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    2.21   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    2.22   *
    2.23   * You should have received a copy of the GNU General Public License along with
    2.24 - * JITCraft. If not, see http://www.gnu.org/licenses/.
    2.25 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
    2.26   */
    2.27  #ifndef _include_jitcraft_fixedarray_h_
    2.28  #define _include_jitcraft_fixedarray_h_
    2.29 @@ -47,6 +47,9 @@
    2.30          assert(i < length());
    2.31          write(zone, &vec()[i], obj);
    2.32      }
    2.33 +    size_t objSize() {
    2.34 +        return SizeFor(length());
    2.35 +    }
    2.36  
    2.37      static size_t SizeFor(unsigned length) {
    2.38          return sizeof(FixedArray) + sizeof(Object *) * length;
    2.39 @@ -73,9 +76,12 @@
    2.40      Address raw() {
    2.41          return reinterpret_cast<uint8 *>(this + 1);
    2.42      }
    2.43 +    size_t objSize() {
    2.44 +        return SizeFor(length());
    2.45 +    }
    2.46  
    2.47      static size_t SizeFor(unsigned length) {
    2.48 -        return Align(sizeof(ByteArray) + sizeof(uint8) * length, kObjectAlignment);
    2.49 +        return sizeof(ByteArray) + sizeof(uint8) * length;
    2.50      }
    2.51  
    2.52      static inline ByteArray *cast(Object *obj) {
     3.1 --- a/src/ObjectVisitor.h	Sun Aug 05 01:28:12 2012 -0700
     3.2 +++ b/src/ObjectVisitor.h	Sun Aug 05 01:45:37 2012 -0700
     3.3 @@ -58,11 +58,11 @@
     3.4          Object **ptr = reinterpret_cast<Object **>(array + 1);
     3.5          for (unsigned i = 0; i < length; i++)
     3.6              this->visit(ptr + i);
     3.7 -        return FixedArray::SizeFor(length);
     3.8 +        return array->objSize();
     3.9      }
    3.10  
    3.11      size_t iterateByteArray(ByteArray *array) {
    3.12 -        return ByteArray::SizeFor(array->length());
    3.13 +        return array->objSize();
    3.14      }
    3.15  
    3.16      size_t iterateCode(Object *obj) {
    3.17 @@ -110,7 +110,7 @@
    3.18      }
    3.19  
    3.20      size_t iterateString(Object *obj) {
    3.21 -        return String::SizeFor(String::cast(obj)->length());
    3.22 +        return String::cast(obj)->objSize();
    3.23      }
    3.24  
    3.25      size_t iterateArray(Array *array, ArrayMap *map) {
    3.26 @@ -134,18 +134,18 @@
    3.27          EnvironmentSlotDescriptor *desc = env->slots();
    3.28          for (unsigned i = 0; i < length; i++)
    3.29              this->visit(desc->type.addr());
    3.30 -        return env->size();
    3.31 +        return env->objSize();
    3.32      }
    3.33  
    3.34      size_t iterateEnvironmentMap(EnvironmentMap *map) {
    3.35          visitField(map, EnvironmentMap::offsetOfSlots());
    3.36 -        return map->size();
    3.37 +        return map->objSize();
    3.38      }
    3.39  
    3.40      size_t iterateStructMap(StructMap *map) {
    3.41          visitField(map, StructMap::offsetOfType());
    3.42          visitField(map, StructMap::offsetOfComposed());
    3.43 -        return map->size();
    3.44 +        return map->objSize();
    3.45      }
    3.46  
    3.47      size_t iterateEnvironment(Environment *env) {
    3.48 @@ -154,7 +154,7 @@
    3.49          unsigned noffsets = map->traceOffsetCount();
    3.50          for (unsigned i = 0; i < noffsets; i++)
    3.51              visitField(env, Environment::offsetOfSlotOffset(offsets[i]));
    3.52 -        return env->size();
    3.53 +        return env->objSize();
    3.54      }
    3.55  
    3.56      size_t iterateStruct(Struct *obj) {
    3.57 @@ -163,16 +163,16 @@
    3.58          unsigned noffsets = map->traceOffsetCount();
    3.59          for (unsigned i = 0; i < noffsets; i++)
    3.60              visitField(obj, Struct::offsetOfSlotOffset(offsets[i]));
    3.61 -        return obj->size();
    3.62 +        return obj->objSize();
    3.63      }
    3.64  
    3.65 -  public:
    3.66 -    size_t iterate(Object *obj) {
    3.67 +  private:
    3.68 +    size_t iterateObject(Object *obj) {
    3.69          visitField(obj, Object::offsetOfMap());
    3.70          return iterate(obj, obj->map());
    3.71      }
    3.72  
    3.73 -    size_t iterate(Object *obj, Map *map) {
    3.74 +    size_t iterateObject(Object *obj, Map *map) {
    3.75          switch (map->kind()) {
    3.76            case MapKind_Type:
    3.77              return iterateType(obj);
    3.78 @@ -230,20 +230,29 @@
    3.79              return sizeof(Map);
    3.80          }
    3.81      }
    3.82 +
    3.83 +  public:
    3.84 +    size_t iterate(Object *obj) {
    3.85 +        return Align(iterateObject(obj), kObjectAlignment);
    3.86 +    }
    3.87 +
    3.88 +    size_t iterate(Object *obj, Map *map) {
    3.89 +        return Align(iterateObject(obj, map), kObjectAlignment);
    3.90 +    }
    3.91  };
    3.92  
    3.93  static inline size_t
    3.94 -SizeOfObject(Object *obj, Map *map)
    3.95 +SizeOfObjectRaw(Object *obj, Map *map)
    3.96  {
    3.97      switch (map->kind()) {
    3.98        case MapKind_Type:
    3.99          return sizeof(Type);
   3.100  
   3.101        case MapKind_FixedArray:
   3.102 -        return FixedArray::SizeFor(FixedArray::cast(obj)->length());
   3.103 +        return FixedArray::cast(obj)->objSize();
   3.104  
   3.105        case MapKind_ByteArray:
   3.106 -        return ByteArray::SizeFor(ByteArray::cast(obj)->length());
   3.107 +        return ByteArray::cast(obj)->objSize();
   3.108  
   3.109        case MapKind_Code:
   3.110          return sizeof(Code);
   3.111 @@ -261,7 +270,7 @@
   3.112          return sizeof(Function);
   3.113  
   3.114        case MapKind_String:
   3.115 -        return String::SizeFor(String::cast(obj)->length());
   3.116 +        return String::cast(obj)->objSize();
   3.117  
   3.118        case MapKind_Native:
   3.119          return sizeof(Native);
   3.120 @@ -270,22 +279,22 @@
   3.121          return Align(Array::SizeOf(Array::cast(obj)), kObjectAlignment);
   3.122  
   3.123        case MapKind_Struct:
   3.124 -        return Struct::cast(obj)->size();
   3.125 +        return Struct::cast(obj)->objSize();
   3.126  
   3.127        case MapKind_EnvironmentSlotDescriptorArray:
   3.128 -        return EnvironmentSlotDescriptorArray::cast(obj)->size();
   3.129 +        return EnvironmentSlotDescriptorArray::cast(obj)->objSize();
   3.130  
   3.131        case MapKind_Environment:
   3.132 -        return Environment::cast(obj)->size();
   3.133 +        return Environment::cast(obj)->objSize();
   3.134  
   3.135        case MapKind_MetaArray:
   3.136          return sizeof(ArrayMap);
   3.137  
   3.138        case MapKind_MetaEnvironment:
   3.139 -        return EnvironmentMap::cast(obj)->size();
   3.140 +        return EnvironmentMap::cast(obj)->objSize();
   3.141  
   3.142        case MapKind_MetaStruct:
   3.143 -        return StructMap::cast(obj)->size();
   3.144 +        return StructMap::cast(obj)->objSize();
   3.145  
   3.146        case MapKind_FreeRegion:
   3.147          return *reinterpret_cast<size_t *>(reinterpret_cast<Address>(obj + 1));
   3.148 @@ -296,6 +305,12 @@
   3.149      }
   3.150  }
   3.151  
   3.152 +static inline size_t
   3.153 +SizeOfObject(Object *obj, Map *map)
   3.154 +{
   3.155 +    return Align(SizeOfObjectRaw(obj, map), kObjectAlignment);
   3.156 +}
   3.157 +
   3.158  class ObjectSizeEvaluator
   3.159  {
   3.160    public:
     4.1 --- a/src/Strings.h	Sun Aug 05 01:28:12 2012 -0700
     4.2 +++ b/src/Strings.h	Sun Aug 05 01:45:37 2012 -0700
     4.3 @@ -38,9 +38,12 @@
     4.4      char *chars() {
     4.5          return reinterpret_cast<char *>(this + 1);
     4.6      }
     4.7 +    size_t objSize() {
     4.8 +        return SizeFor(length());
     4.9 +    }
    4.10  
    4.11      static size_t SizeFor(unsigned length) {
    4.12 -        return Align(sizeof(String) + sizeof(char) * (length + 1), kObjectAlignment);
    4.13 +        return sizeof(String) + sizeof(char) * (length + 1);
    4.14      }
    4.15  
    4.16      static inline String *cast(Object *obj) {
     5.1 --- a/src/Structures.h	Sun Aug 05 01:28:12 2012 -0700
     5.2 +++ b/src/Structures.h	Sun Aug 05 01:45:37 2012 -0700
     5.3 @@ -118,8 +118,8 @@
     5.4      const unsigned *traceOffsets() {
     5.5          return reinterpret_cast<unsigned *>(this + 1);
     5.6      }
     5.7 -    size_t size() {
     5.8 -        return Align(sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
     5.9 +    size_t objSize() {
    5.10 +        return sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount();
    5.11      }
    5.12      Type *type() {
    5.13          return type_;
    5.14 @@ -153,8 +153,8 @@
    5.15    public:
    5.16      static Struct *New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
    5.17  
    5.18 -    size_t size() {
    5.19 -        return Align(sizeof(Struct) + map()->structureSize(), kObjectAlignment);
    5.20 +    size_t objSize() {
    5.21 +        return sizeof(Struct) + map()->structureSize();
    5.22      }
    5.23      StructMap *map() {
    5.24          return StructMap::cast(Object::map());
     6.1 --- a/src/main.cpp	Sun Aug 05 01:28:12 2012 -0700
     6.2 +++ b/src/main.cpp	Sun Aug 05 01:45:37 2012 -0700
     6.3 @@ -16,23 +16,12 @@
     6.4   * JITCraft. If not, see http://www.gnu.org/licenses/.
     6.5   */
     6.6  #include <stdio.h>
     6.7 -#include "Utility.h"
     6.8 -#include "Scanner.h"
     6.9 -#include "Parser.h"
    6.10 -#include "SemanticAnalysis.h"
    6.11 -#include "Zone.h"
    6.12 -#include "BytecodeCompiler.h"
    6.13 -#include "Code.h"
    6.14 -#include "Interpreter.h"
    6.15 -#include "Modules.h"
    6.16 -#include "RootScopes.h"
    6.17 +#include <stdlib.h>
    6.18  #include "../include/keima.h"
    6.19 -#include "Spaces-inl.h"
    6.20 -#include "Heap-inl.h"
    6.21  
    6.22 -using namespace ke;
    6.23 +using namespace Ke;
    6.24  
    6.25 -class MessageReporter : public Ke::IMessageReporter
    6.26 +class MessageReporter : public IMessageReporter
    6.27  {
    6.28    public:
    6.29      void onOutOfMemory() {
    6.30 @@ -41,7 +30,7 @@
    6.31      void onUncaughtException(const char *message) {
    6.32          fprintf(stderr, "Uncaught exception: %s\n", message);
    6.33  
    6.34 -        Ke::IStackFrameIterator *iter = Ke::Keima::NewStackFrameIterator();
    6.35 +        IStackFrameIterator *iter = Keima::NewStackFrameIterator();
    6.36          unsigned index = 0;
    6.37          while (!iter->done()) {
    6.38              fprintf(stderr, "[%d] %s (%s line %d)\n",
    6.39 @@ -111,26 +100,28 @@
    6.40          return 1;
    6.41      }
    6.42  
    6.43 -    if (!Ke::Keima::Initialize())
    6.44 +    if (!Keima::Initialize())
    6.45          return 1;
    6.46 -    Ke::Keima::InstallMessageReporter(&s_Reporter);
    6.47 +    Keima::InstallMessageReporter(&s_Reporter);
    6.48      {
    6.49 -        Ke::HandleScope scope;
    6.50 -        Ke::Local<Ke::Module> module = Ke::Compile(argv[1]);
    6.51 +        HandleScope scope;
    6.52 +        Local<Module> module = Compile(argv[1]);
    6.53          if (!module)
    6.54              return 1;
    6.55  
    6.56 -        //Ke::Local<Ke::Native> native = module->getNative(0);
    6.57 +	// Keima::CollectGarbage();
    6.58 +
    6.59 +        //Local<Native> native = module->getNative(0);
    6.60          //native->bind(reinterpret_cast<void *>(PrintToServer));
    6.61          //module->setNativeIdentity(reinterpret_cast<void *>(5));
    6.62  
    6.63          int result;
    6.64 -        Ke::Local<Ke::Function> fun = module->getPublic(0);
    6.65 -        if (Ke::Keima::Invoke(fun, NULL, 0, true, &result))
    6.66 +        Local<Function> fun = module->getPublic(0);
    6.67 +        if (Keima::Invoke(fun, NULL, 0, true, &result))
    6.68              printf("%d\n", result);
    6.69      }
    6.70  
    6.71 -    Ke::Keima::Shutdown();
    6.72 +    Keima::Shutdown();
    6.73  }
    6.74  
    6.75  /* Overload a few things to prevent libstdc++ linking */