From b711aef1b3336ded41d90753d6b6a7819fc6eb75 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Tue, 25 Feb 2014 19:27:55 -0700 Subject: [PATCH] use Slice in Vector --- src/avian/alloc-vector.h | 109 +++++++++++++++------------ src/codegen/target/x86/assembler.cpp | 4 +- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/src/avian/alloc-vector.h b/src/avian/alloc-vector.h index 051a50bd3e..cd6f9ff28c 100644 --- a/src/avian/alloc-vector.h +++ b/src/avian/alloc-vector.h @@ -11,10 +11,11 @@ #ifndef VECTOR_H #define VECTOR_H -#include -#include "avian/target.h" +#include #include +#include +#include #include #undef max @@ -24,12 +25,13 @@ namespace vm { class Vector { public: - Vector(System* s, avian::util::Allocator* allocator, unsigned minimumCapacity) - : s(s), + Vector(avian::util::Aborter* a, + avian::util::Allocator* allocator, + size_t minimumCapacity) + : a(a), allocator(allocator), - data(0), + data(0, 0), position(0), - capacity(0), minimumCapacity(minimumCapacity) { } @@ -38,61 +40,68 @@ 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) { + void wrap(avian::util::Slice data) + { dispose(); this->data = data; this->position = 0; - this->capacity = capacity; this->minimumCapacity = -1; } - void ensure(unsigned space) { - if (position + space > capacity) { - assert(s, minimumCapacity >= 0); + void ensure(size_t space) + { + if (position + space > data.count) { + assert(a, minimumCapacity >= 0); - unsigned newCapacity = avian::util::max - (position + space, avian::util::max(minimumCapacity, capacity * 2)); + size_t newCapacity = avian::util::max( + position + space, avian::util::max(minimumCapacity, data.count * 2)); uint8_t* newData = static_cast (allocator->allocate(newCapacity)); - if (data) { - memcpy(newData, data, position); - allocator->free(data, capacity); + if (data.begin()) { + memcpy(newData, data.begin(), position); + allocator->free(data.begin(), data.count); } - data = newData; - capacity = newCapacity; + data.items = newData; + data.count = 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; @@ -122,46 +131,52 @@ 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 - T* peek(unsigned offset) { - assert(s, offset + sizeof(T) <= position); - return reinterpret_cast(data + offset); + T* peek(size_t offset) + { + assert(a, offset + sizeof(T) <= position); + return reinterpret_cast(data.begin() + offset); } - - System* s; + + avian::util::Aborter* a; avian::util::Allocator* allocator; - uint8_t* data; - unsigned position; - unsigned capacity; + avian::util::Slice data; + size_t position; int minimumCapacity; }; diff --git a/src/codegen/target/x86/assembler.cpp b/src/codegen/target/x86/assembler.cpp index dcf49e78fe..c3a0e2156e 100644 --- a/src/codegen/target/x86/assembler.cpp +++ b/src/codegen/target/x86/assembler.cpp @@ -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); }