|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +title: Memory Manager |
| 4 | +next_section: usage-rules |
| 5 | +permalink: /docs/api/mm/ |
| 6 | +header_path: os/include/mm/bt_mm.h |
| 7 | +source_path: os/src/mm/bt_mm.c |
| 8 | +--- |
| 9 | + |
| 10 | +The memory manager is responsible for managing all memory within BitThunder. For systems with MMU's the mm, |
| 11 | +is also responsible for managing all virtual memory mappings for each process. |
| 12 | + |
| 13 | +# User-space Memory Allocation API. |
| 14 | + |
| 15 | +<div class="mobile-side-scroller"> |
| 16 | +<table> |
| 17 | + <thead> |
| 18 | + <tr> |
| 19 | + <th>API</th> |
| 20 | + <th>Description</th> |
| 21 | + </tr> |
| 22 | + </thead> |
| 23 | + <tbody> |
| 24 | + |
| 25 | + <tr> |
| 26 | + <td><p><code><a href="{{ site.url }}/api/mm/malloc/">malloc()</a></code></p></td> |
| 27 | + <td><p> |
| 28 | + POSIX compliant malloc implementation. |
| 29 | + </p></td> |
| 30 | + </tr> |
| 31 | + |
| 32 | + <tr> |
| 33 | + <td><p><code><a href="{{ site.url }}/api/mm/free/">free()</a></code></p></td> |
| 34 | + <td><p> |
| 35 | + POSIX compliant free implementation. |
| 36 | + </p></td> |
| 37 | + </tr> |
| 38 | + |
| 39 | + </tbody> |
| 40 | +</table> |
| 41 | +</div> |
| 42 | + |
| 43 | +# Kernel-mode API. |
| 44 | + |
| 45 | +<div class="mobile-side-scroller"> |
| 46 | +<table> |
| 47 | + <thead> |
| 48 | + <tr> |
| 49 | + <th>API</th> |
| 50 | + <th>Description</th> |
| 51 | + </tr> |
| 52 | + </thead> |
| 53 | + <tbody> |
| 54 | + |
| 55 | + <tr> |
| 56 | + <td><p><code><a href="{{ site.url }}/api/mm/bt_kmalloc/">BT_kMalloc()</a></code></p></td> |
| 57 | + <td><p> |
| 58 | + Allocates arbitrary amounts of memory for use in the kernel, e.g. drivers etc. |
| 59 | + </p></td> |
| 60 | + </tr> |
| 61 | + |
| 62 | + <tr> |
| 63 | + <td><p><code><a href="{{ site.url }}/api/mm/bt_kfree/">BT_kFree()</a></code></p></td> |
| 64 | + <td><p> |
| 65 | + Frees a kernel memory allocation. |
| 66 | + </p></td> |
| 67 | + </tr> |
| 68 | + |
| 69 | + <tr> |
| 70 | + <td><p><code><a href="{{ site.url }}/api/mm/bt_ioremap/">bt_ioremap()</a></code></p></td> |
| 71 | + <td><p> |
| 72 | + Remaps a physical address to a usable virtual address. THIS MUST BE USED EVEN WITH NO MMU. |
| 73 | + </p></td> |
| 74 | + </tr> |
| 75 | + |
| 76 | + </tbody> |
| 77 | +</table> |
| 78 | +</div> |
| 79 | + |
| 80 | +# Page Allocation |
| 81 | + |
| 82 | +<div class="mobile-side-scroller"> |
| 83 | +<table> |
| 84 | + <thead> |
| 85 | + <tr> |
| 86 | + <th>API</th> |
| 87 | + <th>Description</th> |
| 88 | + </tr> |
| 89 | + </thead> |
| 90 | + <tbody> |
| 91 | + |
| 92 | + <tr> |
| 93 | + <td><p><code><a href="{{ site.url }}/api/mm/bt_page_alloc/">bt_page_alloc()</a></code></p></td> |
| 94 | + <td><p> |
| 95 | + Allocates page-aligned, page multiples of atleast the requested size. |
| 96 | + </p></td> |
| 97 | + </tr> |
| 98 | + |
| 99 | + <tr> |
| 100 | + <td><p><code><a href="{{ site.url }}/api/mm/bt_page_free/">bt_page_free()</a></code></p></td> |
| 101 | + <td><p> |
| 102 | + Frees page-aligned memory as requested from bt_page_alloc(). |
| 103 | + </p></td> |
| 104 | + </tr> |
| 105 | + |
| 106 | + </tbody> |
| 107 | +</table> |
| 108 | +</div> |
| 109 | + |
| 110 | +# Design Overview |
| 111 | +The memory manager has a few different implementations which are used depending on the kernel configurations. |
| 112 | + |
| 113 | +## Basic MM |
| 114 | +Typical micro-controller applications use a mono-lithic, space-efficient heap. |
| 115 | +The heap consists of a free linked list, and the heap structure is stored within the heaps memory |
| 116 | +directly. This means its possible for threads/processes to cause heap corruption. |
| 117 | + |
| 118 | +## Powerful MM (Page based and Virtual Memory Management) |
| 119 | +In these more powerful systems with MMUs, its possible to configure support for page based memory allocations |
| 120 | +and optionally virtual memory if desired. |
| 121 | + |
| 122 | +The page based allocator becomes the lowest-level memory allocator on which the kernel heap, and process heaps |
| 123 | +are based. |
| 124 | + |
| 125 | +The kernel heap is really efficient SLAB based algorithm, with all O(1) constant time operations. |
| 126 | + |
| 127 | +# To be implemented: |
| 128 | + |
| 129 | + * mmap() |
| 130 | + * User-space heaps. |
| 131 | + |
| 132 | +# In progress |
| 133 | + |
| 134 | + * vmalloc() api - Virtual memory allocation and mappings. |
| 135 | + |
| 136 | +# Further Reading |
| 137 | + |
| 138 | +The full API is defined under: |
| 139 | + |
| 140 | +{% include api/header-path.md %} |
| 141 | + |
| 142 | +See the implementation under: |
| 143 | + |
| 144 | +{% include api/source-path.md %} |
| 145 | + |
| 146 | +<a href="{{ site.gh-blob-url }}/os/src/mm/slab.c"><code>os/src/mm/slab.c</code></a> |
| 147 | +<a href="{{ site.gh-blob-url }}/os/src/mm/bt_page.c"><code>os/src/mm/bt_page.c</code></a> |
0 commit comments