Shop OBEX P1 Docs P2 Docs Learn Events
Need for _FREE constant — Parallax Forums

Need for _FREE constant

Ron CzapalaRon Czapala Posts: 2,418
edited 2010-07-29 03:12 in Propeller 1
I just bought the Propeller Starter Kit and am trying to figure out why you would ever use the _FREE constant.

The manual says "Use _FREE if an application requires a minimum amout of of free memory in order to run properly".

Being·a newbie regarding the Propeller/Spin/etc, what situation would cause that situation?·

I get _STACK but·wonder about _FREE. A search to Forum didn't help...

Thanks,
· Ron

NOTE: I have downloaded a bunch of objects and a search of those and the Propeller tool folder only revealed one spin file with a _FREE constant.
It was in Graphics_Palette.spin but I' not sure what the comment means:
············ _free = ($3000 + $3000) >> 2········· 'accomodate bitmap buffers··

Post Edited (Ron Czapala) : 7/28/2010 10:24:55 PM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-28 22:47
    Ron,

    You're not alone in your confusion regarding _stack vs. _free. I'm not sure I have it figured out either; and, as you've noticed, the docs aren't much help. What does seem certain is that both constant declarations reserve space at the top of RAM, ending at $7FFF, with the free space residing above the stack. What is less certain is where the stack starts -- at the top of stack space building down or at the bottom building up? And, if it's the latter, does the Spin interpreter have a mechanism to protect the free space from stack overflows? If so, what happens then? And if not, what's the point of having separate declarations? Enquiring minds want to know! TM

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-28 22:56
    The stack starts at the end of the program (and its variables area) and goes upward (towards $7FFF) in memory. _stack and _free really do the same thing in that they tell the Spin compiler how much memory is expected to be used past the variables area. The two values are added together and used for error checking (out of memory!) by the compiler. Typically you'd use _stack to supply an estimate of the amount of stack space expected to be used and you'd use _free to supply the amount of space above the stack to be used for things like buffers or display buffers that start at the end of memory and are allocated downwards (towards zero).

    The compiler and the interpreter do not check for stack overflow. It's possible to add a rough check to your program. FemtoBasic does this since it uses a recursive descent parser for expressions and the stack can grow indefinitely without that.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-28 23:14
    Mike & Phil,

    Thanks for the explanation. I was wondering what would the reserved free space be used for, which Mike indicated buffers and display buffers.

    I am really looking forward to delving into the Propller's capabilities. Basic Stamps are fun but rather limited. Arduinos have more memory but need interrupt handling for many situations. The Propeller certainly seems to have the advantage...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-28 23:23
    Thanks, Mike. So it would appear that there's no good reason for separate declarations, right? Just make one of them big enough to accommodate both needs.

    -Phil
  • RossHRossH Posts: 5,519
    edited 2010-07-29 00:16
    @Phil,

    I think they should be kept separate, since they generally change independently. For example, if I have a project that uses video then the space I need for video buffers may remain constant, but the space need for stack may change several times over the life of the project. Or I may change the resultion of video I use (and hence the size of free space I need) but the stack space remains constant.

    Keeping them separate allows me to adjust them as I need to, without having to remember to add them together all the time.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-29 03:12
    I really don't see the point -- especially if there's no checking for overflow into the _free area. Both have to be declared in the top-level program, so deep knowledge of the external object requirements is necessary, which violates encapsulation; and, typically, _stack is never declared anyway. If the program's gonna croak, it will croak regardless of whether _stack is declared or not. It would have made more sense to build the stack down from the beginning of the free area and croak gracefully if the program/VAR area were encroched upon.

    -Phil
Sign In or Register to comment.