<commit_before>#include "StreamBin.h"
#include <cassert>
#include <SenseKit/Plugins/plugin_capi.h>

namespace sensekit {

    StreamBin::StreamBin(size_t bufferLengthInBytes)
        : m_bufferSize(bufferLengthInBytes),
          m_frontBufferLocked(ATOMIC_VAR_INIT(false))
    {
        allocate_buffers(bufferLengthInBytes);
    }

    sensekit_callback_id_t StreamBin::register_front_buffer_ready_callback(FrontBufferReadyCallback callback)
    {
        return m_frontBufferReadySignal += callback;
    }

    void StreamBin::unregister_front_buffer_ready_callback(sensekit_callback_id_t& callbackId)
    {
        m_frontBufferReadySignal -= callbackId;
        callbackId = 0;
    }

    sensekit_frame_t* StreamBin::get_frontBuffer()
    {
        return m_buffers[m_frontBufferIndex];
    }

    size_t StreamBin::inc_index(size_t index)
    {
        size_t newIndex = index;

        do
        {
            newIndex = newIndex + 1 < BUFFER_COUNT ? newIndex + 1 : 0;
        } while (newIndex == m_frontBufferIndex);

        return newIndex;
    }

    sensekit_frame_t* allocate_frame(size_t bufferLengthInBytes)
    {
        sensekit_frame_t* frame = new sensekit_frame_t;
        frame->byteLength = bufferLengthInBytes;
        frame->data = new uint8_t[bufferLengthInBytes];
        memset(frame->data, 0, bufferLengthInBytes);
        frame->frameIndex = -1;
        return frame;
    }

    void deallocate_frame(sensekit_frame_t*& frame)
    {
        if (!frame)
            return;

        delete[] (uint8_t*)frame->data;
        delete frame;
        frame = nullptr;
    }

    void StreamBin::allocate_buffers(size_t bufferLengthInBytes)
    {
        for(auto& frame : m_buffers)
        {
            frame = allocate_frame(bufferLengthInBytes);
        }
    }

    sensekit_frame_t* StreamBin::cycle_buffers()
    {
        if (m_frontBufferLocked)
        {
            m_backBufferTailIndex = inc_index(m_backBufferTailIndex);

            if (m_backBufferTailIndex == m_backBufferHeadIndex)
                m_backBufferHeadIndex = inc_index(m_backBufferHeadIndex);
        }
        else
        {
            sensekit_frame_index_t oldFrameIndex = get_frontBuffer()->frameIndex;
            size_t oldFBI = m_frontBufferIndex;<commit_msg>Eliminate unused variable warning in release<commit_after>#include "StreamBin.h"
#include <cassert>
#include <SenseKit/Plugins/plugin_capi.h>

namespace sensekit {

    StreamBin::StreamBin(size_t bufferLengthInBytes)
        : m_bufferSize(bufferLengthInBytes),
          m_frontBufferLocked(ATOMIC_VAR_INIT(false))
    {
        allocate_buffers(bufferLengthInBytes);
    }

    sensekit_callback_id_t StreamBin::register_front_buffer_ready_callback(FrontBufferReadyCallback callback)
    {
        return m_frontBufferReadySignal += callback;
    }

    void StreamBin::unregister_front_buffer_ready_callback(sensekit_callback_id_t& callbackId)
    {
        m_frontBufferReadySignal -= callbackId;
        callbackId = 0;
    }

    sensekit_frame_t* StreamBin::get_frontBuffer()
    {
        return m_buffers[m_frontBufferIndex];
    }

    size_t StreamBin::inc_index(size_t index)
    {
        size_t newIndex = index;

        do
        {
            newIndex = newIndex + 1 < BUFFER_COUNT ? newIndex + 1 : 0;
        } while (newIndex == m_frontBufferIndex);

        return newIndex;
    }

    sensekit_frame_t* allocate_frame(size_t bufferLengthInBytes)
    {
        sensekit_frame_t* frame = new sensekit_frame_t;
        frame->byteLength = bufferLengthInBytes;
        frame->data = new uint8_t[bufferLengthInBytes];
        memset(frame->data, 0, bufferLengthInBytes);
        frame->frameIndex = -1;
        return frame;
    }

    void deallocate_frame(sensekit_frame_t*& frame)
    {
        if (!frame)
            return;

        delete[] (uint8_t*)frame->data;
        delete frame;
        frame = nullptr;
    }

    void StreamBin::allocate_buffers(size_t bufferLengthInBytes)
    {
        for(auto& frame : m_buffers)
        {
            frame = allocate_frame(bufferLengthInBytes);
        }
    }

    sensekit_frame_t* StreamBin::cycle_buffers()
    {
        if (m_frontBufferLocked)
        {
            m_backBufferTailIndex = inc_index(m_backBufferTailIndex);

            if (m_backBufferTailIndex == m_backBufferHeadIndex)
                m_backBufferHeadIndex = inc_index(m_backBufferHeadIndex);
        }
        else
        {
#ifdef DEBUG
            sensekit_frame_index_t oldFrameIndex = get_frontBuffer()->frameIndex;
#endif
            size_t oldFBI = m_frontBufferIndex;
{'fraction_non_alphanumeric': 0.08179247335218622, 'fraction_numerical': 0.003480530780944094, 'mean_word_length': 2.6463124504361617, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '2614402', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1403, 'n_words': 219}