From 0bf3db5df7fb8749c5ff0f386558260dc127b9df Mon Sep 17 00:00:00 2001 From: bg- Date: Fri, 11 Aug 2006 13:41:31 +0000 Subject: [PATCH] Make malloc machine independent by relying on dependent sbrk(2). --- core/lib/malloc.c | 34 +++++++++++++++------------------- core/lib/malloc.h | 3 ++- cpu/msp430/msp430.c | 30 +++++++++++++++++++++++++++++- platform/sky/contiki-conf.h | 7 ++----- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/core/lib/malloc.c b/core/lib/malloc.c index a06404596..134047c5d 100644 --- a/core/lib/malloc.c +++ b/core/lib/malloc.c @@ -30,7 +30,7 @@ */ -/* $Id: malloc.c,v 1.1 2006/06/17 22:41:18 adamdunkels Exp $ */ +/* $Id: malloc.c,v 1.2 2006/08/11 13:41:31 bg- Exp $ */ #include @@ -54,6 +54,8 @@ char mymem[256]; * with the data segment. */ +char *__malloc_heap_end; + char *__brkval; struct __freelist *__flp; @@ -64,7 +66,7 @@ malloc(size_t len) { struct __freelist *fp1, *fp2; char *cp; - size_t s, avail; + size_t s; if (len <= 0) return 0; @@ -161,23 +163,17 @@ malloc(size_t len) * that we don't collide with the stack. */ if (__brkval == 0) - __brkval = __malloc_heap_start; - cp = __malloc_heap_end; - avail = cp - __brkval; - /* - * Both tests below are needed to catch the case len >= 0xfffe. - */ - if (avail >= len && avail >= len + sizeof(struct __freelist)) { - fp1 = (struct __freelist *)__brkval; - __brkval += len + sizeof(struct __freelist); - fp1->sz = len; - fp1->handle = NULL; - return &fp1[1]; - } - /* - * Step 4: There's no help, just fail. :-/ - */ - return 0; + __brkval = sbrk(len + sizeof(struct __freelist)); + else if (sbrk(len + sizeof(struct __freelist)) == (void *)-1) + return 0; /* There's no help, just fail. :-/ */ + + __malloc_heap_end = sbrk(0); + + fp1 = (struct __freelist *)__brkval; + __brkval += len + sizeof(struct __freelist); + fp1->sz = len; + fp1->handle = NULL; + return &fp1[1]; } void diff --git a/core/lib/malloc.h b/core/lib/malloc.h index af8e6bc72..2a2eb3b69 100644 --- a/core/lib/malloc.h +++ b/core/lib/malloc.h @@ -27,7 +27,7 @@ POSSIBILITY OF SUCH DAMAGE. */ -/* $Id: malloc.h,v 1.1 2006/06/17 22:41:18 adamdunkels Exp $ */ +/* $Id: malloc.h,v 1.2 2006/08/11 13:41:31 bg- Exp $ */ #ifndef MALLOC_H #define MALLOC_H @@ -57,6 +57,7 @@ void free(void *ptr); extern char *__brkval; /* first location not yet allocated */ extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ +extern char *__malloc_heap_end; #define MALLOC_ROUNDUP (sizeof(int) - 1) diff --git a/cpu/msp430/msp430.c b/cpu/msp430/msp430.c index d13ece846..094fb85f1 100644 --- a/cpu/msp430/msp430.c +++ b/cpu/msp430/msp430.c @@ -28,10 +28,11 @@ * * This file is part of the Contiki operating system. * - * @(#)$Id: msp430.c,v 1.1 2006/06/17 22:41:21 adamdunkels Exp $ + * @(#)$Id: msp430.c,v 1.2 2006/08/11 13:41:31 bg- Exp $ */ #include #include +#include #include "net/uip.h" @@ -160,6 +161,33 @@ msp430_cpu_init(void) #define asmv(arg) __asm__ __volatile__(arg) +#define STACK_EXTRA 32 +static char *cur_break = (char *)&__bss_end; + +/* + * Allocate memory from the heap. Check that we don't collide with the + * stack right now (some other routine might later). A watchdog might + * be used to check if cur_break and the stack pointer meet during + * runtime. + */ +void * +sbrk(int incr) +{ + char *stack_pointer; + + asmv("mov r1, %0" : "=r" (stack_pointer)); + stack_pointer -= STACK_EXTRA; + if(incr > (stack_pointer - cur_break)) + return (void *)-1; /* ENOMEM */ + + void *old_break = cur_break; + cur_break += incr; + /* + * If the stack was never here then [old_break .. cur_break] should + * be filled with zeros. + */ + return old_break; } + /* * Mask all interrupts that can be masked. */ diff --git a/platform/sky/contiki-conf.h b/platform/sky/contiki-conf.h index 320f71335..60aeb9549 100644 --- a/platform/sky/contiki-conf.h +++ b/platform/sky/contiki-conf.h @@ -1,5 +1,5 @@ /* -*- C -*- */ -/* @(#)$Id: contiki-conf.h,v 1.1 2006/08/02 14:44:46 bg- Exp $ */ +/* @(#)$Id: contiki-conf.h,v 1.2 2006/08/11 13:41:31 bg- Exp $ */ #ifndef CONTIKI_CONF_H #define CONTIKI_CONF_H @@ -8,6 +8,7 @@ #define UAODV_BAD_ROUTE /* Helper prototypes that should go somewhere. */ +void *sbrk(int); void splx_(int); int splhigh_(void); void msp430_cpu_init(void); /* Rename to cpu_init() later! */ @@ -54,10 +55,6 @@ void msp430_cpu_init(void); /* Rename to cpu_init() later! */ /* Button sensors. */ #define IRQ_PORT2 0x02 -/* Reserve 64 bytes (only) for the runtime stack. */ -#define __malloc_heap_start ((char *)&__bss_end) -#define __malloc_heap_end (((char *)&__stack) - 64) - typedef unsigned char u8_t; typedef unsigned short u16_t; typedef unsigned long u32_t;