Merge pull request #180 from joshuawarner32/move-allocator

Move allocator & Slice into util
This commit is contained in:
Joel Dice 2014-02-27 08:55:24 -07:00
commit 1cd822b23e
43 changed files with 570 additions and 315 deletions

View File

@ -12,11 +12,15 @@
#define AVIAN_CODEGEN_ARCHITECTURE_H
namespace vm {
class Allocator;
class Zone;
}
namespace avian {
namespace util {
class Allocator;
}
namespace codegen {
class Assembler;
@ -125,7 +129,7 @@ virtual void planDestination
unsigned bSize, const OperandMask& bMask,
unsigned cSize, OperandMask& cMask) = 0;
virtual Assembler* makeAssembler(vm::Allocator*, vm::Zone*) = 0;
virtual Assembler* makeAssembler(util::Allocator*, vm::Zone*) = 0;
virtual void acquire() = 0;
virtual void release() = 0;

View File

@ -11,7 +11,9 @@
#ifndef AVIAN_CODEGEN_PROMISE_H
#define AVIAN_CODEGEN_PROMISE_H
#include "avian/allocator.h"
#include <avian/util/allocator.h>
#include <avian/util/abort.h>
#include <avian/system/system.h>
namespace avian {
namespace codegen {
@ -102,8 +104,8 @@ class OffsetPromise: public Promise {
class ListenPromise: public Promise {
public:
ListenPromise(vm::System* s, vm::Allocator* allocator):
s(s), allocator(allocator), listener(0)
ListenPromise(vm::System* s, util::Allocator* allocator)
: s(s), allocator(allocator), listener(0)
{ }
virtual int64_t value() {
@ -122,16 +124,18 @@ class ListenPromise: public Promise {
}
vm::System* s;
vm::Allocator* allocator;
util::Allocator* allocator;
Listener* listener;
Promise* promise;
};
class DelayedPromise: public ListenPromise {
public:
DelayedPromise(vm::System* s, vm::Allocator* allocator, Promise* basis,
DelayedPromise* next):
ListenPromise(s, allocator), basis(basis), next(next)
DelayedPromise(vm::System* s,
util::Allocator* allocator,
Promise* basis,
DelayedPromise* next)
: ListenPromise(s, allocator), basis(basis), next(next)
{ }
virtual int64_t value() {

View File

@ -12,7 +12,7 @@
#define HEAP_H
#include <avian/system/system.h>
#include "avian/allocator.h"
#include <avian/util/allocator.h>
namespace vm {
@ -22,7 +22,7 @@ const unsigned TenureThreshold = 3;
const unsigned FixieTenureThreshold = TenureThreshold + 2;
class Heap: public Allocator {
class Heap : public avian::util::Allocator {
public:
enum CollectionType {
MinorCollection,
@ -65,9 +65,10 @@ class Heap: public Allocator {
virtual void collect(CollectionType type, unsigned footprint,
int pendingAllocation) = 0;
virtual unsigned fixedFootprint(unsigned sizeInWords, bool objectMask) = 0;
virtual void* allocateFixed(Allocator* allocator, unsigned sizeInWords,
virtual void* allocateFixed(avian::util::Allocator* allocator,
unsigned sizeInWords,
bool objectMask) = 0;
virtual void* allocateImmortalFixed(Allocator* allocator,
virtual void* allocateImmortalFixed(avian::util::Allocator* allocator,
unsigned sizeInWords,
bool objectMask) = 0;
virtual void mark(void* p, unsigned offset, unsigned count) = 0;

View File

@ -12,7 +12,7 @@
#define SYSTEM_H
#include "avian/common.h"
#include "avian/allocator.h"
#include <avian/util/allocator.h>
#include <avian/util/abort.h>
namespace vm {
@ -137,7 +137,7 @@ class System : public avian::util::Aborter {
virtual Status load(Library**, const char* name) = 0;
virtual char pathSeparator() = 0;
virtual char fileSeparator() = 0;
virtual const char* toAbsolutePath(Allocator* allocator,
virtual const char* toAbsolutePath(avian::util::Allocator* allocator,
const char* name) = 0;
virtual int64_t now() = 0;
virtual void yield() = 0;

View File

@ -14,6 +14,7 @@
#include <stdlib.h>
#include <avian/util/string.h>
#include <avian/util/slice.h>
#include "avian/environment.h"
@ -72,51 +73,32 @@ public:
unsigned add(util::String str);
};
template<class T>
class Slice {
public:
T* items;
size_t count;
inline Slice(T* items, size_t count):
items(items),
count(count) {}
inline Slice(const Slice<T>& copy):
items(copy.items),
count(copy.count) {}
inline T* begin() {
return items;
}
inline T* end() {
return items + count;
}
};
template<class T>
class DynamicArray : public Slice<T> {
public:
template <class T>
class DynamicArray : public util::Slice<T> {
public:
size_t capacity;
DynamicArray():
Slice<T>((T*)malloc(10 * sizeof(T)), 0),
capacity(10) {}
~DynamicArray() {
free(Slice<T>::items);
DynamicArray() : util::Slice<T>((T*)malloc(10 * sizeof(T)), 0), capacity(10)
{
}
~DynamicArray()
{
free(util::Slice<T>::items);
}
void ensure(size_t more) {
if(Slice<T>::count + more > capacity) {
capacity = capacity * 2 + more;
Slice<T>::items = (T*)realloc(Slice<T>::items, capacity * sizeof(T));
void ensure(size_t more)
{
if (util::Slice<T>::count + more > capacity) {
capacity = capacity * 2 + more;
util::Slice<T>::items
= (T*)realloc(util::Slice<T>::items, capacity * sizeof(T));
}
}
}
void add(const T& item) {
void add(const T& item)
{
ensure(1);
Slice<T>::items[Slice<T>::count++] = item;
util::Slice<T>::items[util::Slice<T>::count++] = item;
}
};
@ -175,7 +157,11 @@ public:
Executable = 1 << 1
};
virtual bool writeObject(OutputStream* out, Slice<SymbolInfo> symbols, Slice<const uint8_t> data, unsigned accessFlags, unsigned alignment) = 0;
virtual bool writeObject(OutputStream* out,
util::Slice<SymbolInfo> symbols,
util::Slice<const uint8_t> data,
unsigned accessFlags,
unsigned alignment) = 0;
static Platform* getPlatform(PlatformInfo info);
};

View File

@ -11,6 +11,10 @@
#ifndef AVIAN_UTIL_ABORT_H
#define AVIAN_UTIL_ABORT_H
// TODO: remove reference back into the source directory!
// Note: this is needed for UNLIKELY
#include <avian/common.h>
namespace avian {
namespace util {
@ -19,6 +23,11 @@ public:
virtual void NO_RETURN abort() = 0;
};
inline Aborter* getAborter(Aborter* a)
{
return a;
}
template<class T>
inline void NO_RETURN abort(T t) {
getAborter(t)->abort();
@ -40,7 +49,7 @@ inline void assert(T t, bool v) {
expect(t, v);
}
#endif
} // namespace util
} // namespace avian

View File

@ -0,0 +1,35 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef AVIAN_UTIL_ALLOCATOR_H
#define AVIAN_UTIL_ALLOCATOR_H
#include <stddef.h>
namespace avian {
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;
};
} // namespace util
} // namespace avian
inline void* operator new(size_t size, avian::util::Allocator* allocator)
{
return allocator->allocate(size);
}
#endif // AVIAN_UTIL_ALLOCATOR_H

View File

@ -0,0 +1,43 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef AVIAN_UTIL_FIXED_ALLOCATOR_H
#define AVIAN_UTIL_FIXED_ALLOCATOR_H
#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, Slice<uint8_t> memory);
virtual void* tryAllocate(unsigned size);
void* allocate(unsigned size, unsigned padAlignment);
virtual void* allocate(unsigned size);
virtual void free(const void* p, unsigned size);
Aborter* a;
Slice<uint8_t> memory;
size_t offset;
};
} // namespace util
} // namespace avian
#endif // AVIAN_UTIL_FIXED_ALLOCATOR_H

View File

@ -11,6 +11,11 @@
#ifndef AVIAN_UTIL_LIST_H
#define AVIAN_UTIL_LIST_H
#include "allocator.h"
namespace avian {
namespace util {
template <class T>
class List {
public:
@ -32,4 +37,7 @@ public:
List<T>* next;
};
#endif // AVIAN_UTIL_LIST_H
} // namespace util
} // namespace avian
#endif // AVIAN_UTIL_LIST_H

View File

@ -0,0 +1,74 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef AVIAN_UTIL_SLICE_H
#define AVIAN_UTIL_SLICE_H
#include "allocator.h"
#include "math.h"
namespace avian {
namespace util {
template <class T>
class Slice {
public:
T* items;
size_t count;
inline Slice(T* items, size_t count) : items(items), count(count)
{
}
inline Slice(const Slice<T>& copy) : items(copy.items), count(copy.count)
{
}
inline T& operator[](size_t index)
{
return items[index];
}
inline T* begin()
{
return items;
}
inline T* end()
{
return items + count;
}
static Slice<T> alloc(Allocator* a, size_t count)
{
return Slice<T>((T*)a->allocate(sizeof(T) * count), count);
}
Slice<T> clone(Allocator* a)
{
Slice<T> ret((T*)a->allocate(count * sizeof(T)), count);
memcpy(ret.items, items, count * sizeof(T));
return ret;
}
void resize(Allocator* a, size_t newCount)
{
T* newItems = (T*)a->allocate(newCount * sizeof(T));
memcpy(newItems, items, min(count, newCount));
a->free(items, count);
items = newItems;
count = newCount;
}
};
} // namespace util
} // namespace avian
#endif // AVIAN_UTIL_SLICE_H

View File

@ -1144,7 +1144,8 @@ compiler-sources = \
$(wildcard $(src)/codegen/compiler/*.cpp) \
$(src)/codegen/registers.cpp \
$(src)/codegen/runtime.cpp \
$(src)/codegen/targets.cpp
$(src)/codegen/targets.cpp \
$(src)/util/fixed-allocator.cpp
x86-assembler-sources = $(wildcard $(src)/codegen/target/x86/*.cpp)

View File

@ -11,10 +11,12 @@
#ifndef VECTOR_H
#define VECTOR_H
#include <avian/system/system.h>
#include "avian/target.h"
#include <avian/target.h>
#include <avian/util/math.h>
#include <avian/util/abort.h>
#include <avian/util/slice.h>
#include <avian/util/allocator.h>
#undef max
#undef min
@ -23,13 +25,14 @@ namespace vm {
class Vector {
public:
Vector(System* s, Allocator* allocator, unsigned minimumCapacity):
s(s),
allocator(allocator),
data(0),
position(0),
capacity(0),
minimumCapacity(minimumCapacity)
Vector(avian::util::Aborter* a,
avian::util::Allocator* allocator,
size_t minimumCapacity)
: a(a),
allocator(allocator),
data(0, 0),
position(0),
minimumCapacity(minimumCapacity)
{ }
~Vector() {
@ -37,61 +40,56 @@ class Vector {
}
void dispose() {
if (data and minimumCapacity >= 0) {
allocator->free(data, capacity);
data = 0;
if (data.items and minimumCapacity > 0) {
allocator->free(data.items, data.count);
data.items = 0;
data.count = 0;
}
}
void wrap(uint8_t* data, unsigned capacity) {
dispose();
void ensure(size_t space)
{
if (position + space > data.count) {
assert(a, minimumCapacity > 0);
this->data = data;
this->position = 0;
this->capacity = capacity;
this->minimumCapacity = -1;
}
void ensure(unsigned space) {
if (position + space > capacity) {
assert(s, minimumCapacity >= 0);
unsigned newCapacity = avian::util::max
(position + space, avian::util::max(minimumCapacity, capacity * 2));
uint8_t* newData = static_cast<uint8_t*>
(allocator->allocate(newCapacity));
if (data) {
memcpy(newData, data, position);
allocator->free(data, capacity);
size_t newCapacity = avian::util::max(
position + space, avian::util::max(minimumCapacity, data.count * 2));
if (data.begin()) {
data.resize(allocator, newCapacity);
} else {
data = avian::util::Slice<uint8_t>::alloc(allocator, newCapacity);
}
data = newData;
capacity = newCapacity;
}
}
void get(unsigned offset, void* dst, unsigned size) {
assert(s, offset + size <= position);
memcpy(dst, data + offset, size);
void get(size_t offset, void* dst, size_t size)
{
assert(a, offset + size <= position);
memcpy(dst, data.begin() + offset, size);
}
void set(unsigned offset, const void* src, unsigned size) {
assert(s, offset + size <= position);
memcpy(data + offset, src, size);
void set(size_t offset, const void* src, size_t size)
{
assert(a, offset + size <= position);
memcpy(data.begin() + offset, src, size);
}
void pop(void* dst, unsigned size) {
void pop(void* dst, size_t size)
{
get(position - size, dst, size);
position -= size;
}
void* allocate(unsigned size) {
void* allocate(size_t size)
{
ensure(size);
void* r = data + position;
void* r = data.begin() + position;
position += size;
return r;
}
void* append(const void* p, unsigned size) {
void* append(const void* p, size_t size)
{
void* r = allocate(size);
memcpy(r, p, size);
return r;
@ -121,47 +119,53 @@ class Vector {
append(&v, BytesPerWord);
}
void set2(unsigned offset, uint16_t v) {
assert(s, offset <= position - 2);
memcpy(data + offset, &v, 2);
void set2(size_t offset, uint16_t v)
{
assert(a, offset <= position - 2);
memcpy(data.begin() + offset, &v, 2);
}
unsigned get(unsigned offset) {
size_t get(size_t offset)
{
uint8_t v; get(offset, &v, 1);
return v;
}
unsigned get2(unsigned offset) {
size_t get2(size_t offset)
{
uint16_t v; get(offset, &v, 2);
return v;
}
unsigned get4(unsigned offset) {
size_t get4(size_t offset)
{
uint32_t v; get(offset, &v, 4);
return v;
}
uintptr_t getAddress(unsigned offset) {
uintptr_t getAddress(size_t offset)
{
uintptr_t v; get(offset, &v, BytesPerWord);
return v;
}
unsigned length() {
size_t length()
{
return position;
}
template <class T>
T* peek(unsigned offset) {
assert(s, offset + sizeof(T) <= position);
return reinterpret_cast<T*>(data + offset);
T* peek(size_t offset)
{
assert(a, offset + sizeof(T) <= position);
return reinterpret_cast<T*>(data.begin() + offset);
}
System* s;
Allocator* allocator;
uint8_t* data;
unsigned position;
unsigned capacity;
int minimumCapacity;
avian::util::Aborter* a;
avian::util::Allocator* allocator;
avian::util::Slice<uint8_t> data;
size_t position;
size_t minimumCapacity;
};
} // namespace vm

View File

@ -8,22 +8,18 @@
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#ifndef APPEND_H
#define APPEND_H
#include "avian/common.h"
#include <avian/common.h>
#include <avian/util/allocator.h>
namespace vm {
class Allocator {
public:
virtual void* tryAllocate(unsigned size) = 0;
virtual void* allocate(unsigned size) = 0;
virtual void free(const void* p, unsigned size) = 0;
};
inline const char*
append(Allocator* allocator, const char* a, const char* b, const char* c)
inline const char* append(avian::util::Allocator* allocator,
const char* a,
const char* b,
const char* c)
{
unsigned al = strlen(a);
unsigned bl = strlen(b);
@ -35,8 +31,9 @@ append(Allocator* allocator, const char* a, const char* b, const char* c)
return p;
}
inline const char*
append(Allocator* allocator, const char* a, const char* b)
inline const char* append(avian::util::Allocator* allocator,
const char* a,
const char* b)
{
unsigned al = strlen(a);
unsigned bl = strlen(b);
@ -46,8 +43,7 @@ append(Allocator* allocator, const char* a, const char* b)
return p;
}
inline const char*
copy(Allocator* allocator, const char* a)
inline const char* copy(avian::util::Allocator* allocator, const char* a)
{
unsigned al = strlen(a);
char* p = static_cast<char*>(allocator->allocate(al + 1));
@ -55,10 +51,6 @@ copy(Allocator* allocator, const char* a)
return p;
}
} // namespace vm
} // namespace vm
inline void* operator new (size_t size, vm::Allocator* allocator) {
return allocator->allocate(size);
}
#endif//ALLOCATOR_H
#endif // APPEND_H

View File

@ -13,7 +13,12 @@
#include "avian/common.h"
#include <avian/system/system.h>
#include "avian/allocator.h"
namespace avian {
namespace util {
class Allocator;
}
}
namespace vm {
@ -174,12 +179,15 @@ class Finder {
virtual void dispose() = 0;
};
AVIAN_EXPORT Finder*
makeFinder(System* s, Allocator* a, const char* path, const char* bootLibrary);
AVIAN_EXPORT Finder* makeFinder(System* s,
avian::util::Allocator* a,
const char* path,
const char* bootLibrary);
Finder*
makeFinder(System* s, Allocator* a, const uint8_t* jarData,
unsigned jarLength);
Finder* makeFinder(System* s,
avian::util::Allocator* a,
const uint8_t* jarData,
unsigned jarLength);
} // namespace vm

View File

@ -14,7 +14,7 @@
#include "avian/lzma.h"
#include "C/Types.h"
#include <avian/system/system.h>
#include "avian/allocator.h"
#include "avian/util/allocator.h"
namespace vm {

View File

@ -12,17 +12,26 @@
#define LZMA_H
#include <avian/system/system.h>
#include "avian/allocator.h"
namespace avian {
namespace util {
class Allocator;
}
}
namespace vm {
uint8_t*
decodeLZMA(System* s, Allocator* a, uint8_t* in, unsigned inSize,
unsigned* outSize);
uint8_t* decodeLZMA(System* s,
avian::util::Allocator* a,
uint8_t* in,
unsigned inSize,
unsigned* outSize);
uint8_t*
encodeLZMA(System* s, Allocator* a, uint8_t* in, unsigned inSize,
unsigned* outSize);
uint8_t* encodeLZMA(System* s,
avian::util::Allocator* a,
uint8_t* in,
unsigned inSize,
unsigned* outSize);
} // namespace vm

View File

@ -1793,43 +1793,6 @@ inline Aborter* getAborter(Thread* t) {
return t->m->system;
}
class FixedAllocator: public Allocator {
public:
FixedAllocator(System* s, uint8_t* base, unsigned capacity):
s(s), base(base), offset(0), capacity(capacity)
{ }
virtual void* tryAllocate(unsigned size) {
return allocate(size);
}
void* allocate(unsigned size, unsigned padAlignment) {
unsigned paddedSize = pad(size, padAlignment);
expect(s, offset + paddedSize < capacity);
void* p = base + offset;
offset += paddedSize;
return p;
}
virtual void* allocate(unsigned size) {
return allocate(size, BytesPerWord);
}
virtual void free(const void* p, unsigned size) {
if (p >= base and static_cast<const uint8_t*>(p) + size == base + offset) {
offset -= size;
} else {
abort(s);
}
}
System* s;
uint8_t* base;
unsigned offset;
unsigned capacity;
};
inline bool
ensure(Thread* t, unsigned sizeInBytes)
{

View File

@ -14,6 +14,7 @@
#include "avian/common.h"
#include <avian/system/system.h>
#include <avian/heap/heap.h>
#include <avian/util/allocator.h>
#include "bootimage.h"
#include "avian/heapwalk.h"
#include "avian/zone.h"
@ -22,6 +23,11 @@ namespace avian {
namespace codegen {
class DelayedPromise;
}
namespace util {
template <class T>
class Slice;
}
}
namespace vm {
@ -139,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;
@ -209,7 +215,7 @@ class Processor {
};
Processor* makeProcessor(System* system,
Allocator* allocator,
avian::util::Allocator* allocator,
const char* crashDumpDirectory,
bool useNativeFeatures);

View File

@ -12,13 +12,12 @@
#define ZONE_H
#include <avian/system/system.h>
#include "avian/allocator.h"
#include <avian/util/allocator.h>
#include <avian/util/math.h>
namespace vm {
class Zone: public Allocator {
class Zone : public avian::util::Allocator {
public:
class Segment {
public:

View File

@ -38,14 +38,14 @@ namespace isa {
// HARDWARE FLAGS
bool vfpSupported() {
// TODO: Use at runtime detection
#if defined(__ARM_PCS_VFP)
#if defined(__ARM_PCS_VFP)
// armhf
return true;
#else
// armel
// TODO: allow VFP use for -mfloat-abi=softfp armel builds.
// GCC -mfloat-abi=softfp flag allows use of VFP while remaining compatible
// with soft-float code.
// with soft-float code.
return false;
#endif
}
@ -793,7 +793,7 @@ class MyAssembler: public Assembler {
assert(&con, b.size == c.size);
assert(&con, b.type == lir::RegisterOperand);
assert(&con, c.type == lir::RegisterOperand);
arch_->con.ternaryOperations[index(&(arch_->con), op, a.type)]
(&con, b.size, a.operand, b.operand, c.operand);
}
@ -814,7 +814,9 @@ class MyAssembler: public Assembler {
unsigned blockOffset = 0;
for (PoolEvent* e = b->poolEventHead; e; e = e->next) {
unsigned size = e->offset - blockOffset;
memcpy(dst + dstOffset, con.code.data + b->offset + blockOffset, size);
memcpy(dst + dstOffset,
con.code.data.begin() + b->offset + blockOffset,
size);
blockOffset = e->offset;
dstOffset += size;
@ -857,7 +859,7 @@ class MyAssembler: public Assembler {
unsigned size = b->size - blockOffset;
memcpy(dst + dstOffset,
con.code.data + b->offset + blockOffset,
con.code.data.begin() + b->offset + blockOffset,
size);
dstOffset += size;

View File

@ -15,12 +15,21 @@ namespace avian {
namespace codegen {
namespace arm {
Context::Context(vm::System* s, vm::Allocator* a, vm::Zone* zone):
s(s), zone(zone), client(0), code(s, a, 1024), tasks(0), result(0),
firstBlock(new(zone) MyBlock(this, 0)),
lastBlock(firstBlock), poolOffsetHead(0), poolOffsetTail(0),
constantPool(0), constantPoolCount(0)
{ }
Context::Context(vm::System* s, util::Allocator* a, vm::Zone* zone)
: s(s),
zone(zone),
client(0),
code(s, a, 1024),
tasks(0),
result(0),
firstBlock(new (zone) MyBlock(this, 0)),
lastBlock(firstBlock),
poolOffsetHead(0),
poolOffsetTail(0),
constantPool(0),
constantPoolCount(0)
{
}
} // namespace arm
} // namespace codegen

View File

@ -17,7 +17,6 @@
namespace vm {
class System;
class Allocator;
class Zone;
} // namespace vm
@ -25,6 +24,7 @@ namespace avian {
namespace util {
class Aborter;
class Allocator;
} // namespace util
namespace codegen {
@ -37,7 +37,7 @@ class ConstantPoolEntry;
class Context {
public:
Context(vm::System* s, vm::Allocator* a, vm::Zone* zone);
Context(vm::System* s, util::Allocator* a, vm::Zone* zone);
vm::System* s;
vm::Zone* zone;

View File

@ -1065,9 +1065,10 @@ void branchLong(Context* con, lir::TernaryOperation op, lir::Operand* al,
}
if (next) {
updateOffset
(con->s, con->code.data + next, reinterpret_cast<intptr_t>
(con->code.data + con->code.length()));
updateOffset(con->s,
con->code.data.begin() + next,
reinterpret_cast<intptr_t>(con->code.data.begin()
+ con->code.length()));
}
}

View File

@ -586,7 +586,7 @@ class MyArchitecture: public Architecture {
}
}
virtual Assembler* makeAssembler(Allocator* allocator, Zone* zone);
virtual Assembler* makeAssembler(util::Allocator* allocator, Zone* zone);
virtual void acquire() {
++ referenceCount;
@ -604,8 +604,8 @@ class MyArchitecture: public Architecture {
class MyAssembler: public Assembler {
public:
MyAssembler(System* s, Allocator* a, Zone* zone, MyArchitecture* arch):
c(s, a, zone), arch_(arch)
MyAssembler(System* s, util::Allocator* a, Zone* zone, MyArchitecture* arch)
: c(s, a, zone), arch_(arch)
{ }
virtual void setClient(Client* client) {
@ -869,7 +869,9 @@ class MyAssembler: public Assembler {
unsigned blockOffset = 0;
for (JumpEvent* e = b->jumpEventHead; e; e = e->next) {
unsigned size = e->offset - blockOffset;
memcpy(dst + dstOffset, c.code.data + b->offset + blockOffset, size);
memcpy(dst + dstOffset,
c.code.data.begin() + b->offset + blockOffset,
size);
blockOffset = e->offset;
dstOffset += size;
@ -903,9 +905,8 @@ class MyAssembler: public Assembler {
unsigned size = b->size - blockOffset;
memcpy(dst + dstOffset,
c.code.data + b->offset + blockOffset,
size);
memcpy(
dst + dstOffset, c.code.data.begin() + b->offset + blockOffset, size);
dstOffset += size;
}
@ -992,7 +993,8 @@ class MyAssembler: public Assembler {
MyArchitecture* arch_;
};
Assembler* MyArchitecture::makeAssembler(Allocator* allocator, Zone* zone) {
Assembler* MyArchitecture::makeAssembler(util::Allocator* allocator, Zone* zone)
{
return new(zone) MyAssembler(this->c.s, allocator, zone, this);
}

View File

@ -16,13 +16,21 @@ namespace avian {
namespace codegen {
namespace powerpc {
Context::Context(vm::System* s, vm::Allocator* a, vm::Zone* zone):
s(s), zone(zone), client(0), code(s, a, 1024), tasks(0), result(0),
firstBlock(new(zone) MyBlock(this, 0)),
lastBlock(firstBlock), jumpOffsetHead(0), jumpOffsetTail(0),
constantPool(0), constantPoolCount(0)
{ }
Context::Context(vm::System* s, util::Allocator* a, vm::Zone* zone)
: s(s),
zone(zone),
client(0),
code(s, a, 1024),
tasks(0),
result(0),
firstBlock(new (zone) MyBlock(this, 0)),
lastBlock(firstBlock),
jumpOffsetHead(0),
jumpOffsetTail(0),
constantPool(0),
constantPoolCount(0)
{
}
} // namespace powerpc
} // namespace codegen

View File

@ -20,12 +20,16 @@
namespace vm {
class System;
class Allocator;
class Zone;
} // namespace vm
namespace avian {
namespace util {
class Allocator;
}
namespace codegen {
namespace powerpc {
@ -36,7 +40,7 @@ class MyBlock;
class Context {
public:
Context(vm::System* s, vm::Allocator* a, vm::Zone* zone);
Context(vm::System* s, util::Allocator* a, vm::Zone* zone);
vm::System* s;
vm::Zone* zone;

View File

@ -924,9 +924,12 @@ void branchLong(Context* c, lir::TernaryOperation op, lir::Operand* al,
}
if (next) {
updateOffset
(c->s, c->code.data + next, true, reinterpret_cast<intptr_t>
(c->code.data + c->code.length()), 0);
updateOffset(
c->s,
c->code.data.begin() + next,
true,
reinterpret_cast<intptr_t>(c->code.data.begin() + c->code.length()),
0);
}
}

View File

@ -15,7 +15,7 @@
#include "avian/target.h"
#include "avian/alloc-vector.h"
#include "avian/common.h"
#include "avian/allocator.h"
#include "avian/util/allocator.h"
#include "avian/zone.h"
#include <avian/util/runtime-array.h>
@ -790,7 +790,7 @@ class MyArchitecture: public Architecture {
}
}
virtual Assembler* makeAssembler(Allocator* allocator, Zone* zone);
virtual Assembler* makeAssembler(util::Allocator* allocator, Zone* zone);
virtual void acquire() {
++ referenceCount;
@ -809,8 +809,8 @@ class MyArchitecture: public Architecture {
class MyAssembler: public Assembler {
public:
MyAssembler(System* s, Allocator* a, Zone* zone, MyArchitecture* arch):
c(s, a, zone, &(arch->c)), arch_(arch)
MyAssembler(System* s, util::Allocator* a, Zone* zone, MyArchitecture* arch)
: c(s, a, zone, &(arch->c)), arch_(arch)
{ }
virtual void setClient(Client* client) {
@ -1084,7 +1084,7 @@ class MyAssembler: public Assembler {
unsigned size = p->offset - b->offset - index;
memcpy(dst + b->start + index + padding,
c.code.data + b->offset + index,
c.code.data.begin() + b->offset + index,
size);
index += size;
@ -1098,7 +1098,7 @@ class MyAssembler: public Assembler {
}
memcpy(dst + b->start + index + padding,
c.code.data + b->offset + index,
c.code.data.begin() + b->offset + index,
b->size - index);
}
@ -1142,7 +1142,8 @@ class MyAssembler: public Assembler {
MyArchitecture* arch_;
};
Assembler* MyArchitecture::makeAssembler(Allocator* allocator, Zone* zone) {
Assembler* MyArchitecture::makeAssembler(util::Allocator* allocator, Zone* zone)
{
return
new(zone) MyAssembler(c.s, allocator, zone, this);
}

View File

@ -8,7 +8,7 @@
There is NO WARRANTY for this software. See license.txt for
details. */
#include "avian/allocator.h"
#include "avian/util/allocator.h"
#include "avian/zone.h"
#include "context.h"
@ -22,11 +22,21 @@ ArchitectureContext::ArchitectureContext(vm::System* s, bool useNativeFeatures):
s(s), useNativeFeatures(useNativeFeatures)
{ }
Context::Context(vm::System* s, vm::Allocator* a, vm::Zone* zone, ArchitectureContext* ac):
s(s), zone(zone), client(0), code(s, a, 1024), tasks(0), result(0),
firstBlock(new(zone) MyBlock(0)),
lastBlock(firstBlock), ac(ac)
{ }
Context::Context(vm::System* s,
util::Allocator* a,
vm::Zone* zone,
ArchitectureContext* ac)
: s(s),
zone(zone),
client(0),
code(s, a, 1024),
tasks(0),
result(0),
firstBlock(new (zone) MyBlock(0)),
lastBlock(firstBlock),
ac(ac)
{
}
} // namespace x86
} // namespace codegen

View File

@ -75,7 +75,10 @@ class ArchitectureContext {
class Context {
public:
Context(vm::System* s, vm::Allocator* a, vm::Zone* zone, ArchitectureContext* ac);
Context(vm::System* s,
util::Allocator* a,
vm::Zone* zone,
ArchitectureContext* ac);
vm::System* s;
vm::Zone* zone;

View File

@ -10,7 +10,7 @@
#include <string.h>
#include "avian/allocator.h"
#include "avian/util/allocator.h"
#include "avian/alloc-vector.h"
#include "avian/common.h"
#include "avian/zone.h"

View File

@ -10,7 +10,7 @@
#include "avian/target.h"
#include "avian/alloc-vector.h"
#include "avian/allocator.h"
#include "avian/util/allocator.h"
#include "avian/zone.h"
#include <avian/util/abort.h>

View File

@ -24,6 +24,8 @@
#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;
@ -1347,8 +1349,7 @@ storeLocal(Context* context, unsigned footprint, Compiler::Operand* value,
(footprint, value, translateLocalIndex(context, footprint, index));
}
FixedAllocator*
codeAllocator(MyThread* t);
avian::util::FixedAllocator* codeAllocator(MyThread* t);
class Frame {
public:
@ -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);
@ -8518,7 +8523,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)
@ -8921,9 +8926,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
}
@ -9021,10 +9027,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) {
@ -9059,7 +9065,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());
}
}
}
@ -9076,24 +9082,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
@ -9103,7 +9110,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,
@ -10031,7 +10038,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
@ -10279,8 +10286,7 @@ setRoot(Thread* t, Root root, object value)
ArrayBody + (root * BytesPerWord), value);
}
FixedAllocator*
codeAllocator(MyThread* t)
avian::util::FixedAllocator* codeAllocator(MyThread* t)
{
return &(processor(t)->codeAllocator);
}

View File

@ -16,7 +16,7 @@
#include "avian/zlib-custom.h"
#include "avian/finder.h"
#include "avian/lzma.h"
#include "avian/append.h"
using namespace vm;
using namespace avian::util;

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;
@ -3204,7 +3205,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);
}
@ -3233,7 +3235,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

@ -61,7 +61,7 @@ mainClass(const char* jar)
System* system = makeSystem();
class MyAllocator: public Allocator {
class MyAllocator : public avian::util::Allocator {
public:
MyAllocator(System* s): s(s) { }

View File

@ -48,7 +48,9 @@
#include "stdint.h"
#include "dirent.h"
#include "sched.h"
#include "avian/arch.h"
#include <avian/arch.h>
#include <avian/append.h>
#include <avian/system/system.h>
#include <avian/system/signal.h>

View File

@ -26,6 +26,7 @@
#include <avian/system/system.h>
#include <avian/system/signal.h>
#include <avian/util/runtime-array.h>
#include <avian/append.h>
#if defined(WINAPI_FAMILY)
@ -836,7 +837,9 @@ class MySystem: public System {
return SO_SUFFIX;
}
virtual const char* toAbsolutePath(Allocator* allocator, const char* name) {
virtual const char* toAbsolutePath(avian::util::Allocator* allocator,
const char* name)
{
#if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
if (strncmp(name, "//", 2) == 0
or strncmp(name, "\\\\", 2) == 0

View File

@ -38,6 +38,7 @@ void operator delete(void*) { abort(); }
namespace {
using namespace avian::tools;
using namespace avian::util;
bool
writeObject(uint8_t* data, size_t size, OutputStream* out, const char* startName,

View File

@ -1917,43 +1917,41 @@ main(int ac, const char** av)
const unsigned CodeCapacity = 30 * 1024 * 1024;
#endif
uint8_t* code = static_cast<uint8_t*>(h->allocate(CodeCapacity));
Slice<uint8_t> code = Slice<uint8_t>::alloc(h, CodeCapacity);
BootImage image;
p->initialize(&image, code, CodeCapacity);
p->initialize(&image, code);
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;
}
uintptr_t arguments[] = {
reinterpret_cast<uintptr_t>(&bootimageOutput),
reinterpret_cast<uintptr_t>(&codeOutput),
reinterpret_cast<uintptr_t>(&image),
reinterpret_cast<uintptr_t>(code),
reinterpret_cast<uintptr_t>(args.entryClass),
reinterpret_cast<uintptr_t>(args.entryMethod),
reinterpret_cast<uintptr_t>(args.entrySpec),
reinterpret_cast<uintptr_t>(args.bootimageStart),
reinterpret_cast<uintptr_t>(args.bootimageEnd),
reinterpret_cast<uintptr_t>(args.codeimageStart),
reinterpret_cast<uintptr_t>(args.codeimageEnd),
static_cast<uintptr_t>(args.useLZMA)
};
uintptr_t arguments[] = {reinterpret_cast<uintptr_t>(&bootimageOutput),
reinterpret_cast<uintptr_t>(&codeOutput),
reinterpret_cast<uintptr_t>(&image),
reinterpret_cast<uintptr_t>(code.begin()),
reinterpret_cast<uintptr_t>(args.entryClass),
reinterpret_cast<uintptr_t>(args.entryMethod),
reinterpret_cast<uintptr_t>(args.entrySpec),
reinterpret_cast<uintptr_t>(args.bootimageStart),
reinterpret_cast<uintptr_t>(args.bootimageEnd),
reinterpret_cast<uintptr_t>(args.codeimageStart),
reinterpret_cast<uintptr_t>(args.codeimageEnd),
static_cast<uintptr_t>(args.useLZMA)};
run(t, writeBootImage, arguments);

View File

@ -1943,7 +1943,7 @@ main(int ac, char** av)
System* system = makeSystem();
class MyAllocator: public Allocator {
class MyAllocator : public avian::util::Allocator {
public:
MyAllocator(System* s): s(s) { }

View File

@ -0,0 +1,54 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#include <avian/util/fixed-allocator.h>
#include <avian/common.h>
namespace avian {
namespace util {
FixedAllocator::FixedAllocator(Aborter* a, Slice<uint8_t> memory)
: a(a), memory(memory), offset(0)
{
}
void* FixedAllocator::tryAllocate(unsigned size)
{
return allocate(size);
}
void* FixedAllocator::allocate(unsigned size, unsigned padAlignment)
{
unsigned paddedSize = vm::pad(size, padAlignment);
expect(a, offset + paddedSize < memory.count);
void* p = memory.begin() + offset;
offset += paddedSize;
return p;
}
void* FixedAllocator::allocate(unsigned size)
{
return allocate(size, vm::BytesPerWord);
}
void FixedAllocator::free(const void* p, unsigned size)
{
if (p >= memory.begin() and static_cast<const uint8_t*>(p) + size
== memory.begin() + offset) {
offset -= size;
} else {
abort(a);
}
}
} // namespace util
} // namespace avian

View File

@ -11,3 +11,4 @@ if [ -z "${openjdk}" ]; then
make ${flags} bootimage=true test
fi
make ${flags} tails=true continuations=true test
make ${flags} codegen-targets=all