Author Topic: reducing ram footprint  (Read 12335 times)

Offline alager

  • Jr. Member
  • **
  • Posts: 92
    • View Profile
reducing ram footprint
« on: October 10, 2012, 09:01:54 PM »
I'm not sure how the other platforms handle this, but in CW 7.2 the following code still uses 24 bytes of ram, as verified by the xMAP file.
Code: [Select]
const CHAR *cMimeTable[] = {                                         // keep the ordering in the table to match the MIME type defines below!!
        (const CHAR *)"HTM",                                             // HTML file - will be interpreted by web server
        (const CHAR *)"JPG",                                             // JPG image
        (const CHAR *)"GIF",                                             // GIF image
        (const CHAR *)"CSS",                                             // CSS Cascading Style Sheets
        (const CHAR *)"JS",                                              // Java script
        (const CHAR *)"BIN",                                             // binary data file
        (const CHAR *)"TXT",                                             // text data file
        (const CHAR *)"ICO",                                             // icon
        (const CHAR *)"???",                                             // all other types will be displayed as unknown
    };

Adding const after the *, not only forces the pointers into flash, but also the values being pointed to:
Code: [Select]
const CHAR * const cMimeTable[] = {                                         // keep the ordering in the table to match the MIME type defines below!!
        (const CHAR *)"HTM",                                             // HTML file - will be interpreted by web server
        (const CHAR *)"JPG",                                             // JPG image
        (const CHAR *)"GIF",                                             // GIF image
        (const CHAR *)"CSS",                                             // CSS Cascading Style Sheets
        (const CHAR *)"JS",                                              // Java script
        (const CHAR *)"BIN",                                             // binary data file
        (const CHAR *)"TXT",                                             // text data file
        (const CHAR *)"ICO",                                             // icon
        (const CHAR *)"???",                                             // all other types will be displayed as unknown
    };

Be sure to update the externs for this also.

Aaron

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3240
    • View Profile
    • uTasker
Re: reducing ram footprint
« Reply #1 on: October 10, 2012, 10:06:09 PM »
Hi Aaron

Thanks for this tip - I never realised that CW was not putting these into FLASH!

When building with VisualStudio the compiler warns about the two consts being used. I will check with CW the next time I work with it and see whether there is maybe also an alternative method to force the same.

Regards

Mark

Offline alager

  • Jr. Member
  • **
  • Posts: 92
    • View Profile
Re: reducing ram footprint
« Reply #2 on: October 10, 2012, 10:16:46 PM »
Interesting, I'm using VS express 2010 and I don't get any warnings about it.
I found the above example after implementing my own lists, and seeing my ram get sucked up.  So I went searching and only found the one instance in config.h.  It's special to the pointer.  If the lists are just arrays, then things end up in rom as expected.

My code ended up being like this:
Code: [Select]
static const char * const MyStrings2[] = {"$OUTLET",
"$BANK",
0
};

with a pointer to it like this:
Code: [Select]
const char * const *pString;
pString = MyStrings2;

I'm amazed it worked at all actually.   :D

Aaron

Offline alager

  • Jr. Member
  • **
  • Posts: 92
    • View Profile
Re: reducing ram footprint
« Reply #3 on: October 12, 2012, 05:14:09 PM »
A few more optimizations.  Turns out that the optimizer is putting
Code: [Select]
const unsigned char cucNullMACIP[MAC_LENGTH] = { 0, 0, 0, 0, 0, 0 };from config.h into .bss because it is initialized to all 0.  I got it to behave as intended by wrapping it in a #pragma
Code: [Select]
#pragma explicit_zero_data on
const unsigned char cucNullMACIP[MAC_LENGTH] = { 0, 0, 0, 0, 0, 0 };   //how to force this into rom?  right now because it's all 0s, it is being put into bss
#pragma explicit_zero_data off

I'm being so picky about this because I'm down to 3 bytes of stack and 12 bytes of heap.  The above change should give me 9 bytes of stack free. 

Aaron