2004-04-04 Yoshinori K. Okuji <okuji@enbug.org>
All symbols prefixed with PUPA_ and pupa_ are renamed to GRUB_ and grub_, respectively. Because the conversion is trivial and mechanical, I omit the details here. Please refer to the CVS if you need more information.
This commit is contained in:
parent
6a1425510d
commit
4b13b216f4
125 changed files with 6198 additions and 6181 deletions
210
kern/mm.c
210
kern/mm.c
|
@ -1,9 +1,9 @@
|
|||
/* mm.c - functions for memory manager */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
*
|
||||
* PUPA is free software; you can redistribute it and/or modify
|
||||
* GRUB is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
@ -14,66 +14,66 @@
|
|||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PUPA; if not, write to the Free Software
|
||||
* along with GRUB; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <pupa/mm.h>
|
||||
#include <pupa/misc.h>
|
||||
#include <pupa/err.h>
|
||||
#include <pupa/types.h>
|
||||
#include <pupa/disk.h>
|
||||
#include <pupa/dl.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/disk.h>
|
||||
#include <grub/dl.h>
|
||||
|
||||
/* Magic words. */
|
||||
#define PUPA_MM_FREE_MAGIC 0x2d3c2808
|
||||
#define PUPA_MM_ALLOC_MAGIC 0x6db08fa4
|
||||
#define GRUB_MM_FREE_MAGIC 0x2d3c2808
|
||||
#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
|
||||
|
||||
typedef struct pupa_mm_header
|
||||
typedef struct grub_mm_header
|
||||
{
|
||||
struct pupa_mm_header *next;
|
||||
pupa_size_t size;
|
||||
pupa_size_t magic;
|
||||
#if PUPA_CPU_SIZEOF_VOID_P == 4
|
||||
struct grub_mm_header *next;
|
||||
grub_size_t size;
|
||||
grub_size_t magic;
|
||||
#if GRUB_CPU_SIZEOF_VOID_P == 4
|
||||
char padding[4];
|
||||
#elif PUPA_CPU_SIZEOF_VOID_P == 8
|
||||
#elif GRUB_CPU_SIZEOF_VOID_P == 8
|
||||
char padding[8];
|
||||
#else
|
||||
# error "unknown word size"
|
||||
#endif
|
||||
}
|
||||
*pupa_mm_header_t;
|
||||
*grub_mm_header_t;
|
||||
|
||||
#if PUPA_CPU_SIZEOF_VOID_P == 4
|
||||
# define PUPA_MM_ALIGN_LOG2 4
|
||||
#elif PUPA_CPU_SIZEOF_VOID_P == 8
|
||||
# define PUPA_MM_ALIGN_LOG2 8
|
||||
#if GRUB_CPU_SIZEOF_VOID_P == 4
|
||||
# define GRUB_MM_ALIGN_LOG2 4
|
||||
#elif GRUB_CPU_SIZEOF_VOID_P == 8
|
||||
# define GRUB_MM_ALIGN_LOG2 8
|
||||
#endif
|
||||
|
||||
#define PUPA_MM_ALIGN (1 << PUPA_MM_ALIGN_LOG2)
|
||||
#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
|
||||
|
||||
typedef struct pupa_mm_region
|
||||
typedef struct grub_mm_region
|
||||
{
|
||||
struct pupa_mm_header *first;
|
||||
struct pupa_mm_region *next;
|
||||
pupa_addr_t addr;
|
||||
pupa_size_t size;
|
||||
struct grub_mm_header *first;
|
||||
struct grub_mm_region *next;
|
||||
grub_addr_t addr;
|
||||
grub_size_t size;
|
||||
}
|
||||
*pupa_mm_region_t;
|
||||
*grub_mm_region_t;
|
||||
|
||||
|
||||
|
||||
static pupa_mm_region_t base;
|
||||
static grub_mm_region_t base;
|
||||
|
||||
/* 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
|
||||
get_header_from_pointer (void *ptr, pupa_mm_header_t *p, pupa_mm_region_t *r)
|
||||
get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)
|
||||
{
|
||||
if ((unsigned) ptr & (PUPA_MM_ALIGN - 1))
|
||||
pupa_fatal ("unaligned pointer %p", ptr);
|
||||
if ((unsigned) ptr & (GRUB_MM_ALIGN - 1))
|
||||
grub_fatal ("unaligned pointer %p", ptr);
|
||||
|
||||
for (*r = base; *r; *r = (*r)->next)
|
||||
if ((unsigned) ptr > (*r)->addr
|
||||
|
@ -81,42 +81,42 @@ get_header_from_pointer (void *ptr, pupa_mm_header_t *p, pupa_mm_region_t *r)
|
|||
break;
|
||||
|
||||
if (! *r)
|
||||
pupa_fatal ("out of range pointer %p", ptr);
|
||||
grub_fatal ("out of range pointer %p", ptr);
|
||||
|
||||
*p = (pupa_mm_header_t) ptr - 1;
|
||||
if ((*p)->magic != PUPA_MM_ALLOC_MAGIC)
|
||||
pupa_fatal ("alloc magic is broken at %p", *p);
|
||||
*p = (grub_mm_header_t) ptr - 1;
|
||||
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
|
||||
pupa_mm_init_region (void *addr, pupa_size_t size)
|
||||
grub_mm_init_region (void *addr, grub_size_t size)
|
||||
{
|
||||
pupa_mm_header_t h;
|
||||
pupa_mm_region_t r, *p, q;
|
||||
grub_mm_header_t h;
|
||||
grub_mm_region_t r, *p, q;
|
||||
|
||||
#if 0
|
||||
pupa_printf ("%s:%d: addr=%p, size=%u\n", __FILE__, __LINE__, addr, size);
|
||||
grub_printf ("%s:%d: addr=%p, size=%u\n", __FILE__, __LINE__, addr, size);
|
||||
#endif
|
||||
|
||||
/* If this region is too small, ignore it. */
|
||||
if (size < PUPA_MM_ALIGN * 2)
|
||||
if (size < GRUB_MM_ALIGN * 2)
|
||||
return;
|
||||
|
||||
/* Allocate a region from the head. */
|
||||
r = (pupa_mm_region_t) (((pupa_addr_t) addr + PUPA_MM_ALIGN - 1)
|
||||
& (~(PUPA_MM_ALIGN - 1)));
|
||||
r = (grub_mm_region_t) (((grub_addr_t) addr + GRUB_MM_ALIGN - 1)
|
||||
& (~(GRUB_MM_ALIGN - 1)));
|
||||
size -= (char *) r - (char *) addr + sizeof (*r);
|
||||
|
||||
h = (pupa_mm_header_t) ((char *) r + PUPA_MM_ALIGN);
|
||||
h = (grub_mm_header_t) ((char *) r + GRUB_MM_ALIGN);
|
||||
h->next = h;
|
||||
h->magic = PUPA_MM_FREE_MAGIC;
|
||||
h->size = (size >> PUPA_MM_ALIGN_LOG2);
|
||||
h->magic = GRUB_MM_FREE_MAGIC;
|
||||
h->size = (size >> GRUB_MM_ALIGN_LOG2);
|
||||
|
||||
r->first = h;
|
||||
r->addr = (pupa_addr_t) h;
|
||||
r->size = (h->size << PUPA_MM_ALIGN_LOG2);
|
||||
r->addr = (grub_addr_t) h;
|
||||
r->size = (h->size << GRUB_MM_ALIGN_LOG2);
|
||||
|
||||
/* Find where to insert this region. Put a smaller one before bigger ones,
|
||||
to prevent fragmentations. */
|
||||
|
@ -132,47 +132,47 @@ pupa_mm_init_region (void *addr, pupa_size_t size)
|
|||
buffer starting from *FIRST. ALIGN must be a power of two. Return a
|
||||
non-NULL if successful, otherwise return NULL. */
|
||||
static void *
|
||||
pupa_real_malloc (pupa_mm_header_t *first, pupa_size_t n, pupa_size_t align)
|
||||
grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
|
||||
{
|
||||
pupa_mm_header_t p, q;
|
||||
grub_mm_header_t p, q;
|
||||
|
||||
if ((*first)->magic == PUPA_MM_ALLOC_MAGIC)
|
||||
if ((*first)->magic == GRUB_MM_ALLOC_MAGIC)
|
||||
return 0;
|
||||
|
||||
for (q = *first, p = q->next; ; q = p, p = p->next)
|
||||
{
|
||||
pupa_off_t extra;
|
||||
grub_off_t extra;
|
||||
|
||||
extra = ((pupa_addr_t) (p + 1) >> PUPA_MM_ALIGN_LOG2) % align;
|
||||
extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align;
|
||||
if (extra)
|
||||
extra = align - extra;
|
||||
|
||||
if (! p)
|
||||
pupa_fatal ("null in the ring");
|
||||
grub_fatal ("null in the ring");
|
||||
|
||||
if (p->magic != PUPA_MM_FREE_MAGIC)
|
||||
pupa_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
|
||||
if (p->magic != GRUB_MM_FREE_MAGIC)
|
||||
grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
|
||||
|
||||
if (p->size >= n + extra)
|
||||
{
|
||||
if (extra == 0 && p->size == n)
|
||||
{
|
||||
q->next = p->next;
|
||||
p->magic = PUPA_MM_ALLOC_MAGIC;
|
||||
p->magic = GRUB_MM_ALLOC_MAGIC;
|
||||
}
|
||||
else if (extra == 0 || p->size == n + extra)
|
||||
{
|
||||
p->size -= n;
|
||||
p += p->size;
|
||||
p->size = n;
|
||||
p->magic = PUPA_MM_ALLOC_MAGIC;
|
||||
p->magic = GRUB_MM_ALLOC_MAGIC;
|
||||
}
|
||||
else
|
||||
{
|
||||
pupa_mm_header_t r;
|
||||
grub_mm_header_t r;
|
||||
|
||||
r = p + extra + n;
|
||||
r->magic = PUPA_MM_FREE_MAGIC;
|
||||
r->magic = GRUB_MM_FREE_MAGIC;
|
||||
r->size = p->size - extra - n;
|
||||
r->next = p->next;
|
||||
|
||||
|
@ -180,7 +180,7 @@ pupa_real_malloc (pupa_mm_header_t *first, pupa_size_t n, pupa_size_t align)
|
|||
p->next = r;
|
||||
p += extra;
|
||||
p->size = n;
|
||||
p->magic = PUPA_MM_ALLOC_MAGIC;
|
||||
p->magic = GRUB_MM_ALLOC_MAGIC;
|
||||
}
|
||||
|
||||
*first = q;
|
||||
|
@ -197,13 +197,13 @@ pupa_real_malloc (pupa_mm_header_t *first, pupa_size_t n, pupa_size_t align)
|
|||
|
||||
/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */
|
||||
void *
|
||||
pupa_memalign (pupa_size_t align, pupa_size_t size)
|
||||
grub_memalign (grub_size_t align, grub_size_t size)
|
||||
{
|
||||
pupa_mm_region_t r;
|
||||
pupa_size_t n = ((size + PUPA_MM_ALIGN - 1) >> PUPA_MM_ALIGN_LOG2) + 1;
|
||||
grub_mm_region_t r;
|
||||
grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
|
||||
int count = 0;
|
||||
|
||||
align = (align >> PUPA_MM_ALIGN_LOG2);
|
||||
align = (align >> GRUB_MM_ALIGN_LOG2);
|
||||
if (align == 0)
|
||||
align = 1;
|
||||
|
||||
|
@ -213,7 +213,7 @@ pupa_memalign (pupa_size_t align, pupa_size_t size)
|
|||
{
|
||||
void *p;
|
||||
|
||||
p = pupa_real_malloc (&(r->first), n, align);
|
||||
p = grub_real_malloc (&(r->first), n, align);
|
||||
if (p)
|
||||
return p;
|
||||
}
|
||||
|
@ -223,13 +223,13 @@ pupa_memalign (pupa_size_t align, pupa_size_t size)
|
|||
{
|
||||
case 0:
|
||||
/* Invalidate disk caches. */
|
||||
pupa_disk_cache_invalidate_all ();
|
||||
grub_disk_cache_invalidate_all ();
|
||||
count++;
|
||||
goto again;
|
||||
|
||||
case 1:
|
||||
/* Unload unneeded modules. */
|
||||
pupa_dl_unload_unneeded ();
|
||||
grub_dl_unload_unneeded ();
|
||||
count++;
|
||||
goto again;
|
||||
|
||||
|
@ -237,43 +237,43 @@ pupa_memalign (pupa_size_t align, pupa_size_t size)
|
|||
break;
|
||||
}
|
||||
|
||||
pupa_error (PUPA_ERR_OUT_OF_MEMORY, "out of memory");
|
||||
grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocate SIZE bytes and return the pointer. */
|
||||
void *
|
||||
pupa_malloc (pupa_size_t size)
|
||||
grub_malloc (grub_size_t size)
|
||||
{
|
||||
return pupa_memalign (0, size);
|
||||
return grub_memalign (0, size);
|
||||
}
|
||||
|
||||
/* Deallocate the pointer PTR. */
|
||||
void
|
||||
pupa_free (void *ptr)
|
||||
grub_free (void *ptr)
|
||||
{
|
||||
pupa_mm_header_t p;
|
||||
pupa_mm_region_t r;
|
||||
grub_mm_header_t p;
|
||||
grub_mm_region_t r;
|
||||
|
||||
if (! ptr)
|
||||
return;
|
||||
|
||||
get_header_from_pointer (ptr, &p, &r);
|
||||
|
||||
if (r->first->magic == PUPA_MM_ALLOC_MAGIC)
|
||||
if (r->first->magic == GRUB_MM_ALLOC_MAGIC)
|
||||
{
|
||||
p->magic = PUPA_MM_FREE_MAGIC;
|
||||
p->magic = GRUB_MM_FREE_MAGIC;
|
||||
r->first = p->next = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
pupa_mm_header_t q;
|
||||
grub_mm_header_t q;
|
||||
|
||||
#if 0
|
||||
q = r->first;
|
||||
do
|
||||
{
|
||||
pupa_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n",
|
||||
grub_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n",
|
||||
__FILE__, __LINE__, q, q->size, q->magic);
|
||||
q = q->next;
|
||||
}
|
||||
|
@ -282,14 +282,14 @@ pupa_free (void *ptr)
|
|||
|
||||
for (q = r->first; q >= p || q->next <= p; q = q->next)
|
||||
{
|
||||
if (q->magic != PUPA_MM_FREE_MAGIC)
|
||||
pupa_fatal ("free magic is broken at %p: 0x%x", q, q->magic);
|
||||
if (q->magic != GRUB_MM_FREE_MAGIC)
|
||||
grub_fatal ("free magic is broken at %p: 0x%x", q, q->magic);
|
||||
|
||||
if (q >= q->next && (q < p || q->next > p))
|
||||
break;
|
||||
}
|
||||
|
||||
p->magic = PUPA_MM_FREE_MAGIC;
|
||||
p->magic = GRUB_MM_FREE_MAGIC;
|
||||
p->next = q->next;
|
||||
q->next = p;
|
||||
|
||||
|
@ -314,67 +314,67 @@ pupa_free (void *ptr)
|
|||
/* Reallocate SIZE bytes and return the pointer. The contents will be
|
||||
the same as that of PTR. */
|
||||
void *
|
||||
pupa_realloc (void *ptr, pupa_size_t size)
|
||||
grub_realloc (void *ptr, grub_size_t size)
|
||||
{
|
||||
pupa_mm_header_t p;
|
||||
pupa_mm_region_t r;
|
||||
grub_mm_header_t p;
|
||||
grub_mm_region_t r;
|
||||
void *q;
|
||||
pupa_size_t n;
|
||||
grub_size_t n;
|
||||
|
||||
if (! ptr)
|
||||
return pupa_malloc (size);
|
||||
return grub_malloc (size);
|
||||
|
||||
if (! size)
|
||||
{
|
||||
pupa_free (ptr);
|
||||
grub_free (ptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* FIXME: Not optimal. */
|
||||
n = ((size + PUPA_MM_ALIGN - 1) >> PUPA_MM_ALIGN_LOG2) + 1;
|
||||
n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
|
||||
get_header_from_pointer (ptr, &p, &r);
|
||||
|
||||
if (p->size >= n)
|
||||
return p;
|
||||
|
||||
q = pupa_malloc (size);
|
||||
q = grub_malloc (size);
|
||||
if (! q)
|
||||
return q;
|
||||
|
||||
pupa_memcpy (q, ptr, size);
|
||||
pupa_free (ptr);
|
||||
grub_memcpy (q, ptr, size);
|
||||
grub_free (ptr);
|
||||
return q;
|
||||
}
|
||||
|
||||
#if MM_DEBUG
|
||||
void
|
||||
pupa_mm_dump (unsigned lineno)
|
||||
grub_mm_dump (unsigned lineno)
|
||||
{
|
||||
pupa_mm_region_t r;
|
||||
grub_mm_region_t r;
|
||||
|
||||
pupa_printf ("called at line %u\n", lineno);
|
||||
grub_printf ("called at line %u\n", lineno);
|
||||
for (r = base; r; r = r->next)
|
||||
{
|
||||
pupa_mm_header_t p;
|
||||
grub_mm_header_t p;
|
||||
|
||||
for (p = (pupa_mm_header_t) ((r->addr + PUPA_MM_ALIGN - 1)
|
||||
& (~(PUPA_MM_ALIGN - 1)));
|
||||
(pupa_addr_t) p < r->addr + r->size;
|
||||
for (p = (grub_mm_header_t) ((r->addr + GRUB_MM_ALIGN - 1)
|
||||
& (~(GRUB_MM_ALIGN - 1)));
|
||||
(grub_addr_t) p < r->addr + r->size;
|
||||
p++)
|
||||
{
|
||||
switch (p->magic)
|
||||
{
|
||||
case PUPA_MM_FREE_MAGIC:
|
||||
pupa_printf ("F:%p:%u:%p\n",
|
||||
p, p->size << PUPA_MM_ALIGN_LOG2, p->next);
|
||||
case GRUB_MM_FREE_MAGIC:
|
||||
grub_printf ("F:%p:%u:%p\n",
|
||||
p, p->size << GRUB_MM_ALIGN_LOG2, p->next);
|
||||
break;
|
||||
case PUPA_MM_ALLOC_MAGIC:
|
||||
pupa_printf ("A:%p:%u\n", p, p->size << PUPA_MM_ALIGN_LOG2);
|
||||
case GRUB_MM_ALLOC_MAGIC:
|
||||
grub_printf ("A:%p:%u\n", p, p->size << GRUB_MM_ALIGN_LOG2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pupa_printf ("\n");
|
||||
grub_printf ("\n");
|
||||
}
|
||||
#endif /* MM_DEBUG */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue