Make malloc machine independent by relying on dependent sbrk(2).

This commit is contained in:
bg- 2006-08-11 13:41:31 +00:00
parent 361cd531b6
commit 0bf3db5df7
4 changed files with 48 additions and 26 deletions

View File

@ -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 <stdlib.h>
@ -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

View File

@ -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)

View File

@ -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 <io.h>
#include <signal.h>
#include <sys/unistd.h>
#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.
*/

View File

@ -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;