add Slice::resize and Slice::alloc

This commit is contained in:
Joshua Warner 2014-02-25 20:17:12 -07:00
parent 892d359ba0
commit db19c7b3a2
3 changed files with 31 additions and 31 deletions

View File

@ -46,12 +46,26 @@ class Slice {
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

View File

@ -47,15 +47,6 @@ class Vector {
}
}
void wrap(avian::util::Slice<uint8_t> data)
{
dispose();
this->data = data;
this->position = 0;
this->minimumCapacity = 0;
}
void ensure(size_t space)
{
if (position + space > data.count) {
@ -63,14 +54,11 @@ class Vector {
size_t newCapacity = avian::util::max(
position + space, avian::util::max(minimumCapacity, data.count * 2));
uint8_t* newData = static_cast<uint8_t*>
(allocator->allocate(newCapacity));
if (data.begin()) {
memcpy(newData, data.begin(), position);
allocator->free(data.begin(), data.count);
data.resize(allocator, newCapacity);
} else {
data = avian::util::Slice<uint8_t>::alloc(allocator, newCapacity);
}
data.items = newData;
data.count = newCapacity;
}
}

View File

@ -1917,9 +1917,9 @@ 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, Slice<uint8_t>(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);
@ -1940,20 +1940,18 @@ main(int ac, const char** av)
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);