<commit_before>#include <truth/panic.h>
#include <truth/region_vector.h>
#include <truth/slab.h>
#include <truth/string.h>
#include <truth/types.h>

#define heap_redzone_size sizeof(long)
#define heap_redzone_fill 0xccccccccu

struct region_vector *heap_metadata_used;
struct region_vector *heap_metadata_free;
byte *heap = NULL;

enum status checked init_heap(void) {
    union address heap_address;
    heap_address.virtual = heap;
    heap_metadata_used = slab_alloc(1, Page_Small, Memory_Writable);
    if (heap_metadata_used == NULL) {
        assert(0);
        return Error_No_Memory;
    }
    heap_metadata_free = slab_alloc(1, Page_Small, Memory_Writable);
    if (heap_metadata_free == NULL) {
        slab_free(1, Page_Small, heap_metadata_used);
        assert(0);
        return Error_No_Memory;
    }
    heap = slab_alloc(1, Page_Small, Memory_Writable);
    if (heap == NULL) {
        slab_free(1, Page_Small, heap_metadata_used);
        slab_free(1, Page_Small, heap_metadata_free);
        assert(0);
        return Error_No_Memory;
    }

    heap_metadata_used = slab_alloc(1, Page_Small, Memory_Writable);
    init_region_vector(heap_metadata_used);
    heap_metadata_free = slab_alloc(1, Page_Small, Memory_Writable);
    init_region_vector(heap_metadata_free);
    region_free(heap_metadata_free, heap_address, Page_Small);
    memset(heap, heap_redzone_fill, Page_Small);
    return Ok;
}

void *kmalloc(size_t bytes) {
    union address address;
    size_t allocation_size = bytes + 2 * heap_redzone_size;

    enum status status = region_alloc(heap_metadata_free,
                                      allocation_size, &address);
    if (status != Ok) {
        return NULL;
    }
    region_free(heap_metadata_used, address, allocation_size);
    unsigned long *redzone_prefix = address.virtual;
    unsigned long *redzone_suffix =
        (unsigned long *)(address.bytes + bytes + heap_redzone_size);
    assert(*redzone_prefix == heap_redzone_fill &&
           *redzone_suffix == heap_redzone_fill);
    return address.bytes + heap_redzone_size;
}

void *kcalloc(size_t count, size_t size) {
    void *address = kmalloc(count * size);<commit_msg>Capitalize global defines in heap<commit_after>#include <truth/panic.h>
#include <truth/region_vector.h>
#include <truth/slab.h>
#include <truth/string.h>
#include <truth/types.h>

#define Heap_Red_Zone_Size sizeof(unsigned long)
#define Heap_Red_Zone_Fill 0xccccccccccccccccul

#define Heap_Size 16

struct region_vector *heap_metadata_used;
struct region_vector *heap_metadata_free;
byte *heap = NULL;

enum status checked init_heap(void) {
    union address heap_address;
    heap_address.virtual = heap;
    heap_metadata_used = slab_alloc(1, Page_Small, Memory_Writable);
    if (heap_metadata_used == NULL) {
        assert(0);
        return Error_No_Memory;
    }
    heap_metadata_free = slab_alloc(1, Page_Small, Memory_Writable);
    if (heap_metadata_free == NULL) {
        slab_free(1, Page_Small, heap_metadata_used);
        assert(0);
        return Error_No_Memory;
    }
    heap = slab_alloc(Heap_Size, Page_Small, Memory_Writable);
    if (heap == NULL) {
        slab_free(1, Page_Small, heap_metadata_used);
        slab_free(1, Page_Small, heap_metadata_free);
        assert(0);
        return Error_No_Memory;
    }

    heap_metadata_used = slab_alloc(1, Page_Small, Memory_Writable);
    init_region_vector(heap_metadata_used);
    heap_metadata_free = slab_alloc(1, Page_Small, Memory_Writable);
    init_region_vector(heap_metadata_free);
    heap_address.virtual = heap;
    region_free(heap_metadata_free, heap_address, 16 * Page_Small);
    memset(heap, (int)Heap_Red_Zone_Fill, Page_Small);
    return Ok;
}

void *kmalloc(size_t bytes) {
    union address address;
    size_t allocation_size = bytes + 2 * Heap_Red_Zone_Size;

    debug_region_vector(heap_metadata_free);
    enum status status = region_alloc(heap_metadata_free,
                                      allocation_size, &address);
    if (status != Ok) {
        return NULL;
    }
    region_free(heap_metadata_used, address, allocation_size);
    unsigned long *redzone_prefix = address.virtual;
    unsigned long *redzone_suffix =
        (unsigned long *)(address.bytes + bytes + Heap_Red_Zone_Size);
    assert(*redzone_prefix == Heap_Red_Zone_Fill &&
           *redzone_suffix == Heap_Red_Zone_Fill);
    return address.bytes + Heap_Red_Zone_Size;
}

void *kcalloc(size_t count, size_t size) {
    void *address = kmalloc(count * size);
{'fraction_non_alphanumeric': 0.09420610349402919, 'fraction_numerical': 0.006413091552410438, 'mean_word_length': 3.837433155080214, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '763167', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1474, 'n_words': 299}