From b083f3df044b6528adacb811e64639d2eff3784e Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Tue, 25 Feb 2014 15:15:37 -0700 Subject: [PATCH] move fixed allocator out of machine.h --- include/avian/util/abort.h | 7 +++- include/avian/util/fixed-allocator.h | 43 ++++++++++++++++++++++ makefile | 3 +- src/avian/machine.h | 37 ------------------- src/compile.cpp | 7 ++-- src/util/fixed-allocator.cpp | 53 ++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 include/avian/util/fixed-allocator.h create mode 100644 src/util/fixed-allocator.cpp diff --git a/include/avian/util/abort.h b/include/avian/util/abort.h index daaf8a7b7c..a7d2a04ff1 100644 --- a/include/avian/util/abort.h +++ b/include/avian/util/abort.h @@ -23,6 +23,11 @@ public: virtual void NO_RETURN abort() = 0; }; +inline Aborter* getAborter(Aborter* a) +{ + return a; +} + template inline void NO_RETURN abort(T t) { getAborter(t)->abort(); @@ -44,7 +49,7 @@ inline void assert(T t, bool v) { expect(t, v); } #endif - + } // namespace util } // namespace avian diff --git a/include/avian/util/fixed-allocator.h b/include/avian/util/fixed-allocator.h new file mode 100644 index 0000000000..27181d446d --- /dev/null +++ b/include/avian/util/fixed-allocator.h @@ -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" + +namespace avian { +namespace util { + +class FixedAllocator : public Allocator { + public: + FixedAllocator(Aborter* a, uint8_t* base, unsigned capacity); + + 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; + + uint8_t* base; + unsigned capacity; + + unsigned offset; +}; + +} // namespace util +} // namespace avian + +#endif // AVIAN_UTIL_FIXED_ALLOCATOR_H diff --git a/makefile b/makefile index c03a64c785..e761c19e9f 100755 --- a/makefile +++ b/makefile @@ -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) diff --git a/src/avian/machine.h b/src/avian/machine.h index e1cca45ad2..7450a18587 100644 --- a/src/avian/machine.h +++ b/src/avian/machine.h @@ -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(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) { diff --git a/src/compile.cpp b/src/compile.cpp index 0f41e6c73a..36e8eaa380 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -24,6 +24,7 @@ #include #include +#include using namespace vm; @@ -1347,8 +1348,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: @@ -10280,8 +10280,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); } diff --git a/src/util/fixed-allocator.cpp b/src/util/fixed-allocator.cpp new file mode 100644 index 0000000000..63e0c0bd5c --- /dev/null +++ b/src/util/fixed-allocator.cpp @@ -0,0 +1,53 @@ +/* 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 + +#include + +namespace avian { +namespace util { + +FixedAllocator::FixedAllocator(Aborter* a, uint8_t* base, unsigned capacity) + : a(a), base(base), capacity(capacity), 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 < capacity); + + void* p = base + 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 >= base and static_cast(p) + size == base + offset) { + offset -= size; + } else { + abort(a); + } +} + +} // namespace util +} // namespace avian