use Slice<uint8_t> in FixedAllocator

This commit is contained in:
Joshua Warner 2014-02-25 15:46:35 -07:00 committed by Joshua Warner
parent b083f3df04
commit a807966143
7 changed files with 70 additions and 55 deletions

View File

@ -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;

View File

@ -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<uint8_t> 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<uint8_t> memory;
size_t offset;
};
} // namespace util

View File

@ -23,6 +23,11 @@ namespace avian {
namespace codegen {
class DelayedPromise;
}
namespace util {
template <class T>
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<uint8_t> code)
= 0;
virtual void
addCompilationHandler(CompilationHandler* handler) = 0;

View File

@ -24,6 +24,7 @@
#include <avian/util/runtime-array.h>
#include <avian/util/list.h>
#include <avian/util/slice.h>
#include <avian/util/fixed-allocator.h>
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<intptr_t>(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<intptr_t>(
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<uintptr_t>(a->base);
uintptr_t end = reinterpret_cast<uintptr_t>(a->base) + a->capacity;
uintptr_t start = reinterpret_cast<uintptr_t>(a->memory.begin());
uintptr_t end = reinterpret_cast<uintptr_t>(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<uint8_t*>(context->executableStart);
context->executableSize = (allocator->memory.begin() + allocator->offset)
- static_cast<uint8_t*>(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<uint8_t>(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<uint8_t> 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<uintptr_t>(codeAllocator.base);
-= reinterpret_cast<uintptr_t>(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<uintptr_t>(codeAllocator.base));
table[index++] = targetVW
(w->map()->find(callNodeTarget(t, p))
| (static_cast<unsigned>(callNodeFlags(t, p)) << TargetBootShift));
table[index++] = targetVW(
callNodeAddress(t, p)
- reinterpret_cast<uintptr_t>(codeAllocator.memory.begin()));
table[index++] = targetVW(
w->map()->find(callNodeTarget(t, p))
| (static_cast<unsigned>(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<uint8_t*>
(s->tryAllocateExecutable(ExecutableAreaSizeInBytes));
codeAllocator.capacity = ExecutableAreaSizeInBytes;
if (codeAllocator.memory.begin() == 0) {
codeAllocator.memory.items = static_cast<uint8_t*>(
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

View File

@ -19,6 +19,7 @@
#include <avian/util/runtime-array.h>
#include <avian/util/list.h>
#include <avian/util/slice.h>
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<uint8_t>)
{
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);

View File

@ -1919,24 +1919,24 @@ main(int ac, const char** av)
uint8_t* code = static_cast<uint8_t*>(h->allocate(CodeCapacity));
BootImage image;
p->initialize(&image, code, CodeCapacity);
p->initialize(&image, Slice<uint8_t>(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;
}

View File

@ -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<uint8_t> 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<const uint8_t*>(p) + size == base + offset) {
if (p >= memory.begin() and static_cast<const uint8_t*>(p) + size
== memory.begin() + offset) {
offset -= size;
} else {
abort(a);