<commit_before>#include "idt.h"
#include "kernel_page.h"
#include "paging.h"
#include "panic.h"

typedef union kernel_page {
    union kernel_page* next;
    char mem[PAGE_SIZE];
}
kernel_page_t;

static kernel_page_t*
allocated_to;

static kernel_page_t*
end;

static kernel_page_t*
next_free;

void
kernel_page_init(virt_t begin_, virt_t end_)
{
    allocated_to = (void*)begin_;
    end = (void*)end_;
}

void*
kernel_page_alloc()
{
    interrupts_disable();

    if(next_free) {
        kernel_page_t* page = next_free;
        next_free = next_free->next;
        interrupts_enable();
        return page;
    }

    interrupts_enable();

    if(allocated_to >= end) {
        return NULL;
    }

    page_map((virt_t)allocated_to, page_alloc(), PE_PRESENT | PE_READ_WRITE);
    return allocated_to++;
}

void
kernel_page_free(void* ptr)
{
    kernel_page_t* page = ptr;

    interrupts_disable();
    page->next = next_free;
    next_free = page;
    interrupts_enable();
}<commit_msg>fix locking around kernel page allocation<commit_after>#include "idt.h"
#include "kernel_page.h"
#include "paging.h"
#include "panic.h"

typedef union kernel_page {
    union kernel_page* next;
    char mem[PAGE_SIZE];
}
kernel_page_t;

static kernel_page_t*
allocated_to;

static kernel_page_t*
end;

static kernel_page_t*
next_free;

void
kernel_page_init(virt_t begin_, virt_t end_)
{
    allocated_to = (void*)begin_;
    end = (void*)end_;
}

void*
kernel_page_alloc()
{
    critical_begin();

    if(next_free) {
        kernel_page_t* page = next_free;
        next_free = next_free->next;
        critical_end();
        return page;
    }

    if(allocated_to >= end) {
        return NULL;
    }

    page_map((virt_t)allocated_to, page_alloc(), PE_PRESENT | PE_READ_WRITE);
    void* retn = allocated_to++;

    critical_end();
    return retn;
}

void
kernel_page_free(void* ptr)
{
    kernel_page_t* page = ptr;

    critical_begin();
    page->next = next_free;
    next_free = page;
    critical_end();
}
{'fraction_non_alphanumeric': 0.11492537313432835, 'fraction_numerical': 0.0, 'mean_word_length': 3.508968609865471, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5490663', 'n_tokens_mistral': 750, 'n_tokens_neox': 733, 'n_words': 148}