# Memory Management

> To use the functions described in this page, the file `<hal.h>` should be included.

JSLK supports memory management by the use of a virtual memory manager and a heap. There is currently no physical memory manager available, although one will be implemented before the first release. The following functions can be used to interface with the memory manager throughout the kernel:

{% tabs %}
{% tab title="kmalloc()" %}

#### `uint32_t kmalloc(uint32_t sz)`

Used to allocate a block of memory.

**Arguments**

`uint32_t sz`: The size of the block to allocate.

**Return value**

The virtual memory address of the allocated block is returned as an `uint32_t`.
{% endtab %}

{% tab title="kmalloc\_a()" %}

#### `uint32_t kmalloc_a(uint32_t sz)`

Used to allocate a page-aligned block of memory.

**Arguments**

`uint32_t sz`: The size of the block to allocate.

**Return value**

The virtual memory address of the allocated block is returned as an `uint32_t`.
{% endtab %}

{% tab title="kmalloc\_p()" %}

#### `uint32_t kmalloc_p(uint32_t sz, uint32_t *phys)`

Used to allocate a block of memory when the physical address of the block is required.

**Arguments**

`uint32_t sz`: The size of the block to allocate.

`uint32_t *phys`: A pointer to the variable where the physical address of the memory block should be stored.

**Return value**

The virtual memory address of the allocated block is returned as an `uint32_t`. The physical memory address of the block is returned to the variable passed to the function as `phys`.
{% endtab %}

{% tab title="kmalloc\_ap()" %}

#### `uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys)`

Used to allocate a page-aligned block of memory when the physical address of the block is required.

**Arguments**

`uint32_t sz`: The size of the block to allocate.

`uint32_t *phys`: A pointer to the variable where the physical address of the memory block should be stored.

**Return value**

The virtual memory address of the allocated block is returned as an `uint32_t`. The physical memory address of the block is returned to the variable passed to the function as `phys`.
{% endtab %}
{% endtabs %}

## x86

Currently, only the first 16 MB of available memory are used by the kernel. To manage memory on the x86, a GDT is set-up as a security measure to protect memory until paging is enabled. After paging is enabled,  Once the virtual memory manager functions become available, and later in the startup when the heap is initialized, the heap integrates with the VMM and overrides its functions. The structures for the page directories, tables and pages can be found in [`paging.h`](https://github.com/sofferjacob/jslk/blob/master/hal/x86/memory/paging.h), the paging code in [`paging.c`](https://github.com/sofferjacob/jslk/blob/master/hal/x86/memory/paging.c).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jslk.gitbook.io/docs/memory-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
