[syslinux:elflink] elflink: remove the malloc.c/free.c/realloc.c/zalloc.c from com32

syslinux-bot for Feng Tang feng.tang at intel.com
Thu Aug 12 21:03:25 PDT 2010


Commit-ID:  880f67714ec0f5187ff7c4dd2ec11879c18437e7
Gitweb:     http://syslinux.zytor.com/commit/880f67714ec0f5187ff7c4dd2ec11879c18437e7
Author:     Feng Tang <feng.tang at intel.com>
AuthorDate: Thu, 24 Jun 2010 16:03:34 +0800
Committer:  Feng Tang <feng.tang at intel.com>
CommitDate: Tue, 20 Jul 2010 11:10:04 +0800

elflink: remove the malloc.c/free.c/realloc.c/zalloc.c from com32



---
 com32/lib/Makefile  |    9 +--
 com32/lib/free.c    |  127 --------------------------------
 com32/lib/malloc.c  |  198 ---------------------------------------------------
 com32/lib/realloc.c |  100 --------------------------
 com32/lib/zalloc.c  |   17 -----
 core/mem/malloc.c   |   32 ++++++++-
 6 files changed, 35 insertions(+), 448 deletions(-)

diff --git a/com32/lib/Makefile b/com32/lib/Makefile
index f803af6..c8558c6 100755
--- a/com32/lib/Makefile
+++ b/com32/lib/Makefile
@@ -106,15 +106,14 @@ LIBCONSOLE_OBJS = \
 	\
 	syslinux/serial.o
 
-	#sys/readdir.o getcwd.o chdir.o fdopendir.o
 LIBOTHER_OBJS = \
 	atoi.o atol.o atoll.o calloc.o creat.o		\
 	ctypes.o errno.o fgetc.o fgets.o fopen.o fprintf.o fputc.o	\
 	fclose.o putchar.o setjmp.o				\
-	fputs.o fread2.o fread.o free.o fwrite2.o fwrite.o getopt.o	\
-	lrand48.o malloc.o stack.o memccpy.o memchr.o memcmp.o		\
+	fputs.o fread2.o fread.o fwrite2.o fwrite.o getopt.o	\
+	lrand48.o stack.o memccpy.o memchr.o memcmp.o		\
 	memcpy.o mempcpy.o memmem.o memmove.o memset.o memswap.o	\
-	perror.o printf.o puts.o qsort.o realloc.o seed48.o snprintf.o	\
+	perror.o printf.o puts.o qsort.o seed48.o snprintf.o	\
 	sprintf.o srand48.o sscanf.o strcasecmp.o strcat.o	\
 	strchr.o strcmp.o strcpy.o strdup.o strerror.o strlen.o		\
 	strnlen.o							\
@@ -124,7 +123,7 @@ LIBOTHER_OBJS = \
 	strtoimax.o strtok.o strtol.o strtoll.o strtoul.o strtoull.o	\
 	strtoumax.o vfprintf.o vprintf.o vsnprintf.o vsprintf.o		\
 	asprintf.o vasprintf.o strlcpy.o strlcat.o			\
-	vsscanf.o zalloc.o						\
+	vsscanf.o							\
 	\
 	lmalloc.o lstrdup.o						\
 	\
diff --git a/com32/lib/free.c b/com32/lib/free.c
deleted file mode 100644
index 564b70e..0000000
--- a/com32/lib/free.c
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * free.c
- *
- * Very simple linked-list based malloc()/free().
- */
-
-#include <stdlib.h>
-#include "malloc.h"
-
-#include <stdio.h>
-
-#if 0
-static struct free_arena_header *__free_block(struct free_arena_header *ah)
-{
-    struct free_arena_header *pah, *nah;
-
-    pah = ah->a.prev;
-    nah = ah->a.next;
-    if (pah->a.type == ARENA_TYPE_FREE &&
-	(char *)pah + pah->a.size == (char *)ah) {
-	/* Coalesce into the previous block */
-	pah->a.size += ah->a.size;
-	pah->a.next = nah;
-	nah->a.prev = pah;
-
-#ifdef DEBUG_MALLOC
-	ah->a.type = ARENA_TYPE_DEAD;
-#endif
-
-	ah = pah;
-	pah = ah->a.prev;
-    } else {
-	/* Need to add this block to the free chain */
-	ah->a.type = ARENA_TYPE_FREE;
-
-	ah->next_free = __malloc_head.next_free;
-	ah->prev_free = &__malloc_head;
-	__malloc_head.next_free = ah;
-	ah->next_free->prev_free = ah;
-    }
-
-    /* In either of the previous cases, we might be able to merge
-       with the subsequent block... */
-    if (nah->a.type == ARENA_TYPE_FREE &&
-	(char *)ah + ah->a.size == (char *)nah) {
-	ah->a.size += nah->a.size;
-
-	/* Remove the old block from the chains */
-	nah->next_free->prev_free = nah->prev_free;
-	nah->prev_free->next_free = nah->next_free;
-	ah->a.next = nah->a.next;
-	nah->a.next->a.prev = ah;
-
-#ifdef DEBUG_MALLOC
-	nah->a.type = ARENA_TYPE_DEAD;
-#endif
-    }
-
-    /* Return the block that contains the called block */
-    return ah;
-}
-
-/*
- * This is used to insert a block which is not previously on the
- * free list.  Only the a.size field of the arena header is assumed
- * to be valid.
- */
-void __inject_free_block(struct free_arena_header *ah)
-{
-    struct free_arena_header *nah;
-    size_t a_end = (size_t) ah + ah->a.size;
-    size_t n_end;
-
-    mp("size = %d, type = 0x%x, end = 0x%x",
-	ah->a.size, ah->a.type, a_end);
-
-    for (nah = __malloc_head.a.next; nah->a.type != ARENA_TYPE_HEAD;
-	 nah = nah->a.next) {
-	n_end = (size_t) nah + nah->a.size;
-
-	/* Is nah entirely beyond this block? */
-	if ((size_t) nah >= a_end)
-	    break;
-
-	/* Is this block entirely beyond nah? */
-	if ((size_t) ah >= n_end)
-	    continue;
-
-	/* Otherwise we have some sort of overlap - reject this block */
-	return;
-    }
-
-    /* Now, nah should point to the successor block */
-    ah->a.next = nah;
-    ah->a.prev = nah->a.prev;
-    nah->a.prev = ah;
-    ah->a.prev->a.next = ah;
-
-    __free_block(ah);
-}
-#endif
-
-#if 0
-void free(void *ptr)
-{
-    struct free_arena_header *ah;
-
-    if (!ptr)
-	return;
-
-    ah = (struct free_arena_header *)
-	((struct arena_header *)ptr - 1);
-
-#ifdef DEBUG_MALLOC
-    assert(ah->a.type == ARENA_TYPE_USED);
-#endif
-
-	if (ah->a.type != ARENA_TYPE_USED || ah->a.size == 0) {
-		printf("error in free, ptr = %p\n", ptr);
-		return;
-	}
-
-    __free_block(ah);
-
-    /* Here we could insert code to return memory to the system. */
-}
-#endif
diff --git a/com32/lib/malloc.c b/com32/lib/malloc.c
deleted file mode 100644
index 4a86f64..0000000
--- a/com32/lib/malloc.c
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * malloc.c
- *
- * Very simple linked-list based malloc()/free().
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include <com32.h>
-#include <errno.h>
-#include <syslinux/memscan.h>
-#include "init.h"
-#include "malloc.h"
-
-#include <stdio.h>
-
-#if 0
-struct free_arena_header __malloc_head = {
-    {
-     ARENA_TYPE_HEAD,
-     0,
-     &__malloc_head,
-     &__malloc_head,
-     },
-    &__malloc_head,
-    &__malloc_head
-};
-
-/* This is extern so it can be overridden by the user application */
-extern size_t __stack_size;
-extern void *__mem_end;		/* Produced after argv parsing */
-
-static inline size_t sp(void)
-{
-    size_t sp;
-    asm volatile ("movl %%esp,%0":"=rm" (sp));
-    return sp;
-}
-
-#define E820_MEM_MAX 0xfff00000	/* 4 GB - 1 MB */
-
-static int consider_memory_area(void *dummy, addr_t start,
-				addr_t len, bool valid)
-{
-    struct free_arena_header *fp;
-    addr_t end;
-
-    (void)dummy;
-
-    if (valid && start < E820_MEM_MAX) {
-	if (len > E820_MEM_MAX - start)
-	    len = E820_MEM_MAX - start;
-
-	end = start + len;
-
-	mp("start = 0x%x, len = 0x%x",
-		start, len);
-
-	if (end > __com32.cs_memsize) {
-	    if (start <= __com32.cs_memsize) {
-		start = __com32.cs_memsize;
-		len = end - start;
-	    }
-
-	    if (len >= 2 * sizeof(struct arena_header)) {
-		fp = (struct free_arena_header *)start;
-		fp->a.size = len;
-		mp("will inject a block start:0x%x size 0x%x", start, len);
-		__inject_free_block(fp);
-	    }
-	}
-    }
-
-    return 0;
-}
-
-static void __constructor init_memory_arena(void)
-{
-    struct free_arena_header *fp;
-    size_t start, total_space;
-
-    //mp("enter");
-
-//	mp("skip this init as core has a mem_init");
-    return;
-
-    start = (size_t) ARENA_ALIGN_UP(__mem_end);
-    total_space = sp() - start;
-
-    mp("start = 0x%x, sp() = 0x%0x, space = 0x%x", start, sp(), total_space);
-
-    if (__stack_size == 0 || __stack_size > total_space >> 1)
-	__stack_size = total_space >> 1;	/* Half for the stack, half for the heap... */
-
-    if (total_space < __stack_size + 4 * sizeof(struct arena_header))
-	__stack_size = total_space - 4 * sizeof(struct arena_header);
-
-    fp = (struct free_arena_header *)start;
-    fp->a.size = total_space - __stack_size;
-
-    __inject_free_block(fp);
-
-    /* Scan the memory map to look for other suitable regions */
-    if (!__com32.cs_memsize)
-	return;			/* Old Syslinux core, can't do this... */
-
-    syslinux_scan_memory(consider_memory_area, NULL);
-}
-
-static void *__malloc_from_block(struct free_arena_header *fp, size_t size)
-{
-    size_t fsize;
-    struct free_arena_header *nfp, *na;
-
-    fsize = fp->a.size;
-
-    /* We need the 2* to account for the larger requirements of a free block */
-    if (fsize >= size + 2 * sizeof(struct arena_header)) {
-	/* Bigger block than required -- split block */
-	nfp = (struct free_arena_header *)((char *)fp + size);
-	na = fp->a.next;
-
-	nfp->a.type = ARENA_TYPE_FREE;
-	nfp->a.size = fsize - size;
-	fp->a.type = ARENA_TYPE_USED;
-	fp->a.size = size;
-
-	/* Insert into all-block chain */
-	nfp->a.prev = fp;
-	nfp->a.next = na;
-	na->a.prev = nfp;
-	fp->a.next = nfp;
-
-	/* Replace current block on free chain */
-	nfp->next_free = fp->next_free;
-	nfp->prev_free = fp->prev_free;
-	fp->next_free->prev_free = nfp;
-	fp->prev_free->next_free = nfp;
-    } else {
-	/* Allocate the whole block */
-	fp->a.type = ARENA_TYPE_USED;
-
-	/* Remove from free chain */
-	fp->next_free->prev_free = fp->prev_free;
-	fp->prev_free->next_free = fp->next_free;
-    }
-
-    return (void *)(&fp->a + 1);
-}
-
-void *malloc(size_t size)
-{
-    struct free_arena_header *fp;
-    char *buf = NULL;
-
-    mp("enter, size = %d", size);
-
-    if (size == 0)
-	return NULL;
-
-    /* Add the obligatory arena header, and round up */
-    size = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
-
-    for (fp = __malloc_head.next_free; fp->a.type != ARENA_TYPE_HEAD;
-	fp = fp->next_free) {
-	if (fp->a.size >= size) {
-	    /* Found fit -- allocate out of this block */
-	    //return __malloc_from_block(fp, size);
-	    buf = __malloc_from_block(fp, size);
-	}
-    }
-
-    /* Nothing found... need to request a block from the kernel */
-    //return NULL;		/* No kernel to get stuff from */
-    mp("will return 0x%p", buf);
-    return buf;
-}
-#endif
-
-/* need to revisit this later */
-int posix_memalign(void **memptr, size_t align, size_t size)
-{
-	void *ptr;
-	unsigned long tmp;
-
-	ptr = malloc(size + align - 1);
-	if (!ptr)
-		return -ENOMEM;
-
-	/* do the alignment  */
-	tmp = (unsigned long)ptr;
-	tmp = (tmp + align -1) & ~(align - 1);
-	ptr = (void *)tmp;
-
-	*memptr = ptr;
-	return 0;
-
-}
diff --git a/com32/lib/realloc.c b/com32/lib/realloc.c
deleted file mode 100644
index 3d3c10f..0000000
--- a/com32/lib/realloc.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * realloc.c
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include <minmax.h>
-
-#include "malloc.h"
-
-#if 0
-void *realloc(void *ptr, size_t size)
-{
-    struct free_arena_header *ah, *nah;
-    void *newptr;
-    size_t newsize, oldsize, xsize;
-
-    if (!ptr)
-	return malloc(size);
-
-    if (size == 0) {
-	free(ptr);
-	return NULL;
-    }
-
-    ah = (struct free_arena_header *)
-	((struct arena_header *)ptr - 1);
-
-    /* Actual size of the old block */
-    oldsize = ah->a.size;
-
-    /* Add the obligatory arena header, and round up */
-    newsize = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
-
-    if (oldsize >= newsize && newsize >= (oldsize >> 2) &&
-	oldsize - newsize < 4096) {
-	/* This allocation is close enough already. */
-	return ptr;
-    } else {
-	xsize = oldsize;
-
-	nah = ah->a.next;
-	if ((char *)nah == (char *)ah + ah->a.size &&
-	    nah->a.type == ARENA_TYPE_FREE &&
-	    oldsize + nah->a.size >= newsize) {
-	    /* Merge in subsequent free block */
-	    ah->a.next = nah->a.next;
-	    ah->a.next->a.prev = ah;
-	    nah->next_free->prev_free = nah->prev_free;
-	    nah->prev_free->next_free = nah->next_free;
-	    xsize = (ah->a.size += nah->a.size);
-	}
-
-	if (xsize >= newsize) {
-	    /* We can reallocate in place */
-	    if (xsize >= newsize + 2 * sizeof(struct arena_header)) {
-		/* Residual free block at end */
-		nah = (struct free_arena_header *)((char *)ah + newsize);
-		nah->a.type = ARENA_TYPE_FREE;
-		nah->a.size = xsize - newsize;
-		ah->a.size = newsize;
-
-		/* Insert into block list */
-		nah->a.next = ah->a.next;
-		ah->a.next = nah;
-		nah->a.next->a.prev = nah;
-		nah->a.prev = ah;
-
-		/* Insert into free list */
-		if (newsize > oldsize) {
-		    /* Hack: this free block is in the path of a memory object
-		       which has already been grown at least once.  As such, put
-		       it at the *end* of the freelist instead of the beginning;
-		       trying to save it for future realloc()s of the same block. */
-		    nah->prev_free = __malloc_head.prev_free;
-		    nah->next_free = &__malloc_head;
-		    __malloc_head.prev_free = nah;
-		    nah->prev_free->next_free = nah;
-		} else {
-		    nah->next_free = __malloc_head.next_free;
-		    nah->prev_free = &__malloc_head;
-		    __malloc_head.next_free = nah;
-		    nah->next_free->prev_free = nah;
-		}
-	    }
-	    /* otherwise, use up the whole block */
-	    return ptr;
-	} else {
-	    /* Last resort: need to allocate a new block and copy */
-	    oldsize -= sizeof(struct arena_header);
-	    newptr = malloc(size);
-	    if (newptr) {
-		memcpy(newptr, ptr, min(size, oldsize));
-		free(ptr);
-	    }
-	    return newptr;
-	}
-    }
-}
-#endif
diff --git a/com32/lib/zalloc.c b/com32/lib/zalloc.c
deleted file mode 100644
index 0e6ed28..0000000
--- a/com32/lib/zalloc.c
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * zalloc.c
- */
-
-#include <stdlib.h>
-#include <string.h>
-
-void *zalloc(size_t size)
-{
-    void *ptr;
-
-    ptr = malloc(size);
-    if (ptr)
-	memset(ptr, 0, size);
-
-    return ptr;
-}
diff --git a/core/mem/malloc.c b/core/mem/malloc.c
index c1c9296..adab9b6 100644
--- a/core/mem/malloc.c
+++ b/core/mem/malloc.c
@@ -65,7 +65,6 @@ static void *_malloc(size_t size, enum heap heap, malloc_tag_t tag)
     struct free_arena_header *fp;
     struct free_arena_header *head = &__core_malloc_head[heap];
     void *p = NULL;
-    static once = 0;
 
     dprintf("_malloc(%zu, %u, %u) @ %p = ",
 	size, heap, tag, __builtin_return_address(0));
@@ -207,3 +206,34 @@ void *realloc(void *ptr, size_t size)
 	}
     }
 }
+
+void *zalloc(size_t size)
+{
+    void *ptr;
+
+    ptr = malloc(size);
+    if (ptr)
+	memset(ptr, 0, size);
+
+    return ptr;
+}
+
+/* need to revisit this later */
+int posix_memalign(void **memptr, size_t align, size_t size)
+{
+	void *ptr;
+	unsigned long tmp;
+
+	ptr = malloc(size + align - 1);
+	if (!ptr)
+		return -ENOMEM;
+
+	/* do the alignment  */
+	tmp = (unsigned long)ptr;
+	tmp = (tmp + align -1) & ~(align - 1);
+	ptr = (void *)tmp;
+
+	*memptr = ptr;
+	return 0;
+
+}



More information about the Syslinux-commits mailing list