<commit_before>///
/// @file  MemoryPool.cpp
///
/// Copyright (C) 2018 <NAME>, <<EMAIL>>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve/MemoryPool.hpp>

#include <stdint.h>
#include <memory>
#include <vector>

namespace primesieve {

void MemoryPool::allocateBuckets()
{
  if (memory_.empty())
    memory_.reserve(128);

  using std::unique_ptr;
  Bucket* buckets = new Bucket[count_];
  memory_.emplace_back(unique_ptr<Bucket[]>(buckets));

  for (uint64_t i = 0; i < count_; i++)
  {
    Bucket* next = nullptr;
    if (i + 1 < count_)
      next = &buckets[i + 1];

    buckets[i].reset();
    buckets[i].setNext(next);
  }

  stock_ = buckets;
  increaseAllocCount();
}

void MemoryPool::increaseAllocCount()
{
  count_ += count_ / 3;
  if (count_ > maxCount_)
    count_ = maxCount_;
}

Bucket* MemoryPool::getBucket()
{
  if (!stock_)
    allocateBuckets();

  Bucket* bucket = stock_;
  stock_ = stock_->next();
  return bucket;
}

void MemoryPool::addBucket(Bucket*& dest)
{
  Bucket& bucket = *getBucket();
  bucket.setNext(dest);
  dest = &bucket;
}

void MemoryPool::freeBucket(Bucket* b)
{
  Bucket& bucket = *b;
  bucket.reset();
  bucket.setNext(stock_);
  stock_ = &bucket;
}

} // namespace<commit_msg>Reduce memory usage<commit_after>///
/// @file  MemoryPool.cpp
///
/// Copyright (C) 2018 <NAME>, <<EMAIL>>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve/MemoryPool.hpp>

#include <stdint.h>
#include <memory>
#include <vector>

namespace primesieve {

void MemoryPool::allocateBuckets()
{
  if (memory_.empty())
    memory_.reserve(128);

  using std::unique_ptr;
  Bucket* buckets = new Bucket[count_];
  memory_.emplace_back(unique_ptr<Bucket[]>(buckets));

  for (uint64_t i = 0; i < count_; i++)
  {
    Bucket* next = nullptr;
    if (i + 1 < count_)
      next = &buckets[i + 1];

    buckets[i].reset();
    buckets[i].setNext(next);
  }

  stock_ = buckets;
  increaseAllocCount();
}

void MemoryPool::increaseAllocCount()
{
  count_ += count_ / 8;
  if (count_ > maxCount_)
    count_ = maxCount_;
}

Bucket* MemoryPool::getBucket()
{
  if (!stock_)
    allocateBuckets();

  Bucket* bucket = stock_;
  stock_ = stock_->next();
  return bucket;
}

void MemoryPool::addBucket(Bucket*& dest)
{
  Bucket& bucket = *getBucket();
  bucket.setNext(dest);
  dest = &bucket;
}

void MemoryPool::freeBucket(Bucket* b)
{
  Bucket& bucket = *b;
  bucket.reset();
  bucket.setNext(stock_);
  stock_ = &bucket;
}

} // namespace
{'fraction_non_alphanumeric': 0.16259541984732825, 'fraction_numerical': 0.009923664122137405, 'mean_word_length': 3.5582608695652174, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '4075335', 'n_tokens_mistral': 970, 'n_tokens_neox': 911, 'n_words': 223}