186 lines
5.5 KiB
HTML
186 lines
5.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="description" content="MontaukOS manual page: malloc(3) - userspace heap allocation">
|
|
<title>malloc(3) - MontaukOS Manual</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" rel="stylesheet">
|
|
<style>
|
|
body {
|
|
font-family: 'Open Sans', Arial, sans-serif;
|
|
max-width: 960px;
|
|
margin: 0 auto;
|
|
padding: 1em;
|
|
line-height: 1.5;
|
|
display: flex;
|
|
gap: 2em;
|
|
}
|
|
p { margin: 0 0 0.5em; }
|
|
h2 { margin: 0.5em 0; }
|
|
.sidebar {
|
|
width: 160px;
|
|
flex-shrink: 0;
|
|
}
|
|
.sidebar ul {
|
|
list-style: none;
|
|
padding: 0;
|
|
}
|
|
.sidebar li {
|
|
margin: 0.5em 0;
|
|
}
|
|
.sidebar a {
|
|
color: #0066CC;
|
|
text-decoration: none;
|
|
font-weight: 600;
|
|
}
|
|
.sidebar a:hover {
|
|
color: #004499;
|
|
text-decoration: underline;
|
|
}
|
|
.sidebar .current {
|
|
color: #004499;
|
|
}
|
|
.sidebar hr {
|
|
border: none;
|
|
border-top: 1px solid #999;
|
|
margin: 0.75em 0;
|
|
}
|
|
.main {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
a { color: #0000EE; }
|
|
a:visited { color: #0066CC; }
|
|
hr { border-style: solid; border-width: 1px 0 0 0; border-color: #999; }
|
|
pre { background: #f0f0f0; padding: 0.5em; overflow-x: auto; }
|
|
code { background: #f0f0f0; padding: 0 0.15em; }
|
|
pre code { padding: 0; }
|
|
.center { text-align: center; }
|
|
.box {
|
|
border: 1px solid #999;
|
|
padding: 0.5em;
|
|
margin: 0.5em 0;
|
|
}
|
|
@media (max-width: 700px) {
|
|
body {
|
|
flex-direction: column;
|
|
gap: 1em;
|
|
padding: 1em 0.75em;
|
|
}
|
|
.sidebar {
|
|
width: auto;
|
|
}
|
|
.sidebar ul {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 0 1em;
|
|
}
|
|
.sidebar hr {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="sidebar">
|
|
<ul>
|
|
<li><a href="../../index.html">Home</a></li>
|
|
<li><a href="../../downloads.html">Downloads</a></li>
|
|
<li><a href="../../history.html">History</a></li>
|
|
<li><a href="../index.html">Documentation</a></li>
|
|
<li><a href="https://git.montaukos.org/daniel/MontaukOS/issues">Issue tracker</a></li>
|
|
<li><a href="https://git.montaukos.org/daniel/MontaukOS">Git</a></li>
|
|
</ul>
|
|
<hr>
|
|
<ul>
|
|
<li><a href="index.html" class="current">Man Pages</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="main">
|
|
<div class="center">
|
|
<h1>malloc(3)</h1>
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<pre><code><strong>NAME</strong>
|
|
malloc, mfree, realloc - userspace heap allocation
|
|
|
|
|
|
<strong>SYNOPSIS</strong>
|
|
<strong>void* montauk::malloc(uint64_t size);</strong>
|
|
<strong>void montauk::mfree(void* ptr);</strong>
|
|
<strong>void* montauk::realloc(void* ptr, uint64_t size);</strong>
|
|
|
|
|
|
<strong>DESCRIPTION</strong>
|
|
The userspace heap provides dynamic memory allocation on top of
|
|
anonymous virtual memory. The Montauk C++ API and libc's
|
|
malloc/free API use the same process-wide allocator.
|
|
|
|
|
|
<strong>malloc</strong>
|
|
Allocates 'size' bytes from the free list. Returns a 16-byte
|
|
aligned pointer, or nullptr on failure. When the free list is
|
|
empty, it reserves more pages from the kernel. Physical pages
|
|
are committed as they are first touched.
|
|
|
|
char* buf = (char*)montauk::malloc(1024);
|
|
|
|
|
|
<strong>mfree</strong>
|
|
Returns the block to the userspace allocator. Arena blocks are
|
|
immediately reusable; large direct mappings are returned to the
|
|
kernel, including their virtual address range.
|
|
Passing nullptr is a safe no-op.
|
|
|
|
montauk::mfree(buf);
|
|
|
|
|
|
<strong>realloc</strong>
|
|
Resizes the allocation to 'size' bytes. A block with sufficient
|
|
capacity is retained; otherwise a new block is allocated, the
|
|
smaller of old/new requested sizes is copied, and the old block
|
|
is freed. Integer overflow fails without changing the old block.
|
|
If ptr is nullptr, behaves like malloc.
|
|
|
|
buf = (char*)montauk::realloc(buf, 2048);
|
|
|
|
|
|
<strong>IMPLEMENTATION</strong>
|
|
The allocator uses segregated size-class bins and a coalescing
|
|
address-ordered overflow list. Headers retain both requested size
|
|
and actual block extent. A process-wide lock serializes C and C++
|
|
allocation calls. All returned pointers are 16-byte aligned.
|
|
|
|
Allocations of 256 KiB or more use direct page mappings so they
|
|
can be released promptly. Smaller allocations use growing arenas.
|
|
|
|
|
|
<strong>LOW-LEVEL PAGE API</strong>
|
|
For large allocations or when direct page control is needed:
|
|
|
|
void* montauk::alloc(uint64_t size); // SYS_ALLOC
|
|
void montauk::free(void* ptr); // SYS_FREE
|
|
|
|
alloc() reserves zero-filled, read/write, non-executable pages.
|
|
Size is rounded up to 4 KiB. Freed ranges are reusable.
|
|
|
|
|
|
<strong>SEE ALSO</strong>
|
|
<a href="syscalls.html">syscalls(2)</a>, <a href="file.html">file(2)</a></code></pre>
|
|
<hr>
|
|
|
|
<div class="center">
|
|
<a href="../index.html">Back to Documentation Index</a>
|
|
</div>
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|