<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sstream>
#include <kernel/memmap.hpp>

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(const uintptr_t begin, const uintptr_t end, const char* name,
                                       const std::string& description, In_use_delg in_use_operation)
  : name_{name}
  , description_{description}
  , in_use_op_{in_use_operation}
{
  if (begin > end) {
    throw Memory_range_exception{"Start is larger than end: " + std::to_string(begin) + " > " + std::to_string(end)};
  }

  if ((end - begin + 1) > static_cast<uintptr_t>(max_size())) {
    throw Memory_range_exception{"Maximum range size is " + std::to_string(max_size())};
  }

  range_ = Memory_range(reinterpret_cast<uint8_t*>(begin), (end - begin + 1));
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(Memory_range&& range, const char* name, const std::string& description)
  : range_{std::move(range)}
  , name_{name}
  , description_{description}
{
  if (not is_valid_range(range_)) {
    throw Memory_range_exception{"The specified range is invalid"};
  }
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(Memory_range&& range, const char* name)
  : Fixed_memory_range(std::move(range), name, "N/A")
{
  if (not is_valid_range(range_)) {
    throw Memory_range_exception{"The specified range is invalid"};
  }
}

///////////////////////////////////////////////////////////////////////////////
bool Fixed_memory_range::is_valid_range(const Memory_range& range) noexcept {
  const auto start = reinterpret_cast<uintptr_t>(range.data());
  const auto end   = reinterpret_cast<uintptr_t>(range.data() + range.size() - 1);
  return (start < end) and ((end - start + 1) <= static_cast<uintptr_t>(max_size()));
}

///////////////////////////////////////////////////////////////////////////////
ptrdiff_t Fixed_memory_range::resize(const ptrdiff_t size) {
  Expects(size > 0);
  range_ = Memory_range{range_.data(), size};
  return size;
}

///////////////////////////////////////////////////////////////////////////////
bool Fixed_memory_range::overlaps(const Fixed_memory_range& other) const noexcept {
  return (in_range(other.addr_start()) or in_range(other.addr_end()))   //< Other range overlaps with my range
      or (other.in_range(addr_start()) and other.in_range(addr_end())); //< My range is inside other range
}

///////////////////////////////////////////////////////////////////////////////
std::string Fixed_memory_range::to_string() const {
  std::stringstream out;

  out << name_      << " "      << std::hex      << addr_start() << " - "
      << addr_end() << " ("     << description_  << ", "
      << std::dec   << bytes_in_use() << " / "   <<  " bytes used) ";

  return out.str();
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::operator std::string () const {
  return to_string();
}

///////////////////////////////////////////////////////////////////////////////
Memory_map::Key Memory_map::in_range(const Key addr) {
  if (UNLIKELY(map_.empty())) {
    return 0;
  }

  auto closest_above = map_.lower_bound(addr);

  // This might be an exact match to the beginning of the range
  if (UNLIKELY(closest_above->first == addr)) {
    return closest_above->first;
  }

  if ((closest_above == map_.end()) or (closest_above not_eq map_.begin())) {
    closest_above--;
  }

  if (closest_above->second.in_range(addr)) {
    return closest_above->first;<commit_msg>memmap: add missing size to range output<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sstream>
#include <kernel/memmap.hpp>

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(const uintptr_t begin, const uintptr_t end, const char* name,
                                       const std::string& description, In_use_delg in_use_operation)
  : name_{name}
  , description_{description}
  , in_use_op_{in_use_operation}
{
  if (begin > end) {
    throw Memory_range_exception{"Start is larger than end: " + std::to_string(begin) + " > " + std::to_string(end)};
  }

  if ((end - begin + 1) > static_cast<uintptr_t>(max_size())) {
    throw Memory_range_exception{"Maximum range size is " + std::to_string(max_size())};
  }

  range_ = Memory_range(reinterpret_cast<uint8_t*>(begin), (end - begin + 1));
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(Memory_range&& range, const char* name, const std::string& description)
  : range_{std::move(range)}
  , name_{name}
  , description_{description}
{
  if (not is_valid_range(range_)) {
    throw Memory_range_exception{"The specified range is invalid"};
  }
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::Fixed_memory_range(Memory_range&& range, const char* name)
  : Fixed_memory_range(std::move(range), name, "N/A")
{
  if (not is_valid_range(range_)) {
    throw Memory_range_exception{"The specified range is invalid"};
  }
}

///////////////////////////////////////////////////////////////////////////////
bool Fixed_memory_range::is_valid_range(const Memory_range& range) noexcept {
  const auto start = reinterpret_cast<uintptr_t>(range.data());
  const auto end   = reinterpret_cast<uintptr_t>(range.data() + range.size() - 1);
  return (start < end) and ((end - start + 1) <= static_cast<uintptr_t>(max_size()));
}

///////////////////////////////////////////////////////////////////////////////
ptrdiff_t Fixed_memory_range::resize(const ptrdiff_t size) {
  Expects(size > 0);
  range_ = Memory_range{range_.data(), size};
  return size;
}

///////////////////////////////////////////////////////////////////////////////
bool Fixed_memory_range::overlaps(const Fixed_memory_range& other) const noexcept {
  return (in_range(other.addr_start()) or in_range(other.addr_end()))   //< Other range overlaps with my range
      or (other.in_range(addr_start()) and other.in_range(addr_end())); //< My range is inside other range
}

///////////////////////////////////////////////////////////////////////////////
std::string Fixed_memory_range::to_string() const {
  std::stringstream out;

  out << name_      << " "      << std::hex      << addr_start() << " - "
      << addr_end() << " ("     << description_  << ", "
      << std::dec   << bytes_in_use() << " / " << size() <<  " bytes used) ";

  return out.str();
}

///////////////////////////////////////////////////////////////////////////////
Fixed_memory_range::operator std::string () const {
  return to_string();
}

///////////////////////////////////////////////////////////////////////////////
Memory_map::Key Memory_map::in_range(const Key addr) {
  if (UNLIKELY(map_.empty())) {
    return 0;
  }

  auto closest_above = map_.lower_bound(addr);

  // This might be an exact match to the beginning of the range
  if (UNLIKELY(closest_above->first == addr)) {
    return closest_above->first;
  }

  if ((closest_above == map_.end()) or (closest_above not_eq map_.begin())) {
    closest_above--;
  }

  if (closest_above->second.in_range(addr)) {
    return closest_above->first;
{'fraction_non_alphanumeric': 0.27347404449515117, 'fraction_numerical': 0.004335424985738734, 'mean_word_length': 2.042693509198195, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4785120', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2341, 'n_words': 622}