From: MaartenBrock Date: Sun, 10 Dec 2006 15:04:52 +0000 (+0000) Subject: * device/include/malloc.h: removed init_dynamic_memory X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=a18a4b38667685c800b464aa6bc72751f8813025;p=fw%2Fsdcc * device/include/malloc.h: removed init_dynamic_memory * device/lib/malloc.c: made init_dynamic_memory static and automatically call it once from malloc. Also use _sdcc_heap[] from _heap.c * device/lib/_heap.c: new, added, contains _sdcc_heap[] * device/lib/libsdcc.lib, * device/lib/Makefile.in, * support/regression/ports/mcs51-xstack-auto/spec.mk: added _heap.c * doc/sdccman.lyx: documented use of new _heap.c * support/regression/tests/malloc.c: removed init_dynamic_memory * src/cdbFile.c(spacesToUnderscores): new function, (cdbWriteEndFunction, cdbWriteCLine): use spacesToUnderscores, fixes bug 1068030 git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4508 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/ChangeLog b/ChangeLog index 3c9251ec..54969365 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2006-12-10 Maarten Brock + + * device/include/malloc.h: removed init_dynamic_memory + * device/lib/malloc.c: made init_dynamic_memory static and automatically + call it once from malloc. Also use _sdcc_heap[] from _heap.c + * device/lib/_heap.c: new, added, contains _sdcc_heap[] + * device/lib/libsdcc.lib, + * device/lib/Makefile.in, + * support/regression/ports/mcs51-xstack-auto/spec.mk: added _heap.c + * doc/sdccman.lyx: documented use of new _heap.c + * support/regression/tests/malloc.c: removed init_dynamic_memory + * src/cdbFile.c(spacesToUnderscores): new function, + (cdbWriteEndFunction, cdbWriteCLine): use spacesToUnderscores, fixes bug + 1068030 + 2006-12-09 Borut Razem * support/cpp2/sdcc.h: prevent multiple inclusion diff --git a/device/include/malloc.h b/device/include/malloc.h index fcaca83f..0ddf4f8c 100644 --- a/device/include/malloc.h +++ b/device/include/malloc.h @@ -36,7 +36,6 @@ void free (void * ptr); #else -extern void init_dynamic_memory(void __xdata * heap, unsigned int size); extern void __xdata * calloc (size_t nmemb, size_t size); extern void __xdata * malloc (size_t size); extern void __xdata * realloc (void * ptr, size_t size); diff --git a/device/lib/Makefile.in b/device/lib/Makefile.in index a41c7d17..1839140a 100644 --- a/device/lib/Makefile.in +++ b/device/lib/Makefile.in @@ -90,7 +90,7 @@ SOURCES = _autobaud.c _bp.c _decdptr.c \ _strncpy.c _strpbrk.c _strrchr.c _strspn.c \ _strstr.c _strtok.c \ _memcmp.c _memcpy.c _memmove.c _memset.c \ - calloc.c malloc.c realloc.c free.c \ + _heap.c calloc.c malloc.c realloc.c free.c \ printf_large.c sprintf.c vprintf.c puts.c gets.c \ printf_fast.c printf_fast_f.c printf_tiny.c printfl.c \ assert.c time.c bpx.c \ @@ -151,7 +151,7 @@ XA51SOURCES = _atof.c _atoi.c _atol.c _schar2fs.c \ _strncpy.c _strpbrk.c _strrchr.c _strspn.c \ _strstr.c _strtok.c \ _uchar2fs.c _uint2fs.c _ulong2fs.c \ - calloc.c malloc.c realloc.c free.c \ + _heap.c calloc.c malloc.c realloc.c free.c \ puts.c gets.c \ printf_large.c puts.c gets.c \ assert.c time.c \ @@ -182,7 +182,7 @@ HC08SOURCES = \ _strncpy.c _strpbrk.c _strrchr.c _strspn.c \ _strstr.c _strtok.c \ _memcmp.c _memcpy.c _memmove.c _memset.c \ - calloc.c malloc.c realloc.c free.c \ + _heap.c calloc.c malloc.c realloc.c free.c \ printf_large.c sprintf.c vprintf.c puts.c gets.c \ assert.c time.c \ fabsf.c frexpf.c ldexpf.c expf.c powf.c sincosf.c sinf.c \ diff --git a/device/lib/_heap.c b/device/lib/_heap.c new file mode 100644 index 00000000..fc91fa8f --- /dev/null +++ b/device/lib/_heap.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + _heap.c - memory heap for malloc and friends + + Copyright (C) 2006 - Maarten Brock, sourceforge.brock@dse.nl + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +/* + This is the default heap. If you need a different size (make a copy and) + recompile it with -D HEAP_SIZE= where is whatever you need. + Link the resulting object explicitly with your project. +*/ + +#ifndef HEAP_SIZE +#define HEAP_SIZE 1024 +#endif + +__xdata char _sdcc_heap[HEAP_SIZE]; +const unsigned int _sdcc_heap_size = HEAP_SIZE; diff --git a/device/lib/libsdcc.lib b/device/lib/libsdcc.lib index eaad6ac5..76d60173 100644 --- a/device/lib/libsdcc.lib +++ b/device/lib/libsdcc.lib @@ -39,6 +39,7 @@ labs rand _atoi _atol +_heap calloc malloc realloc diff --git a/device/lib/malloc.c b/device/lib/malloc.c index 8c8d5c7f..33f8fedd 100644 --- a/device/lib/malloc.c +++ b/device/lib/malloc.c @@ -150,39 +150,24 @@ malloc (unsigned int size) #define HEADER_SIZE sizeof(MEMHEADER) - MEMHEADER xdata * _sdcc_first_memheader; + MEMHEADER xdata * _sdcc_first_memheader = NULL; - void init_dynamic_memory(void xdata * heap, unsigned int size) + extern xdata char _sdcc_heap[]; + extern const unsigned int _sdcc_heap_size; + + static void init_dynamic_memory(void) { + char xdata * heap = (char xdata *)_sdcc_heap; + unsigned int size = _sdcc_heap_size; - //This function MUST be called after the RESET. - //Parameters: heap - pointer to memory allocated by the linker - // size - size of this memory pool - //Example: - // #define DYNAMIC_MEMORY_SIZE 0x2000 - // ..... - // unsigned char xdata dynamic_memory_pool[DYNAMIC_MEMORY_SIZE]; - // unsigned char xdata * current_buffer; - // ..... - // void main(void) - // { - // ... - // init_dynamic_memory(dynamic_memory_pool,DYNAMIC_MEMORY_SIZE); - // Now it is possible to use malloc. - // ... - // current_buffer = malloc(0x100); - // - // - char xdata * array = (char xdata *)heap; - - if ( !array ) //Reserved memory starts at 0x0000 but that's NULL... + if ( !heap ) //Reserved memory starts at 0x0000 but that's NULL... { //So, we lost one byte! - array++; + heap++; size--; } - _sdcc_first_memheader = (MEMHEADER xdata * ) array; + _sdcc_first_memheader = (MEMHEADER xdata * ) heap; //Reserve a mem for last header - _sdcc_first_memheader->next = (MEMHEADER xdata * )(array + size - sizeof(MEMHEADER xdata *)); + _sdcc_first_memheader->next = (MEMHEADER xdata * )(heap + size - sizeof(MEMHEADER xdata *)); _sdcc_first_memheader->next->next = (MEMHEADER xdata * ) NULL; //And mark it as last _sdcc_first_memheader->len = 0; //Empty and ready. } @@ -193,8 +178,13 @@ malloc (unsigned int size) register MEMHEADER xdata * new_header; register void xdata * ret; - if (size>(0xFFFF-HEADER_SIZE)) return (void xdata *) NULL; //To prevent overflow in next line + if (size>(0xFFFF-HEADER_SIZE)) + return (void xdata *) NULL; //To prevent overflow in next line size += HEADER_SIZE; //We need a memory for header too + + if (!_sdcc_first_memheader) + init_dynamic_memory(); + current_header = _sdcc_first_memheader; CRITICAL { diff --git a/doc/sdccman.lyx b/doc/sdccman.lyx index 5ba1663d..8aba4206 100644 --- a/doc/sdccman.lyx +++ b/doc/sdccman.lyx @@ -16056,88 +16056,35 @@ not \layout Standard -Before using dynamic memory allocation +As of SDCC 2.6.2 you no longer need to call an initialization routine before + using dynamic memory allocation \begin_inset LatexCommand \index{dynamic memory allocation (malloc)} \end_inset - with SDCC, you have to provide heap + and a default heap \begin_inset LatexCommand \index{heap (malloc)} \end_inset - space for malloc to allocate memory from -\family typewriter -. + space of 1024 bytes is provided for malloc to allocate memory from. \family default -You can acomplish this by including the following code into your source: +If you need a different heap size you need to recompile _heap.c with the + required size defined in HEAP_SIZE. It is recommended to make a copy of this + file into your project directory and compile it there with: \layout Verse \family typewriter -#include -\begin_inset LatexCommand \index{malloc.h} - -\end_inset - - /* calloc -\begin_inset LatexCommand \index{calloc} - -\end_inset - -, malloc -\begin_inset LatexCommand \index{malloc} - -\end_inset - -, realloc -\begin_inset LatexCommand \index{realloc} - -\end_inset - -, and free -\begin_inset LatexCommand \index{free (malloc)} - -\end_inset - - */ -\newline - -\newline -#define HEAPSIZE 0x1000 /* Adjust depending on available memory */ -\newline -unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory - */ +sdcc -c _heap.c -D HEAD_SIZE=2048 \newline +\family default +And then link it with: +\layout Verse -\newline -void main (void) -\newline -{ -\newline -\SpecialChar ~ -\SpecialChar ~ -\SpecialChar ~ -/* Your variable declarations come here*/ -\newline -\SpecialChar ~ -\SpecialChar ~ -\SpecialChar ~ -... -\newline -\SpecialChar ~ -\SpecialChar ~ -\SpecialChar ~ -init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE); -\newline -\SpecialChar ~ -\SpecialChar ~ -\SpecialChar ~ -... - /* Rest of your code*/ -\newline -} +\family typewriter +sdcc main.rel _heap.rel \newline \layout Subsection diff --git a/src/cdbFile.c b/src/cdbFile.c index 3f7906a9..ccfa1f3a 100644 --- a/src/cdbFile.c +++ b/src/cdbFile.c @@ -45,6 +45,33 @@ DEBUGFILE cdbDebugFile = FILE *cdbFilePtr = NULL; char *cdbModuleName = NULL; +/****************************************************************** + * spacesToUnderscores - replace all non alpha-numerics with + * underscores + * + * + *****************************************************************/ + +static char * +spacesToUnderscores (char *dest, const char *src, size_t len) +{ + unsigned int i; + char *p; + + assert(dest != NULL); + assert(src != NULL); + assert(len > 0); + + --len; + for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) { + *p++ = (isspace((unsigned char)*src) || (*src == '-')) ? '_' : *src; + } + *p = '\0'; + + return dest; +} + + /****************************************************************** * * @@ -127,6 +154,7 @@ int cdbWriteEndFunction(symbol *pSym, iCode *ic, int offset) sprintf (debugSym, "C$%s$%d$%d$%d", FileBaseName (ic->filename), pSym->lastLine, ic->level, ic->block); + spacesToUnderscores (debugSym, debugSym, sizeof (debugSym)); emitDebuggerSymbol (debugSym); } @@ -263,6 +291,7 @@ int cdbWriteCLine(iCode *ic) sprintf (debugSym, "C$%s$%d$%d$%d", FileBaseName (ic->filename), ic->lineno, ic->level, ic->block); + spacesToUnderscores (debugSym, debugSym, sizeof (debugSym)); emitDebuggerSymbol (debugSym); return 1; diff --git a/support/regression/ports/mcs51-xstack-auto/spec.mk b/support/regression/ports/mcs51-xstack-auto/spec.mk index 2b01fae0..96b2a74d 100644 --- a/support/regression/ports/mcs51-xstack-auto/spec.mk +++ b/support/regression/ports/mcs51-xstack-auto/spec.mk @@ -39,7 +39,7 @@ SOURCES = _atoi.c _atol.c _autobaud.c _bp.c _schar2fs.c \ _strncpy.c _strpbrk.c _strrchr.c _strspn.c \ _strstr.c _strtok.c \ _uchar2fs.c _uint2fs.c _ulong2fs.c \ - calloc.c malloc.c realloc.c free.c \ + _heap.c calloc.c malloc.c realloc.c free.c \ serial.c ser_ir.c printfl.c \ printf_large.c sprintf.c vprintf.c puts.c gets.c \ assert.c time.c bpx.c diff --git a/support/regression/tests/malloc.c b/support/regression/tests/malloc.c index 593f8c5e..683da53b 100644 --- a/support/regression/tests/malloc.c +++ b/support/regression/tests/malloc.c @@ -6,7 +6,9 @@ #include #endif +#if defined(SDCC_pic16) xdata char heap[100]; +#endif void testMalloc(void) @@ -18,11 +20,9 @@ testMalloc(void) #if !defined(__GNUC__) && !defined(SDCC_gbz80) && !defined(SDCC_z80) #if defined(SDCC_pic16) _initHeap(heap, sizeof heap); -#else - init_dynamic_memory(heap, sizeof(heap)); #endif - p1 = malloc(200); + p1 = malloc(2000); ASSERT(p1 == NULL); LOG(("p1 == NULL when out of memory\n")); #ifdef PORT_HOST