diff --git a/include/avian/codegen/architecture.h b/include/avian/codegen/architecture.h index 91a41551f0..b1cd7e8013 100644 --- a/include/avian/codegen/architecture.h +++ b/include/avian/codegen/architecture.h @@ -20,7 +20,7 @@ class Zone; namespace avian { namespace util { -class Allocator; +class Alloc; } namespace codegen { @@ -143,7 +143,7 @@ class Architecture { unsigned cSize, OperandMask& cMask) = 0; - virtual Assembler* makeAssembler(util::Allocator*, vm::Zone*) = 0; + virtual Assembler* makeAssembler(util::Alloc*, vm::Zone*) = 0; virtual void acquire() = 0; virtual void release() = 0; diff --git a/include/avian/codegen/ir.h b/include/avian/codegen/ir.h index 8b2b48aa9d..d694f0c154 100644 --- a/include/avian/codegen/ir.h +++ b/include/avian/codegen/ir.h @@ -35,11 +35,6 @@ class Type { Float, Address, - // Represents individual halves of two-word types - // (double/long on 32-bit systems) - // TODO: remove when possible - Half, - // Represents the lack of a return value // TODO: remove when possible Void, diff --git a/include/avian/codegen/promise.h b/include/avian/codegen/promise.h index 6d683533dc..dce86bb9dc 100644 --- a/include/avian/codegen/promise.h +++ b/include/avian/codegen/promise.h @@ -118,7 +118,7 @@ class OffsetPromise : public Promise { class ListenPromise : public Promise { public: - ListenPromise(vm::System* s, util::Allocator* allocator) + ListenPromise(vm::System* s, util::AllocOnly* allocator) : s(s), allocator(allocator), listener(0) { } @@ -142,7 +142,7 @@ class ListenPromise : public Promise { } vm::System* s; - util::Allocator* allocator; + util::AllocOnly* allocator; Listener* listener; Promise* promise; }; @@ -150,7 +150,7 @@ class ListenPromise : public Promise { class DelayedPromise : public ListenPromise { public: DelayedPromise(vm::System* s, - util::Allocator* allocator, + util::AllocOnly* allocator, Promise* basis, DelayedPromise* next) : ListenPromise(s, allocator), basis(basis), next(next) diff --git a/include/avian/heap/heap.h b/include/avian/heap/heap.h index e4e5ba929f..53dc343141 100644 --- a/include/avian/heap/heap.h +++ b/include/avian/heap/heap.h @@ -58,10 +58,10 @@ class Heap : public avian::util::Allocator { unsigned footprint, int pendingAllocation) = 0; virtual unsigned fixedFootprint(unsigned sizeInWords, bool objectMask) = 0; - virtual void* allocateFixed(avian::util::Allocator* allocator, + virtual void* allocateFixed(avian::util::Alloc* allocator, unsigned sizeInWords, bool objectMask) = 0; - virtual void* allocateImmortalFixed(avian::util::Allocator* allocator, + virtual void* allocateImmortalFixed(avian::util::Alloc* allocator, unsigned sizeInWords, bool objectMask) = 0; virtual void mark(void* p, unsigned offset, unsigned count) = 0; diff --git a/include/avian/system/memory.h b/include/avian/system/memory.h new file mode 100644 index 0000000000..527d54da68 --- /dev/null +++ b/include/avian/system/memory.h @@ -0,0 +1,49 @@ + /* Copyright (c) 2008-2014, 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_SYSTEM_MEMORY_H +#define AVIAN_SYSTEM_MEMORY_H + +#include + +#include + +namespace avian { +namespace system { + +class Memory { + public: + enum Permissions { + Read = 1 << 0, + Write = 1 << 1, + Execute = 1 << 2, + + // Utility munged constants + ReadWrite = Read | Write, + ReadExecute = Read | Execute, + ReadWriteExecute = Read | Write | Execute + }; + + static const size_t PageSize; + + // Allocate a contiguous range of pages. + static util::Slice allocate(size_t sizeInBytes, Permissions perms = ReadWrite); + + // Free a contiguous range of pages. + static void free(util::Slice pages); + + // TODO: In the future: + // static void setPermissions(util::Slice pages, Permissions perms); +}; + +} // namespace system +} // namespace avian + +#endif diff --git a/include/avian/system/system.h b/include/avian/system/system.h index ea31823f46..304112dfe8 100644 --- a/include/avian/system/system.h +++ b/include/avian/system/system.h @@ -113,10 +113,6 @@ class System : public avian::util::Aborter { virtual bool success(Status) = 0; virtual void* tryAllocate(unsigned sizeInBytes) = 0; virtual void free(const void* p) = 0; -#if !defined(AVIAN_AOT_ONLY) - virtual void* tryAllocateExecutable(unsigned sizeInBytes) = 0; - virtual void freeExecutable(const void* p, unsigned sizeInBytes) = 0; -#endif virtual Status attach(Runnable*) = 0; virtual Status start(Runnable*) = 0; virtual Status make(Mutex**) = 0; @@ -134,7 +130,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(avian::util::Allocator* allocator, + virtual const char* toAbsolutePath(avian::util::AllocOnly* allocator, const char* name) = 0; virtual int64_t now() = 0; virtual void yield() = 0; diff --git a/include/avian/util/allocator.h b/include/avian/util/allocator.h index 4b6d421b98..2e1fe30587 100644 --- a/include/avian/util/allocator.h +++ b/include/avian/util/allocator.h @@ -16,18 +16,25 @@ namespace avian { namespace util { -class Allocator { +class AllocOnly { 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; + virtual void* allocate(size_t size) = 0; +}; + +class Alloc : public AllocOnly { + public: + virtual void free(const void* p, size_t size) = 0; +}; + +class Allocator : public Alloc { + public: + virtual void* tryAllocate(size_t size) = 0; }; } // namespace util } // namespace avian -inline void* operator new(size_t size, avian::util::Allocator* allocator) +inline void* operator new(size_t size, avian::util::AllocOnly* allocator) { return allocator->allocate(size); } diff --git a/include/avian/util/fixed-allocator.h b/include/avian/util/fixed-allocator.h index 03fc61fcda..220bd5b1db 100644 --- a/include/avian/util/fixed-allocator.h +++ b/include/avian/util/fixed-allocator.h @@ -20,17 +20,17 @@ namespace util { // An Allocator that allocates, bump-pointer style, out of a pre-defined chunk // of memory. -class FixedAllocator : public Allocator { +class FixedAllocator : public Alloc { public: FixedAllocator(Aborter* a, Slice memory); - virtual void* tryAllocate(unsigned size); + virtual void* tryAllocate(size_t size); - void* allocate(unsigned size, unsigned padAlignment); + void* allocate(size_t size, unsigned padAlignment); - virtual void* allocate(unsigned size); + virtual void* allocate(size_t size); - virtual void free(const void* p, unsigned size); + virtual void free(const void* p, size_t size); Aborter* a; Slice memory; diff --git a/include/avian/util/slice.h b/include/avian/util/slice.h index 69667b63c4..a5003f1ac5 100644 --- a/include/avian/util/slice.h +++ b/include/avian/util/slice.h @@ -69,12 +69,12 @@ class Slice { return Slice(this->begin() + begin, count); } - static Slice alloc(Allocator* a, size_t count) + static Slice alloc(AllocOnly* a, size_t count) { return Slice((T*)a->allocate(sizeof(T) * count), count); } - static Slice allocAndSet(Allocator* a, size_t count, const T& item) + static Slice allocAndSet(AllocOnly* a, size_t count, const T& item) { Slice slice(alloc(a, count)); for (size_t i = 0; i < count; i++) { @@ -83,14 +83,14 @@ class Slice { return slice; } - Slice clone(Allocator* a, size_t newCount) + Slice clone(AllocOnly* a, size_t newCount) { T* newItems = (T*)a->allocate(newCount * sizeof(T)); memcpy(newItems, items, min(count, newCount) * sizeof(T)); return Slice(newItems, newCount); } - Slice cloneAndSet(Allocator* a, size_t newCount, const T& item) + Slice cloneAndSet(AllocOnly* a, size_t newCount, const T& item) { Slice slice(clone(a, newCount)); for (size_t i = count; i < newCount; i++) { @@ -99,7 +99,7 @@ class Slice { return slice; } - void resize(Allocator* a, size_t newCount) + void resize(Alloc* a, size_t newCount) { Slice slice(clone(a, newCount)); a->free(items, count); diff --git a/include/avian/util/string.h b/include/avian/util/string.h index 704dd9a5b1..49d854ab67 100644 --- a/include/avian/util/string.h +++ b/include/avian/util/string.h @@ -30,38 +30,6 @@ class String { } }; -class Tokenizer { - public: - Tokenizer(const char* s, char delimiter) - : s(s), limit(0), delimiter(delimiter) - { - } - - Tokenizer(String str, char delimiter) - : s(str.text), limit(str.text + str.length), delimiter(delimiter) - { - } - - bool hasMore() - { - while (s != limit and *s == delimiter) - ++s; - return s != limit and *s != 0; - } - - String next() - { - const char* p = s; - while (s != limit and *s and *s != delimiter) - ++s; - return String(p, s - p); - } - - const char* s; - const char* limit; - char delimiter; -}; - } // namespace util } // namespace avain diff --git a/include/avian/util/tokenizer.h b/include/avian/util/tokenizer.h new file mode 100644 index 0000000000..5da035bac8 --- /dev/null +++ b/include/avian/util/tokenizer.h @@ -0,0 +1,54 @@ +/* Copyright (c) 2008-2014, 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_TOKENIZER_H +#define AVIAN_UTIL_TOKENIZER_H + +#include "string.h" + +namespace avian { +namespace util { + +class Tokenizer { + public: + Tokenizer(const char* s, char delimiter) + : s(s), limit(0), delimiter(delimiter) + { + } + + Tokenizer(String str, char delimiter) + : s(str.text), limit(str.text + str.length), delimiter(delimiter) + { + } + + bool hasMore() + { + while (s != limit and *s == delimiter) + ++s; + return s != limit and *s != 0; + } + + String next() + { + const char* p = s; + while (s != limit and *s and *s != delimiter) + ++s; + return String(p, s - p); + } + + const char* s; + const char* limit; + char delimiter; +}; + +} // namespace util +} // namespace avain + +#endif // AVIAN_UTIL_TOKENIZER_H diff --git a/makefile b/makefile index e0e85411a0..2a763ecb71 100755 --- a/makefile +++ b/makefile @@ -754,10 +754,6 @@ openjdk-extra-cflags += $(classpath-extra-cflags) find-tool = $(shell if ( command -v "$(1)$(2)" >/dev/null ); then (echo "$(1)$(2)") else (echo "$(2)"); fi) -ifeq ($(build-platform),windows) - static-on-windows = -static -endif - ifeq ($(platform),windows) target-format = pe @@ -787,6 +783,7 @@ ifeq ($(platform),windows) strip = $(prefix)strip --strip-all else build-system = windows + static-on-windows = -static common-cflags += "-I$(JAVA_HOME)/include/win32" build-cflags = $(common-cflags) -I$(src) -I$(inc) -mthreads openjdk-extra-cflags = @@ -1138,6 +1135,7 @@ vm-depends := $(generated-code) \ vm-sources = \ $(src)/system/$(system).cpp \ $(src)/system/$(system)/signal.cpp \ + $(src)/system/$(system)/memory.cpp \ $(src)/finder.cpp \ $(src)/machine.cpp \ $(src)/util.cpp \ @@ -1273,6 +1271,7 @@ generator-sources = \ $(src)/tools/type-generator/main.cpp \ $(src)/system/$(build-system).cpp \ $(src)/system/$(build-system)/signal.cpp \ + $(src)/system/$(build-system)/memory.cpp \ $(src)/finder.cpp \ $(src)/util/arg-parser.cpp diff --git a/src/avian/alloc-vector.h b/src/avian/alloc-vector.h index a859864d7c..29b82f1551 100644 --- a/src/avian/alloc-vector.h +++ b/src/avian/alloc-vector.h @@ -26,7 +26,7 @@ namespace vm { class Vector { public: Vector(avian::util::Aborter* a, - avian::util::Allocator* allocator, + avian::util::Alloc* allocator, size_t minimumCapacity) : a(a), allocator(allocator), @@ -175,7 +175,7 @@ class Vector { } avian::util::Aborter* a; - avian::util::Allocator* allocator; + avian::util::Alloc* allocator; avian::util::Slice data; size_t position; size_t minimumCapacity; diff --git a/src/avian/append.h b/src/avian/append.h index 22a8854d59..2a0be10151 100644 --- a/src/avian/append.h +++ b/src/avian/append.h @@ -16,7 +16,7 @@ namespace vm { -inline const char* append(avian::util::Allocator* allocator, +inline const char* append(avian::util::AllocOnly* allocator, const char* a, const char* b, const char* c) @@ -31,7 +31,7 @@ inline const char* append(avian::util::Allocator* allocator, return p; } -inline const char* append(avian::util::Allocator* allocator, +inline const char* append(avian::util::AllocOnly* allocator, const char* a, const char* b) { @@ -43,7 +43,7 @@ inline const char* append(avian::util::Allocator* allocator, return p; } -inline const char* copy(avian::util::Allocator* allocator, const char* a) +inline const char* copy(avian::util::AllocOnly* allocator, const char* a) { unsigned al = strlen(a); char* p = static_cast(allocator->allocate(al + 1)); diff --git a/src/avian/classpath-common.h b/src/avian/classpath-common.h index 83d62d6eae..8c915c3a83 100644 --- a/src/avian/classpath-common.h +++ b/src/avian/classpath-common.h @@ -13,6 +13,7 @@ #include #include +#include using namespace avian::util; diff --git a/src/avian/finder.h b/src/avian/finder.h index cb37d2789b..fe0e34e888 100644 --- a/src/avian/finder.h +++ b/src/avian/finder.h @@ -16,7 +16,7 @@ namespace avian { namespace util { -class Allocator; +class Alloc; } } @@ -202,12 +202,12 @@ class Finder { }; AVIAN_EXPORT Finder* makeFinder(System* s, - avian::util::Allocator* a, + avian::util::Alloc* a, const char* path, const char* bootLibrary); Finder* makeFinder(System* s, - avian::util::Allocator* a, + avian::util::Alloc* a, const uint8_t* jarData, unsigned jarLength); diff --git a/src/avian/lzma.h b/src/avian/lzma.h index 12642ded3f..7d6ec23ec7 100644 --- a/src/avian/lzma.h +++ b/src/avian/lzma.h @@ -15,20 +15,20 @@ namespace avian { namespace util { -class Allocator; +class AllocOnly; } } namespace vm { uint8_t* decodeLZMA(System* s, - avian::util::Allocator* a, + avian::util::AllocOnly* a, uint8_t* in, unsigned inSize, unsigned* outSize); uint8_t* encodeLZMA(System* s, - avian::util::Allocator* a, + avian::util::AllocOnly* a, uint8_t* in, unsigned inSize, unsigned* outSize); diff --git a/src/avian/machine.h b/src/avian/machine.h index c4ec3360fa..36f65ee868 100644 --- a/src/avian/machine.h +++ b/src/avian/machine.h @@ -1596,7 +1596,7 @@ inline bool ensure(Thread* t, unsigned sizeInBytes) object allocate2(Thread* t, unsigned sizeInBytes, bool objectMask); object allocate3(Thread* t, - Allocator* allocator, + Alloc* allocator, Machine::AllocationType type, unsigned sizeInBytes, bool objectMask); diff --git a/src/avian/zone.h b/src/avian/zone.h index 71e80aec90..e3b0662bd6 100644 --- a/src/avian/zone.h +++ b/src/avian/zone.h @@ -17,7 +17,7 @@ namespace vm { -class Zone : public avian::util::Allocator { +class Zone : public avian::util::AllocOnly { public: class Segment { public: @@ -31,9 +31,8 @@ class Zone : public avian::util::Allocator { uint8_t data[0]; }; - Zone(System* s, Allocator* allocator, unsigned minimumFootprint) - : s(s), - allocator(allocator), + Zone(avian::util::Allocator* allocator, size_t minimumFootprint) + : allocator(allocator), segment(0), minimumFootprint(minimumFootprint < sizeof(Segment) ? 0 @@ -56,6 +55,46 @@ class Zone : public avian::util::Allocator { segment = 0; } + virtual void* allocate(size_t size) + { + size = pad(size); + void* p = tryAllocate(size); + if (p) { + return p; + } else { + ensure(size); + void* r = segment->data + segment->position; + segment->position += size; + return r; + } + } + + void* peek(size_t size) + { + size = pad(size); + Segment* s = segment; + while (s->position < size) { + size -= s->position; + s = s->next; + } + return s->data + (s->position - size); + } + + void pop(size_t size) + { + size = pad(size); + Segment* s = segment; + while (s->position < size) { + size -= s->position; + Segment* next = s->next; + allocator->free(s, sizeof(Segment) + s->size); + s = next; + } + s->position -= size; + segment = s; + } + + private: static unsigned padToPage(unsigned size) { return (size + (LikelyPageSizeInBytes - 1)) & ~(LikelyPageSizeInBytes - 1); @@ -95,7 +134,7 @@ class Zone : public avian::util::Allocator { } } - virtual void* tryAllocate(unsigned size) + void* tryAllocate(size_t size) { size = pad(size); if (tryEnsure(size)) { @@ -107,54 +146,7 @@ class Zone : public avian::util::Allocator { } } - virtual void* allocate(unsigned size) - { - size = pad(size); - void* p = tryAllocate(size); - if (p) { - return p; - } else { - ensure(size); - void* r = segment->data + segment->position; - segment->position += size; - return r; - } - } - - void* peek(unsigned size) - { - size = pad(size); - Segment* s = segment; - while (s->position < size) { - size -= s->position; - s = s->next; - } - return s->data + (s->position - size); - } - - void pop(unsigned size) - { - size = pad(size); - Segment* s = segment; - while (s->position < size) { - size -= s->position; - Segment* next = s->next; - allocator->free(s, sizeof(Segment) + s->size); - s = next; - } - s->position -= size; - segment = s; - } - - virtual void free(const void*, unsigned) - { - // not supported - abort(s); - } - - System* s; - Allocator* allocator; - void* context; + avian::util::Allocator* allocator; Segment* segment; unsigned minimumFootprint; }; diff --git a/src/codegen/compiler.cpp b/src/codegen/compiler.cpp index 3a72d36176..67c622741f 100644 --- a/src/codegen/compiler.cpp +++ b/src/codegen/compiler.cpp @@ -1309,7 +1309,6 @@ unsigned typeFootprint(Context* c, ir::Type type) return type.rawSize() / 4; case ir::Type::Object: case ir::Type::Address: - case ir::Type::Half: return 1; case ir::Type::Void: return 0; diff --git a/src/codegen/target/arm/assembler.cpp b/src/codegen/target/arm/assembler.cpp index 9f9b0dcfce..7de408deba 100644 --- a/src/codegen/target/arm/assembler.cpp +++ b/src/codegen/target/arm/assembler.cpp @@ -619,7 +619,7 @@ class MyArchitecture : public Architecture { } } - virtual Assembler* makeAssembler(Allocator* allocator, Zone* zone); + virtual Assembler* makeAssembler(Alloc* allocator, Zone* zone); virtual void acquire() { @@ -639,7 +639,7 @@ class MyArchitecture : public Architecture { class MyAssembler : public Assembler { public: - MyAssembler(System* s, Allocator* a, Zone* zone, MyArchitecture* arch) + MyAssembler(System* s, Alloc* a, Zone* zone, MyArchitecture* arch) : con(s, a, zone), arch_(arch) { } @@ -1075,7 +1075,7 @@ class MyAssembler : public Assembler { MyArchitecture* arch_; }; -Assembler* MyArchitecture::makeAssembler(Allocator* allocator, Zone* zone) +Assembler* MyArchitecture::makeAssembler(Alloc* allocator, Zone* zone) { return new (zone) MyAssembler(this->con.s, allocator, zone, this); } diff --git a/src/codegen/target/arm/context.cpp b/src/codegen/target/arm/context.cpp index 1c2757abbc..a690caf410 100644 --- a/src/codegen/target/arm/context.cpp +++ b/src/codegen/target/arm/context.cpp @@ -15,7 +15,7 @@ namespace avian { namespace codegen { namespace arm { -Context::Context(vm::System* s, util::Allocator* a, vm::Zone* zone) +Context::Context(vm::System* s, util::Alloc* a, vm::Zone* zone) : s(s), zone(zone), client(0), diff --git a/src/codegen/target/arm/context.h b/src/codegen/target/arm/context.h index 15d3c2e0f0..45d937da9b 100644 --- a/src/codegen/target/arm/context.h +++ b/src/codegen/target/arm/context.h @@ -24,7 +24,7 @@ namespace avian { namespace util { class Aborter; -class Allocator; +class Alloc; } // namespace util namespace codegen { @@ -37,7 +37,7 @@ class ConstantPoolEntry; class Context { public: - Context(vm::System* s, util::Allocator* a, vm::Zone* zone); + Context(vm::System* s, util::Alloc* a, vm::Zone* zone); vm::System* s; vm::Zone* zone; diff --git a/src/codegen/target/x86/assembler.cpp b/src/codegen/target/x86/assembler.cpp index e124a97a85..e9ada91b2e 100644 --- a/src/codegen/target/x86/assembler.cpp +++ b/src/codegen/target/x86/assembler.cpp @@ -887,7 +887,7 @@ class MyArchitecture : public Architecture { } } - virtual Assembler* makeAssembler(util::Allocator* allocator, Zone* zone); + virtual Assembler* makeAssembler(util::Alloc* allocator, Zone* zone); virtual void acquire() { @@ -908,7 +908,7 @@ class MyArchitecture : public Architecture { class MyAssembler : public Assembler { public: - MyAssembler(System* s, util::Allocator* a, Zone* zone, MyArchitecture* arch) + MyAssembler(System* s, util::Alloc* a, Zone* zone, MyArchitecture* arch) : c(s, a, zone, &(arch->c)), arch_(arch) { } @@ -1273,7 +1273,7 @@ class MyAssembler : public Assembler { MyArchitecture* arch_; }; -Assembler* MyArchitecture::makeAssembler(util::Allocator* allocator, Zone* zone) +Assembler* MyArchitecture::makeAssembler(util::Alloc* allocator, Zone* zone) { return new (zone) MyAssembler(c.s, allocator, zone, this); } diff --git a/src/codegen/target/x86/context.cpp b/src/codegen/target/x86/context.cpp index 93cdef29cd..c86e5734ef 100644 --- a/src/codegen/target/x86/context.cpp +++ b/src/codegen/target/x86/context.cpp @@ -24,7 +24,7 @@ ArchitectureContext::ArchitectureContext(vm::System* s, bool useNativeFeatures) } Context::Context(vm::System* s, - util::Allocator* a, + util::Alloc* a, vm::Zone* zone, ArchitectureContext* ac) : s(s), diff --git a/src/codegen/target/x86/context.h b/src/codegen/target/x86/context.h index 9a1eb912b8..93a6f1cc5b 100644 --- a/src/codegen/target/x86/context.h +++ b/src/codegen/target/x86/context.h @@ -26,7 +26,7 @@ namespace vm { class System; -class Allocator; +class Alloc; class Zone; } // namespace vm @@ -80,7 +80,7 @@ class ArchitectureContext { class Context { public: Context(vm::System* s, - util::Allocator* a, + util::Alloc* a, vm::Zone* zone, ArchitectureContext* ac); diff --git a/src/compile.cpp b/src/compile.cpp index 7a546feb3a..bd5234db04 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -15,6 +15,8 @@ #include "avian/target.h" #include "avian/arch.h" +#include + #include #include #include @@ -1156,7 +1158,7 @@ class Context { Context(MyThread* t, BootContext* bootContext, GcMethod* method) : thread(t), - zone(t->m->system, t->m->heap, InitialZoneCapacityInBytes), + zone(t->m->heap, InitialZoneCapacityInBytes), assembler(t->arch->makeAssembler(t->m->heap, &zone)), client(t), compiler(makeCompiler(t->m->system, assembler, &zone, &client)), @@ -1189,7 +1191,7 @@ class Context { Context(MyThread* t) : thread(t), - zone(t->m->system, t->m->heap, InitialZoneCapacityInBytes), + zone(t->m->heap, InitialZoneCapacityInBytes), assembler(t->arch->makeAssembler(t->m->heap, &zone)), client(t), compiler(0), @@ -1262,7 +1264,7 @@ class Context { TraceElement* traceLog; Slice visitTable; Slice rootTable; - Allocator* executableAllocator; + Alloc* executableAllocator; void* executableStart; unsigned executableSize; unsigned objectPoolCount; @@ -3741,8 +3743,7 @@ class Stack { Stack* s; }; - Stack(MyThread* t) - : thread(t), zone(t->m->system, t->m->heap, 0), resource(this) + Stack(MyThread* t) : thread(t), zone(t->m->heap, 0), resource(this) { } @@ -8842,8 +8843,7 @@ class MyProcessor : public Processor { { if (codeAllocator.memory.begin()) { #if !defined(AVIAN_AOT_ONLY) - s->freeExecutable(codeAllocator.memory.begin(), - codeAllocator.memory.count); + Memory::free(codeAllocator.memory); #endif } @@ -9023,12 +9023,10 @@ class MyProcessor : public Processor { { #if !defined(AVIAN_AOT_ONLY) if (codeAllocator.memory.begin() == 0) { - codeAllocator.memory.items = static_cast( - s->tryAllocateExecutable(ExecutableAreaSizeInBytes)); + codeAllocator.memory = Memory::allocate(ExecutableAreaSizeInBytes, + Memory::ReadWriteExecute); - expect(t, codeAllocator.memory.items); - - codeAllocator.memory.count = ExecutableAreaSizeInBytes; + expect(t, codeAllocator.memory.begin()); } #endif diff --git a/src/finder.cpp b/src/finder.cpp index f211928663..59479a2534 100644 --- a/src/finder.cpp +++ b/src/finder.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "avian/zlib-custom.h" #include "avian/finder.h" @@ -55,7 +56,7 @@ class DirectoryElement : public Element { public: class Iterator : public Element::Iterator { public: - Iterator(System* s, Allocator* allocator, const char* name, unsigned skip) + Iterator(System* s, Alloc* allocator, const char* name, unsigned skip) : s(s), allocator(allocator), name(name), @@ -112,7 +113,7 @@ class DirectoryElement : public Element { } System* s; - Allocator* allocator; + Alloc* allocator; const char* name; unsigned skip; System::Directory* directory; @@ -120,7 +121,7 @@ class DirectoryElement : public Element { Iterator* it; }; - DirectoryElement(System* s, Allocator* allocator, const char* name) + DirectoryElement(System* s, Alloc* allocator, const char* name) : s(s), allocator(allocator), originalName(name), @@ -187,7 +188,7 @@ class DirectoryElement : public Element { } System* s; - Allocator* allocator; + Alloc* allocator; const char* originalName; const char* name; const char* urlPrefix_; @@ -197,7 +198,7 @@ class DirectoryElement : public Element { class PointerRegion : public System::Region { public: PointerRegion(System* s, - Allocator* allocator, + Alloc* allocator, const uint8_t* start, size_t length, bool freePointer = false) @@ -228,7 +229,7 @@ class PointerRegion : public System::Region { } System* s; - Allocator* allocator; + Alloc* allocator; const uint8_t* start_; size_t length_; bool freePointer; @@ -236,7 +237,7 @@ class PointerRegion : public System::Region { class DataRegion : public System::Region { public: - DataRegion(System* s, Allocator* allocator, size_t length) + DataRegion(System* s, Alloc* allocator, size_t length) : s(s), allocator(allocator), length_(length) { } @@ -257,7 +258,7 @@ class DataRegion : public System::Region { } System* s; - Allocator* allocator; + Alloc* allocator; size_t length_; uint8_t data[0]; }; @@ -276,7 +277,7 @@ class JarIndex { const uint8_t* entry; }; - JarIndex(System* s, Allocator* allocator, unsigned capacity) + JarIndex(System* s, Alloc* allocator, unsigned capacity) : s(s), allocator(allocator), capacity(capacity), @@ -287,14 +288,14 @@ class JarIndex { memset(table, 0, sizeof(List*) * capacity); } - static JarIndex* make(System* s, Allocator* allocator, unsigned capacity) + static JarIndex* make(System* s, Alloc* allocator, unsigned capacity) { return new (allocator->allocate(sizeof(JarIndex) + (sizeof(List*) * capacity))) JarIndex(s, allocator, capacity); } - static JarIndex* open(System* s, Allocator* allocator, System::Region* region) + static JarIndex* open(System* s, Alloc* allocator, System::Region* region) { JarIndex* index = make(s, allocator, 32); @@ -437,7 +438,7 @@ class JarIndex { } System* s; - Allocator* allocator; + Alloc* allocator; unsigned capacity; unsigned position; @@ -449,7 +450,7 @@ class JarElement : public Element { public: class Iterator : public Element::Iterator { public: - Iterator(System* s, Allocator* allocator, JarIndex* index) + Iterator(System* s, Alloc* allocator, JarIndex* index) : s(s), allocator(allocator), index(index), position(0) { } @@ -471,13 +472,13 @@ class JarElement : public Element { } System* s; - Allocator* allocator; + Alloc* allocator; JarIndex* index; unsigned position; }; JarElement(System* s, - Allocator* allocator, + Alloc* allocator, const char* name, bool canonicalizePath = true) : s(s), @@ -494,7 +495,7 @@ class JarElement : public Element { } JarElement(System* s, - Allocator* allocator, + Alloc* allocator, const uint8_t* jarData, unsigned jarLength) : s(s), @@ -598,7 +599,7 @@ class JarElement : public Element { } System* s; - Allocator* allocator; + Alloc* allocator; const char* originalName; const char* name; const char* urlPrefix_; @@ -610,7 +611,7 @@ class JarElement : public Element { class BuiltinElement : public JarElement { public: BuiltinElement(System* s, - Allocator* allocator, + Alloc* allocator, const char* name, const char* libraryName) : JarElement(s, allocator, name, false), @@ -710,7 +711,7 @@ unsigned baseName(const char* name, char fileSeparator) void add(System* s, Element** first, Element** last, - Allocator* allocator, + Alloc* allocator, const char* name, unsigned nameLength, const char* bootLibrary); @@ -718,7 +719,7 @@ void add(System* s, void addTokens(System* s, Element** first, Element** last, - Allocator* allocator, + Alloc* allocator, const char* jarName, unsigned jarNameBase, const char* tokens, @@ -755,7 +756,7 @@ bool continuationLine(const uint8_t* base, void addJar(System* s, Element** first, Element** last, - Allocator* allocator, + Alloc* allocator, const char* name, const char* bootLibrary) { @@ -851,7 +852,7 @@ void addJar(System* s, void add(System* s, Element** first, Element** last, - Allocator* allocator, + Alloc* allocator, const char* token, unsigned tokenLength, const char* bootLibrary) @@ -903,7 +904,7 @@ void add(System* s, } Element* parsePath(System* s, - Allocator* allocator, + Alloc* allocator, const char* path, const char* bootLibrary) { @@ -920,7 +921,7 @@ Element* parsePath(System* s, class MyIterator : public Finder::IteratorImp { public: - MyIterator(System* s, Allocator* allocator, Element* path) + MyIterator(System* s, Alloc* allocator, Element* path) : s(s), allocator(allocator), e(path ? path->next : 0), @@ -955,7 +956,7 @@ class MyIterator : public Finder::IteratorImp { } System* s; - Allocator* allocator; + Alloc* allocator; Element* e; Element::Iterator* it; }; @@ -963,7 +964,7 @@ class MyIterator : public Finder::IteratorImp { class MyFinder : public Finder { public: MyFinder(System* system, - Allocator* allocator, + Alloc* allocator, const char* path, const char* bootLibrary) : system(system), @@ -974,7 +975,7 @@ class MyFinder : public Finder { } MyFinder(System* system, - Allocator* allocator, + Alloc* allocator, const uint8_t* jarData, unsigned jarLength) : system(system), @@ -1070,7 +1071,7 @@ class MyFinder : public Finder { } System* system; - Allocator* allocator; + Alloc* allocator; Element* path_; const char* pathString; }; @@ -1080,7 +1081,7 @@ class MyFinder : public Finder { namespace vm { AVIAN_EXPORT Finder* makeFinder(System* s, - Allocator* a, + Alloc* a, const char* path, const char* bootLibrary) { @@ -1088,7 +1089,7 @@ AVIAN_EXPORT Finder* makeFinder(System* s, } Finder* makeFinder(System* s, - Allocator* a, + Alloc* a, const uint8_t* jarData, unsigned jarLength) { diff --git a/src/heap/heap.cpp b/src/heap/heap.cpp index 54ce4a4209..c4b8ff35a7 100644 --- a/src/heap/heap.cpp +++ b/src/heap/heap.cpp @@ -60,10 +60,10 @@ class Context; Aborter* getAborter(Context* c); -void* tryAllocate(Context* c, unsigned size); -void* allocate(Context* c, unsigned size); -void* allocate(Context* c, unsigned size, bool limit); -void free(Context* c, const void* p, unsigned size); +void* tryAllocate(Context* c, size_t size); +void* allocate(Context* c, size_t size); +void* allocate(Context* c, size_t size, bool limit); +void free(Context* c, const void* p, size_t size); #ifdef USE_ATOMIC_OPERATIONS inline void markBitAtomic(uintptr_t* map, unsigned i) @@ -1863,7 +1863,7 @@ void collect(Context* c) } } -void* allocate(Context* c, unsigned size, bool limit) +void* allocate(Context* c, size_t size, bool limit) { ACQUIRE(c->lock); @@ -1888,12 +1888,12 @@ void* allocate(Context* c, unsigned size, bool limit) return 0; } -void* tryAllocate(Context* c, unsigned size) +void* tryAllocate(Context* c, size_t size) { return allocate(c, size, true); } -void* allocate(Context* c, unsigned size) +void* allocate(Context* c, size_t size) { void* p = allocate(c, size, false); expect(c->system, p); @@ -1901,7 +1901,7 @@ void* allocate(Context* c, unsigned size) return p; } -void free(Context* c, const void* p, unsigned size) +void free(Context* c, const void* p, size_t size) { ACQUIRE(c->lock); @@ -1925,7 +1925,7 @@ void free(Context* c, const void* p, unsigned size) c->count -= size; } -void free_(Context* c, const void* p, unsigned size) +void free_(Context* c, const void* p, size_t size) { free(c, p, size); } @@ -1963,17 +1963,17 @@ class MyHeap : public Heap { return local::limitExceeded(&c, pendingAllocation); } - virtual void* tryAllocate(unsigned size) + virtual void* tryAllocate(size_t size) { return local::tryAllocate(&c, size); } - virtual void* allocate(unsigned size) + virtual void* allocate(size_t size) { return local::allocate(&c, size); } - virtual void free(const void* p, unsigned size) + virtual void free(const void* p, size_t size) { free_(&c, p, size); } @@ -1994,7 +1994,7 @@ class MyHeap : public Heap { return Fixie::totalSize(sizeInWords, objectMask); } - void* allocateFixed(Allocator* allocator, + void* allocateFixed(Alloc* allocator, unsigned sizeInWords, bool objectMask, Fixie** handle, @@ -2011,7 +2011,7 @@ class MyHeap : public Heap { ->body(); } - virtual void* allocateFixed(Allocator* allocator, + virtual void* allocateFixed(Alloc* allocator, unsigned sizeInWords, bool objectMask) { @@ -2019,7 +2019,7 @@ class MyHeap : public Heap { allocator, sizeInWords, objectMask, &(c.fixies), false); } - virtual void* allocateImmortalFixed(Allocator* allocator, + virtual void* allocateImmortalFixed(Alloc* allocator, unsigned sizeInWords, bool objectMask) { diff --git a/src/lzma-decode.cpp b/src/lzma-decode.cpp index a1af7ae9fc..0630eda03d 100644 --- a/src/lzma-decode.cpp +++ b/src/lzma-decode.cpp @@ -27,7 +27,7 @@ int32_t read4(const uint8_t* in) namespace vm { uint8_t* decodeLZMA(System* s, - avian::util::Allocator* a, + avian::util::AllocOnly* a, uint8_t* in, unsigned inSize, unsigned* outSize) diff --git a/src/lzma-encode.cpp b/src/lzma-encode.cpp index 8dc26586a3..cfaf3f3512 100644 --- a/src/lzma-encode.cpp +++ b/src/lzma-encode.cpp @@ -25,7 +25,7 @@ SRes myProgress(void*, UInt64, UInt64) namespace vm { uint8_t* encodeLZMA(System* s, - Allocator* a, + AllocOnly* a, uint8_t* in, unsigned inSize, unsigned* outSize) diff --git a/src/machine.cpp b/src/machine.cpp index f8e312ebc2..519d7ff01f 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -4175,7 +4175,7 @@ object allocate2(Thread* t, unsigned sizeInBytes, bool objectMask) } object allocate3(Thread* t, - Allocator* allocator, + Alloc* allocator, Machine::AllocationType type, unsigned sizeInBytes, bool objectMask) diff --git a/src/main.cpp b/src/main.cpp index dad5a1a768..fdfd52d864 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,27 +62,22 @@ const char* mainClass(const char* jar) System* system = makeSystem(); - class MyAllocator : public avian::util::Allocator { + class MyAllocator : public avian::util::Alloc { public: MyAllocator(System* s) : s(s) { } - virtual void* tryAllocate(unsigned size) + virtual void* allocate(size_t size) { - return s->tryAllocate(size); - } - - virtual void* allocate(unsigned size) - { - void* p = tryAllocate(size); + void* p = s->tryAllocate(size); if (p == 0) { abort(s); } return p; } - virtual void free(const void* p, unsigned) + virtual void free(const void* p, size_t) { s->free(p); } diff --git a/src/system/posix.cpp b/src/system/posix.cpp index 1fb94e58e2..9dd2c7c016 100644 --- a/src/system/posix.cpp +++ b/src/system/posix.cpp @@ -54,6 +54,7 @@ typedef struct ucontext { #include #include +#include #include #define ACQUIRE(x) MutexResource MAKE_NAME(mutexResource_)(x) @@ -664,39 +665,7 @@ class MySystem : public System { ::free(const_cast(p)); } - virtual void* tryAllocateExecutable(unsigned sizeInBytes) - { -#ifdef MAP_32BIT - // map to the lower 32 bits of memory when possible so as to avoid - // expensive relative jumps - const unsigned Extra = MAP_32BIT; -#else - const unsigned Extra = 0; -#endif - - void* p = mmap(0, - sizeInBytes, - PROT_EXEC | PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON | Extra, - -1, - 0); - - if (p == MAP_FAILED) { - return 0; - } else { - // fprintf(stderr, "executable from %p to %p\n", p, - // static_cast(p) + sizeInBytes); - return static_cast(p); - } - } - - virtual void freeExecutable(const void* p, unsigned sizeInBytes) - { - munmap(const_cast(p), sizeInBytes); - } - - virtual bool success(Status s) - { + virtual bool success(Status s) { return s == 0; } @@ -887,7 +856,7 @@ class MySystem : public System { return SO_SUFFIX; } - virtual const char* toAbsolutePath(Allocator* allocator, const char* name) + virtual const char* toAbsolutePath(AllocOnly* allocator, const char* name) { if (name[0] == '/') { return copy(allocator, name); diff --git a/src/system/posix/memory.cpp b/src/system/posix/memory.cpp new file mode 100644 index 0000000000..cdea451148 --- /dev/null +++ b/src/system/posix/memory.cpp @@ -0,0 +1,63 @@ +/* Copyright (c) 2008-2014, 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 + +#include + +#include "sys/mman.h" + +namespace avian { +namespace system { + +const size_t Memory::PageSize = 1 << 12; + +util::Slice Memory::allocate(size_t sizeInBytes, + Permissions perms) +{ + unsigned prot = 0; + if(perms & Read) { + prot |= PROT_READ; + } + if(perms & Write) { + prot |= PROT_WRITE; + } + if(perms & Execute) { + prot |= PROT_EXEC; + } +#ifdef MAP_32BIT + // map to the lower 32 bits of memory when possible so as to avoid + // expensive relative jumps + const unsigned Extra = MAP_32BIT; +#else + const unsigned Extra = 0; +#endif + + void* p = mmap(0, + sizeInBytes, + prot, + MAP_PRIVATE | MAP_ANON | Extra, + -1, + 0); + + if (p == MAP_FAILED) { + return util::Slice(0, 0); + } else { + return util::Slice(static_cast(p), sizeInBytes); + } +} + +void Memory::free(util::Slice pages) +{ + munmap(const_cast(pages.begin()), pages.count); +} + +} // namespace system +} // namespace avian diff --git a/src/system/windows.cpp b/src/system/windows.cpp index f6a390e797..54fa952321 100644 --- a/src/system/windows.cpp +++ b/src/system/windows.cpp @@ -675,22 +675,7 @@ class MySystem : public System { ::free(const_cast(p)); } -#if !defined(AVIAN_AOT_ONLY) - virtual void* tryAllocateExecutable(unsigned sizeInBytes) - { - return VirtualAlloc( - 0, sizeInBytes, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); - } - - virtual void freeExecutable(const void* p, unsigned) - { - int r UNUSED = VirtualFree(const_cast(p), 0, MEM_RELEASE); - assertT(this, r); - } -#endif - - virtual bool success(Status s) - { + virtual bool success(Status s) { return s == 0; } @@ -908,7 +893,7 @@ class MySystem : public System { return SO_SUFFIX; } - virtual const char* toAbsolutePath(avian::util::Allocator* allocator, + virtual const char* toAbsolutePath(avian::util::AllocOnly* allocator, const char* name) { #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) diff --git a/src/system/windows/memory.cpp b/src/system/windows/memory.cpp new file mode 100644 index 0000000000..6d4978262b --- /dev/null +++ b/src/system/windows/memory.cpp @@ -0,0 +1,55 @@ +/* Copyright (c) 2008-2014, 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 + +#include + +#include + +namespace avian { +namespace system { + +const size_t Memory::PageSize = 1 << 12; + +util::Slice Memory::allocate(size_t sizeInBytes, + Permissions perms) +{ + unsigned prot; + switch(perms) { + case Read: + prot = PAGE_READONLY; + break; + case ReadWrite: + prot = PAGE_READWRITE; + break; + case ReadExecute: + prot = PAGE_EXECUTE_READ; + break; + case ReadWriteExecute: + prot = PAGE_EXECUTE_READWRITE; + break; + default: + UNREACHABLE_; + } + void* ret = VirtualAlloc( + 0, sizeInBytes, MEM_COMMIT | MEM_RESERVE, prot); + return util::Slice((uint8_t*)ret, sizeInBytes); +} + +void Memory::free(util::Slice pages) +{ + int r = VirtualFree(pages.begin(), 0, MEM_RELEASE); + (void) r; + ASSERT(r); +} + +} // namespace system +} // namespace avian diff --git a/src/tools/bootimage-generator/main.cpp b/src/tools/bootimage-generator/main.cpp index b85cc1317c..8860496be3 100644 --- a/src/tools/bootimage-generator/main.cpp +++ b/src/tools/bootimage-generator/main.cpp @@ -1431,7 +1431,7 @@ void writeBootImage2(Thread* t, // sequence point, for gc (don't recombine statements) roots(t)->setOutOfMemoryError(t, throwable); - Zone zone(t->m->system, t->m->heap, 64 * 1024); + Zone zone(t->m->heap, 64 * 1024); class MyCompilationHandler : public Processor::CompilationHandler { public: diff --git a/src/tools/type-generator/main.cpp b/src/tools/type-generator/main.cpp index d983ebfded..ccb2464650 100644 --- a/src/tools/type-generator/main.cpp +++ b/src/tools/type-generator/main.cpp @@ -1630,27 +1630,22 @@ int main(int ac, char** av) System* system = makeSystem(); - class MyAllocator : public avian::util::Allocator { + class MyAllocator : public avian::util::Alloc { public: MyAllocator(System* s) : s(s) { } - virtual void* tryAllocate(unsigned size) + virtual void* allocate(size_t size) { - return s->tryAllocate(size); - } - - virtual void* allocate(unsigned size) - { - void* p = tryAllocate(size); + void* p = s->tryAllocate(size); if (p == 0) { abort(s); } return p; } - virtual void free(const void* p, unsigned) + virtual void free(const void* p, size_t) { s->free(p); } diff --git a/src/util/fixed-allocator.cpp b/src/util/fixed-allocator.cpp index 91d0d88621..bd250ce011 100644 --- a/src/util/fixed-allocator.cpp +++ b/src/util/fixed-allocator.cpp @@ -20,14 +20,14 @@ FixedAllocator::FixedAllocator(Aborter* a, Slice memory) { } -void* FixedAllocator::tryAllocate(unsigned size) +void* FixedAllocator::tryAllocate(size_t size) { return allocate(size); } -void* FixedAllocator::allocate(unsigned size, unsigned padAlignment) +void* FixedAllocator::allocate(size_t size, unsigned padAlignment) { - unsigned paddedSize = vm::pad(size, padAlignment); + size_t paddedSize = vm::pad(size, padAlignment); expect(a, offset + paddedSize < memory.count); void* p = memory.begin() + offset; @@ -35,12 +35,12 @@ void* FixedAllocator::allocate(unsigned size, unsigned padAlignment) return p; } -void* FixedAllocator::allocate(unsigned size) +void* FixedAllocator::allocate(size_t size) { return allocate(size, vm::BytesPerWord); } -void FixedAllocator::free(const void* p, unsigned size) +void FixedAllocator::free(const void* p, size_t size) { if (p >= memory.begin() and static_cast(p) + size == memory.begin() + offset) { diff --git a/unittest/codegen/assembler-test.cpp b/unittest/codegen/assembler-test.cpp index fa20b9a26c..f463b0c5b4 100644 --- a/unittest/codegen/assembler-test.cpp +++ b/unittest/codegen/assembler-test.cpp @@ -52,7 +52,7 @@ class Asm { Assembler* a; Asm(BasicEnv& env) - : zone(env.s, env.heap, 8192), a(env.arch->makeAssembler(env.heap, &zone)) + : zone(env.heap, 8192), a(env.arch->makeAssembler(env.heap, &zone)) { }