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

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

static void print_zero_memory_requested(const char *calling_function,
                                        const char *target_function,
                                        const char *variable_name)
{
    fprintf(stderr, "Error allocating memory: The function '%s' called '%s' "
                    "and requested zero memory ('%s' == 0). The pointer should"
                    " be explicitly set to NULL instead.\n",
            calling_function, target_function, variable_name);
}

static void print_memory_allocation_error(const char *calling_function,
                                          const char *target_function,
                                          const size_t size)
{
    fprintf(stderr, "Error allocating memory: The function '%s' called '%s' "
                    "requesting %zu bytes of memory, but an error occurred "
                    "allocating this amount of memory.\n",
            calling_function, target_function, size);
}

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 *memory = malloc(size);
    if (!memory)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                size);
        return NULL;
    }

    return memset(memory, 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 *memory = calloc(number_of_elements, element_size);
    if (!memory)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                number_of_elements * element_size);
    }

    // No memset because calloc already initializes memory to all zero<commit_msg>Add missig `return NULL` to `el_unsafe_calloc`<commit_after>#include "elegan-c/memory.h"

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

static void print_zero_memory_requested(const char *calling_function,
                                        const char *target_function,
                                        const char *variable_name)
{
    fprintf(stderr, "Error allocating memory: The function '%s' called '%s' "
                    "and requested zero memory ('%s' == 0). The pointer should"
                    " be explicitly set to NULL instead.\n",
            calling_function, target_function, variable_name);
}

static void print_memory_allocation_error(const char *calling_function,
                                          const char *target_function,
                                          const size_t size)
{
    fprintf(stderr, "Error allocating memory: The function '%s' called '%s' "
                    "requesting %zu bytes of memory, but an error occurred "
                    "allocating this amount of memory.\n",
            calling_function, target_function, size);
}

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 *memory = malloc(size);
    if (!memory)
    {
        handle_allocation_error(handlers, calling_file, calling_line, __func__,
                                size);
        return NULL;
    }

    return memset(memory, 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 *memory = calloc(number_of_elements, element_size);
    if (!memory)
    {
        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
{'fraction_non_alphanumeric': 0.07645693656523982, 'fraction_numerical': 0.0012893243940175349, 'mean_word_length': 1.8793615441722347, '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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3042694', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2022, 'n_words': 536}