Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #23490

Re: Use RAM to modify a field

From Rob Gaddi <rgaddi@highlandtechnology.invalid>
Newsgroups comp.arch.embedded
Subject Re: Use RAM to modify a field
Date 2016-04-04 21:48 +0000
Organization A noiseless patient Spider
Message-ID <ndunfq$rgk$1@dont-email.me> (permalink)
References (3 earlier) <mJudnSzjsJI5OJ_KnZ2dnUU7-R-dnZ2d@giganews.com> <mJudnS_jsJLVKJ_KnZ2dnUU7-R-dnZ2d@giganews.com> <nducvq$g02$1@dont-email.me> <mJudnSjjsJICT5_KnZ2dnUU7-R-dnZ2d@giganews.com> <M46dnd3PoYfsQJ_KnZ2dnUU7-fPNnZ2d@giganews.com>

Show all headers | View raw


srao wrote:

>>On Mon, 04 Apr 2016 18:49:30 +0000, Rob Gaddi wrote:
>>
>>> Tim Wescott wrote:
>>> 
>>>> If you want to permanently allocate some space at compile time, from
>>>> within a function, then you need to use "static" -- otherwise the
>>>> compiler will try to create it on the heap or on the stack.  For an
> 8kB
>>>> hunk of memory, this would probably cause Bad Things to happen.
>>>>
>>>> void my_function(void)
>>>> {
>>>>   char bob[8192];  // Allocates on stack or heap -- ick static char
> sue
>>>>   [8192];  // allocates in RAM, only visible
>>>>                            // within this function
>>>> }
>>>>
>>>> Note that doing this, with this large a chunk of memory, is probably
> an
>>>> indication that you're doing something wrong.  I could easily see
> your
>>>> buffer as being static within a C file (or a C++ structure) that's
>>>> dedicated to talking to flash -- but I can't easily see it within a
>>>> function.
>>>>
>>>>
>>> Eh, I've definitely decided I'm just going to drop a megablock like
> that
>>> on the stack.  Especially if:
>>>   A) You're trying to be MISRA or MISRish, and have nixed malloc from
>>> your vocabulary; and
>>>   B) You've got a single uniform memory, i.e. on-die RAM is enough.
>>> 
>>> Once you don't have a heap the only things left are static allocations
>>> and the stack, so you might as well have a giant stack.
>>> 
>>> The nice thing about putting it on the stack is that it's
> self-cleaning.
>>>  Unlike with heap allocation there's no concerns about fragmentation
>>> or running out of memory; you hit that closing brace and the
> allocation
>>> neatly and perfectly unwinds itself.  Especially since, for something
>>> like a flash update, it's not like you're going to be doing any kinds
> of
>>> deeply nested calls underneath it; you're basically at the bottom of
>>> where you're going to take the stack at that point.
>>> 
>>> It "feels wrong", definitely, because you know that that's not how
> it's
>>> done.  But it works beautifully and gives you fully deterministic
>>> multipurposing of memory.
>>
>>That would work if you're running a single thread of execution.  But if 
>>you're running multiple threads of execution (as in an RTOS) then you'd 
>>need to provide a humongous block of RAM for each task's thread, or you'd
>
>>have to police which threads use which functions.
>>
>>-- 
>>
>>Tim Wescott
>>Wescott Design Services
>>http://www.wescottdesign.com
>
> the 80C186 does have 64K bytes of STACK space. Though it actually is more
> like a single threaded execution, and is also a part of a flash update. If
> stack is indeed a best way to go, do you mean we would use a local
> variable to store the whole 8K buffer array? 
> ---------------------------------------
> Posted through http://www.EmbeddedRelated.com

"Best" is relative.  Using the stack for this is, IMO, "not clearly
stupid".

There are problems.  You need to be dead sure that you're not going to
overflow the stack; and that includes if you get an interrupt while
you're in the flash update routine, which is going to push your stack up
as well.

If you've got no interrupts and are completely deterministic
in your execution then it gets pretty easy to determine whether you've
got a risk: just check your stack pointer when you hit that routine as
is, and make sure there's 8K left.  Or, better, that there's well more
than 8K left because you don't want to run into a problem later.

If you're not deterministic than you need to make sure that even under
the worst case execution you're not going to overrun it.  I tend to
have my initialization code fill the entire stack with 0xDEADBEEF.
That way, at any time, I can run a stack check that will give me the
high water mark on stack usage, so I can run around stress testing my
application to make sure I've got plenty of margin.

Another approach is basically a "monoheap".  You block off a static
region of memory, similar to what Tim and others have suggested, and you
make sure that it only has one use at a time; if your flash update code
is using it than no one else is.  Then you don't have to worry about
malloc/free and fragmentation because there's only one big chunk; you've
got dibs on all of it or none.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order.  See above to fix.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-01 19:37 -0500
  Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-02 19:35 -0500
    Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-04 08:55 +0200
      Re: Use RAM to modify a field Jack <jack4747@gmail.com> - 2016-04-04 00:52 -0700
      Re: Use RAM to modify a field Grant Edwards <invalid@invalid.invalid> - 2016-04-04 14:24 +0000
      Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:25 -0500
        Re: Use RAM to modify a field upsidedown@downunder.com - 2016-04-04 21:42 +0300
          Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:41 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:02 +0200
    Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 11:42 -0500
    Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 12:09 -0500
      Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:34 -0500
        Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 13:41 -0500
          Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 18:49 +0000
            Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:46 -0500
              Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 16:32 -0500
                Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 21:48 +0000
          Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:55 -0500
            Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 17:49 -0500
              Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 12:39 -0500
                Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-05 15:55 -0500
                Re: Use RAM to modify a field lasselangwadtchristensen@gmail.com - 2016-04-05 14:45 -0700
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 19:22 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:29 +0200
        Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:50 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:38 +0200
            Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:11 -0500
            Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:46 -0500
        Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:17 +0200
          Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 09:58 +0300
            Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:54 -0500
              Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 22:46 +0300
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 18:21 -0500
                Re: Use RAM to modify a field Reinhardt Behm <rbehm@hushmail.com> - 2016-04-06 12:58 +0800
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-06 11:57 -0500
                Re: Use RAM to modify a field Dave Nadler <drn@nadler.com> - 2016-04-06 10:20 -0700
          Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:04 -0500

csiph-web