/* mm.c - functions for memory manager */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2005,2007,2008,2009 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/*
The design of this memory manager.
This is a simple implementation of malloc with a few extensions. These are
the extensions:
- memalign is implemented efficiently.
- multiple regions may be used as free space. They may not be
contiguous.
Regions are managed by a singly linked list, and the meta information is
stored in the beginning of each region. Space after the meta information
is used to allocate memory.
The memory space is used as cells instead of bytes for simplicity. This
is important for some CPUs which may not access multiple bytes at a time
when the first byte is not aligned at a certain boundary (typically,
4-byte or 8-byte). The size of each cell is equal to the size of struct
cell precisely. One cell is 16 bytes on 32-bit platforms and 32 bytes
on 64-bit platforms.
There are two types of blocks: allocated blocks and free blocks.
In allocated blocks, the header of each block has only its size. Note that
this size is based on cells but not on bytes. The header is located right
before the returned pointer, that is, the header resides at the previous
cell.
Free blocks constitutes a ring, using a singly linked list. The first free
block is pointed to by the meta information of a region. The allocator
attempts to pick up the second block instead of the first one. This is
a typical optimization against defragmentation, and makes the
implementation a bit easier.
For safety, both allocated blocks and free ones are marked by magic
numbers. Whenever anything unexpected is detected, GRUB aborts the
operation.
*/
#include <config.h>
#include <grub/mm_private.h>
#ifdef MM_DEBUG
#endif
/* Get a header from the pointer PTR, and set *P and *R to a pointer
to the header and a pointer to its region, respectively. PTR must
be allocated. */
static void
{
for (*r = grub_mm_base; *r; *r = (*r)->next)
break;
if (! *r)
if ((*p)->magic != GRUB_MM_ALLOC_MAGIC)
grub_fatal ("alloc magic is broken at %p", *p);
}
/* Initialize a region starting from ADDR and whose size is SIZE,
to use it as free space. */
void
{
grub_mm_region_t r, *p, q;
#if 0
#endif
/* Allocate a region from the head. */
/* If this region is too small, ignore it. */
if (size < GRUB_MM_ALIGN)
return;
h = (grub_mm_header_t) (r + 1);
h->next = h;
h->magic = GRUB_MM_FREE_MAGIC;
r->first = h;
/* Find where to insert this region. Put a smaller one before bigger ones,
to prevent fragmentation. */
for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
break;
*p = r;
r->next = q;
}
/* Allocate the number of units N with the alignment ALIGN from the ring
buffer starting from *FIRST. ALIGN must be a power of two. Both N and
ALIGN are in units of GRUB_MM_ALIGN. Return a non-NULL if successful,
otherwise return NULL. */
static void *
{
grub_mm_header_t p, q;
/* When everything is allocated side effect is that *first will have alloc
magic marked, meaning that there is no room in this region. */
return 0;
/* Try to search free slot for allocation in this memory region. */
{
if (extra)
if (! p)
grub_fatal ("null in the ring");
if (p->magic != GRUB_MM_FREE_MAGIC)
{
{
/* There is no special alignment requirement and memory block
is complete match.
1. Just mark memory block as allocated and remove it from
free list.
Result:
+---------------+ previous block's next
| alloc, size=n | |
+---------------+ v
*/
}
{
/* There might be alignment requirement, when taking it into
account memory block fits in.
1. Allocate new area at end of memory block.
2. Reduce size of available blocks from original node.
3. Mark new area as allocated and "remove" it from free
list.
Result:
+---------------+
| free, size-=n | next --+
+---------------+ |
| alloc, size=n | |
+---------------+ v
*/
p->size -= n;
p += p->size;
}
else if (extra == 0)
{
r = p + extra + n;
r->magic = GRUB_MM_FREE_MAGIC;
q->next = r;
if (q == p)
{
q = r;
r->next = r;
}
}
else
{
/* There is alignment requirement and there is room in memory
block. Split memory block to three pieces.
1. Create new memory block right after section being
allocated. Mark it as free.
2. Add new memory block to free chain.
3. Mark current memory block having only extra blocks.
4. Advance to aligned block and mark that as allocated and
"remove" it from free list.
Result:
+------------------------------+
| free, size=extra | next --+
+------------------------------+ |
| alloc, size=n | |
+------------------------------+ |
| free, size=orig.size-extra-n | <------+, next --+
+------------------------------+ v
*/
r = p + extra + n;
r->magic = GRUB_MM_FREE_MAGIC;
r->next = p;
q->next = r;
p += extra;
}
p->magic = GRUB_MM_ALLOC_MAGIC;
p->size = n;
/* Mark find as a start marker for next allocation to fasten it.
This will have side effect of fragmenting memory as small
pieces before this will be un-used. */
*first = q;
return p + 1;
}
/* Search was completed without result. */
if (p == *first)
break;
}
return 0;
}
/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */
void *
{
int count = 0;
if (!grub_mm_base)
goto fail;
if (align == 0)
align = 1;
for (r = grub_mm_base; r; r = r->next)
{
void *p;
if (p)
return p;
}
/* If failed, increase free memory somehow. */
switch (count)
{
case 0:
/* Invalidate disk caches. */
count++;
goto again;
#if 0
case 1:
/* Unload unneeded modules. */
count++;
goto again;
#endif
default:
break;
}
fail:
return 0;
}
/* Allocate SIZE bytes and return the pointer. */
void *
{
return grub_memalign (0, size);
}
/* Allocate SIZE bytes, clear them and return the pointer. */
void *
{
void *ret;
if (ret)
return ret;
}
/* Deallocate the pointer PTR. */
void
{
if (! ptr)
return;
get_header_from_pointer (ptr, &p, &r);
{
p->magic = GRUB_MM_FREE_MAGIC;
}
else
{
#if 0
q = r->first;
do
{
grub_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n",
q = q->next;
}
while (q != r->first);
#endif
{
if (q->magic != GRUB_MM_FREE_MAGIC)
break;
}
p->magic = GRUB_MM_FREE_MAGIC;
q->next = p;
{
if (p->next == q)
q = p;
}
if (q + q->size == p)
{
p->magic = 0;
}
r->first = q;
}
}
/* Reallocate SIZE bytes and return the pointer. The contents will be
the same as that of PTR. */
void *
{
void *q;
grub_size_t n;
if (! ptr)
return grub_malloc (size);
if (! size)
{
return 0;
}
/* FIXME: Not optimal. */
get_header_from_pointer (ptr, &p, &r);
if (p->size >= n)
return ptr;
q = grub_malloc (size);
if (! q)
return q;
return q;
}
#ifdef MM_DEBUG
int grub_mm_debug = 0;
void
grub_mm_dump_free (void)
{
for (r = grub_mm_base; r; r = r->next)
{
/* Follow the free list. */
p = r->first;
do
{
if (p->magic != GRUB_MM_FREE_MAGIC)
grub_printf ("F:%p:%u:%p\n",
p = p->next;
}
while (p != r->first);
}
grub_printf ("\n");
}
void
{
for (r = grub_mm_base; r; r = r->next)
{
p++)
{
switch (p->magic)
{
case GRUB_MM_FREE_MAGIC:
grub_printf ("F:%p:%u:%p\n",
break;
case GRUB_MM_ALLOC_MAGIC:
break;
}
}
}
grub_printf ("\n");
}
void *
{
void *ptr;
if (grub_mm_debug)
if (grub_mm_debug)
return ptr;
}
void *
{
void *ptr;
if (grub_mm_debug)
if (grub_mm_debug)
return ptr;
}
void
{
if (grub_mm_debug)
}
void *
{
if (grub_mm_debug)
if (grub_mm_debug)
return ptr;
}
void *
{
void *ptr;
if (grub_mm_debug)
if (grub_mm_debug)
return ptr;
}
#endif /* MM_DEBUG */