<commit_before>/*
 * A buffer which grows automatically.  Compared to growing_buffer, it
 * is optimized to be read as one complete buffer, instead of many
 * smaller chunks.  Additionally, it can be reused.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "expansible_buffer.hxx"
#include "pool.hxx"
#include "util/StringView.hxx"

#include <inline/poison.h>

#include <assert.h>
#include <string.h>

struct ExpansibleBuffer {
    struct pool *pool;
    char *buffer;
    size_t hard_limit;
    size_t max_size, size;
};

ExpansibleBuffer *
expansible_buffer_new(struct pool *pool, size_t initial_size,
                      size_t hard_limit)
{
    assert(initial_size > 0);
    assert(hard_limit >= initial_size);

    auto eb = NewFromPool<ExpansibleBuffer>(*pool);

    eb->pool = pool;
    eb->buffer = (char *)p_malloc(pool, initial_size);
    eb->hard_limit = hard_limit;
    eb->max_size = initial_size;
    eb->size = 0;

    return eb;
}

void
expansible_buffer_reset(ExpansibleBuffer *eb)
{
    poison_undefined(eb->buffer, eb->max_size);

    eb->size = 0;
}

bool
expansible_buffer_is_empty(const ExpansibleBuffer *eb)
{
    return eb->size == 0;
}

size_t
expansible_buffer_length(const ExpansibleBuffer *eb)
{
    return eb->size;
}

static bool
expansible_buffer_resize(ExpansibleBuffer *eb, size_t max_size)
{
    assert(eb != nullptr);
    assert(max_size > eb->max_size);

    if (max_size > eb->hard_limit)
        return false;<commit_msg>expansible_buffer: add constructor<commit_after>/*
 * A buffer which grows automatically.  Compared to growing_buffer, it
 * is optimized to be read as one complete buffer, instead of many
 * smaller chunks.  Additionally, it can be reused.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "expansible_buffer.hxx"
#include "pool.hxx"
#include "util/StringView.hxx"

#include <inline/poison.h>

#include <assert.h>
#include <string.h>

struct ExpansibleBuffer {
    struct pool *const pool;
    char *buffer;
    const size_t hard_limit;
    size_t max_size;
    size_t size = 0;

    ExpansibleBuffer(struct pool &_pool,
                     size_t initial_size, size_t _hard_limit)
        :pool(&_pool),
         buffer((char *)p_malloc(pool, initial_size)),
         hard_limit(_hard_limit),
         max_size(initial_size) {
        assert(initial_size > 0);
        assert(hard_limit >= initial_size);
    }
};

ExpansibleBuffer *
expansible_buffer_new(struct pool *pool, size_t initial_size,
                      size_t hard_limit)
{
    return NewFromPool<ExpansibleBuffer>(*pool, *pool,
                                         initial_size, hard_limit);
}

void
expansible_buffer_reset(ExpansibleBuffer *eb)
{
    poison_undefined(eb->buffer, eb->max_size);

    eb->size = 0;
}

bool
expansible_buffer_is_empty(const ExpansibleBuffer *eb)
{
    return eb->size == 0;
}

size_t
expansible_buffer_length(const ExpansibleBuffer *eb)
{
    return eb->size;
}

static bool
expansible_buffer_resize(ExpansibleBuffer *eb, size_t max_size)
{
    assert(eb != nullptr);
    assert(max_size > eb->max_size);

    if (max_size > eb->hard_limit)
        return false;
{'fraction_non_alphanumeric': 0.10965052901571017, 'fraction_numerical': 0.0025649246553382495, 'mean_word_length': 3.425531914893617, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '484989', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1053, 'n_words': 246}