<commit_before>#include "sixtracklib/common/mem_pool.h"
#include "sixtracklib/_impl/definitions.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "sixtracklib/common/impl/alignment_impl.h"
#include "sixtracklib/common/alignment.h"

/* ========================================================================= */

extern void NS( MemPool_init_aligned)(
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T capacity, 
    SIXTRL_UINT64_T const chunk_size, SIXTRL_UINT64_T const begin_alignment );

extern bool NS(MemPool_clear_to_aligned_position)( 
    NS(MemPool)* SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const alignment  );

extern bool NS( MemPool_reserve_aligned )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool,
    SIXTRL_UINT64_T const new_capacity, SIXTRL_UINT64_T const alignment );

/* ------------------------------------------------------------------------- */

SIXTRL_STATIC SIXTRL_GLOBAL_DEC unsigned char* 
NS( MemPool_get_buffer )( NS( MemPool ) * SIXTRL_RESTRICT pool );

SIXTRL_STATIC SIXTRL_GLOBAL_DEC unsigned char const*
NS( MemPool_get_const_buffer )( const NS( MemPool ) *const SIXTRL_RESTRICT pool );

/* ------------------------------------------------------------------------- */

SIXTRL_STATIC void NS( MemPool_set_capacity )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const new_capacity );

SIXTRL_STATIC void NS( MemPool_set_size )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const new_size );

SIXTRL_STATIC void NS( MemPool_set_chunk_size )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const chunk_size );

SIXTRL_STATIC void NS( MemPool_set_buffer )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, 
    SIXTRL_GLOBAL_DEC unsigned char* new_buffer );

SIXTRL_STATIC void NS( MemPool_set_begin_pos )( 
    NS( MemPool )* SIXTRL_RESTRICT SIXTRL_RESTRICT pool,
    SIXTRL_GLOBAL_DEC unsigned char* SIXTRL_RESTRICT begin_pos );

SIXTRL_STATIC void NS( MemPool_set_begin_offset )(
    NS( MemPool )* SIXTRL_RESTRICT SIXTRL_RESTRICT pool,
    SIXTRL_UINT64_T const begin_offset );

/* ========================================================================= */

void NS( MemPool_init_aligned)(
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T capacity, 
    SIXTRL_UINT64_T const chunk_size, SIXTRL_UINT64_T const begin_alignment )
{
    static size_t const ZERO = (SIXTRL_UINT64_T)0u;
    
    NS( MemPool_preset( pool ) );

    if( ( pool != 0 ) && ( chunk_size > ZERO ) && ( begin_alignment > ZERO ) )
    {
        typedef unsigned char uchar_t;
        typedef SIXTRL_GLOBAL_DEC unsigned char* g_ptr_uchar_t;
        
        g_ptr_uchar_t new_buffer = 0;

        SIXTRL_UINT64_T requested_capacity = begin_alignment +
            ( ( capacity > chunk_size ) ? capacity : chunk_size );

        SIXTRL_UINT64_T const num_chunks = requested_capacity / chunk_size;
        SIXTRL_UINT64_T const calc_capacity = num_chunks * chunk_size;

        capacity = ( calc_capacity < requested_capacity )
                 ? ( calc_capacity + chunk_size ) : ( calc_capacity );

        /* use sizeof( unsigned char ) because of some theoretical ambiguity
         * in C++ which allows for sizeof( unsigned char ) > sizeof( char ) */
        new_buffer = ( g_ptr_uchar_t )malloc( capacity * sizeof( uchar_t ) );

        if( new_buffer != 0 )
        {
            uintptr_t const buffer_addr = ( uintptr_t )new_buffer;
            uintptr_t const addr_mod    = buffer_addr % begin_alignment;
            
            SIXTRL_UINT64_T const begin_offset = ( addr_mod == ( uintptr_t )0 )
                ? ( SIXTRL_UINT64_T )( begin_alignment - addr_mod ) : ZERO;
            
            g_ptr_uchar_t begin_pos = new_buffer + begin_offset;
                
            if( begin_offset > ZERO )
            {
                SIXTRL_ASSERT( capacity > begin_offset );
                capacity -= begin_offset;
            }
            
            NS( MemPool_set_buffer )( pool, new_buffer );
            NS( MemPool_set_begin_pos )( pool, begin_pos );
            NS( MemPool_set_capacity )( pool, capacity );
            NS( MemPool_set_begin_offset )( pool, begin_offset );
            NS( MemPool_set_size )( pool, ZERO );
            NS( MemPool_set_chunk_size )( pool, chunk_size );
        }
    }

    return;
}

/* -------------------------------------------------------------------------- */

bool NS(MemPool_clear_to_aligned_position)( <commit_msg>sixtracklib/common: bugfix mis-alignment for inital buffer start address<commit_after>#include "sixtracklib/common/mem_pool.h"
#include "sixtracklib/_impl/definitions.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "sixtracklib/common/impl/alignment_impl.h"
#include "sixtracklib/common/alignment.h"

/* ========================================================================= */

extern void NS( MemPool_init_aligned)(
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T capacity, 
    SIXTRL_UINT64_T const chunk_size, SIXTRL_UINT64_T const begin_alignment );

extern bool NS(MemPool_clear_to_aligned_position)( 
    NS(MemPool)* SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const alignment  );

extern bool NS( MemPool_reserve_aligned )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool,
    SIXTRL_UINT64_T const new_capacity, SIXTRL_UINT64_T const alignment );

/* ------------------------------------------------------------------------- */

SIXTRL_STATIC SIXTRL_GLOBAL_DEC unsigned char* 
NS( MemPool_get_buffer )( NS( MemPool ) * SIXTRL_RESTRICT pool );

SIXTRL_STATIC SIXTRL_GLOBAL_DEC unsigned char const*
NS( MemPool_get_const_buffer )( const NS( MemPool ) *const SIXTRL_RESTRICT pool );

/* ------------------------------------------------------------------------- */

SIXTRL_STATIC void NS( MemPool_set_capacity )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const new_capacity );

SIXTRL_STATIC void NS( MemPool_set_size )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const new_size );

SIXTRL_STATIC void NS( MemPool_set_chunk_size )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T const chunk_size );

SIXTRL_STATIC void NS( MemPool_set_buffer )( 
    NS( MemPool ) * SIXTRL_RESTRICT pool, 
    SIXTRL_GLOBAL_DEC unsigned char* new_buffer );

SIXTRL_STATIC void NS( MemPool_set_begin_pos )( 
    NS( MemPool )* SIXTRL_RESTRICT SIXTRL_RESTRICT pool,
    SIXTRL_GLOBAL_DEC unsigned char* SIXTRL_RESTRICT begin_pos );

SIXTRL_STATIC void NS( MemPool_set_begin_offset )(
    NS( MemPool )* SIXTRL_RESTRICT SIXTRL_RESTRICT pool,
    SIXTRL_UINT64_T const begin_offset );

/* ========================================================================= */

void NS( MemPool_init_aligned)(
    NS( MemPool ) * SIXTRL_RESTRICT pool, SIXTRL_UINT64_T capacity, 
    SIXTRL_UINT64_T const chunk_size, SIXTRL_UINT64_T const begin_alignment )
{
    static size_t const ZERO = (SIXTRL_UINT64_T)0u;
    
    NS( MemPool_preset( pool ) );

    if( ( pool != 0 ) && ( chunk_size > ZERO ) && ( begin_alignment > ZERO ) )
    {
        typedef unsigned char uchar_t;
        typedef SIXTRL_GLOBAL_DEC unsigned char* g_ptr_uchar_t;
        
        g_ptr_uchar_t new_buffer = 0;

        SIXTRL_UINT64_T requested_capacity = begin_alignment +
            ( ( capacity > chunk_size ) ? capacity : chunk_size );

        SIXTRL_UINT64_T const num_chunks = requested_capacity / chunk_size;
        SIXTRL_UINT64_T const calc_capacity = num_chunks * chunk_size;

        capacity = ( calc_capacity < requested_capacity )
                 ? ( calc_capacity + chunk_size ) : ( calc_capacity );

        /* use sizeof( unsigned char ) because of some theoretical ambiguity
         * in C++ which allows for sizeof( unsigned char ) > sizeof( char ) */
        new_buffer = ( g_ptr_uchar_t )malloc( capacity * sizeof( uchar_t ) );

        if( new_buffer != 0 )
        {
            uintptr_t const buffer_addr = ( uintptr_t )new_buffer;
            uintptr_t const addr_mod    = buffer_addr % begin_alignment;
            
            SIXTRL_UINT64_T const begin_offset = ( addr_mod != ( uintptr_t )0 )
                ? ( SIXTRL_UINT64_T )( begin_alignment - addr_mod ) : ZERO;
            
            g_ptr_uchar_t begin_pos = new_buffer + begin_offset;
                
            if( begin_offset > ZERO )
            {
                SIXTRL_ASSERT( capacity > begin_offset );
                capacity -= begin_offset;
            }
            
            NS( MemPool_set_buffer )( pool, new_buffer );
            NS( MemPool_set_begin_pos )( pool, begin_pos );
            NS( MemPool_set_capacity )( pool, capacity );
            NS( MemPool_set_begin_offset )( pool, begin_offset );
            NS( MemPool_set_size )( pool, ZERO );
            NS( MemPool_set_chunk_size )( pool, chunk_size );
        }
    }

    return;
}

/* -------------------------------------------------------------------------- */

bool NS(MemPool_clear_to_aligned_position)( 
{'fraction_non_alphanumeric': 0.1587633403014633, 'fraction_numerical': 0.009461987017273627, 'mean_word_length': 2.6272944932162807, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '3000192', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2924, 'n_words': 617}