<commit_before>#include "elegan-c/memory.h"

#include <stdio.h>
#include <string.h>

static void handle_zero_request(const el_handlers handlers,
                                const_cstring calling_file,
                                const int calling_line,
                                const_cstring target_function)
{
    if (handlers.error_stream)
    {
        fprintf(handlers.error_stream,
                "Error allocating memory: Function '%s' was called from file "
                "'%s' at line '%d', and zero memory was requested.\n",
                target_function, calling_file, calling_line);
    }

    if (handlers.error_function)
    {
        handlers.error_function(EXIT_FAILURE);
    }
}

static void handle_allocation_error(const el_handlers handlers,
                                    const_cstring calling_file,
                                    const int calling_line,
                                    const_cstring target_function,
                                    const size_t requested_size)
{
    if (handlers.error_stream)
    {
        fprintf(handlers.error_stream,
                "Error allocating memory: Function '%s' was called from file "
                "'%s' at line '%d' and '%zu' bytes of memory were requested, "
                "but this amount could not be allocated.\n",
                target_function, calling_file, calling_line, requested_size);
    }

    if (handlers.error_function)
    {
        handlers.error_function(EXIT_FAILURE);
    }
}

void *el_unsafe_malloc(const size_t size, const el_handlers handlers,
                       const_cstring calling_file, const int calling_line)
{
    if (size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        return NULL;
    }

    void *pointer = malloc(size);
    if (!pointer)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                size);
        return NULL;
    }

    return memset(pointer, 0, size);
}

void el_free_address(void **pointer_address)
{
    if (pointer_address)
    {
        free(*pointer_address);
        *pointer_address = NULL;
    }
}

void *el_unsafe_calloc(const size_t number_of_elements,
                       const size_t element_size, const el_handlers handlers,
                       const_cstring calling_file, const int calling_line)
{
    if (number_of_elements == 0 || element_size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        return NULL;
    }

    void *pointer = calloc(number_of_elements, element_size);
    if (!pointer)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                number_of_elements * element_size);
        return NULL;
    }

    // No memset because calloc already initializes memory to all zero
    return pointer;
}

void *el_unsafe_realloc(void **pointer_address, const size_t size,
                        const el_handlers handlers, const_cstring calling_file,
                        const int calling_line)
{
    if (size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        el_free_address(pointer_address);
        return NULL;
    }

    if (!pointer_address || !(*pointer_address))
    {
        void *pointer = el_custom_malloc(size, el_null_handlers());
        if (!pointer)
        {
            handle_allocation_error(handlers, calling_file, calling_line,
                                    __func__, size);
            return NULL;
        }
        else
        {
            return pointer;
        }
    }

    void *pointer = realloc(*pointer_address, size);
    if (!pointer)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                size);
        return NULL;
    }

    if (pointer != (*pointer_address))
    {
        (*pointer_address) = NULL;
    }

    return pointer;
}<commit_msg>Improve internal structure of el_unsafe_malloc<commit_after>#include "elegan-c/memory.h"

#include <stdio.h>
#include <string.h>

static void handle_zero_request(const el_handlers handlers,
                                const_cstring calling_file,
                                const int calling_line,
                                const_cstring target_function)
{
    if (handlers.error_stream)
    {
        fprintf(handlers.error_stream,
                "Error allocating memory: Function '%s' was called from file "
                "'%s' at line '%d', and zero memory was requested.\n",
                target_function, calling_file, calling_line);
    }

    if (handlers.error_function)
    {
        handlers.error_function(EXIT_FAILURE);
    }
}

static void handle_allocation_error(const el_handlers handlers,
                                    const_cstring calling_file,
                                    const int calling_line,
                                    const_cstring target_function,
                                    const size_t requested_size)
{
    if (handlers.error_stream)
    {
        fprintf(handlers.error_stream,
                "Error allocating memory: Function '%s' was called from file "
                "'%s' at line '%d' and '%zu' bytes of memory were requested, "
                "but this amount could not be allocated.\n",
                target_function, calling_file, calling_line, requested_size);
    }

    if (handlers.error_function)
    {
        handlers.error_function(EXIT_FAILURE);
    }
}

void *el_unsafe_malloc(const size_t size, const el_handlers handlers,
                       const_cstring calling_file, const int calling_line)
{
    if (size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        return NULL;
    }

    void *pointer = malloc(size);
    if (!pointer)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                size);
        return NULL;
    }

    return memset(pointer, 0, size);
}

void el_free_address(void **pointer_address)
{
    if (pointer_address)
    {
        free(*pointer_address);
        *pointer_address = NULL;
    }
}

void *el_unsafe_calloc(const size_t number_of_elements,
                       const size_t element_size, const el_handlers handlers,
                       const_cstring calling_file, const int calling_line)
{
    if (number_of_elements == 0 || element_size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        return NULL;
    }

    void *pointer = calloc(number_of_elements, element_size);
    if (!pointer)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                number_of_elements * element_size);
        return NULL;
    }

    // No memset because calloc already initializes memory to all zero
    return pointer;
}

void *el_unsafe_realloc(void **pointer_address, const size_t size,
                        const el_handlers handlers, const_cstring calling_file,
                        const int calling_line)
{
    if (size == 0)
    {
        handle_zero_request(handlers, calling_file, calling_line, __func__);
        el_free_address(pointer_address);
        return NULL;
    }

    void *pointer = NULL;
    if (!pointer_address || !(*pointer_address))
    {
        pointer = el_custom_malloc(size, el_null_handlers());
        if (!pointer)
        {
            handle_allocation_error(handlers, calling_file, calling_line,
                                    __func__, size);
            return NULL;
        }
    }
    else
    {
        pointer = realloc(*pointer_address, size);
        if (!pointer)
        {
            handle_allocation_error(handlers, calling_file, calling_line,
                                    __func__, size);
            return NULL;
        }

        if (pointer != (*pointer_address))
        {
            (*pointer_address) = NULL;
        }
    }

    return pointer;
}
{'fraction_non_alphanumeric': 0.07991093518060366, 'fraction_numerical': 0.0012370113805047005, 'mean_word_length': 1.8813257305773343, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4957839', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2203, 'n_words': 517}