Dart - add custom allocator support (#6711)

* Dart - add custom allocator support

* Dart - only copy written bytes during resize, not the whole old buffer.
This commit is contained in:
Ivan Dlugos
2021-07-08 22:02:09 +02:00
committed by GitHub
parent c0ba2870c9
commit 8ab35b2a5f
2 changed files with 88 additions and 14 deletions

View File

@@ -133,6 +133,22 @@ class CheckOtherLangaugesData {
}
}
/// Test a custom, fixed-memory allocator (no actual allocations performed)
class CustomAllocator extends Allocator {
final _memory = ByteData(10 * 1024);
@override
ByteData allocate(int size) {
if (size > _memory.lengthInBytes) {
throw UnsupportedError('Trying to allocate too much');
}
return ByteData.sublistView(_memory, 0, size);
}
@override
void deallocate(ByteData _) {}
}
@reflectiveTest
class BuilderTest {
void test_monsterBuilder([Builder? builder]) {
@@ -247,7 +263,7 @@ class BuilderTest {
}
void test_low() {
Builder builder = new Builder(initialSize: 0);
final builder = Builder(initialSize: 0, allocator: CustomAllocator());
expect((builder..putUint8(1)).lowFinish(), [1]);
expect((builder..putUint32(2)).lowFinish(), [2, 0, 0, 0, 0, 0, 0, 1]);
expect((builder..putUint8(3)).lowFinish(),
@@ -263,7 +279,7 @@ class BuilderTest {
void test_table_default() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
final builder = Builder(initialSize: 0, allocator: CustomAllocator());
builder.startTable();
builder.addInt32(0, 10, 10);
builder.addInt32(1, 20, 10);