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


Groups > sci.electronics.design > #482672

Re: Paper on dynamic memory allocation for real-time systems

From upsidedown@downunder.com
Newsgroups sci.electronics.design
Subject Re: Paper on dynamic memory allocation for real-time systems
Message-ID <72euuctnmge4easu90hfa67b1fkpic3e7d@4ax.com> (permalink)
References <%D4GB.50140$6V2.21096@fx21.iad> <osihhn$r04$2@dont-email.me> <85uqucp76e1m9j14ito1m9kqr5fnukkg2o@4ax.com> <UnnHB.93265$Iu1.44719@fx20.iad>
Date 2017-10-24 15:56 +0300
Organization Elisa Customer

Show all headers | View raw


On Mon, 23 Oct 2017 10:53:39 -0400, bitrex
<bitrex@de.lete.earthlink.net> wrote:

>On 10/23/2017 01:17 AM, upsidedown@downunder.com wrote:
>> On Sun, 22 Oct 2017 16:38:15 -0000 (UTC), Cursitor Doom
>> <curd@notformail.com> wrote:
>> 
>>> On Thu, 19 Oct 2017 12:44:43 -0400, bitrex wrote:
>>>
>>>> "Dynamic memory allocation has been used for decades. However, it has
>>>> seldom been used in real-time systems since the worst case of spatial
>>>> and temporal requirements for allocation and deallocation operations is
>>>> either unbounded or bounded but with a very large bound."
>>>
>>>
>>> Never really understood why C had such a notorious issue with DMA (the
>>> free() function in particular) but I'm willing to be disabused of my
>>> outstanding ignorance of the matter if anyone knows.
>> 
>> I have often wondered, why C-programmers are so keen of using dynamic
>> memory allocation instead of stack allocation. Is it because some
>> early processors had a very limited stack, typically intended only for
>> return addresses ?
>> 
>> In Pascal, you could allocate short lived memory areas on  the stack
>> and pass a pointer to that stack area as a procedure parameter. Of
>> course, this was simplified by having source level nested procedures,
>> so you didn't have to explicitly pass these pointers to low level
>> procedures, since these allocations were readily visible on the outer
>> source code nesting level.
>> 
>
>The stack on modern desktop machines is usually limited as well to 
>something like 1 or 8 MB. 

On a 32 bit virtual memory machine, why on earth would the stack be
limited to a few megabytes ? Usually the stack is set at 2 GiB growing
downwards and heap above code growing upwards towards th stack. There
might be a gigabyte or more between heap and stack. Initially, most of
this area between is marked as unmapped pages. When both stack and
heap grows, now and then a memory access hits a page slightly below
currently allocated stack or slightly above currently allocated heap,
a page fault will occur. The OS then checks, is it time to extend the
stack or heap. If OK, the inaccessible page is returned as a demand
zero page to the stack or heap an program execution continues. 

The practical limitation is the size of the page file. With several GB
of page file, this might not be an issue, but the OS can always
determine, if there is a risk that the stack and heap areas collide
and refuse allocation.

The statement about 1 or 8 MiB sizes sounds more like the _initial_
stack size mapped to a created process, but after consuming it, 1 to 8
MiB chunks are allocated to the process over and over again.

Things get hairy on 32 bit multithread processes, since each thread
needs a private stack/heap so the size of that area should be known at
thread creation time. 

On 64 bit platforms, you could allocate stack/heap areas say every 100
GiB so no big problems.

>This is to protect the OS; if a heap 
>allocation call goes awry or you attempt to dereference a heap allocated 
>pointer outside the program's virtual address space the worst that can 
>happen is you get a bad alloc or segfault and your program crashes.

The OS can protect itself, no big problem.

>There's no such intrinsic protection for the stack; without some kind of 
>artificial limitation an AWOL recursive function could corrupt the
>OS kernel at best and be a huge security risk at worst (write arbitrary 
>code into the OS' address space? nice.)

On any virtual memory system, the OS can easily protect itself simply
by determining what to do with a stack page fault.

>You can change the default stack limit from within C using a syscall but 
>then your code won't work in some other environment.

Can you really do that after the thread is created. Allocating virtual
addresses during thread creation is trivial.

Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-19 12:44 -0400
  Re: Paper on dynamic memory allocation for real-time systems Cursitor Doom <curd@notformail.com> - 2017-10-22 16:38 +0000
    Re: Paper on dynamic memory allocation for real-time systems Bill Martin <wwm@wwmartin.net> - 2017-10-22 10:59 -0700
      Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-22 14:01 -0400
        Re: Paper on dynamic memory allocation for real-time systems Bill Martin <wwm@wwmartin.net> - 2017-10-22 11:09 -0700
    Re: Paper on dynamic memory allocation for real-time systems pcdhobbs@gmail.com - 2017-10-22 11:16 -0700
      Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-22 14:26 -0400
        Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-22 14:30 -0400
    Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-22 14:23 -0400
      Re: Paper on dynamic memory allocation for real-time systems Cursitor Doom <curd@notformail.com> - 2017-10-22 22:49 +0000
        Re: Paper on dynamic memory allocation for real-time systems pcdhobbs@gmail.com - 2017-10-22 16:15 -0700
    Re: Paper on dynamic memory allocation for real-time systems upsidedown@downunder.com - 2017-10-23 08:17 +0300
      Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-23 09:21 +0100
      Re: Paper on dynamic memory allocation for real-time systems David Brown <david.brown@hesbynett.no> - 2017-10-23 11:33 +0200
        Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-23 12:00 -0400
          Re: Paper on dynamic memory allocation for real-time systems David Brown <david.brown@hesbynett.no> - 2017-10-24 10:19 +0200
            Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-24 10:08 +0100
              Re: Paper on dynamic memory allocation for real-time systems David Brown <david.brown@hesbynett.no> - 2017-10-24 11:31 +0200
              Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-24 16:48 -0400
                Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-25 00:49 +0100
                Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-24 20:04 -0400
        Re: Paper on dynamic memory allocation for real-time systems Joseph Gwinn <joegwinn@comcast.net> - 2017-10-24 16:46 -0400
      Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-23 10:53 -0400
        Re: Paper on dynamic memory allocation for real-time systems David Brown <david.brown@hesbynett.no> - 2017-10-24 10:29 +0200
        Re: Paper on dynamic memory allocation for real-time systems upsidedown@downunder.com - 2017-10-24 15:55 +0300
        Re: Paper on dynamic memory allocation for real-time systems upsidedown@downunder.com - 2017-10-24 15:56 +0300
      Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-23 12:35 -0400
      Re: Paper on dynamic memory allocation for real-time systems Sylvia Else <sylvia@not.at.this.address> - 2017-10-24 19:26 +1100
        Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-24 10:12 +0100
          Re: Paper on dynamic memory allocation for real-time systems Sylvia Else <sylvia@not.at.this.address> - 2017-10-25 11:55 +1100
            Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-25 08:40 +0100
      Re: Paper on dynamic memory allocation for real-time systems Jasen Betts <jasen@xnet.co.nz> - 2017-10-25 05:58 +0000
        Re: Paper on dynamic memory allocation for real-time systems Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-25 08:42 +0100
        Re: Paper on dynamic memory allocation for real-time systems kevin93 <kevin@whitedigs.com> - 2017-10-25 07:29 -0700
          Re: Paper on dynamic memory allocation for real-time systems Jasen Betts <jasen@xnet.co.nz> - 2017-10-28 19:49 +0000
        Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-25 17:28 -0400
        Re: Paper on dynamic memory allocation for real-time systems David Brown <david.brown@hesbynett.no> - 2017-10-27 09:25 +0200
  Re: Paper on dynamic memory allocation for real-time systems Sylvia Else <sylvia@not.at.this.address> - 2017-10-24 19:49 +1100
    Re: Paper on dynamic memory allocation for real-time systems bitrex <bitrex@de.lete.earthlink.net> - 2017-10-24 19:20 -0400

csiph-web