[Patch] TEST - prom-libc malloc

Nathan Lynch ntl at pobox.com
Thu Apr 19 02:54:59 EST 2007


Paul Nasrat wrote:
> Przemek Iskra <sparky at pld-linux.org> sent in a patch to replace the
> malloc in yaboot with one Ethan originally developed in prom-libc. It's
> possible this might be of use in increasing the tftp filesize limit.

Some minor comments on the patch:


> diff --git a/include/stdlib.h b/include/stdlib.h
> index 0b5e99a..def230c 100644
> --- a/include/stdlib.h
> +++ b/include/stdlib.h
> @@ -6,19 +6,30 @@
>  #define __STDLIB_H
>  
>  #include "stdarg.h"
> +#include "stddef.h"
>  
> -extern void malloc_init(void *bottom, unsigned long size);
> -extern void malloc_dispose(void);
> +extern int __alloc_freelist_init(void);
> +//extern void malloc_init(void *bottom, unsigned long size);
> +//extern void malloc_dispose(void);
>  
>  extern void *malloc(unsigned int size);
>  extern void *realloc(void *ptr, unsigned int size);
>  extern void free (void *m);
> -extern void mark (void **ptr);
> -extern void release (void *ptr);
> +//extern void mark (void **ptr);
> +//extern void release (void *ptr);
>  
>  extern int sprintf(char * buf, const char *fmt, ...);
>  extern int vsprintf(char *buf, const char *fmt, va_list args);
>  extern long simple_strtol(const char *cp,char **endp,unsigned int base);
>  #define strtol(x,y,z) simple_strtol(x,y,z)
>  
> +extern inline int atoi( const char *ptr );
> +
> +extern inline int
> +atoi (const char *ptr)
> +{
> +	return (int) strtol (ptr, (char **) NULL, 10);
> +}

Adding an atoi function seems unrelated to the rest of the patch?


> +
> +
>  #endif
> diff --git a/lib/malloc.c b/lib/malloc.c
> index 672bb3e..87cc7f5 100644
> --- a/lib/malloc.c
> +++ b/lib/malloc.c
> @@ -21,87 +21,219 @@
>  
>  #include "types.h"
>  #include "stddef.h"
> +#include "string.h"
> +
> +#define MALLOC_DEBUG 1
>  
>  /* Imported functions */
>  extern void prom_printf (char *fmt, ...);
> +extern void *prom_claim (void *virt, unsigned int size, unsigned int align);
> +
> +/*
> + * memory allocation routines from prom-libc
> + */
> +
> +void free(void *memory);

Declarations should go in headers, not .c files.


> -static char *malloc_ptr = 0;
> -static char *malloc_top = 0;
> -static char *last_alloc = 0;
> +/*** bits/malloc.h ***/
> +struct __alloc_area
> +{
> +	struct __alloc_area *next;
> +	unsigned int length;
> +	long long foo;
> +};

What is foo?  Needs a comment, at least.

<snip>

> -void free (void *m)
> +void free(void *memory)
>  {
> -    if (!malloc_ptr)
> +     struct __alloc_area *free;
> +     struct __alloc_area *before;
> +     struct __alloc_area *current;
> +
> +#ifdef MALLOC_DEBUG
> +     /* check that memory isn't a NULL pointer (possibly from a failed
> +      * malloc). this check is nice, but not compulsory
> +      */
> +     if (!memory) {
> +	  prom_printf("WARNING: attempt to free a NULL pointer\n");
>      	return;
> -    if (m == last_alloc)
> -	malloc_ptr = (char *) last_alloc - sizeof(int);
>  }
> +#endif /* MALLOC_DEBUG */

In Linux it's considered useful for "free" functions to handle NULL
and not treat it as a bug.  It can simplify callers and reduce code
size.  Perhaps not a big deal for yaboot.



More information about the Yaboot-devel mailing list