sandbox: memblock: add documentation and license

This commit is contained in:
2023-01-31 13:11:35 +00:00
parent caf4d2b969
commit 30d9c011f3
2 changed files with 245 additions and 15 deletions

View File

@@ -1,3 +1,24 @@
/*
The Clear BSD License
Copyright (c) 2023 Max Wash
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
*/
#include "socks/types.h"
#include <stdio.h>
#include <stdbool.h>
@@ -15,8 +36,8 @@
#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. */
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)
static memblock_region_t init_memory_regions[MEMBLOCK_INIT_MEMORY_REGION_COUNT];
@@ -115,7 +136,7 @@ int memblock_add_range(memblock_type_t *type, uintptr_t base, size_t size, membl
memblock_region_t 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. */
to make sure the region being added doesn't violate this rule. */
for (unsigned int i = 0; i < type->count; i++) {
memblock_region_t *cur_region = &type->regions[i];
@@ -153,7 +174,7 @@ int memblock_add_range(memblock_type_t *type, uintptr_t base, size_t size, membl
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. */
decrementing i means we'll stay at the current index and process this region. */
i--;
continue;
}
@@ -162,9 +183,9 @@ int memblock_add_range(memblock_type_t *type, uintptr_t base, size_t size, membl
/* 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. */
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;
@@ -300,8 +321,8 @@ void __next_memory_region(memblock_iter_t *it, memblock_type_t *type_a, memblock
memblock_region_t *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. */
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;
@@ -336,14 +357,14 @@ void __next_memory_region(memblock_iter_t *it, memblock_type_t *type_a, memblock
}
/* 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 */
specified iteration bounds */
it->it_base = MAX(it->it_base, start);
it->it_limit = MIN(it->it_limit, end);
@@ -355,7 +376,7 @@ void __next_memory_region(memblock_iter_t *it, memblock_type_t *type_a, memblock
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. */
compare the larger region with the next region of the incremented type. */
if (m_end <= r_end) {
idx_a++;
} else {