<commit_before>/*
 * Copyright 2014 - 2017 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <math.h>
#include <aeron_alloc.h>
#include "concurrent/aeron_counters_manager.h"
#include "aeron_atomic.h"

int aeron_counters_manager_init(
    volatile aeron_counters_manager_t *manager,
    uint8_t *metadata_buffer,
    size_t metadata_length,
    uint8_t *values_buffer,
    size_t values_length)
{
    int result = -1;

    if (AERON_COUNTERS_MANAGER_IS_VALID_BUFFER_SIZES(metadata_length, values_length))
    {
        manager->metadata = metadata_buffer;
        manager->metadata_length = metadata_length;
        manager->values = values_buffer;
        manager->values_length = values_length;
        manager->id_high_water_mark = -1;
        manager->free_list_index = -1;
        manager->free_list_length = 2;
        result = aeron_alloc((void **)&manager->free_list, sizeof(int32_t) * manager->free_list_length);
    }

    return result;

}

void aeron_counters_manager_close(aeron_counters_manager_t *manager)
{
    aeron_free(manager->free_list);
}

int32_t aeron_counters_manager_allocate(
    volatile aeron_counters_manager_t *manager,
    const char *label,
    size_t label_length,
    int32_t type_id,
    aeron_counters_manager_key_func_t key_func,
    void *clientd)
{
    const int32_t counter_id = aeron_counters_manager_next_counter_id(manager);

    if ((counter_id * AERON_COUNTERS_MANAGER_VALUE_LENGTH) + AERON_COUNTERS_MANAGER_VALUE_LENGTH > manager->values_length)
    {
        return -1;
    }

    if ((counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH) + AERON_COUNTERS_MANAGER_METADATA_LENGTH > manager->metadata_length)
    {
        return -1;
    }

    aeron_counter_metadata_descriptor_t *metadata =
        (aeron_counter_metadata_descriptor_t *)(manager->metadata + (counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH));

    metadata->type_id = type_id;
    key_func((uint8_t *)&metadata->key, sizeof(metadata->key), clientd);
    memcpy(metadata->label, label, fmin(sizeof(metadata->label), label_length));
    metadata->label_length = (int32_t)label_length;
    AERON_PUT_ORDERED(metadata->state, AERON_COUNTER_RECORD_ALLOCATED);

    return counter_id;
}

int32_t aeron_counters_manager_next_counter_id(volatile aeron_counters_manager_t *manager)
{
    if (manager->free_list_index < 0)
    {
        return ++manager->id_high_water_mark;
    }

    int32_t counter_id = manager->free_list[manager->free_list_index--];
    aeron_counter_value_descriptor_t *value =
        (aeron_counter_value_descriptor_t *)(manager->values + (counter_id * AERON_COUNTERS_MANAGER_VALUE_LENGTH));
    AERON_PUT_ORDERED(value->counter_value, 0L);

    return counter_id;
}

int aeron_counters_manager_free(volatile aeron_counters_manager_t *manager, int32_t counter_id)
{
    aeron_counter_metadata_descriptor_t *metadata =
        (aeron_counter_metadata_descriptor_t *)(manager->metadata + (counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH));

    AERON_PUT_ORDERED(metadata->state, AERON_COUNTER_RECORD_RECLAIMED);

    if ((manager->free_list_index + 1) >= (int32_t)manager->free_list_length)
    {
        size_t new_length = manager->free_list_length + (manager->free_list_length >> 1);
        int32_t *new_array = NULL;

        if (aeron_alloc((void **)&new_array, sizeof(int32_t) * new_length) < 0)
        {
            return -1;
        }

        aeron_free(manager->free_list);
        manager->free_list_length = new_length;
        manager->free_list = new_array;
    }

    manager->free_list[++manager->free_list_index] = counter_id;
    return 0;
}<commit_msg>[C]: use realloc on counters manager free list.<commit_after>/*
 * Copyright 2014 - 2017 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <math.h>
#include <aeron_alloc.h>
#include "concurrent/aeron_counters_manager.h"
#include "aeron_atomic.h"

int aeron_counters_manager_init(
    volatile aeron_counters_manager_t *manager,
    uint8_t *metadata_buffer,
    size_t metadata_length,
    uint8_t *values_buffer,
    size_t values_length)
{
    int result = -1;

    if (AERON_COUNTERS_MANAGER_IS_VALID_BUFFER_SIZES(metadata_length, values_length))
    {
        manager->metadata = metadata_buffer;
        manager->metadata_length = metadata_length;
        manager->values = values_buffer;
        manager->values_length = values_length;
        manager->id_high_water_mark = -1;
        manager->free_list_index = -1;
        manager->free_list_length = 2;
        result = aeron_alloc((void **)&manager->free_list, sizeof(int32_t) * manager->free_list_length);
    }

    return result;

}

void aeron_counters_manager_close(aeron_counters_manager_t *manager)
{
    aeron_free(manager->free_list);
}

int32_t aeron_counters_manager_allocate(
    volatile aeron_counters_manager_t *manager,
    const char *label,
    size_t label_length,
    int32_t type_id,
    aeron_counters_manager_key_func_t key_func,
    void *clientd)
{
    const int32_t counter_id = aeron_counters_manager_next_counter_id(manager);

    if ((counter_id * AERON_COUNTERS_MANAGER_VALUE_LENGTH) + AERON_COUNTERS_MANAGER_VALUE_LENGTH > manager->values_length)
    {
        return -1;
    }

    if ((counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH) + AERON_COUNTERS_MANAGER_METADATA_LENGTH > manager->metadata_length)
    {
        return -1;
    }

    aeron_counter_metadata_descriptor_t *metadata =
        (aeron_counter_metadata_descriptor_t *)(manager->metadata + (counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH));

    metadata->type_id = type_id;
    key_func((uint8_t *)&metadata->key, sizeof(metadata->key), clientd);
    memcpy(metadata->label, label, fmin(sizeof(metadata->label), label_length));
    metadata->label_length = (int32_t)label_length;
    AERON_PUT_ORDERED(metadata->state, AERON_COUNTER_RECORD_ALLOCATED);

    return counter_id;
}

int32_t aeron_counters_manager_next_counter_id(volatile aeron_counters_manager_t *manager)
{
    if (manager->free_list_index < 0)
    {
        return ++manager->id_high_water_mark;
    }

    int32_t counter_id = manager->free_list[manager->free_list_index--];
    aeron_counter_value_descriptor_t *value =
        (aeron_counter_value_descriptor_t *)(manager->values + (counter_id * AERON_COUNTERS_MANAGER_VALUE_LENGTH));
    AERON_PUT_ORDERED(value->counter_value, 0L);

    return counter_id;
}

int aeron_counters_manager_free(volatile aeron_counters_manager_t *manager, int32_t counter_id)
{
    aeron_counter_metadata_descriptor_t *metadata =
        (aeron_counter_metadata_descriptor_t *)(manager->metadata + (counter_id * AERON_COUNTERS_MANAGER_METADATA_LENGTH));

    AERON_PUT_ORDERED(metadata->state, AERON_COUNTER_RECORD_RECLAIMED);

    if ((manager->free_list_index + 1) >= (int32_t)manager->free_list_length)
    {
        size_t new_length = manager->free_list_length + (manager->free_list_length >> 1);

        if (aeron_reallocf((void **)&manager->free_list, sizeof(int32_t) * new_length) < 0)
        {
            return -1;
        }

        manager->free_list_length = new_length;
    }

    manager->free_list[++manager->free_list_index] = counter_id;
    return 0;
}
{'fraction_non_alphanumeric': 0.09439670821735448, 'fraction_numerical': 0.011860099237565048, 'mean_word_length': 4.1974842767295595, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4821350', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2764, 'n_words': 521}