kernel: adjust formatting
This commit is contained in:
190
vm/memblock.c
190
vm/memblock.c
@@ -19,27 +19,29 @@
|
||||
contributors may be used to endorse or promote products derived from this
|
||||
software without specific prior written permission.
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include <mango/types.h>
|
||||
#include <mango/libc/string.h>
|
||||
#include <mango/memblock.h>
|
||||
#include <mango/types.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
#define ITER(a, b) ((uint64_t)(a) | ((uint64_t)(b) << 32))
|
||||
#define ITER_END ULLONG_MAX
|
||||
#define ITER_END ULLONG_MAX
|
||||
#define IDX_A(idx) ((idx) & 0xFFFFFFFF)
|
||||
#define IDX_B(idx) (((idx) >> 32) & 0xFFFFFFFF)
|
||||
|
||||
/* the maximum possible value for a pointer type.
|
||||
Note that any pointers returned by the memblock API will still
|
||||
be bounded by the defined memory regions, and not by this constant. */
|
||||
#define ADDR_MAX (~(uintptr_t)0)
|
||||
#define ADDR_MAX (~(uintptr_t)0)
|
||||
|
||||
static struct memblock_region init_memory_regions[MEMBLOCK_INIT_MEMORY_REGION_COUNT];
|
||||
static struct memblock_region init_reserved_regions[MEMBLOCK_INIT_RESERVED_REGION_COUNT];
|
||||
static struct memblock_region
|
||||
init_memory_regions[MEMBLOCK_INIT_MEMORY_REGION_COUNT];
|
||||
static struct memblock_region
|
||||
init_reserved_regions[MEMBLOCK_INIT_RESERVED_REGION_COUNT];
|
||||
|
||||
static phys_addr_t do_alloc(size_t size, phys_addr_t align);
|
||||
|
||||
@@ -59,16 +61,21 @@ static void memblock_double_capacity(struct memblock_type *type)
|
||||
{
|
||||
size_t new_max = type->max * 2;
|
||||
|
||||
phys_addr_t new_regions_p = do_alloc(new_max * sizeof(struct memblock_region), 8);
|
||||
phys_addr_t new_regions_p
|
||||
= do_alloc(new_max * sizeof(struct memblock_region), 8);
|
||||
|
||||
void *new_regions = (void *)(new_regions_p + memblock.m_voffset);
|
||||
memcpy(new_regions, type->regions, type->count * sizeof(struct memblock_region));
|
||||
memcpy(new_regions,
|
||||
type->regions,
|
||||
type->count * sizeof(struct memblock_region));
|
||||
|
||||
type->regions = new_regions;
|
||||
type->max = new_max;
|
||||
}
|
||||
|
||||
static int memblock_insert_region(struct memblock_type *type, struct memblock_region *to_add)
|
||||
static int memblock_insert_region(
|
||||
struct memblock_type *type,
|
||||
struct memblock_region *to_add)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
|
||||
@@ -110,13 +117,17 @@ static int memblock_remove_region(struct memblock_type *type, unsigned int i)
|
||||
int memblock_init(uintptr_t alloc_start, uintptr_t alloc_end, uintptr_t voffset)
|
||||
{
|
||||
memblock.m_alloc_start = alloc_start;
|
||||
memblock.m_alloc_end =alloc_end;
|
||||
memblock.m_alloc_end = alloc_end;
|
||||
memblock.m_voffset = voffset;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int memblock_add_range(struct memblock_type *type, uintptr_t base, size_t size, enum memblock_region_status status)
|
||||
int memblock_add_range(
|
||||
struct memblock_type *type,
|
||||
uintptr_t base,
|
||||
size_t size,
|
||||
enum memblock_region_status status)
|
||||
{
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
@@ -131,14 +142,17 @@ int memblock_add_range(struct memblock_type *type, uintptr_t base, size_t size,
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct memblock_region new_region = { .base = base, .limit = limit, .status = status };
|
||||
struct memblock_region new_region
|
||||
= {.base = base, .limit = limit, .status = status};
|
||||
|
||||
/* two regions with different statuses CANNOT intersect. we first need to check
|
||||
to make sure the region being added doesn't violate this rule. */
|
||||
/* two regions with different statuses CANNOT intersect. we first need
|
||||
to check to make sure the region being added doesn't violate this
|
||||
rule. */
|
||||
for (unsigned int i = 0; i < type->count; i++) {
|
||||
struct memblock_region *cur_region = &type->regions[i];
|
||||
|
||||
if (new_region.base > cur_region->limit || new_region.limit < cur_region->base) {
|
||||
if (new_region.base > cur_region->limit
|
||||
|| new_region.limit < cur_region->base) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -154,47 +168,67 @@ int memblock_add_range(struct memblock_type *type, uintptr_t base, size_t size,
|
||||
for (unsigned int i = 0; i < type->count; i++) {
|
||||
struct memblock_region *cur_region = &type->regions[i];
|
||||
|
||||
/* case 1: the region being added and the current region have no connection what-so-ever (no overlaps) */
|
||||
if (cur_region->limit + 1 < new_region.base || cur_region->base > new_region.limit) {
|
||||
/* case 1: the region being added and the current region have no
|
||||
* connection what-so-ever (no overlaps) */
|
||||
if (cur_region->limit + 1 < new_region.base
|
||||
|| cur_region->base > new_region.limit) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* case 2: the region being added matches a region already in the list. */
|
||||
if (cur_region->base == new_region.base && cur_region->limit == new_region.limit) {
|
||||
/* case 2: the region being added matches a region already in
|
||||
* the list. */
|
||||
if (cur_region->base == new_region.base
|
||||
&& cur_region->limit == new_region.limit) {
|
||||
/* nothing needs to be done */
|
||||
add_new = false;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
/* case 3: the region being added completely contains a region already in the list. */
|
||||
if (cur_region->base > new_region.base && cur_region->limit <= new_region.limit) {
|
||||
/* case 3: the region being added completely contains a region
|
||||
* already in the list. */
|
||||
if (cur_region->base > new_region.base
|
||||
&& cur_region->limit <= new_region.limit) {
|
||||
memblock_remove_region(type, i);
|
||||
|
||||
/* after memblock_remove_region(), a different region will have moved into the array slot referenced by i.
|
||||
decrementing i means we'll stay at the current index and process this region. */
|
||||
/* after memblock_remove_region(), a different region
|
||||
will have moved into the array slot referenced by i.
|
||||
decrementing i means we'll stay at the current index
|
||||
and process this region. */
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* case 4: the region being added meets or partially overlaps a
|
||||
* region already in the list. */
|
||||
|
||||
/* case 4: the region being added meets or partially overlaps a region already in the list. */
|
||||
|
||||
/* there can be an overlap at the beginning and the end of the region being added,
|
||||
anything else is either a full overlap (case 3) or not within the region being added at all.
|
||||
to handle this, remove the region that's already in the list and extend the region being added to cover it.
|
||||
the two regions may overlap and have incompatible statuses, but this case was handled earlier in this function. */
|
||||
if ((new_region.base > cur_region->base || new_region.base == cur_region->limit - 1) && new_region.status == cur_region->status) {
|
||||
/* the new region overlaps the END of the current region, change the base of the new region to match that of the current region. */
|
||||
/* there can be an overlap at the beginning and the end of the
|
||||
region being added, anything else is either a full overlap
|
||||
(case 3) or not within the region being added at all. to
|
||||
handle this, remove the region that's already in the list and
|
||||
extend the region being added to cover it. the two regions
|
||||
may overlap and have incompatible statuses, but this case was
|
||||
handled earlier in this function. */
|
||||
if ((new_region.base > cur_region->base
|
||||
|| new_region.base == cur_region->limit - 1)
|
||||
&& new_region.status == cur_region->status) {
|
||||
/* the new region overlaps the END of the current
|
||||
* region, change the base of the new region to match
|
||||
* that of the current region. */
|
||||
new_region.base = cur_region->base;
|
||||
} else if ((new_region.base < cur_region->base || new_region.limit + 1 == cur_region->base) && new_region.status == cur_region->status) {
|
||||
/* the new region overlaps the BEGINNING of the current region, change the limit of the new region to match that of the current region. */
|
||||
} else if (
|
||||
(new_region.base < cur_region->base
|
||||
|| new_region.limit + 1 == cur_region->base)
|
||||
&& new_region.status == cur_region->status) {
|
||||
/* the new region overlaps the BEGINNING of the current
|
||||
* region, change the limit of the new region to match
|
||||
* that of the current region. */
|
||||
new_region.limit = cur_region->limit;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* with the new region updated to include the current region, we can remove the current region from the list */
|
||||
/* with the new region updated to include the current region, we
|
||||
* can remove the current region from the list */
|
||||
memblock_remove_region(type, i);
|
||||
i--;
|
||||
}
|
||||
@@ -216,7 +250,11 @@ int memblock_add(uintptr_t base, size_t size)
|
||||
memblock_double_capacity(&memblock.memory);
|
||||
}
|
||||
|
||||
return memblock_add_range(&memblock.memory, base, size, MEMBLOCK_MEMORY);
|
||||
return memblock_add_range(
|
||||
&memblock.memory,
|
||||
base,
|
||||
size,
|
||||
MEMBLOCK_MEMORY);
|
||||
}
|
||||
|
||||
int memblock_reserve(uintptr_t base, size_t size)
|
||||
@@ -225,7 +263,11 @@ int memblock_reserve(uintptr_t base, size_t size)
|
||||
memblock_double_capacity(&memblock.reserved);
|
||||
}
|
||||
|
||||
return memblock_add_range(&memblock.reserved, base, size, MEMBLOCK_RESERVED);
|
||||
return memblock_add_range(
|
||||
&memblock.reserved,
|
||||
base,
|
||||
size,
|
||||
MEMBLOCK_RESERVED);
|
||||
}
|
||||
|
||||
static phys_addr_t do_alloc(size_t size, phys_addr_t align)
|
||||
@@ -245,7 +287,8 @@ static phys_addr_t do_alloc(size_t size, phys_addr_t align)
|
||||
phys_addr_t region_end = memblock.m_alloc_end - memblock.m_voffset;
|
||||
|
||||
struct memblock_iter it;
|
||||
for_each_free_mem_range (&it, region_start, region_end) {
|
||||
for_each_free_mem_range(&it, region_start, region_end)
|
||||
{
|
||||
phys_addr_t base = it.it_base;
|
||||
if (base & (align - 1)) {
|
||||
base &= ~(align - 1);
|
||||
@@ -270,7 +313,11 @@ static phys_addr_t do_alloc(size_t size, phys_addr_t align)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int status = memblock_add_range(&memblock.reserved, allocated_base, allocated_limit - allocated_base, MEMBLOCK_ALLOC);
|
||||
int status = memblock_add_range(
|
||||
&memblock.reserved,
|
||||
allocated_base,
|
||||
allocated_limit - allocated_base,
|
||||
MEMBLOCK_ALLOC);
|
||||
if (status != 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -312,9 +359,11 @@ int memblock_free_phys(phys_addr_t addr, size_t size)
|
||||
}
|
||||
|
||||
void __next_memory_region(
|
||||
struct memblock_iter *it,
|
||||
struct memblock_type *type_a, struct memblock_type *type_b,
|
||||
uintptr_t start, uintptr_t end)
|
||||
struct memblock_iter *it,
|
||||
struct memblock_type *type_a,
|
||||
struct memblock_type *type_b,
|
||||
uintptr_t start,
|
||||
uintptr_t end)
|
||||
{
|
||||
unsigned int idx_a = IDX_A(it->__idx);
|
||||
unsigned int idx_b = IDX_B(it->__idx);
|
||||
@@ -344,70 +393,85 @@ void __next_memory_region(
|
||||
}
|
||||
|
||||
if (m_start > end) {
|
||||
/* we have gone past the requested memory range and can now stop */
|
||||
/* we have gone past the requested memory range and can
|
||||
* now stop */
|
||||
break;
|
||||
}
|
||||
|
||||
for (; idx_b < type_b->count + 1; idx_b++) {
|
||||
struct memblock_region *r = &type_b->regions[idx_b];
|
||||
|
||||
/* r_start and r_end delimit the region of memory between the current and previous reserved regions.
|
||||
if we have gone past the last reserved region, these variables delimit the range between the end
|
||||
of the last reserved region and the end of memory. */
|
||||
/* r_start and r_end delimit the region of memory
|
||||
between the current and previous reserved regions. if
|
||||
we have gone past the last reserved region, these
|
||||
variables delimit the range between the end of the
|
||||
last reserved region and the end of memory. */
|
||||
uintptr_t r_start = idx_b > 0 ? r[-1].limit + 1 : 0;
|
||||
uintptr_t r_end;
|
||||
|
||||
if (idx_b < type_b->count) {
|
||||
r_end = r->base;
|
||||
|
||||
/* we decrement r_end to get the address of the last byte of the free region.
|
||||
if r_end is already zero, there is a reserved region starting at address 0x0.
|
||||
as long as r_end == r_start == 0x00000, we will skip this region. */
|
||||
/* we decrement r_end to get the address of the
|
||||
last byte of the free region. if r_end is
|
||||
already zero, there is a reserved region
|
||||
starting at address 0x0. as long as r_end ==
|
||||
r_start == 0x00000, we will skip this region.
|
||||
*/
|
||||
if (r_end) {
|
||||
r_end--;
|
||||
}
|
||||
} else {
|
||||
/* this maximum value will be clamped to the bounds of memblock.memory
|
||||
before being returned to the caller */
|
||||
/* this maximum value will be clamped to the
|
||||
bounds of memblock.memory before being
|
||||
returned to the caller */
|
||||
r_end = ADDR_MAX;
|
||||
}
|
||||
|
||||
if (r_start >= r_end) {
|
||||
/* this free region has a length of zero, move to the next one */
|
||||
/* this free region has a length of zero, move
|
||||
* to the next one */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r_start >= m_end) {
|
||||
/* we've gone past the end of the current memory region, and need to go to the next one */
|
||||
/* we've gone past the end of the current memory
|
||||
* region, and need to go to the next one */
|
||||
break;
|
||||
}
|
||||
|
||||
/* we've already gone past this free memory region. move to the next one */
|
||||
/* we've already gone past this free memory region. move
|
||||
* to the next one */
|
||||
if (m_start >= r_end) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* we want the area that is overlapped by both
|
||||
region M (m_start - m_end) : The region defined as system memory.
|
||||
region R (r_start - r_end) : The region defined as free / outside of any reserved regions.
|
||||
region M (m_start - m_end) : The region defined
|
||||
as system memory. region R (r_start - r_end) : The
|
||||
region defined as free / outside of any reserved
|
||||
regions.
|
||||
*/
|
||||
it->it_base = MAX(m_start, r_start);
|
||||
it->it_limit = MIN(m_end, r_end);
|
||||
|
||||
/* further limit the region to the intersection between the region itself and the
|
||||
specified iteration bounds */
|
||||
/* further limit the region to the intersection between
|
||||
the region itself and the specified iteration bounds
|
||||
*/
|
||||
it->it_base = MAX(it->it_base, start);
|
||||
it->it_limit = MIN(it->it_limit, end);
|
||||
|
||||
if (it->it_limit <= it->it_base) {
|
||||
/* this region is not part of the specified bounds, skip it. */
|
||||
/* this region is not part of the specified
|
||||
* bounds, skip it. */
|
||||
continue;
|
||||
}
|
||||
|
||||
it->it_status = MEMBLOCK_MEMORY;
|
||||
|
||||
/* whichever region is smaller, increment the pointer for that type, so we can
|
||||
compare the larger region with the next region of the incremented type. */
|
||||
/* whichever region is smaller, increment the pointer
|
||||
for that type, so we can compare the larger region
|
||||
with the next region of the incremented type. */
|
||||
if (m_end <= r_end) {
|
||||
idx_a++;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user