diff --git a/include/avian/util/slice.h b/include/avian/util/slice.h index 3b17f1e843..a29d045123 100644 --- a/include/avian/util/slice.h +++ b/include/avian/util/slice.h @@ -46,12 +46,26 @@ class Slice { return items + count; } + static Slice alloc(Allocator* a, size_t count) + { + return Slice((T*)a->allocate(sizeof(T) * count), count); + } + Slice clone(Allocator* a) { Slice 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 diff --git a/src/avian/alloc-vector.h b/src/avian/alloc-vector.h index 4f7c903740..fb5d5b1f51 100644 --- a/src/avian/alloc-vector.h +++ b/src/avian/alloc-vector.h @@ -47,15 +47,6 @@ class Vector { } } - void wrap(avian::util::Slice 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 - (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::alloc(allocator, newCapacity); } - data.items = newData; - data.count = newCapacity; } } diff --git a/src/tools/bootimage-generator/main.cpp b/src/tools/bootimage-generator/main.cpp index 5bcafc1500..bd3f44d9cb 100644 --- a/src/tools/bootimage-generator/main.cpp +++ b/src/tools/bootimage-generator/main.cpp @@ -1917,9 +1917,9 @@ main(int ac, const char** av) const unsigned CodeCapacity = 30 * 1024 * 1024; #endif - uint8_t* code = static_cast(h->allocate(CodeCapacity)); + Slice code = Slice::alloc(h, CodeCapacity); BootImage image; - p->initialize(&image, Slice(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(&bootimageOutput), - reinterpret_cast(&codeOutput), - reinterpret_cast(&image), - reinterpret_cast(code), - reinterpret_cast(args.entryClass), - reinterpret_cast(args.entryMethod), - reinterpret_cast(args.entrySpec), - reinterpret_cast(args.bootimageStart), - reinterpret_cast(args.bootimageEnd), - reinterpret_cast(args.codeimageStart), - reinterpret_cast(args.codeimageEnd), - static_cast(args.useLZMA) - }; + uintptr_t arguments[] = {reinterpret_cast(&bootimageOutput), + reinterpret_cast(&codeOutput), + reinterpret_cast(&image), + reinterpret_cast(code.begin()), + reinterpret_cast(args.entryClass), + reinterpret_cast(args.entryMethod), + reinterpret_cast(args.entrySpec), + reinterpret_cast(args.bootimageStart), + reinterpret_cast(args.bootimageEnd), + reinterpret_cast(args.codeimageStart), + reinterpret_cast(args.codeimageEnd), + static_cast(args.useLZMA)}; run(t, writeBootImage, arguments);