use Slice in Vector

This commit is contained in:
Joshua Warner 2014-02-25 19:27:55 -07:00
parent a807966143
commit b711aef1b3
2 changed files with 64 additions and 49 deletions

View File

@ -11,10 +11,11 @@
#ifndef VECTOR_H #ifndef VECTOR_H
#define 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/math.h>
#include <avian/util/abort.h>
#include <avian/util/slice.h>
#include <avian/util/allocator.h> #include <avian/util/allocator.h>
#undef max #undef max
@ -24,12 +25,13 @@ namespace vm {
class Vector { class Vector {
public: public:
Vector(System* s, avian::util::Allocator* allocator, unsigned minimumCapacity) Vector(avian::util::Aborter* a,
: s(s), avian::util::Allocator* allocator,
size_t minimumCapacity)
: a(a),
allocator(allocator), allocator(allocator),
data(0), data(0, 0),
position(0), position(0),
capacity(0),
minimumCapacity(minimumCapacity) minimumCapacity(minimumCapacity)
{ } { }
@ -38,61 +40,68 @@ class Vector {
} }
void dispose() { void dispose() {
if (data and minimumCapacity >= 0) { if (data.items and minimumCapacity >= 0) {
allocator->free(data, capacity); allocator->free(data.items, data.count);
data = 0; data.items = 0;
data.count = 0;
} }
} }
void wrap(uint8_t* data, unsigned capacity) { void wrap(avian::util::Slice<uint8_t> data)
{
dispose(); dispose();
this->data = data; this->data = data;
this->position = 0; this->position = 0;
this->capacity = capacity;
this->minimumCapacity = -1; this->minimumCapacity = -1;
} }
void ensure(unsigned space) { void ensure(size_t space)
if (position + space > capacity) { {
assert(s, minimumCapacity >= 0); if (position + space > data.count) {
assert(a, minimumCapacity >= 0);
unsigned newCapacity = avian::util::max size_t newCapacity = avian::util::max(
(position + space, avian::util::max(minimumCapacity, capacity * 2)); position + space, avian::util::max(minimumCapacity, data.count * 2));
uint8_t* newData = static_cast<uint8_t*> uint8_t* newData = static_cast<uint8_t*>
(allocator->allocate(newCapacity)); (allocator->allocate(newCapacity));
if (data) { if (data.begin()) {
memcpy(newData, data, position); memcpy(newData, data.begin(), position);
allocator->free(data, capacity); allocator->free(data.begin(), data.count);
} }
data = newData; data.items = newData;
capacity = newCapacity; data.count = newCapacity;
} }
} }
void get(unsigned offset, void* dst, unsigned size) { void get(size_t offset, void* dst, size_t size)
assert(s, offset + size <= position); {
memcpy(dst, data + offset, size); assert(a, offset + size <= position);
memcpy(dst, data.begin() + offset, size);
} }
void set(unsigned offset, const void* src, unsigned size) { void set(size_t offset, const void* src, size_t size)
assert(s, offset + size <= position); {
memcpy(data + offset, src, 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); get(position - size, dst, size);
position -= size; position -= size;
} }
void* allocate(unsigned size) { void* allocate(size_t size)
{
ensure(size); ensure(size);
void* r = data + position; void* r = data.begin() + position;
position += size; position += size;
return r; return r;
} }
void* append(const void* p, unsigned size) { void* append(const void* p, size_t size)
{
void* r = allocate(size); void* r = allocate(size);
memcpy(r, p, size); memcpy(r, p, size);
return r; return r;
@ -122,46 +131,52 @@ class Vector {
append(&v, BytesPerWord); append(&v, BytesPerWord);
} }
void set2(unsigned offset, uint16_t v) { void set2(size_t offset, uint16_t v)
assert(s, offset <= position - 2); {
memcpy(data + offset, &v, 2); 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); uint8_t v; get(offset, &v, 1);
return v; return v;
} }
unsigned get2(unsigned offset) { size_t get2(size_t offset)
{
uint16_t v; get(offset, &v, 2); uint16_t v; get(offset, &v, 2);
return v; return v;
} }
unsigned get4(unsigned offset) { size_t get4(size_t offset)
{
uint32_t v; get(offset, &v, 4); uint32_t v; get(offset, &v, 4);
return v; return v;
} }
uintptr_t getAddress(unsigned offset) { uintptr_t getAddress(size_t offset)
{
uintptr_t v; get(offset, &v, BytesPerWord); uintptr_t v; get(offset, &v, BytesPerWord);
return v; return v;
} }
unsigned length() { size_t length()
{
return position; return position;
} }
template <class T> template <class T>
T* peek(unsigned offset) { T* peek(size_t offset)
assert(s, offset + sizeof(T) <= position); {
return reinterpret_cast<T*>(data + offset); assert(a, offset + sizeof(T) <= position);
return reinterpret_cast<T*>(data.begin() + offset);
} }
System* s; avian::util::Aborter* a;
avian::util::Allocator* allocator; avian::util::Allocator* allocator;
uint8_t* data; avian::util::Slice<uint8_t> data;
unsigned position; size_t position;
unsigned capacity;
int minimumCapacity; int minimumCapacity;
}; };

View File

@ -1084,7 +1084,7 @@ class MyAssembler: public Assembler {
unsigned size = p->offset - b->offset - index; unsigned size = p->offset - b->offset - index;
memcpy(dst + b->start + index + padding, memcpy(dst + b->start + index + padding,
c.code.data + b->offset + index, c.code.data.begin() + b->offset + index,
size); size);
index += size; index += size;
@ -1098,7 +1098,7 @@ class MyAssembler: public Assembler {
} }
memcpy(dst + b->start + index + padding, memcpy(dst + b->start + index + padding,
c.code.data + b->offset + index, c.code.data.begin() + b->offset + index,
b->size - index); b->size - index);
} }