From a80796614347a68052c724e08ca3bc9e9032e1ff Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Tue, 25 Feb 2014 15:46:35 -0700 Subject: [PATCH] use Slice in FixedAllocator --- include/avian/util/allocator.h | 1 + include/avian/util/fixed-allocator.h | 12 ++-- src/avian/processor.h | 9 ++- src/compile.cpp | 79 ++++++++++++++------------ src/interpret.cpp | 5 +- src/tools/bootimage-generator/main.cpp | 8 +-- src/util/fixed-allocator.cpp | 11 ++-- 7 files changed, 70 insertions(+), 55 deletions(-) diff --git a/include/avian/util/allocator.h b/include/avian/util/allocator.h index 8b964717d9..8764d5f62f 100644 --- a/include/avian/util/allocator.h +++ b/include/avian/util/allocator.h @@ -18,6 +18,7 @@ namespace util { class Allocator { public: + // TODO: use size_t instead of unsigned virtual void* tryAllocate(unsigned size) = 0; virtual void* allocate(unsigned size) = 0; virtual void free(const void* p, unsigned size) = 0; diff --git a/include/avian/util/fixed-allocator.h b/include/avian/util/fixed-allocator.h index 27181d446d..11aa57425b 100644 --- a/include/avian/util/fixed-allocator.h +++ b/include/avian/util/fixed-allocator.h @@ -13,13 +13,16 @@ #include "allocator.h" #include "abort.h" +#include "slice.h" namespace avian { namespace util { +// An Allocator that allocates, bump-pointer style, out of a pre-defined chunk +// of memory. class FixedAllocator : public Allocator { public: - FixedAllocator(Aborter* a, uint8_t* base, unsigned capacity); + FixedAllocator(Aborter* a, Slice memory); virtual void* tryAllocate(unsigned size); @@ -30,11 +33,8 @@ class FixedAllocator : public Allocator { virtual void free(const void* p, unsigned size); Aborter* a; - - uint8_t* base; - unsigned capacity; - - unsigned offset; + Slice memory; + size_t offset; }; } // namespace util diff --git a/src/avian/processor.h b/src/avian/processor.h index 8744d25a6b..cae0c42f79 100644 --- a/src/avian/processor.h +++ b/src/avian/processor.h @@ -23,6 +23,11 @@ namespace avian { namespace codegen { class DelayedPromise; } + +namespace util { +template +class Slice; +} } namespace vm { @@ -140,8 +145,8 @@ class Processor { virtual object getStackTrace(Thread* t, Thread* target) = 0; - virtual void - initialize(BootImage* image, uint8_t* code, unsigned capacity) = 0; + virtual void initialize(BootImage* image, avian::util::Slice code) + = 0; virtual void addCompilationHandler(CompilationHandler* handler) = 0; diff --git a/src/compile.cpp b/src/compile.cpp index 36e8eaa380..857866ad8c 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -24,6 +24,7 @@ #include #include +#include #include using namespace vm; @@ -1640,15 +1641,19 @@ class Frame { Value absoluteAddressOperand(avian::codegen::Promise* p) { return context->bootContext - ? c->binaryOp(lir::Add, - TargetBytesPerWord, c->memory - (c->register_(t->arch->thread()), Compiler::AddressType, - TARGET_THREAD_CODEIMAGE), c->promiseConstant - (new(&context->zone) - avian::codegen::OffsetPromise - (p, - reinterpret_cast(codeAllocator(t)->base)), - Compiler::AddressType)) - : addressOperand(p); + ? c->binaryOp( + lir::Add, + TargetBytesPerWord, + c->memory(c->register_(t->arch->thread()), + Compiler::AddressType, + TARGET_THREAD_CODEIMAGE), + c->promiseConstant( + new (&context->zone) avian::codegen::OffsetPromise( + p, + -reinterpret_cast( + codeAllocator(t)->memory.begin())), + Compiler::AddressType)) + : addressOperand(p); } Value machineIp(unsigned logicalIp) { @@ -3130,8 +3135,9 @@ useLongJump(MyThread* t, uintptr_t target) { uintptr_t reach = t->arch->maximumImmediateJump(); FixedAllocator* a = codeAllocator(t); - uintptr_t start = reinterpret_cast(a->base); - uintptr_t end = reinterpret_cast(a->base) + a->capacity; + uintptr_t start = reinterpret_cast(a->memory.begin()); + uintptr_t end = reinterpret_cast(a->memory.begin()) + + a->memory.count; assert(t, end - start < reach); return (target > end && (target - start) > reach) @@ -6962,9 +6968,8 @@ finish(MyThread* t, FixedAllocator* allocator, Context* context) TARGET_THREAD_STACKLIMIT); // we must acquire the class lock here at the latest - - unsigned codeSize = c->resolve - (allocator->base + allocator->offset); + + unsigned codeSize = c->resolve(allocator->memory.begin() + allocator->offset); unsigned total = pad(codeSize, TargetBytesPerWord) + pad(c->poolSize(), TargetBytesPerWord); @@ -6983,8 +6988,8 @@ finish(MyThread* t, FixedAllocator* allocator, Context* context) FixedSizeOfArray + ((context->objectPoolCount + 1) * BytesPerWord), true); - context->executableSize = (allocator->base + allocator->offset) - - static_cast(context->executableStart); + context->executableSize = (allocator->memory.begin() + allocator->offset) + - static_cast(context->executableStart); initArray(t, pool, context->objectPoolCount + 1); mark(t, pool, 0); @@ -8519,7 +8524,7 @@ class MyProcessor: public Processor { divideByZeroHandler(Machine::ArithmeticExceptionType, Machine::ArithmeticException, FixedSizeOfArithmeticException), - codeAllocator(s, 0, 0), + codeAllocator(s, Slice(0, 0)), callTableSize(0), useNativeFeatures(useNativeFeatures), compilationHandlers(0) @@ -8922,9 +8927,10 @@ class MyProcessor: public Processor { } virtual void dispose() { - if (codeAllocator.base) { + if (codeAllocator.memory.begin()) { #if !defined(AVIAN_AOT_ONLY) - s->freeExecutable(codeAllocator.base, codeAllocator.capacity); + s->freeExecutable(codeAllocator.memory.begin(), + codeAllocator.memory.count); #endif } @@ -9022,10 +9028,10 @@ class MyProcessor: public Processor { return visitor.trace ? visitor.trace : makeObjectArray(t, 0); } - virtual void initialize(BootImage* image, uint8_t* code, unsigned capacity) { + virtual void initialize(BootImage* image, Slice code) + { bootImage = image; - codeAllocator.base = code; - codeAllocator.capacity = capacity; + codeAllocator.memory = code; } virtual void addCompilationHandler(CompilationHandler* handler) { @@ -9060,7 +9066,7 @@ class MyProcessor: public Processor { { if (wordArrayBody(t, root(t, VirtualThunks), i)) { wordArrayBody(t, root(t, VirtualThunks), i) - -= reinterpret_cast(codeAllocator.base); + -= reinterpret_cast(codeAllocator.memory.begin()); } } } @@ -9077,24 +9083,25 @@ class MyProcessor: public Processor { for (object p = arrayBody(t, root(t, CallTable), i); p; p = callNodeNext(t, p)) { - table[index++] = targetVW - (callNodeAddress(t, p) - - reinterpret_cast(codeAllocator.base)); - table[index++] = targetVW - (w->map()->find(callNodeTarget(t, p)) - | (static_cast(callNodeFlags(t, p)) << TargetBootShift)); + table[index++] = targetVW( + callNodeAddress(t, p) + - reinterpret_cast(codeAllocator.memory.begin())); + table[index++] = targetVW( + w->map()->find(callNodeTarget(t, p)) + | (static_cast(callNodeFlags(t, p)) << TargetBootShift)); } } return table; } - virtual void boot(Thread* t, BootImage* image, uint8_t* code) { + virtual void boot(Thread* t, BootImage* image, uint8_t* code) + { #if !defined(AVIAN_AOT_ONLY) - if (codeAllocator.base == 0) { - codeAllocator.base = static_cast - (s->tryAllocateExecutable(ExecutableAreaSizeInBytes)); - codeAllocator.capacity = ExecutableAreaSizeInBytes; + if (codeAllocator.memory.begin() == 0) { + codeAllocator.memory.items = static_cast( + s->tryAllocateExecutable(ExecutableAreaSizeInBytes)); + codeAllocator.memory.count = ExecutableAreaSizeInBytes; } #endif @@ -9104,7 +9111,7 @@ class MyProcessor: public Processor { roots = makeArray(t, RootCount); setRoot(t, CallTable, makeArray(t, 128)); - + setRoot(t, MethodTreeSentinal, makeTreeNode(t, 0, 0, 0)); setRoot(t, MethodTree, root(t, MethodTreeSentinal)); set(t, root(t, MethodTree), TreeNodeLeft, @@ -10032,7 +10039,7 @@ compileThunks(MyThread* t, FixedAllocator* allocator) BootImage* image = p->bootImage; if (image) { - uint8_t* imageBase = p->codeAllocator.base; + uint8_t* imageBase = p->codeAllocator.memory.begin(); image->thunks.default_ = thunkToThunk(p->thunks.default_, imageBase); image->thunks.defaultVirtual = thunkToThunk diff --git a/src/interpret.cpp b/src/interpret.cpp index 7ab3ce98a7..426f7e2e62 100644 --- a/src/interpret.cpp +++ b/src/interpret.cpp @@ -19,6 +19,7 @@ #include #include +#include using namespace vm; using namespace avian::system; @@ -3205,7 +3206,8 @@ class MyProcessor: public Processor { return makeObjectArray(t, 0); } - virtual void initialize(BootImage*, uint8_t*, unsigned) { + virtual void initialize(BootImage*, avian::util::Slice) + { abort(s); } @@ -3234,7 +3236,6 @@ class MyProcessor: public Processor { virtual void boot(vm::Thread*, BootImage* image, uint8_t* code) { expect(s, image == 0 and code == 0); } - virtual void callWithCurrentContinuation(vm::Thread*, object) { abort(s); diff --git a/src/tools/bootimage-generator/main.cpp b/src/tools/bootimage-generator/main.cpp index 3d7cf47788..5bcafc1500 100644 --- a/src/tools/bootimage-generator/main.cpp +++ b/src/tools/bootimage-generator/main.cpp @@ -1919,24 +1919,24 @@ main(int ac, const char** av) uint8_t* code = static_cast(h->allocate(CodeCapacity)); BootImage image; - p->initialize(&image, code, CodeCapacity); + p->initialize(&image, Slice(code, CodeCapacity)); Machine* m = new (h->allocate(sizeof(Machine))) Machine (s, h, f, 0, p, c, 0, 0, 0, 0, 128 * 1024); Thread* t = p->makeThread(m, 0, 0); - + enter(t, Thread::ActiveState); enter(t, Thread::IdleState); FileOutputStream bootimageOutput(args.bootimage); if (!bootimageOutput.isValid()) { - fprintf(stderr, "unable to open %s\n", args.bootimage); + fprintf(stderr, "unable to open %s\n", args.bootimage); return -1; } FileOutputStream codeOutput(args.codeimage); if (!codeOutput.isValid()) { - fprintf(stderr, "unable to open %s\n", args.codeimage); + fprintf(stderr, "unable to open %s\n", args.codeimage); return -1; } diff --git a/src/util/fixed-allocator.cpp b/src/util/fixed-allocator.cpp index 63e0c0bd5c..df5d02e292 100644 --- a/src/util/fixed-allocator.cpp +++ b/src/util/fixed-allocator.cpp @@ -15,8 +15,8 @@ namespace avian { namespace util { -FixedAllocator::FixedAllocator(Aborter* a, uint8_t* base, unsigned capacity) - : a(a), base(base), capacity(capacity), offset(0) +FixedAllocator::FixedAllocator(Aborter* a, Slice memory) + : a(a), memory(memory), offset(0) { } @@ -28,9 +28,9 @@ void* FixedAllocator::tryAllocate(unsigned size) void* FixedAllocator::allocate(unsigned size, unsigned padAlignment) { unsigned paddedSize = vm::pad(size, padAlignment); - expect(a, offset + paddedSize < capacity); + expect(a, offset + paddedSize < memory.count); - void* p = base + offset; + void* p = memory.begin() + offset; offset += paddedSize; return p; } @@ -42,7 +42,8 @@ void* FixedAllocator::allocate(unsigned size) void FixedAllocator::free(const void* p, unsigned size) { - if (p >= base and static_cast(p) + size == base + offset) { + if (p >= memory.begin() and static_cast(p) + size + == memory.begin() + offset) { offset -= size; } else { abort(a);