Added the ability to select the stack size on UNIX platforms.

Fixes issue 92.


git-svn-id: http://picoc.googlecode.com/svn/trunk@477 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2010-07-27 14:03:06 +00:00
parent 0ffbd21f2e
commit 8e02f32095
4 changed files with 79 additions and 104 deletions

28
heap.c
View file

@ -4,18 +4,25 @@
#define FREELIST_BUCKETS 8 /* freelists for 4, 8, 12 ... 32 byte allocs */ #define FREELIST_BUCKETS 8 /* freelists for 4, 8, 12 ... 32 byte allocs */
#define SPLIT_MEM_THRESHOLD 16 /* don't split memory which is close in size */ #define SPLIT_MEM_THRESHOLD 16 /* don't split memory which is close in size */
#ifdef SURVEYOR_HOST #ifdef USE_MALLOC_STACK
#define HEAP_SIZE C_HEAPSIZE static unsigned char *HeapMemory = NULL; /* stack memory since our heap is malloc()ed */
static void *HeapBottom = NULL; /* the bottom of the (downward-growing) heap */
static void *StackFrame = NULL; /* the current stack frame */
void *HeapStackTop = NULL; /* the top of the stack */
#else
# ifdef SURVEYOR_HOST
# define HEAP_SIZE C_HEAPSIZE
static unsigned char *HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */ static unsigned char *HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */
static void *HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */ static void *HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */
static void *StackFrame = (void *)C_HEAPSTART; /* the current stack frame */ static void *StackFrame = (void *)C_HEAPSTART; /* the current stack frame */
void *HeapStackTop = (void *)C_HEAPSTART; /* the top of the stack */ void *HeapStackTop = (void *)C_HEAPSTART; /* the top of the stack */
void *HeapMemStart = (void *)C_HEAPSTART; void *HeapMemStart = (void *)C_HEAPSTART;
#else # else
static unsigned char HeapMemory[HEAP_SIZE]; /* all memory - stack and heap */ static unsigned char HeapMemory[HEAP_SIZE]; /* all memory - stack and heap */
static void *HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */ static void *HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */
static void *StackFrame = &HeapMemory[0]; /* the current stack frame */ static void *StackFrame = &HeapMemory[0]; /* the current stack frame */
void *HeapStackTop = &HeapMemory[0]; /* the top of the stack */ void *HeapStackTop = &HeapMemory[0]; /* the top of the stack */
# endif
#endif #endif
static struct AllocNode *FreeListBucket[FREELIST_BUCKETS]; /* we keep a pool of freelist buckets to reduce fragmentation */ static struct AllocNode *FreeListBucket[FREELIST_BUCKETS]; /* we keep a pool of freelist buckets to reduce fragmentation */
@ -35,23 +42,34 @@ void ShowBigList()
#endif #endif
/* initialise the stack and heap storage */ /* initialise the stack and heap storage */
void HeapInit() void HeapInit(int StackOrHeapSize)
{ {
int Count; int Count;
int AlignOffset = 0; int AlignOffset = 0;
#ifdef USE_MALLOC_STACK
HeapMemory = malloc(StackOrHeapSize);
#endif
while (((unsigned long)&HeapMemory[AlignOffset] & (sizeof(ALIGN_TYPE)-1)) != 0) while (((unsigned long)&HeapMemory[AlignOffset] & (sizeof(ALIGN_TYPE)-1)) != 0)
AlignOffset++; AlignOffset++;
StackFrame = &HeapMemory[AlignOffset]; StackFrame = &HeapMemory[AlignOffset];
HeapStackTop = &HeapMemory[AlignOffset]; HeapStackTop = &HeapMemory[AlignOffset];
*(void **)StackFrame = NULL; *(void **)StackFrame = NULL;
HeapBottom = &HeapMemory[HEAP_SIZE-sizeof(ALIGN_TYPE)+AlignOffset]; HeapBottom = &HeapMemory[StackOrHeapSize-sizeof(ALIGN_TYPE)+AlignOffset];
FreeListBig = NULL; FreeListBig = NULL;
for (Count = 0; Count < FREELIST_BUCKETS; Count++) for (Count = 0; Count < FREELIST_BUCKETS; Count++)
FreeListBucket[Count] = NULL; FreeListBucket[Count] = NULL;
} }
void HeapCleanup()
{
#ifdef USE_MALLOC_STACK
free(HeapMemory);
#endif
}
/* allocate some space on the stack, in the current stack frame /* allocate some space on the stack, in the current stack frame
* clears memory. can return NULL if out of stack space */ * clears memory. can return NULL if out of stack space */
void *HeapAllocStack(int Size) void *HeapAllocStack(int Size)

37
picoc.c
View file

@ -5,10 +5,10 @@
/* initialise everything */ /* initialise everything */
void Initialise() void Initialise(int StackSize)
{ {
BasicIOInit(); BasicIOInit();
HeapInit(); HeapInit(StackSize);
TableInit(); TableInit();
VariableInit(); VariableInit();
LexInit(); LexInit();
@ -31,6 +31,7 @@ void Cleanup()
VariableCleanup(); VariableCleanup();
TypeCleanup(); TypeCleanup();
TableStrFree(); TableStrFree();
HeapCleanup();
} }
/* platform-dependent code for running programs is in this file */ /* platform-dependent code for running programs is in this file */
@ -63,6 +64,7 @@ int main(int argc, char **argv)
{ {
int ParamCount = 1; int ParamCount = 1;
int DontRunMain = FALSE; int DontRunMain = FALSE;
int StackSize = getenv("STACKSIZE") ? atoi(getenv("STACKSIZE")) : STACK_SIZE;
if (argc < 2) if (argc < 2)
{ {
@ -72,7 +74,7 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
Initialise(); Initialise(StackSize);
if (strcmp(argv[ParamCount], "-s") == 0 || strcmp(argv[ParamCount], "-m") == 0) if (strcmp(argv[ParamCount], "-s") == 0 || strcmp(argv[ParamCount], "-m") == 0)
{ {
@ -110,7 +112,7 @@ int picoc(char *SourceStr)
{ {
int ix; int ix;
Initialise(); Initialise(HEAP_SIZE);
if (SourceStr) { if (SourceStr) {
for (ix=0; ix<strlen(SourceStr); ix++) /* clear out ctrl-z from XMODEM transfer */ for (ix=0; ix<strlen(SourceStr); ix++) /* clear out ctrl-z from XMODEM transfer */
if (SourceStr[ix] == 0x1A) if (SourceStr[ix] == 0x1A)
@ -132,32 +134,5 @@ int picoc(char *SourceStr)
Cleanup(); Cleanup();
return 0; return 0;
} }
# else
# ifdef SRV1_UNIX_HOST
/*
* Howard - this was my guess at what might suit you.
* Please feel free to change this to whatever type of main function suits you best
*/
int picoc(char *SourceFile, int Interactive)
{
Initialise();
if (Interactive)
ParseInteractive();
else
{
if (PlatformSetExitPoint())
{
Cleanup();
return 1;
}
PlatformScanFile(SourceFile);
}
Cleanup();
return 0;
}
# endif
# endif # endif
#endif #endif

View file

@ -371,7 +371,8 @@ struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *P
struct ValueType *TypeCreateOpaqueStruct(struct ParseState *Parser, const char *StructName, int Size); struct ValueType *TypeCreateOpaqueStruct(struct ParseState *Parser, const char *StructName, int Size);
/* heap.c */ /* heap.c */
void HeapInit(); void HeapInit(int StackSize);
void HeapCleanup();
void *HeapAllocStack(int Size); void *HeapAllocStack(int Size);
int HeapPopStack(void *Addr, int Size); int HeapPopStack(void *Addr, int Size);
void HeapUnpopStack(int Size); void HeapUnpopStack(int Size);

View file

@ -37,7 +37,8 @@
/* host platform includes */ /* host platform includes */
#ifdef UNIX_HOST #ifdef UNIX_HOST
# define HEAP_SIZE (128*1024) /* space for the the stack */ # define STACK_SIZE (128*1024) /* space for the the stack */
# define USE_MALLOC_STACK /* stack is allocated using malloc() */
# define USE_MALLOC_HEAP /* heap is allocated using malloc() */ # define USE_MALLOC_HEAP /* heap is allocated using malloc() */
# include <stdio.h> # include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
@ -62,87 +63,67 @@
extern jmp_buf ExitBuf; extern jmp_buf ExitBuf;
#else #else
# ifdef SRV1_UNIX_HOST # ifdef FLYINGFOX_HOST
# define HEAP_SIZE (128*1024) /* space for the the stack */ # define HEAP_SIZE (16*1024) /* space for the heap and the stack */
# define USE_MALLOC_HEAP /* heap is allocated using malloc() */ # define NO_HASH_INCLUDE
# include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
# include <ctype.h> # include <ctype.h>
# include <string.h> # include <string.h>
# include <assert.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include <stdarg.h> # include <stdarg.h>
# include <setjmp.h> # include <setjmp.h>
# include <math.h> # include <math.h>
# define assert(x)
extern jmp_buf ExitBuf; # define BUILTIN_MINI_STDLIB
# undef BIG_ENDIAN
# else # else
# ifdef FLYINGFOX_HOST # ifdef SURVEYOR_HOST
# define HEAP_SIZE (16*1024) /* space for the heap and the stack */ # define NO_FP
# define NO_CTYPE
# define NO_HASH_INCLUDE # define NO_HASH_INCLUDE
# include <stdlib.h> # define NO_MODULUS
# include <ctype.h> # include <cdefBF537.h>
# include <string.h> # include "../string.h"
# include <sys/types.h> # include "../print.h"
# include <stdarg.h> # include "../srv.h"
# include <setjmp.h> # include "../setjmp.h"
# include <math.h> # include "../stdarg.h"
# include "../colors.h"
# include "../neural.h"
# include "../gps.h"
# include "../i2c.h"
# include "../jpeg.h"
# include "../malloc.h"
# define assert(x) # define assert(x)
# define BUILTIN_MINI_STDLIB # undef INTERACTIVE_PROMPT_STATEMENT
# undef INTERACTIVE_PROMPT_LINE
# define INTERACTIVE_PROMPT_STATEMENT "> "
# define INTERACTIVE_PROMPT_LINE "- "
# undef BIG_ENDIAN # undef BIG_ENDIAN
# define NO_CALLOC
# define NO_REALLOC
# define BROKEN_FLOAT_CASTS
# define BUILTIN_MINI_STDLIB
# else # else
# ifdef SURVEYOR_HOST # ifdef UMON_HOST
# define HEAP_SIZE (128*1024) /* space for the heap and the stack */
# define NO_FP # define NO_FP
# define NO_CTYPE
# define NO_HASH_INCLUDE
# define NO_MODULUS
# include <cdefBF537.h>
# include "../string.h"
# include "../print.h"
# include "../srv.h"
# include "../setjmp.h"
# include "../stdarg.h"
# include "../colors.h"
# include "../neural.h"
# include "../gps.h"
# include "../i2c.h"
# include "../jpeg.h"
# include "../malloc.h"
# define assert(x)
# undef INTERACTIVE_PROMPT_STATEMENT
# undef INTERACTIVE_PROMPT_LINE
# define INTERACTIVE_PROMPT_STATEMENT "> "
# define INTERACTIVE_PROMPT_LINE "- "
# undef BIG_ENDIAN
# define NO_CALLOC
# define NO_REALLOC
# define BROKEN_FLOAT_CASTS
# define BUILTIN_MINI_STDLIB # define BUILTIN_MINI_STDLIB
# else # include <stdlib.h>
# ifdef UMON_HOST # include <string.h>
# define HEAP_SIZE (128*1024) /* space for the heap and the stack */ # include <ctype.h>
# define NO_FP # include <sys/types.h>
# define BUILTIN_MINI_STDLIB # include <stdarg.h>
# include <stdlib.h> # include <math.h>
# include <string.h> # include "monlib.h"
# include <ctype.h> # define assert(x)
# include <sys/types.h> # define malloc mon_malloc
# include <stdarg.h> # define calloc(a,b) mon_malloc(a*b)
# include <math.h> # define realloc mon_realloc
# include "monlib.h" # define free mon_free
# define assert(x) # undef PlatformSetExitPoint
# define malloc mon_malloc # define PlatformSetExitPoint()
# define calloc(a,b) mon_malloc(a*b)
# define realloc mon_realloc
# define free mon_free
# undef PlatformSetExitPoint
# define PlatformSetExitPoint()
# endif
# endif # endif
# endif # endif