<commit_before>#ifndef VGA_ARENA_H
#define VGA_ARENA_H

#include <cstddef>
#include <new>
#include <type_traits>

#include "etl/utility.h"

namespace vga {

/*
 * Resets the arena state, implicitly destroying all objects allocated there.
 * Note that destructors are not called!
 */
void arena_reset();

std::size_t arena_bytes_free();
std::size_t arena_bytes_total();

/*
 * Fundamental allocation primitive; you probably don't want to use this
 * directly when the templates below are available....
 */
void * arena_alloc(std::size_t);

/*
 * Equivalent of operator new; constructs a T from arena memory by forwarding
 * the given arguments to a constructor.
 *
 * Consider arena_make as an alternative -- it works with types with non-trivial
 * destructors.
 */
template <typename T, typename ... Args>
T * arena_new(Args && ... args) {
  static_assert(std::is_trivially_destructible<T>::value,
                "vga::arena_new is only safe to use with types that won't mind "
                "if their destructor never gets called.");

  void * raw = arena_alloc(sizeof(T));
  return new(raw) T(etl::forward<Args>(args)...);
}

/*
 * Trivial smart pointer used with arena_make.
 */
template <typename T>
class ArenaPtr {
public:
  ArenaPtr() : _ptr(nullptr) {}

  explicit ArenaPtr(T * ptr) : _ptr(ptr) {}

  ArenaPtr(ArenaPtr && other) : _ptr(other._ptr) {
    other._ptr = nullptr;
  }
  ArenaPtr(ArenaPtr const &) = delete;
<commit_msg>arena: bool-context conversion for ArenaPtr.<commit_after>#ifndef VGA_ARENA_H
#define VGA_ARENA_H

#include <cstddef>
#include <new>
#include <type_traits>

#include "etl/utility.h"

namespace vga {

/*
 * Resets the arena state, implicitly destroying all objects allocated there.
 * Note that destructors are not called!
 */
void arena_reset();

std::size_t arena_bytes_free();
std::size_t arena_bytes_total();

/*
 * Fundamental allocation primitive; you probably don't want to use this
 * directly when the templates below are available....
 */
void * arena_alloc(std::size_t);

/*
 * Equivalent of operator new; constructs a T from arena memory by forwarding
 * the given arguments to a constructor.
 *
 * Consider arena_make as an alternative -- it works with types with non-trivial
 * destructors.
 */
template <typename T, typename ... Args>
T * arena_new(Args && ... args) {
  static_assert(std::is_trivially_destructible<T>::value,
                "vga::arena_new is only safe to use with types that won't mind "
                "if their destructor never gets called.");

  void * raw = arena_alloc(sizeof(T));
  return new(raw) T(etl::forward<Args>(args)...);
}

/*
 * Trivial smart pointer used with arena_make.
 */
template <typename T>
class ArenaPtr {
public:
  ArenaPtr() : _ptr(nullptr) {}

  explicit ArenaPtr(T * ptr) : _ptr(ptr) {}

  ArenaPtr(ArenaPtr && other) : _ptr(other._ptr) {
    other._ptr = nullptr;
  }
  ArenaPtr(ArenaPtr const &) = delete;

  explicit operator bool() const {
    return bool(_ptr);
  }

{'fraction_non_alphanumeric': 0.11954331766286098, 'fraction_numerical': 0.0, 'mean_word_length': 3.836038961038961, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3963854', 'n_tokens_mistral': 953, 'n_tokens_neox': 932, 'n_words': 296}