grub/kern/mm.c
okuji 5a79f472c7 2007-07-22 Yoshinori K. Okuji <okuji@enbug.org>
Migrate to GNU General Public License Version 3.

    * COPYING: Replaced with the plain text version of GPLv3.

    * config.guess: Updated from gnulib.
    * config.sub: Likewise.

    * geninit.sh: Output a GPLv3 copyright notice.
    * geninitheader.sh: Likewise.
    * genmodsrc.sh: Likewise.
    * gensymlist.sh.in: Likewise.

    * boot/i386/pc/boot.S: Upgraded to GPLv3.
    * boot/i386/pc/diskboot.S: Likewise.
    * boot/i386/pc/pxeboot.S: Likewise.
    * commands/blocklist.c: Likewise.
    * commands/boot.c: Likewise.
    * commands/cat.c: Likewise.
    * commands/cmp.c: Likewise.
    * commands/configfile.c: Likewise.
    * commands/echo.c: Likewise.
    * commands/help.c: Likewise.
    * commands/ls.c: Likewise.
    * commands/search.c: Likewise.
    * commands/terminal.c: Likewise.
    * commands/test.c: Likewise.
    * commands/videotest.c: Likewise.
    * commands/i386/cpuid.c: Likewise.
    * commands/i386/pc/halt.c: Likewise.
    * commands/i386/pc/play.c: Likewise.
    * commands/i386/pc/reboot.c: Likewise.
    * commands/i386/pc/vbeinfo.c: Likewise.
    * commands/i386/pc/vbetest.c: Likewise.
    * commands/ieee1275/halt.c: Likewise.
    * commands/ieee1275/reboot.c: Likewise.
    * commands/ieee1275/suspend.c: Likewise.
    * disk/loopback.c: Likewise.
    * disk/lvm.c: Likewise.
    * disk/raid.c: Likewise.
    * disk/efi/efidisk.c: Likewise.
    * disk/i386/pc/biosdisk.c: Likewise.
    * disk/ieee1275/ofdisk.c: Likewise.
    * font/manager.c: Likewise.
    * fs/affs.c: Likewise.
    * fs/ext2.c: Likewise.
    * fs/fat.c: Likewise.
    * fs/fshelp.c: Likewise.
    * fs/hfs.c: Likewise.
    * fs/hfsplus.c: Likewise.
    * fs/iso9660.c: Likewise.
    * fs/jfs.c: Likewise.
    * fs/minix.c: Likewise.
    * fs/sfs.c: Likewise.
    * fs/ufs.c: Likewise.
    * fs/xfs.c: Likewise.
    * hello/hello.c: Likewise.
    * include/grub/acorn_filecore.h: Likewise.
    * include/grub/arg.h: Likewise.
    * include/grub/bitmap.h: Likewise.
    * include/grub/boot.h: Likewise.
    * include/grub/cache.h: Likewise.
    * include/grub/device.h: Likewise.
    * include/grub/disk.h: Likewise.
    * include/grub/dl.h: Likewise.
    * include/grub/elfload.h: Likewise.
    * include/grub/env.h: Likewise.
    * include/grub/err.h: Likewise.
    * include/grub/file.h: Likewise.
    * include/grub/font.h: Likewise.
    * include/grub/fs.h: Likewise.
    * include/grub/fshelp.h: Likewise.
    * include/grub/gzio.h: Likewise.
    * include/grub/hfs.h: Likewise.
    * include/grub/kernel.h: Likewise.
    * include/grub/loader.h: Likewise.
    * include/grub/lvm.h: Likewise.
    * include/grub/misc.h: Likewise.
    * include/grub/mm.h: Likewise.
    * include/grub/net.h: Likewise.
    * include/grub/normal.h: Likewise.
    * include/grub/parser.h: Likewise.
    * include/grub/partition.h: Likewise.
    * include/grub/pc_partition.h: Likewise.
    * include/grub/raid.h: Likewise.
    * include/grub/rescue.h: Likewise.
    * include/grub/script.h: Likewise.
    * include/grub/setjmp.h: Likewise.
    * include/grub/symbol.h: Likewise.
    * include/grub/term.h: Likewise.
    * include/grub/terminfo.h: Likewise.
    * include/grub/tparm.h: Likewise.
    * include/grub/types.h: Likewise.
    * include/grub/video.h: Likewise.
    * include/grub/efi/api.h: Likewise.
    * include/grub/efi/chainloader.h: Likewise.
    * include/grub/efi/console.h: Likewise.
    * include/grub/efi/console_control.h: Likewise.
    * include/grub/efi/disk.h: Likewise.
    * include/grub/efi/efi.h: Likewise.
    * include/grub/efi/pe32.h: Likewise.
    * include/grub/efi/time.h: Likewise.
    * include/grub/i386/linux.h: Likewise.
    * include/grub/i386/setjmp.h: Likewise.
    * include/grub/i386/types.h: Likewise.
    * include/grub/i386/efi/kernel.h: Likewise.
    * include/grub/i386/efi/loader.h: Likewise.
    * include/grub/i386/efi/time.h: Likewise.
    * include/grub/i386/pc/biosdisk.h: Likewise.
    * include/grub/i386/pc/boot.h: Likewise.
    * include/grub/i386/pc/chainloader.h: Likewise.
    * include/grub/i386/pc/console.h: Likewise.
    * include/grub/i386/pc/init.h: Likewise.
    * include/grub/i386/pc/kernel.h: Likewise.
    * include/grub/i386/pc/loader.h: Likewise.
    * include/grub/i386/pc/memory.h: Likewise.
    * include/grub/i386/pc/multiboot.h: Likewise.
    * include/grub/i386/pc/serial.h: Likewise.
    * include/grub/i386/pc/time.h: Likewise.
    * include/grub/i386/pc/vbe.h: Likewise.
    * include/grub/i386/pc/vbeblit.h: Likewise.
    * include/grub/i386/pc/vbefill.h: Likewise.
    * include/grub/i386/pc/vbeutil.h: Likewise.
    * include/grub/i386/pc/vga.h: Likewise.
    * include/grub/ieee1275/ieee1275.h: Likewise.
    * include/grub/ieee1275/ofdisk.h: Likewise.
    * include/grub/powerpc/libgcc.h: Likewise.
    * include/grub/powerpc/setjmp.h: Likewise.
    * include/grub/powerpc/types.h: Likewise.
    * include/grub/powerpc/ieee1275/biosdisk.h: Likewise.
    * include/grub/powerpc/ieee1275/console.h: Likewise.
    * include/grub/powerpc/ieee1275/ieee1275.h: Likewise.
    * include/grub/powerpc/ieee1275/kernel.h: Likewise.
    * include/grub/powerpc/ieee1275/loader.h: Likewise.
    * include/grub/powerpc/ieee1275/multiboot.h: Likewise.
    * include/grub/powerpc/ieee1275/time.h: Likewise.
    * include/grub/powerpc/ieee1275/util/biosdisk.h: Likewise.
    * include/grub/sparc64/libgcc.h: Likewise.
    * include/grub/sparc64/setjmp.h: Likewise.
    * include/grub/sparc64/types.h: Likewise.
    * include/grub/sparc64/ieee1275/console.h: Likewise.
    * include/grub/sparc64/ieee1275/ieee1275.h: Likewise.
    * include/grub/sparc64/ieee1275/kernel.h: Likewise.
    * include/grub/sparc64/ieee1275/time.h: Likewise.
    * include/grub/util/biosdisk.h: Likewise.
    * include/grub/util/getroot.h: Likewise.
    * include/grub/util/lvm.h: Likewise.
    * include/grub/util/misc.h: Likewise.
    * include/grub/util/raid.h: Likewise.
    * include/grub/util/resolve.h: Likewise.
    * io/gzio.c: Likewise.
    * kern/device.c: Likewise.
    * kern/disk.c: Likewise.
    * kern/dl.c: Likewise.
    * kern/elf.c: Likewise.
    * kern/env.c: Likewise.
    * kern/err.c: Likewise.
    * kern/file.c: Likewise.
    * kern/fs.c: Likewise.
    * kern/loader.c: Likewise.
    * kern/main.c: Likewise.
    * kern/misc.c: Likewise.
    * kern/mm.c: Likewise.
    * kern/parser.c: Likewise.
    * kern/partition.c: Likewise.
    * kern/rescue.c: Likewise.
    * kern/term.c: Likewise.
    * kern/efi/efi.c: Likewise.
    * kern/efi/init.c: Likewise.
    * kern/efi/mm.c: Likewise.
    * kern/i386/dl.c: Likewise.
    * kern/i386/efi/init.c: Likewise.
    * kern/i386/efi/startup.S: Likewise.
    * kern/i386/pc/init.c: Likewise.
    * kern/i386/pc/lzo1x.S: Likewise.
    * kern/i386/pc/startup.S: Likewise.
    * kern/ieee1275/ieee1275.c: Likewise.
    * kern/powerpc/cache.S: Likewise.
    * kern/powerpc/dl.c: Likewise.
    * kern/powerpc/ieee1275/cmain.c: Likewise.
    * kern/powerpc/ieee1275/crt0.S: Likewise.
    * kern/powerpc/ieee1275/init.c: Likewise.
    * kern/powerpc/ieee1275/openfw.c: Likewise.
    * kern/sparc64/cache.S: Likewise.
    * kern/sparc64/dl.c: Likewise.
    * kern/sparc64/ieee1275/init.c: Likewise.
    * kern/sparc64/ieee1275/openfw.c: Likewise.
    * loader/efi/chainloader.c: Likewise.
    * loader/efi/chainloader_normal.c: Likewise.
    * loader/i386/efi/linux.c: Likewise.
    * loader/i386/efi/linux_normal.c: Likewise.
    * loader/i386/pc/chainloader.c: Likewise.
    * loader/i386/pc/chainloader_normal.c: Likewise.
    * loader/i386/pc/linux.c: Likewise.
    * loader/i386/pc/linux_normal.c: Likewise.
    * loader/i386/pc/multiboot.c: Likewise.
    * loader/i386/pc/multiboot_normal.c: Likewise.
    * loader/powerpc/ieee1275/linux.c: Likewise.
    * loader/powerpc/ieee1275/linux_normal.c: Likewise.
    * normal/arg.c: Likewise.
    * normal/cmdline.c: Likewise.
    * normal/command.c: Likewise.
    * normal/completion.c: Likewise.
    * normal/execute.c: Likewise.
    * normal/function.c: Likewise.
    * normal/lexer.c: Likewise.
    * normal/main.c: Likewise.
    * normal/menu.c: Likewise.
    * normal/menu_entry.c: Likewise.
    * normal/misc.c: Likewise.
    * normal/parser.y: Likewise.
    * normal/script.c: Likewise.
    * normal/i386/setjmp.S: Likewise.
    * normal/powerpc/setjmp.S: Likewise.
    * normal/sparc64/setjmp.S: Likewise.
    * partmap/acorn.c: Likewise.
    * partmap/amiga.c: Likewise.
    * partmap/apple.c: Likewise.
    * partmap/gpt.c: Likewise.
    * partmap/pc.c: Likewise.
    * partmap/sun.c: Likewise.
    * term/gfxterm.c: Likewise.
    * term/terminfo.c: Likewise.
    * term/efi/console.c: Likewise.
    * term/i386/pc/console.c: Likewise.
    * term/i386/pc/serial.c: Likewise.
    * term/i386/pc/vesafb.c: Likewise.
    * term/i386/pc/vga.c: Likewise.
    * term/ieee1275/ofconsole.c: Likewise.
    * util/biosdisk.c: Likewise.
    * util/console.c: Likewise.
    * util/genmoddep.c: Likewise.
    * util/getroot.c: Likewise.
    * util/grub-emu.c: Likewise.
    * util/grub-mkdevicemap.c: Likewise.
    * util/grub-probe.c: Likewise.
    * util/lvm.c: Likewise.
    * util/misc.c: Likewise.
    * util/raid.c: Likewise.
    * util/resolve.c: Likewise.
    * util/update-grub.in: Likewise.
    * util/update-grub_lib.in: Likewise.
    * util/grub.d/00_header.in: Likewise.
    * util/grub.d/10_hurd.in: Likewise.
    * util/grub.d/10_linux.in: Likewise.
    * util/i386/efi/grub-install.in: Likewise.
    * util/i386/efi/grub-mkimage.c: Likewise.
    * util/i386/pc/grub-install.in: Likewise.
    * util/i386/pc/grub-mkimage.c: Likewise.
    * util/i386/pc/grub-mkrescue.in: Likewise.
    * util/i386/pc/grub-setup.c: Likewise.
    * util/i386/pc/misc.c: Likewise.
    * util/powerpc/ieee1275/grub-install.in: Likewise.
    * util/powerpc/ieee1275/grub-mkimage.c: Likewise.
    * util/powerpc/ieee1275/misc.c: Likewise.
    * video/bitmap.c: Likewise.
    * video/video.c: Likewise.
    * video/i386/pc/vbe.c: Likewise.
    * video/i386/pc/vbeblit.c: Likewise.
    * video/i386/pc/vbefill.c: Likewise.
    * video/i386/pc/vbeutil.c: Likewise.
    * video/readers/tga.c: Likewise.
2007-07-21 23:32:33 +00:00

504 lines
12 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* mm.c - functions for memory manager */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2005,2007 Free Software Foundation, Inc.
*
* 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 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
grub_mm_header, so the header of each allocated/free block fits into one
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.h>
#include <grub/misc.h>
#include <grub/err.h>
#include <grub/types.h>
#include <grub/disk.h>
#include <grub/dl.h>
#ifdef MM_DEBUG
# undef grub_malloc
# undef grub_realloc
# undef grub_free
# undef grub_memalign
#endif
/* Magic words. */
#define GRUB_MM_FREE_MAGIC 0x2d3c2808
#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
typedef struct grub_mm_header
{
struct grub_mm_header *next;
grub_size_t size;
grub_size_t magic;
#if GRUB_CPU_SIZEOF_VOID_P == 4
char padding[4];
#elif GRUB_CPU_SIZEOF_VOID_P == 8
char padding[8];
#else
# error "unknown word size"
#endif
}
*grub_mm_header_t;
#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 5
#endif
#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
typedef struct grub_mm_region
{
struct grub_mm_header *first;
struct grub_mm_region *next;
grub_addr_t addr;
grub_size_t size;
}
*grub_mm_region_t;
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, grub_mm_header_t *p, grub_mm_region_t *r)
{
if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1))
grub_fatal ("unaligned pointer %p", ptr);
for (*r = base; *r; *r = (*r)->next)
if ((grub_addr_t) ptr > (*r)->addr
&& (grub_addr_t) ptr <= (*r)->addr + (*r)->size)
break;
if (! *r)
grub_fatal ("out of range pointer %p", ptr);
*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
grub_mm_init_region (void *addr, grub_size_t size)
{
grub_mm_header_t h;
grub_mm_region_t r, *p, q;
grub_dprintf ("mem", "Using memory for heap: addr=%p, size=%u\n", addr, (unsigned int) size);
/* If this region is too small, ignore it. */
if (size < GRUB_MM_ALIGN * 2)
return;
/* Allocate a region from the head. */
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 = (grub_mm_header_t) ((char *) r + GRUB_MM_ALIGN);
h->next = h;
h->magic = GRUB_MM_FREE_MAGIC;
h->size = (size >> GRUB_MM_ALIGN_LOG2);
r->first = h;
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. */
for (p = &base, q = *p; q; p = &(q->next), q = *p)
if (q->size > r->size)
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. Return a
non-NULL if successful, otherwise return NULL. */
static void *
grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
{
grub_mm_header_t p, q;
if ((*first)->magic == GRUB_MM_ALLOC_MAGIC)
return 0;
for (q = *first, p = q->next; ; q = p, p = p->next)
{
grub_off_t extra;
extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align;
if (extra)
extra = align - extra;
if (! p)
grub_fatal ("null in the ring");
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 = GRUB_MM_ALLOC_MAGIC;
}
else if (extra == 0 || p->size == n + extra)
{
p->size -= n;
p += p->size;
p->size = n;
p->magic = GRUB_MM_ALLOC_MAGIC;
}
else
{
grub_mm_header_t r;
r = p + extra + n;
r->magic = GRUB_MM_FREE_MAGIC;
r->size = p->size - extra - n;
r->next = p->next;
p->size = extra;
p->next = r;
p += extra;
p->size = n;
p->magic = GRUB_MM_ALLOC_MAGIC;
}
*first = q;
return p + 1;
}
if (p == *first)
break;
}
return 0;
}
/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */
void *
grub_memalign (grub_size_t align, grub_size_t size)
{
grub_mm_region_t r;
grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
int count = 0;
align = (align >> GRUB_MM_ALIGN_LOG2);
if (align == 0)
align = 1;
again:
for (r = base; r; r = r->next)
{
void *p;
p = grub_real_malloc (&(r->first), n, align);
if (p)
return p;
}
/* If failed, increase free memory somehow. */
switch (count)
{
case 0:
/* Invalidate disk caches. */
grub_disk_cache_invalidate_all ();
count++;
goto again;
case 1:
/* Unload unneeded modules. */
grub_dl_unload_unneeded ();
count++;
goto again;
default:
break;
}
grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
return 0;
}
/* Allocate SIZE bytes and return the pointer. */
void *
grub_malloc (grub_size_t size)
{
return grub_memalign (0, size);
}
/* Deallocate the pointer PTR. */
void
grub_free (void *ptr)
{
grub_mm_header_t p;
grub_mm_region_t r;
if (! ptr)
return;
get_header_from_pointer (ptr, &p, &r);
if (r->first->magic == GRUB_MM_ALLOC_MAGIC)
{
p->magic = GRUB_MM_FREE_MAGIC;
r->first = p->next = p;
}
else
{
grub_mm_header_t q;
#if 0
q = r->first;
do
{
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;
}
while (q != r->first);
#endif
for (q = r->first; q >= p || q->next <= p; q = q->next)
{
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 = GRUB_MM_FREE_MAGIC;
p->next = q->next;
q->next = p;
if (p + p->size == p->next)
{
if (p->next == q)
q = p;
p->next->magic = 0;
p->size += p->next->size;
p->next = p->next->next;
}
if (q + q->size == p)
{
p->magic = 0;
q->size += p->size;
q->next = p->next;
}
r->first = q;
}
}
/* Reallocate SIZE bytes and return the pointer. The contents will be
the same as that of PTR. */
void *
grub_realloc (void *ptr, grub_size_t size)
{
grub_mm_header_t p;
grub_mm_region_t r;
void *q;
grub_size_t n;
if (! ptr)
return grub_malloc (size);
if (! size)
{
grub_free (ptr);
return 0;
}
/* FIXME: Not optimal. */
n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
get_header_from_pointer (ptr, &p, &r);
if (p->size >= n)
return ptr;
q = grub_malloc (size);
if (! q)
return q;
grub_memcpy (q, ptr, size);
grub_free (ptr);
return q;
}
#ifdef MM_DEBUG
int grub_mm_debug = 0;
void
grub_mm_dump_free (void)
{
grub_mm_region_t r;
for (r = base; r; r = r->next)
{
grub_mm_header_t p;
/* Follow the free list. */
p = r->first;
do
{
if (p->magic != GRUB_MM_FREE_MAGIC)
grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
grub_printf ("F:%p:%u:%p\n",
p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
p = p->next;
}
while (p != r->first);
}
grub_printf ("\n");
}
void
grub_mm_dump (unsigned lineno)
{
grub_mm_region_t r;
grub_printf ("called at line %u\n", lineno);
for (r = base; r; r = r->next)
{
grub_mm_header_t p;
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 GRUB_MM_FREE_MAGIC:
grub_printf ("F:%p:%u:%p\n",
p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
break;
case GRUB_MM_ALLOC_MAGIC:
grub_printf ("A:%p:%u\n", p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2);
break;
}
}
}
grub_printf ("\n");
}
void *
grub_debug_malloc (const char *file, int line, grub_size_t size)
{
void *ptr;
if (grub_mm_debug)
grub_printf ("%s:%d: malloc (0x%x) = ", file, line, size);
ptr = grub_malloc (size);
if (grub_mm_debug)
grub_printf ("%p\n", ptr);
return ptr;
}
void
grub_debug_free (const char *file, int line, void *ptr)
{
if (grub_mm_debug)
grub_printf ("%s:%d: free (%p)\n", file, line, ptr);
grub_free (ptr);
}
void *
grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t size)
{
if (grub_mm_debug)
grub_printf ("%s:%d: realloc (%p, 0x%x) = ", file, line, ptr, size);
ptr = grub_realloc (ptr, size);
if (grub_mm_debug)
grub_printf ("%p\n", ptr);
return ptr;
}
void *
grub_debug_memalign (const char *file, int line, grub_size_t align,
grub_size_t size)
{
void *ptr;
if (grub_mm_debug)
grub_printf ("%s:%d: memalign (0x%x, 0x%x) = ",
file, line, align, size);
ptr = grub_memalign (align, size);
if (grub_mm_debug)
grub_printf ("%p\n", ptr);
return ptr;
}
#endif /* MM_DEBUG */