Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

memory allocation..

memory allocation..

2004-07-13 by tanermutlunun

hello..
   I am using LPC2106 and I need to allocate some data to free flash
memory using malloc.However malloc allocates memory to the heap, but I
want to put it to any free address, how can I do it with malloc or
anyway..
   Thanks in advance

Re: [lpc2000] memory allocation..

2004-07-13 by h s

Option1)
Set the heap address in the linker file.
 
Option2)
Write your own malloc/free functions. Pass them an array address which can be placed anywhere in the unused memory area.    

tanermutlunun <tanermutlunun@...> wrote:
hello..
   I am using LPC2106 and I need to allocate some data to free flash
memory using malloc.However malloc allocates memory to the heap, but I
want to put it to any free address, how can I do it with malloc or
anyway..
   Thanks in advance


Yahoo! Groups SponsorADVERTISEMENT


---------------------------------
Yahoo! Groups Links

   To visit your group on the web, go to:
http://groups.yahoo.com/group/lpc2000/
  
   To unsubscribe from this group, send an email to:
lpc2000-unsubscribe@yahoogroups.com
  
   Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 


		
---------------------------------
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.

[Non-text portions of this message have been removed]

Re: [lpc2000] memory allocation..

2004-07-13 by Robert Adsett

At 04:32 AM 7/13/04 -0700, you wrote:
>Option1)
>Set the heap address in the linker file.

That won't work for using malloc to store data in flash

>
>Option2)
>Write your own malloc/free functions. Pass them an array address which can 
>be placed anywhere in the unused memory area.

This might.  To store data in flash using malloc/free you not only would 
have to write your own malloc & free but ensure (somehow) that you never 
wrote to the flash except through a routine that programmed the flash using 
the IAP facilities.  You would also need to keep track of three kinds of 
buffers in use, not in use but programmed, not in use and available. You 
would need to add logic to clean up the flash area if the micro was reset 
or powered of with the memory structures in an inconsistant state and do 
appropriate moving of data when you had to erase sectors etc...  Also 
writing would take considerably longer than reading.

Using flash to store data really has more in common with a file system than 
malloc/free.

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,
be they legal, genetic, or physical.  If you don't believe me, try to
chew a radio signal. "

                         Kelvin Throop, III

Re: [lpc2000] memory allocation..

2004-07-13 by Taner Mutlu

Thanks for help, 
  I still believe that malloc/free should work..
I have atteched some code illustrating malloc/free and
the linker file (taken from an example coming with
KEIL)

  I have loaded it but couldnot find where it actually
allocates the Text[] array..
  Am  I missing some point..
  Thanks in advance
  

=====
Taner Mutlu 
Elektronik M�h.
GLOBAL Bilgisayar ve Kontrol Sistemleri
taner@...


		
__________________________________
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo 

[Non-text portions of this message have been removed]

Re: [lpc2000] memory allocation..

2004-07-13 by Robert Adsett

At 06:04 AM 7/13/04 -0700, you wrote:
>Thanks for help,
>   I still believe that malloc/free should work..

For RAM yes, for Flash unlikely, although with C++ and a smart pointer 
setup you might be able to simulate it.

>I have atteched some code illustrating malloc/free and
>the linker file (taken from an example coming with
>KEIL)
>
>   I have loaded it but couldnot find where it actually
>allocates the Text[] array..
>   Am  I missing some point..

Maybe that Yahoo Groups strips attachments ;)

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,
be they legal, genetic, or physical.  If you don't believe me, try to
chew a radio signal. "

                         Kelvin Throop, III

Re: memory allocation..

2004-07-13 by ian48harry

Guys:

For this and earlier FLASH programming threads.

Don't forget that you can program the FLASH without using the built 
in capability from boot.  The programming spec is well defined and 
can be included in your own code to either program new sections and 
updates of code or to load data into FLASH.

Ian

--- In lpc2000@yahoogroups.com, Robert Adsett <subscriptions@a...> 
wrote:
> At 04:32 AM 7/13/04 -0700, you wrote:
> >Option1)
> >Set the heap address in the linker file.
> 
> That won't work for using malloc to store data in flash
> 
> >
> >Option2)
> >Write your own malloc/free functions. Pass them an array address 
which can 
> >be placed anywhere in the unused memory area.
> 
> This might.  To store data in flash using malloc/free you not only 
would 
> have to write your own malloc & free but ensure (somehow) that you 
never 
> wrote to the flash except through a routine that programmed the 
flash using 
> the IAP facilities.  You would also need to keep track of three 
kinds of 
> buffers in use, not in use but programmed, not in use and 
available. You 
> would need to add logic to clean up the flash area if the micro was 
reset 
> or powered of with the memory structures in an inconsistant state 
and do 
> appropriate moving of data when you had to erase sectors etc...  
Also 
> writing would take considerably longer than reading.
> 
> Using flash to store data really has more in common with a file 
system than 
> malloc/free.
> 
> Robert
> 
> " 'Freedom' has no meaning of itself.  There are always 
restrictions,
> be they legal, genetic, or physical.  If you don't believe me, try 
to
Show quoted textHide quoted text
> chew a radio signal. "
> 
>                          Kelvin Throop, III

Re: [lpc2000] memory allocation..

2004-07-13 by Taner Mutlu

yes there is a problem with yahoo groups..

#include <stdlib.h>

/* Mapped to Read-Only Data Section (.rodata) */

const char Text[] =
  "This is a dummy text which will be loaded into the
.rodata section.";

const unsigned short TableS[16] = {
  0x0123, 0x4567, 0x89ab, 0xcdef, 0xfedc, 0xba98,
0x7654, 0x3210,
  0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb,
0xccdd, 0xeeff
};
const unsigned long  TableL[8] = {
  0x01234567, 0x89abcdef, 0xfedcba98, 0x76543210,
  0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff
};


/* Mapped to Intialized Data Section (.data) in
Internal RAM */
unsigned char FillVal = 0x00;


/* Mapped to Uninitialized Data Section (.bss) in
External RAM */

unsigned char Buffer[32768];

struct {
  unsigned short us[16];
  unsigned long  ul[8];
} Table;


/* Mapped to Read-Only Code Section (.text) */

int main (void)  {
  char *m, *p, *t;
  int   i;

  for (i = 0; i < 16; i++) {
    Table.us[i] = TableS[i];       /* Load TableS */  

  }
  for (i = 0; i < 8; i++) {
    Table.ul[i] = TableL[i];       /* Load TableL */
  }

  for (i = 0; i < sizeof(Buffer); i++) {
    Buffer[i] = FillVal;           /* Fill Buffer */
  }

  m = malloc(1024);     /* Allocate Memory on Heap */
  if (m == NULL) return (1);

  p = m;
  t = (char *)&Text[0];
  while (*t) {
    *p++ = *t++;                /* Copy Text to Heap
*/
  }
  *p = 0;                      /* Terminate String */

  free(m);                /* Free Allocated Memory */ 
     

  return (0);
}

=====
Taner Mutlu 
Elektronik M\ufffdh.
GLOBAL Bilgisayar ve Kontrol Sistemleri
taner@...
www.tanermutlu.5u.com


	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail

Re: [lpc2000] memory allocation..

2004-07-13 by Robert Adsett

At 07:05 AM 7/13/04 -0700, you wrote:
>yes there is a problem with yahoo groups..

Well, I consider it a feature.  That alone keeps the size of messages to a 
manageable level and stops the current crop of worms dead in their 
tracks.  But I'm heading off topic.


>#include <stdlib.h>
>
>/* Mapped to Read-Only Data Section (.rodata) */
>
>const char Text[] =
>   "This is a dummy text which will be loaded into the
>.rodata section.";

Keil uses GNU so unless they've done something odd in the linker file this 
will be in flash along with all other const data.


>   m = malloc(1024);     /* Allocate Memory on Heap */
>   if (m == NULL) return (1);

(A)
And this will be in RAM (SRAM if you want to get particular).  The heap 
location is set by the startup (crt0.s) usually using information supplied 
by the link file at link time.


>   p = m;
>   t = (char *)&Text[0];
>   while (*t) {
>     *p++ = *t++;                /* Copy Text to Heap
>*/
>   }
>   *p = 0;                      /* Terminate String */

(B)
This copies from flash to ram.


>   free(m);                /* Free Allocated Memory */

(C)
and notifies the heap manager that the area can be re-used in a subsequent 
malloc call.

To do this with malloc allocating from flash instead of SRAM then you would 
have to rewrite malloc for step A so that it would use the IAP routines to 
set up the appropriate structures in flash (and observe the flash 
limitations).  The in step B you could not use a simple assignment but 
would again need to call the IAP routines to copy to flash and finally in 
step C you would have to mark the area as unused and at some point do some 
sort of garbage collection to actually erase unused areas (while preserving 
used areas) so they could be reused in a subsequent malloc call.  As I said 
more like a file system than heap management.

Robert


" 'Freedom' has no meaning of itself.  There are always restrictions,
be they legal, genetic, or physical.  If you don't believe me, try to
chew a radio signal. "

                         Kelvin Throop, III

Re: [lpc2000] memory allocation..

2004-07-13 by Taner Mutlu

Thank you very much Robert, that was clear enough..


=====
Taner Mutlu 
Elektronik M\ufffdh.
GLOBAL Bilgisayar ve Kontrol Sistemleri
taner@...
www.tanermutlu.5u.com


		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.