mirror of
https://git.savannah.gnu.org/git/grub.git
synced 2026-10-29 18:49:22 +00:00
mm: Document grub_mm_init_region()
The grub_mm_init_region() does some things that seem magical, especially around region merging. Make it a bit clearer. Signed-off-by: Daniel Axtens <dja@axtens.net> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
1 parent
1f8d0b0173
commit
246d69b7ea
1 file changed
+29
@@ -128,23 +128,52 @@ grub_mm_init_region (void *addr, grub_size_t size)
|
|||||||
if (((grub_addr_t) addr + 0x1000) > ~(grub_addr_t) size)
|
if (((grub_addr_t) addr + 0x1000) > ~(grub_addr_t) size)
|
||||||
size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
|
size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
|
||||||
|
|
||||||
|
/* Attempt to merge this region with every existing region */
|
||||||
for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
|
for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
|
||||||
|
/*
|
||||||
|
* Is the new region immediately below an existing region? That
|
||||||
|
* is, is the address of the memory we're adding now (addr) + size
|
||||||
|
* of the memory we're adding (size) + the bytes we couldn't use
|
||||||
|
* at the start of the region we're considering (q->pre_size)
|
||||||
|
* equal to the address of q? In other words, does the memory
|
||||||
|
* looks like this?
|
||||||
|
*
|
||||||
|
* addr q
|
||||||
|
* |----size-----|-q->pre_size-|<q region>|
|
||||||
|
*/
|
||||||
if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
|
if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* Yes, we can merge the memory starting at addr into the
|
||||||
|
* existing region from below. Align up addr to GRUB_MM_ALIGN
|
||||||
|
* so that our new region has proper alignment.
|
||||||
|
*/
|
||||||
r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
|
r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
|
||||||
|
/* Copy the region data across */
|
||||||
*r = *q;
|
*r = *q;
|
||||||
|
/* Consider all the new size as pre-size */
|
||||||
r->pre_size += size;
|
r->pre_size += size;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we have enough pre-size to create a block, create a
|
||||||
|
* block with it. Mark it as allocated and pass it to
|
||||||
|
* grub_free (), which will sort out getting it into the free
|
||||||
|
* list.
|
||||||
|
*/
|
||||||
if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
|
if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
|
||||||
{
|
{
|
||||||
h = (grub_mm_header_t) (r + 1);
|
h = (grub_mm_header_t) (r + 1);
|
||||||
|
/* block size is pre-size converted to cells */
|
||||||
h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
|
h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
|
||||||
h->magic = GRUB_MM_ALLOC_MAGIC;
|
h->magic = GRUB_MM_ALLOC_MAGIC;
|
||||||
|
/* region size grows by block size converted back to bytes */
|
||||||
r->size += h->size << GRUB_MM_ALIGN_LOG2;
|
r->size += h->size << GRUB_MM_ALIGN_LOG2;
|
||||||
|
/* adjust pre_size to be accurate */
|
||||||
r->pre_size &= (GRUB_MM_ALIGN - 1);
|
r->pre_size &= (GRUB_MM_ALIGN - 1);
|
||||||
*p = r;
|
*p = r;
|
||||||
grub_free (h + 1);
|
grub_free (h + 1);
|
||||||
}
|
}
|
||||||
|
/* Replace the old region with the new region */
|
||||||
*p = r;
|
*p = r;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user