<commit_before>#include "malloc.hpp"

namespace ANAlloc {

Allocator::Allocator(UInt _start, Tree & _tree, int _psLog)
  : start(_start), length(1UL << (_tree.GetDepth() - 1 + _psLog)),
    tree(_tree), psLog(_psLog), freeSize(length) {
  assert(tree.GetType(Path::Root()) == NodeTypeFree);
}

void Allocator::Reserve(UInt resStart, UInt resSize) {
  assert(resSize + resStart <= length);
  
  UInt blockStart = resStart >> psLog;
  UInt blockEnd = (resStart + resSize) >> psLog;
  if (blockEnd << psLog != resStart + resSize) {
    ++blockEnd;
  }
  UInt blockCount = blockEnd - blockStart;
  
  assert(tree.GetType(Path::Root()) == NodeTypeFree);
  tree.SetType(Path::Root(), NodeTypeData);
  tree.Carve(Path::Root(), blockStart, blockCount);
  
  freeSize -= blockCount << psLog;
}

bool Allocator::Alloc(UInt size, UInt & result) {
  int depth = DepthForSize(size);
  if (depth < 0) return false;
  
  if (tree.AllocHeuristic() > depth) return false;
  
  Path p;
  bool res = tree.Alloc(depth, p);
  if (!res) return false;
  
  // compute the resultant address
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - depth - 1));
  freeSize -= nodeSize;
  result = start + nodeSize * p.GetIndex();
  return true;
}

bool Allocator::Align(UInt size, UInt align, UInt & result) {
  if (start % align) {
    return false;
  }
  
  int sizeDepth = DepthForSize(size);
  int alignDepth = DepthForSize(align);
  
  // if they are trying to allocate this entire allocator and align it by
  // whatever the allocator is aligned to, why stop them?
  if (sizeDepth < 0) return false;
  if (alignDepth < 0) alignDepth = 0;
  
  if (tree.AllocHeuristic() > sizeDepth) return false;
  
  Path p;
  bool res = tree.Align(sizeDepth, alignDepth, p);
  if (!res) return false;
  
  // compute the resultant address
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - sizeDepth - 1));
  freeSize -= nodeSize;
  result = start + nodeSize * p.GetIndex();
  return true;
}

void Allocator::Free(UInt addr) {
  UInt baseIndex = (addr - start) >> psLog;
  
  Path p;
  bool res = tree.FindByShadow(baseIndex, p);
  assert(res);
  tree.Dealloc(p);
  
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - p.GetDepth() - 1));
  freeSize += nodeSize;
}

bool Allocator::OwnsAddress(UInt addr) const {
  return addr >= start && addr < start + length;
}

UInt Allocator::GetFreeSize() const {
  return freeSize;
}

UInt Allocator::GetTotalSize() const {
  return length;
}<commit_msg>avoid warning with no assert()<commit_after>#include "malloc.hpp"

namespace ANAlloc {

Allocator::Allocator(UInt _start, Tree & _tree, int _psLog)
  : start(_start), length(1UL << (_tree.GetDepth() - 1 + _psLog)),
    tree(_tree), psLog(_psLog), freeSize(length) {
  assert(tree.GetType(Path::Root()) == NodeTypeFree);
}

void Allocator::Reserve(UInt resStart, UInt resSize) {
  assert(resSize + resStart <= length);
  
  UInt blockStart = resStart >> psLog;
  UInt blockEnd = (resStart + resSize) >> psLog;
  if (blockEnd << psLog != resStart + resSize) {
    ++blockEnd;
  }
  UInt blockCount = blockEnd - blockStart;
  
  assert(tree.GetType(Path::Root()) == NodeTypeFree);
  tree.SetType(Path::Root(), NodeTypeData);
  tree.Carve(Path::Root(), blockStart, blockCount);
  
  freeSize -= blockCount << psLog;
}

bool Allocator::Alloc(UInt size, UInt & result) {
  int depth = DepthForSize(size);
  if (depth < 0) return false;
  
  if (tree.AllocHeuristic() > depth) return false;
  
  Path p;
  bool res = tree.Alloc(depth, p);
  if (!res) return false;
  
  // compute the resultant address
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - depth - 1));
  freeSize -= nodeSize;
  result = start + nodeSize * p.GetIndex();
  return true;
}

bool Allocator::Align(UInt size, UInt align, UInt & result) {
  if (start % align) {
    return false;
  }
  
  int sizeDepth = DepthForSize(size);
  int alignDepth = DepthForSize(align);
  
  // if they are trying to allocate this entire allocator and align it by
  // whatever the allocator is aligned to, why stop them?
  if (sizeDepth < 0) return false;
  if (alignDepth < 0) alignDepth = 0;
  
  if (tree.AllocHeuristic() > sizeDepth) return false;
  
  Path p;
  bool res = tree.Align(sizeDepth, alignDepth, p);
  if (!res) return false;
  
  // compute the resultant address
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - sizeDepth - 1));
  freeSize -= nodeSize;
  result = start + nodeSize * p.GetIndex();
  return true;
}

void Allocator::Free(UInt addr) {
  UInt baseIndex = (addr - start) >> psLog;
  
  Path p;
  bool res = tree.FindByShadow(baseIndex, p);
  assert(res);
  (void)res; // avoid unused variable warning
  tree.Dealloc(p);
  
  UInt nodeSize = 1UL << (psLog + (tree.GetDepth() - p.GetDepth() - 1));
  freeSize += nodeSize;
}

bool Allocator::OwnsAddress(UInt addr) const {
  return addr >= start && addr < start + length;
}

UInt Allocator::GetFreeSize() const {
  return freeSize;
}

UInt Allocator::GetTotalSize() const {
  return length;
}
{'fraction_non_alphanumeric': 0.14073479221039953, 'fraction_numerical': 0.004818309576390283, 'mean_word_length': 3.695570216776626, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310571', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1708, 'n_words': 477}