From 544fa259287a2b919d8ed05bc201a5133032ef05 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 16 Jan 2007 08:36:42 +0000 Subject: Rename e{malloc,realloc,free} to u{malloc,realloc,free}, to more obviously reflect the fact that they allocate and deallocate user memory (i.e. things reached through a userptr_t). --- src/arch/i386/core/emalloc.c | 169 ------------------------------------------- src/arch/i386/core/umalloc.c | 169 +++++++++++++++++++++++++++++++++++++++++++ src/core/ebuffer.c | 6 +- src/core/image.c | 1 - src/include/gpxe/emalloc.h | 17 ----- src/include/gpxe/umalloc.h | 17 +++++ src/tests/emalloc_test.c | 26 ------- src/tests/umalloc_test.c | 26 +++++++ src/usr/fetch.c | 8 +- src/usr/imgmgmt.c | 6 +- 10 files changed, 222 insertions(+), 223 deletions(-) delete mode 100644 src/arch/i386/core/emalloc.c create mode 100644 src/arch/i386/core/umalloc.c delete mode 100644 src/include/gpxe/emalloc.h create mode 100644 src/include/gpxe/umalloc.h delete mode 100644 src/tests/emalloc_test.c create mode 100644 src/tests/umalloc_test.c diff --git a/src/arch/i386/core/emalloc.c b/src/arch/i386/core/emalloc.c deleted file mode 100644 index 09f2beb48..000000000 --- a/src/arch/i386/core/emalloc.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2007 Michael Brown . - * - * This program 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 any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -/** - * @file - * - * External memory allocation - * - */ - -#include -#include -#include - -/** Alignment of external allocated memory */ -#define EM_ALIGN ( 4 * 1024 ) - -/** Equivalent of NOWHERE for user pointers */ -#define UNOWHERE ( ~UNULL ) - -/** Start of Etherboot text, as defined by the linker */ -extern char _text[]; - -/** Top of allocatable memory */ -#define TOP ( virt_to_user ( _text ) ) - -/** An external memory block */ -struct external_memory { - /** Size of this memory block (excluding this header) */ - size_t size; - /** Block is currently in use */ - int used; -}; - -/** Current lowest allocated block - * - * A value of UNULL indicates that no blocks are currently allocated. - */ -userptr_t bottom = UNULL; - -/** - * Collect free blocks - * - */ -static void ecollect_free ( void ) { - struct external_memory extmem; - - /* Walk the free list and collect empty blocks */ - while ( bottom != TOP ) { - copy_from_user ( &extmem, bottom, -sizeof ( extmem ), - sizeof ( extmem ) ); - if ( extmem.used ) - break; - DBG ( "EXTMEM freeing [%lx,%lx)\n", user_to_phys ( bottom, 0 ), - user_to_phys ( bottom, extmem.size ) ); - bottom = userptr_add ( bottom, - ( extmem.size + sizeof ( extmem ) ) ); - } -} - -/** - * Reallocate external memory - * - * @v old_ptr Memory previously allocated by emalloc(), or UNULL - * @v new_size Requested size - * @ret new_ptr Allocated memory, or UNULL - * - * Calling realloc() with a new size of zero is a valid way to free a - * memory block. - */ -userptr_t erealloc ( userptr_t ptr, size_t new_size ) { - struct external_memory extmem; - userptr_t new = ptr; - size_t align; - - /* Initialise external memory allocator if necessary */ - if ( ! bottom ) - bottom = TOP; - - /* Get block properties into extmem */ - if ( ptr && ( ptr != UNOWHERE ) ) { - /* Determine old size */ - copy_from_user ( &extmem, ptr, -sizeof ( extmem ), - sizeof ( extmem ) ); - } else { - /* Create a zero-length block */ - ptr = bottom = userptr_add ( bottom, -sizeof ( extmem ) ); - DBG ( "EXTMEM allocating [%lx,%lx)\n", - user_to_phys ( ptr, 0 ), user_to_phys ( ptr, 0 ) ); - extmem.size = 0; - } - extmem.used = ( new_size > 0 ); - - /* Expand/shrink block if possible */ - if ( ptr == bottom ) { - /* Update block */ - new = userptr_add ( ptr, - ( new_size - extmem.size ) ); - align = ( user_to_phys ( new, 0 ) & ( EM_ALIGN - 1 ) ); - new_size += align; - new = userptr_add ( new, -align ); - DBG ( "EXTMEM expanding [%lx,%lx) to [%lx,%lx)\n", - user_to_phys ( ptr, 0 ), - user_to_phys ( ptr, extmem.size ), - user_to_phys ( new, 0 ), - user_to_phys ( new, new_size )); - memmove_user ( new, 0, ptr, 0, ( ( extmem.size < new_size ) ? - extmem.size : new_size ) ); - extmem.size = new_size; - bottom = new; - } else { - /* Cannot expand; can only pretend to shrink */ - if ( new_size > extmem.size ) { - /* Refuse to expand */ - DBG ( "EXTMEM cannot expand [%lx,%lx)\n", - user_to_phys ( ptr, 0 ), - user_to_phys ( ptr, extmem.size ) ); - return UNULL; - } - } - - /* Write back block properties */ - copy_to_user ( new, -sizeof ( extmem ), &extmem, - sizeof ( extmem ) ); - - /* Collect any free blocks and update hidden memory region */ - ecollect_free(); - hide_region ( EXTMEM, user_to_phys ( bottom, -sizeof ( extmem ) ), - user_to_phys ( TOP, 0 ) ); - - return ( new_size ? new : UNOWHERE ); -} - -/** - * Allocate external memory - * - * @v size Requested size - * @ret ptr Memory, or UNULL - * - * Memory is guaranteed to be aligned to a page boundary. - */ -userptr_t emalloc ( size_t size ) { - return erealloc ( UNULL, size ); -} - -/** - * Free external memory - * - * @v ptr Memory allocated by emalloc(), or UNULL - * - * If @c ptr is UNULL, no action is taken. - */ -void efree ( userptr_t ptr ) { - erealloc ( ptr, 0 ); -} diff --git a/src/arch/i386/core/umalloc.c b/src/arch/i386/core/umalloc.c new file mode 100644 index 000000000..5eef50866 --- /dev/null +++ b/src/arch/i386/core/umalloc.c @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2007 Michael Brown . + * + * This program 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 any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/** + * @file + * + * External memory allocation + * + */ + +#include +#include +#include + +/** Alignment of external allocated memory */ +#define EM_ALIGN ( 4 * 1024 ) + +/** Equivalent of NOWHERE for user pointers */ +#define UNOWHERE ( ~UNULL ) + +/** Start of Etherboot text, as defined by the linker */ +extern char _text[]; + +/** Top of allocatable memory */ +#define TOP ( virt_to_user ( _text ) ) + +/** An external memory block */ +struct external_memory { + /** Size of this memory block (excluding this header) */ + size_t size; + /** Block is currently in use */ + int used; +}; + +/** Current lowest allocated block + * + * A value of UNULL indicates that no blocks are currently allocated. + */ +userptr_t bottom = UNULL; + +/** + * Collect free blocks + * + */ +static void ecollect_free ( void ) { + struct external_memory extmem; + + /* Walk the free list and collect empty blocks */ + while ( bottom != TOP ) { + copy_from_user ( &extmem, bottom, -sizeof ( extmem ), + sizeof ( extmem ) ); + if ( extmem.used ) + break; + DBG ( "EXTMEM freeing [%lx,%lx)\n", user_to_phys ( bottom, 0 ), + user_to_phys ( bottom, extmem.size ) ); + bottom = userptr_add ( bottom, + ( extmem.size + sizeof ( extmem ) ) ); + } +} + +/** + * Reallocate external memory + * + * @v old_ptr Memory previously allocated by umalloc(), or UNULL + * @v new_size Requested size + * @ret new_ptr Allocated memory, or UNULL + * + * Calling realloc() with a new size of zero is a valid way to free a + * memory block. + */ +userptr_t urealloc ( userptr_t ptr, size_t new_size ) { + struct external_memory extmem; + userptr_t new = ptr; + size_t align; + + /* Initialise external memory allocator if necessary */ + if ( ! bottom ) + bottom = TOP; + + /* Get block properties into extmem */ + if ( ptr && ( ptr != UNOWHERE ) ) { + /* Determine old size */ + copy_from_user ( &extmem, ptr, -sizeof ( extmem ), + sizeof ( extmem ) ); + } else { + /* Create a zero-length block */ + ptr = bottom = userptr_add ( bottom, -sizeof ( extmem ) ); + DBG ( "EXTMEM allocating [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), user_to_phys ( ptr, 0 ) ); + extmem.size = 0; + } + extmem.used = ( new_size > 0 ); + + /* Expand/shrink block if possible */ + if ( ptr == bottom ) { + /* Update block */ + new = userptr_add ( ptr, - ( new_size - extmem.size ) ); + align = ( user_to_phys ( new, 0 ) & ( EM_ALIGN - 1 ) ); + new_size += align; + new = userptr_add ( new, -align ); + DBG ( "EXTMEM expanding [%lx,%lx) to [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), + user_to_phys ( ptr, extmem.size ), + user_to_phys ( new, 0 ), + user_to_phys ( new, new_size )); + memmove_user ( new, 0, ptr, 0, ( ( extmem.size < new_size ) ? + extmem.size : new_size ) ); + extmem.size = new_size; + bottom = new; + } else { + /* Cannot expand; can only pretend to shrink */ + if ( new_size > extmem.size ) { + /* Refuse to expand */ + DBG ( "EXTMEM cannot expand [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), + user_to_phys ( ptr, extmem.size ) ); + return UNULL; + } + } + + /* Write back block properties */ + copy_to_user ( new, -sizeof ( extmem ), &extmem, + sizeof ( extmem ) ); + + /* Collect any free blocks and update hidden memory region */ + ecollect_free(); + hide_region ( EXTMEM, user_to_phys ( bottom, -sizeof ( extmem ) ), + user_to_phys ( TOP, 0 ) ); + + return ( new_size ? new : UNOWHERE ); +} + +/** + * Allocate external memory + * + * @v size Requested size + * @ret ptr Memory, or UNULL + * + * Memory is guaranteed to be aligned to a page boundary. + */ +userptr_t umalloc ( size_t size ) { + return urealloc ( UNULL, size ); +} + +/** + * Free external memory + * + * @v ptr Memory allocated by umalloc(), or UNULL + * + * If @c ptr is UNULL, no action is taken. + */ +void ufree ( userptr_t ptr ) { + urealloc ( ptr, 0 ); +} diff --git a/src/core/ebuffer.c b/src/core/ebuffer.c index 5abfca745..16d49a12e 100644 --- a/src/core/ebuffer.c +++ b/src/core/ebuffer.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include /** @@ -46,7 +46,7 @@ static int ebuffer_expand ( struct buffer *buffer, size_t new_len ) { actual_len <<= 1; /* Reallocate buffer */ - new_addr = erealloc ( buffer->addr, actual_len ); + new_addr = urealloc ( buffer->addr, actual_len ); if ( ! new_addr ) return -ENOMEM; @@ -63,7 +63,7 @@ static int ebuffer_expand ( struct buffer *buffer, size_t new_len ) { * @ret rc Return status code * * Allocates space for the buffer and stores it in @c buffer->addr. - * The space must eventually be freed by calling efree(buffer->addr). + * The space must eventually be freed by calling ufree(buffer->addr). */ int ebuffer_alloc ( struct buffer *buffer, size_t len ) { memset ( buffer, 0, sizeof ( *buffer ) ); diff --git a/src/core/image.c b/src/core/image.c index e0e842c0e..0ff0a7065 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -23,7 +23,6 @@ #include #include #include -#include #include /** @file diff --git a/src/include/gpxe/emalloc.h b/src/include/gpxe/emalloc.h deleted file mode 100644 index cee220dc8..000000000 --- a/src/include/gpxe/emalloc.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _GPXE_EMALLOC_H -#define _GPXE_EMALLOC_H - -/** - * @file - * - * External memory allocation - * - */ - -#include - -extern userptr_t emalloc ( size_t size ); -extern userptr_t erealloc ( userptr_t ptr, size_t new_size ); -extern void efree ( userptr_t ptr ); - -#endif /* _GPXE_EMALLOC_H */ diff --git a/src/include/gpxe/umalloc.h b/src/include/gpxe/umalloc.h new file mode 100644 index 000000000..49ec22b47 --- /dev/null +++ b/src/include/gpxe/umalloc.h @@ -0,0 +1,17 @@ +#ifndef _GPXE_UMALLOC_H +#define _GPXE_UMALLOC_H + +/** + * @file + * + * User memory allocation + * + */ + +#include + +extern userptr_t umalloc ( size_t size ); +extern userptr_t urealloc ( userptr_t ptr, size_t new_size ); +extern void ufree ( userptr_t ptr ); + +#endif /* _GPXE_UMALLOC_H */ diff --git a/src/tests/emalloc_test.c b/src/tests/emalloc_test.c deleted file mode 100644 index e8428f5d5..000000000 --- a/src/tests/emalloc_test.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include -#include - -void emalloc_test ( void ) { - struct memory_map memmap; - userptr_t bob; - userptr_t fred; - - printf ( "Before allocation:\n" ); - get_memmap ( &memmap ); - - bob = emalloc ( 1234 ); - bob = erealloc ( bob, 12345 ); - fred = emalloc ( 999 ); - - printf ( "After allocation:\n" ); - get_memmap ( &memmap ); - - efree ( bob ); - efree ( fred ); - - printf ( "After freeing:\n" ); - get_memmap ( &memmap ); -} diff --git a/src/tests/umalloc_test.c b/src/tests/umalloc_test.c new file mode 100644 index 000000000..98de87844 --- /dev/null +++ b/src/tests/umalloc_test.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +void umalloc_test ( void ) { + struct memory_map memmap; + userptr_t bob; + userptr_t fred; + + printf ( "Before allocation:\n" ); + get_memmap ( &memmap ); + + bob = ymalloc ( 1234 ); + bob = yrealloc ( bob, 12345 ); + fred = ymalloc ( 999 ); + + printf ( "After allocation:\n" ); + get_memmap ( &memmap ); + + ufree ( bob ); + ufree ( fred ); + + printf ( "After freeing:\n" ); + get_memmap ( &memmap ); +} diff --git a/src/usr/fetch.c b/src/usr/fetch.c index 11197e9fd..9260a48f2 100644 --- a/src/usr/fetch.c +++ b/src/usr/fetch.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include #include @@ -45,9 +45,9 @@ * @ret len Length of loaded file * @ret rc Return status code * - * Fetch file to an external buffer allocated with emalloc(). The + * Fetch file to an external buffer allocated with umalloc(). The * caller is responsible for eventually freeing the buffer with - * efree(). + * ufree(). */ int fetch ( const char *uri_string, userptr_t *data, size_t *len ) { struct uri *uri; @@ -101,7 +101,7 @@ int fetch ( const char *uri_string, userptr_t *data, size_t *len ) { return 0; err: - efree ( buffer.addr ); + ufree ( buffer.addr ); err_ebuffer_alloc: free_uri ( uri ); err_parse_uri: diff --git a/src/usr/imgmgmt.c b/src/usr/imgmgmt.c index eb2330a33..abd48bb39 100644 --- a/src/usr/imgmgmt.c +++ b/src/usr/imgmgmt.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include @@ -66,7 +66,7 @@ int imgfetch ( const char *filename, const char *name, return 0; err: - efree ( image->data ); + ufree ( image->data ); free ( image ); return rc; } @@ -139,6 +139,6 @@ void imgstat ( struct image *image ) { */ void imgfree ( struct image *image ) { unregister_image ( image ); - efree ( image->data ); + ufree ( image->data ); free ( image ); } -- cgit v1.2.3-55-g7522