DVPlatformMemory.configured constructor

DVPlatformMemory.configured({
  1. DVSize? asked,
  2. DVMemoryProfile? profile,
  3. DVSize? segment,
  4. _B? touchPages,
  5. DVMemoryTarget? target,
  6. DVMemoryBacking? backing,
  7. DVSize? ceiling,
  8. String? ceilingSource,
  9. _I id = 0,
})

The constructor DV.Memory.allocate uses once configuration is applied.

ceiling caps the reservation; ceilingSource says where the ceiling came from, for the diagnostic.

Implementation

DVPlatformMemory.configured({
  DVSize? asked,
  DVMemoryProfile? profile,
  DVSize? segment,
  _B? touchPages,
  DVMemoryTarget? target,
  DVMemoryBacking? backing,
  DVSize? ceiling,
  String? ceilingSource,
  this.id = 0,
}) : target = target ?? DVMemoryTarget.current,
     _backing = backing ?? dvDefaultMemoryBacking() {
  final DVMemoryProfile chosen = profile ?? this.target.profile;
  final DVSize seg = segment ?? chosen.segment;
  if (!seg.isPowerOfTwo ||
      seg < const DVSize.kb(4) ||
      seg > const DVSize.gb(1)) {
    throw ArgumentError.value(
      '$seg',
      'segment',
      'must be a power of two from 4KB to 1GB',
    );
  }
  segmentBytes = seg.bytes;

  final DVSize wanted = asked ?? seg;
  if (wanted.bytes <= 0) {
    throw ArgumentError.value('$wanted', 'size', 'must be positive');
  }
  askedBytes = wanted.bytes;
  final _B capped = ceiling != null && wanted > ceiling;
  final _I budget = capped ? ceiling.bytes : wanted.bytes;

  var touch = touchPages ?? this.target.profile == DVMemoryProfile.desktop;
  if (touch && this.target.profile.refusesTouchPages) {
    _diagnostics.add(
      DVMemoryDiagnostic(
        'DV-MEMORY-004',
        'touchPages enabled on a mobile/embedded target '
            '(${this.target.id}); pages were not committed.',
      ),
    );
    touch = false;
  }
  if (this.target.isWeb) touch = false;

  final _I count = (budget + segmentBytes - 1) ~/ segmentBytes;
  for (var i = 0; i < count; i++) {
    final _I length = i == count - 1
        ? budget - (count - 1) * segmentBytes
        : segmentBytes;
    final DVMemorySegmentStore? store = _backing.reserve(
      length,
      touchPages: touch,
    );
    if (store == null) break;
    _segments.add(store);
  }

  final _I secured = securedBytes;
  if (secured < askedBytes) {
    final String why = capped
        ? ' The ${ceilingSource ?? 'target'} ceiling is $ceiling.'
        : '';
    _diagnostics.add(
      DVMemoryDiagnostic(
        'DV-MEMORY-001',
        'Configured budget not fully secured '
            '(asked $wanted, granted ${DVSize.bytes(secured)}).$why',
      ),
    );
  }
}