tryReserve method

bool tryReserve(
  1. int bytes,
  2. int limit
)

实验性(可能调整):尝试占用非负 bytes,总量不超过 limit 才返回 true。

失败不改变账本;成功后调用方须配对 release,例如 tryReserve(400, 1024)。

Implementation

bool tryReserve(int bytes, int limit) {
  assert(bytes >= 0);
  if (_usedBytes + bytes > limit) {
    assert(() {
      debugOnFallback?.call(bytes, _usedBytes, limit);
      return true;
    }());
    return false;
  }
  _usedBytes += bytes;
  return true;
}