summaryrefslogtreecommitdiff
path: root/src/kernel/cxx.cc
blob: f9541b576c5cd7856a299182d525b2d1d61b9463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "common/types.h"
#include "kernel/pmm.h"
#include "drivers/uart.h"

/*
 * C++ Kernel Runtime Support
 *
 * * Strategy: Map directly to PMM::alloc_page().
 * Pros: Simple, works immediately for VFS.
 * Cons: Allocates 4KB for every object (wasteful for small objects),
 * but acceptable for current stage.
 */

void *operator new(unsigned long size)
{
    // Safety check: PMM allocates 4096 bytes max per call
    if (size > 4096)
    {
        Drivers::uart_puts("panic: new allocation too big (max 4096)\n");
        while (1)
            ;
    }

    void *p = PMM::alloc_page();
    if (!p)
    {
        Drivers::uart_puts("panic: new out of memory\n");
        while (1)
            ;
    }
    // PMM::alloc_page returns zeroed memory usually,
    // but standard 'new' doesn't guarantee it.
    // The constructor will handle initialization.
    return p;
}

void *operator new[](unsigned long size)
{
    return operator new(size);
}

void operator delete(void *p)
{
    if (p)
        PMM::free_page(p);
}

void operator delete[](void *p)
{
    if (p)
        PMM::free_page(p);
}

// C++14 Sized Deallocation (Required by modern compilers)
// The compiler optimizes 'delete' calls to pass the size of the object
void operator delete(void *p, unsigned long size)
{
    (void)size; // We ignore size because PMM always frees a full page
    if (p)
        PMM::free_page(p);
}

void operator delete[](void *p, unsigned long size)
{
    (void)size;
    if (p)
        PMM::free_page(p);
}