FakeVmoHolder.fromVmo constructor

*<Null safety>*

FakeVmoHolder.fromVmo(Vmo vmo, {int retries: 1024})

Implementation

factory FakeVmoHolder.fromVmo(Vmo vmo, {int retries = 1024}) {
  while (retries > 0) {
    retries--;

    // Spin until we find an even generation count (no concurrent update).
    var headerData = vmo.read(16);
    if (headerData.status != 0 ||
        headerData.bytes.getInt64(8, Endian.little) % 2 != 0) {
      continue;
    }

    // Read the entire VMO.
    var sizeResult = vmo.getSize();
    if (sizeResult.status != 0) {
      continue;
    }

    var size = sizeResult.size;
    var fullData = vmo.read(size);
    if (fullData.status != 0) {
      continue;
    }

    // Read the header again, and check that the generation counts match.
    // This means we have a consistent snapshot.
    var headerDataAfter = vmo.read(16);
    if (headerDataAfter.status != 0 ||
        headerData.bytes.getInt64(8, Endian.little) !=
            headerDataAfter.bytes.getInt64(8, Endian.little)) {
      continue;
    }

    return FakeVmoHolder.usingData(fullData.bytes);
  }

  return null;
}