Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.electronics.design > #482158 > unrolled thread
| Started by | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| First post | 2017-10-19 12:44 -0400 |
| Last post | 2017-10-24 19:20 -0400 |
| Articles | 19 on this page of 39 — 11 participants |
Back to article view | Back to sci.electronics.design
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
Page 2 of 2 — ← Prev page 1 [2]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2017-10-24 20:04 -0400 |
| Message-ID | <ayQHB.154586$J83.89829@fx06.iad> |
| In reply to | #482774 |
On 10/24/2017 07:49 PM, Tom Gardner wrote: > On 24/10/17 21:48, bitrex wrote: >> On 10/24/2017 05:08 AM, Tom Gardner wrote: >>> On 24/10/17 09:19, David Brown wrote: >>>> Basically, the rule is "don't write buggy code". >>> >>> Without commenting on the rest of your post, that sentiment >>> is unhelpful motherhood and apple pie. >>> >>> I have simple rule which is very useful at cutting through >>> the spiel from salesmen and politicians. Simply invert the >>> statement, and if the result is manifest nonsense, then >>> the original statement carries no useful information. >>> >>> In this case "write buggy code" would be manifest nonsense, >>> so "don't write buggy code", while true, is unhelpful. >>> >>> Perhaps it would be better to choose a tool that gets the >>> job done with reduced ways of introducing tool-specific bugs. >> >> Walking a tightrope with a net tends to make you careless and prone to >> accidents, so remove the net and just learn to be more careful! > > So remove airbags in cars, and replace them with spikes? > > That would make things safer by slowing drivers down > unnecessarily, just as the probability of tool-induced > bugs slows down developers unnecessarily. That's the joke
[toc] | [prev] | [next] | [standalone]
| From | Joseph Gwinn <joegwinn@comcast.net> |
|---|---|
| Date | 2017-10-24 16:46 -0400 |
| Message-ID | <0001HW.1F9FDE0B078DBA9B1177122CF@news.giganews.com> |
| In reply to | #482517 |
On Oct 23, 2017, David Brown wrote (in article <oskd0f$lkb$1@dont-email.me>): > On 23/10/17 07:17, 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. > > The two key points are memory fragmentation (which has already been > mentioned) and non-deterministic timing. In embedded systems or other > software with constraints on timing, dynamic memory allocation and > deallocation can easily be a problem because it is very difficult to > predict the timing - sometimes the functions run quickly, sometimes they > can take a long time. In the hard realtime embedded world circa 1980 to 1990 or so, for problems that did not lend themselves to simple static allocation schemes, we usually built our own dedicated memory allocator. The standard design was to have a linked-list of empty buffers (all the same size), and one would get an empty buffer as needed from the list, and when done with the buffer it would be linked back into the list of empty buffers. In many systems there were two such buffer lists, one with a large number of small buffers, the other with a small number of large buffers. The small buffers were used to pass access to the large buffers, which could be accessed by multiple threads in parallel (using reference counts and inversion-proof mutual exclusion mechanisms). This approach has the advantage that it is constant time, and does not get slower as the system is more and more heavily loaded, causing free memory becomes scarce. This scheme is basically bulletproof. > There is an additional challenge that has become more relevant in recent > times - it gets really complicated when you have multiple threads or > multiple processors trying to do such allocations. > > > > > 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. > > Many C programmers are quite happy with memory allocated on the stack. > There are two ways of doing it - VLAs and alloca(). These have the same > uses as stack allocated memory in Pascal - and the same advantages and > disadvantages regarding lifetime. A better alternative for soft realtime applications is Knuthâs Buddy-System allocator:<https://en.wikipedia.org/wiki/Buddy_memory_allocation>. Joe Gwinn
[toc] | [prev] | [next] | [standalone]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2017-10-23 10:53 -0400 |
| Message-ID | <UnnHB.93265$Iu1.44719@fx20.iad> |
| In reply to | #482513 |
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. 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. 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.) You can change the default stack limit from within C using a syscall but then your code won't work in some other environment.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-10-24 10:29 +0200 |
| Message-ID | <osmtln$mrs$1@dont-email.me> |
| In reply to | #482540 |
On 23/10/17 16:53, bitrex 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. 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. > > 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.) Of course there is protection for the stack. A general-purpose OS that lets stack corruption in an application cause kernel corruption or other problems is a broken OS. On Linux, Windows, etc., stack corruption does no more harm than corruption anywhere else. And on embedded systems without memory protection where application code /can/ corrupt your kernel (if you have one), that applies equally to heaps and other memory as well as the stack. > > You can change the default stack limit from within C using a syscall but > then your code won't work in some other environment. Programs mainly use stacks for function calls, and small bits of data - and they need it to be fast. That is the typical usage pattern. So OS's (like Linux and Windows) will allocate a relatively small amount of memory, but make sure it is ready for use and not paged out virtual memory. It is likely that they will make it contiguous memory to reduce the number of indirections, MMU lookups, etc. Programs can ask for larger stack space if they need it - a program that unexpectedly requires lots of stack space most likely has a runaway recursion bug, so it is good to stop it.
[toc] | [prev] | [next] | [standalone]
| From | upsidedown@downunder.com |
|---|---|
| Date | 2017-10-24 15:55 +0300 |
| Message-ID | <et7uucpsmm2021n1g233f4fopom8b7s643@4ax.com> |
| In reply to | #482540 |
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.
[toc] | [prev] | [next] | [standalone]
| From | upsidedown@downunder.com |
|---|---|
| Date | 2017-10-24 15:56 +0300 |
| Message-ID | <72euuctnmge4easu90hfa67b1fkpic3e7d@4ax.com> |
| In reply to | #482540 |
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.
[toc] | [prev] | [next] | [standalone]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2017-10-23 12:35 -0400 |
| Message-ID | <jToHB.45775$K_2.32039@fx07.iad> |
| In reply to | #482513 |
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.
>
in C++11:
#include <memory>
#include <new>
struct Foo
{
int bar = 0;
int baz = 1;
int bloop = 2;
};
void my_func(const Foo* stuff)
{
//etc...
}
void my_func2(std::unique_ptr<Foo> stuff)
{
//etc...
}
int main()
{
/* stack allocated structure, instantiation
calls implicitly-declared constructor,
above default values assigned to fields,
will be popped off stack at end of function */
Foo foo1;
// foo1 is a value
/* default-constructed heap allocated struct Foo, the
smart pointer wrapper is constructed on the stack and will
automatically destroy the wrapped heap structure
and free memory at end of function */
auto foo2 = std::unique_ptr<Foo>(new Foo{});
//foo2 can be used like an ordinary "dumb pointer"
//but wrapper is non-copyable and cannot be passed by value
foo1.bar = 2;
foo2->baz = 3;
foo2->bloop = foo1.bar;
//as usual due to overloads
foo2 = new Foo{}; //ERROR, no way
delete foo2; //ERROR, foo2 is a stack-allocated wrapper class,
//not a pointer
my_func1(foo2); //OK, the smart pointer is overloaded to behave
//as a const-qualified pointer to Foo when passed
my_func2(std::move(foo2)); //OK, passed as an rvalue reference,
//foo2 in "main" releases control
//of managed heap-allocated object
}
[toc] | [prev] | [next] | [standalone]
| From | Sylvia Else <sylvia@not.at.this.address> |
|---|---|
| Date | 2017-10-24 19:26 +1100 |
| Message-ID | <f58bpmFc0q0U1@mid.individual.net> |
| In reply to | #482513 |
On 23/10/2017 4:17 PM, upsidedown@downunder.com wrote: > 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 ? Very often one's using dynamic memory exactly because one wants it to persist after the function returns. Otherwise, unless the memory requirement is large, or of unknown size (at time of programming), one uses an automatic variable stack allocated variable. Sylvia.
[toc] | [prev] | [next] | [standalone]
| From | Tom Gardner <spamjunk@blueyonder.co.uk> |
|---|---|
| Date | 2017-10-24 10:12 +0100 |
| Message-ID | <juDHB.117981$uZ4.67324@fx43.am4> |
| In reply to | #482634 |
On 24/10/17 09:26, Sylvia Else wrote: > On 23/10/2017 4:17 PM, upsidedown@downunder.com wrote: > >> 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 ? > > Very often one's using dynamic memory exactly because one wants it to persist > after the function returns. Otherwise, unless the memory requirement is large, > or of unknown size (at time of programming), one uses an automatic variable > stack allocated variable. I'm unconvinced. Persisting a variable after a function returns can be satisfactorily achieved either by allocating it in any of the calling functions "further up" the stack, or failing that as a global. Dynamic memory is necessary when the number of such variables cannot be determined at compile time.
[toc] | [prev] | [next] | [standalone]
| From | Sylvia Else <sylvia@not.at.this.address> |
|---|---|
| Date | 2017-10-25 11:55 +1100 |
| Message-ID | <f5a5o9Fp8ffU2@mid.individual.net> |
| In reply to | #482642 |
On 24/10/2017 8:12 PM, Tom Gardner wrote: > On 24/10/17 09:26, Sylvia Else wrote: >> On 23/10/2017 4:17 PM, upsidedown@downunder.com wrote: >> >>> 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 ? >> >> Very often one's using dynamic memory exactly because one wants it to >> persist >> after the function returns. Otherwise, unless the memory requirement >> is large, >> or of unknown size (at time of programming), one uses an automatic >> variable >> stack allocated variable. > > I'm unconvinced. Persisting a variable after a function > returns can be satisfactorily achieved either by allocating > it in any of the calling functions "further up" the stack, > or failing that as a global. > > Dynamic memory is necessary when the number of such variables > cannot be determined at compile time. > A fairly typical scenario is parsing a hierarchical data stream which may something as relatively simple as XML, or may be the text of a program in a high-level language such as C. The number and nature of the elements cannot be determined in advance of the parsing (which tends to be recursive), so it is not practical to allocate them further up the stack. Sylvia.
[toc] | [prev] | [next] | [standalone]
| From | Tom Gardner <spamjunk@blueyonder.co.uk> |
|---|---|
| Date | 2017-10-25 08:40 +0100 |
| Message-ID | <deXHB.16265$Dz7.12070@fx29.am4> |
| In reply to | #482793 |
On 25/10/17 01:55, Sylvia Else wrote: > On 24/10/2017 8:12 PM, Tom Gardner wrote: >> On 24/10/17 09:26, Sylvia Else wrote: >>> On 23/10/2017 4:17 PM, upsidedown@downunder.com wrote: >>> >>>> 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 ? >>> >>> Very often one's using dynamic memory exactly because one wants it to persist >>> after the function returns. Otherwise, unless the memory requirement is large, >>> or of unknown size (at time of programming), one uses an automatic variable >>> stack allocated variable. >> >> I'm unconvinced. Persisting a variable after a function >> returns can be satisfactorily achieved either by allocating >> it in any of the calling functions "further up" the stack, >> or failing that as a global. >> >> Dynamic memory is necessary when the number of such variables >> cannot be determined at compile time. >> > > A fairly typical scenario is parsing a hierarchical data stream which may > something as relatively simple as XML, or may be the text of a program in a > high-level language such as C. > > The number and nature of the elements cannot be determined in advance of the > parsing (which tends to be recursive), so it is not practical to allocate them > further up the stack. Just so. And the key point w.r.t. using a heap is that the number cannot be determined at compile time, not their lifetime.
[toc] | [prev] | [next] | [standalone]
| From | Jasen Betts <jasen@xnet.co.nz> |
|---|---|
| Date | 2017-10-25 05:58 +0000 |
| Message-ID | <osp96g$4n2$1@gonzo.alcatraz> |
| In reply to | #482513 |
On 2017-10-23, upsidedown@downunder.com <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 ? yeah, 8086 had only a 16 bit stack pointer but I never tried alloca in a huge memory model. in the 8 bit world 6800 had only an 8 bit stack pointer, and PIC16 even fewer, and stack outside of main memory. AVR and Z80 have 16 bit stack pointers > In Pascal, you could allocate short lived memory areas on the stack > and pass a pointer to that stack area as a procedure parameter. In C too, "alloca()" I think the "a" on the end stands for "auto" The standard does not require that it be allocated on the stack, but if "longjmp()" is also provided, the only sensible implementation (that I am aware of) is to do it that way. > Of course, this was simplified by having source level nested procedures, GCC has that in C too, not sure if it's part of some C standard or purely a GCC extension. -- This email has not been checked by half-arsed antivirus software
[toc] | [prev] | [next] | [standalone]
| From | Tom Gardner <spamjunk@blueyonder.co.uk> |
|---|---|
| Date | 2017-10-25 08:42 +0100 |
| Message-ID | <HfXHB.16266$Dz7.11002@fx29.am4> |
| In reply to | #482838 |
On 25/10/17 06:58, Jasen Betts wrote: > 6800 had only an 8 bit stack pointer, The 6800 had a full 16 bit PC, stack pointer and index register. (The 6809 had two SPs and two index registers; it was the best of its class but came too late to be influential)
[toc] | [prev] | [next] | [standalone]
| From | kevin93 <kevin@whitedigs.com> |
|---|---|
| Date | 2017-10-25 07:29 -0700 |
| Message-ID | <92b2102b-2530-427d-af80-0b160b8ef212@googlegroups.com> |
| In reply to | #482838 |
On Tuesday, October 24, 2017 at 11:01:21 PM UTC-7, Jasen Betts wrote: ... > in the 8 bit world > > 6800 had only an 8 bit stack pointer, ... The 6800 and descendants had a 16-bit stack pointer - the 6502 and variants (used by Apple, BBC, Commodore etc) only had 8 bits. kevin
[toc] | [prev] | [next] | [standalone]
| From | Jasen Betts <jasen@xnet.co.nz> |
|---|---|
| Date | 2017-10-28 19:49 +0000 |
| Message-ID | <ot2n0c$8ir$1@gonzo.alcatraz> |
| In reply to | #482868 |
On 2017-10-25, kevin93 <kevin@whitedigs.com> wrote: > On Tuesday, October 24, 2017 at 11:01:21 PM UTC-7, Jasen Betts wrote: > ... >> in the 8 bit world >> >> 6800 had only an 8 bit stack pointer, > ... > > The 6800 and descendants had a 16-bit stack pointer - the 6502 and variants (used by Apple, BBC, Commodore etc) only had 8 bits. > > kevin 6502 that's the one I meant, thanks. -- This email has not been checked by half-arsed antivirus software
[toc] | [prev] | [next] | [standalone]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2017-10-25 17:28 -0400 |
| Message-ID | <Sl7IB.33566$fc3.27236@fx35.iad> |
| In reply to | #482838 |
On 10/25/2017 01:58 AM, Jasen Betts wrote: > On 2017-10-23, upsidedown@downunder.com <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 ? > > yeah, 8086 had only a 16 bit stack pointer but I never tried alloca in > a huge memory model. > > in the 8 bit world > > 6800 had only an 8 bit stack pointer, > and PIC16 even fewer, and stack > outside of main memory. > > AVR and Z80 have 16 bit stack pointers On AVR 8 bit at least an out-of-control recursive function could in theory damage hardware, as the SRAM alias addresses for the I/O port registers are within the stack pointer's address space. Halt and Catch Fire
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-10-27 09:25 +0200 |
| Message-ID | <osun1j$6pf$1@dont-email.me> |
| In reply to | #482838 |
On 25/10/17 07:58, Jasen Betts wrote: > On 2017-10-23, upsidedown@downunder.com <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 ? > > yeah, 8086 had only a 16 bit stack pointer but I never tried alloca in > a huge memory model. > > in the 8 bit world > > 6800 had only an 8 bit stack pointer, > and PIC16 even fewer, and stack > outside of main memory. > > AVR and Z80 have 16 bit stack pointers (Some AVRs have 16-bit stack pointers, but that's nit-picking.) > > >> In Pascal, you could allocate short lived memory areas on the stack >> and pass a pointer to that stack area as a procedure parameter. > > In C too, "alloca()" I think the "a" on the end stands for "auto" > > The standard does not require that it be allocated on the stack, but > if "longjmp()" is also provided, the only sensible implementation > (that I am aware of) is to do it that way. The C standard does not require a stack at all. A stack is, of course, the most efficient known mechanism for function calls, local variables, parameters, etc. - but it is not required by C. There are devices with no data stack and only a small dedicated hardware return stack (like small PIC's and some AVR Tiny devices), and plenty of devices with such inefficient access to data on the stack that compilers use static memory allocations for parameters and local variables (like many old-style 8-bit microcontrollers). The C standard also does not have alloca(). It is, however, in the Posix standards - and there a stack /is/ required. longjmp() is in the C standard, but is required only for "hosted" implementations and not for "freestanding" implementations. Devices like the AVR Tiny without a stack are unlikely to support longjmp(). (I haven't checked gcc's support here - as someone interested in clear, structured and reliable coding, I would not touch longjmp with a bargepole.) There are a few C implementations for bigger systems that do not have a normal stack as such. AFAIUI, there are are some mainframe systems where function frames are allocated in a linked list in a heap, rather than on a stack. And these should support longjmp. But whether it is an efficient implementation is another matter - the C standard only requires that it works according to spec, not that it is a "sensible" implementation! > >> Of course, this was simplified by having source level nested procedures, > > GCC has that in C too, not sure if it's part of some C standard or > purely a GCC extension. It is not part of any C standard - it is a gcc extension. gcc supports a number of languages, not just C and C++. Amongst others it supports Ada, which /does/ have nested procedures. (It also has out-of-tree support for Pascal.) Since the machinery is all in place in the compiler, and there was a clear and obvious syntax, gcc developers decided to make it available in C also. But the implementation of nested functions in C requires trampolines on many architectures - executable code snippets on the stack. This breaks code safety restrictions about non-executable stacks. If you want nested functions, you would be better to use C++ and lambdas rather than gcc's extension to C here. > > -- > This email has not been checked by half-arsed antivirus software > I wonder if your half-arsed antivirus software would have spotted the error in your signature delimiter - or that this is not an email? :-)
[toc] | [prev] | [next] | [standalone]
| From | Sylvia Else <sylvia@not.at.this.address> |
|---|---|
| Date | 2017-10-24 19:49 +1100 |
| Message-ID | <f58d5pFcbkrU1@mid.individual.net> |
| In reply to | #482158 |
On 20/10/2017 3:44 AM, 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. > > In this paper, a new allocator called TLSF (Two Level Segregated Fit) is > presented. TLSF is designed and implemented to accommodate real-time > constraints. The proposed allocator exhibits time-bounded behaviour, > O(1), and maintains a very good execution time. This paper describes in > detail the data structures and functions provided by TLSF. We also > compare TLSF with a representative set of allocators > regarding their temporal cost and fragmentation." > > <http://www.gii.upv.es/tlsf/files/jrts2008.pdf> Even so, if I'm designing a real-time system, I can either allocate all the memory I'm going to need, either "dynamically" during initialization, or statically. Then I don't have to concern my self with trying to figure out whether the worst case execution time or worst case fragmentation may break my program. Indeed, I don't have to determine what my worst case memory requirement is. I may then be creating something that needs more physical memory than might otherwise have been enough, but memory isn't that expensive. I suppose there may be real-time systems out there where failure IS an option, but they'd be few and far between. Sylvia.
[toc] | [prev] | [next] | [standalone]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2017-10-24 19:20 -0400 |
| Message-ID | <dVPHB.30937$E_2.14555@fx41.iad> |
| In reply to | #482637 |
On 10/24/2017 04:49 AM, Sylvia Else wrote: > On 20/10/2017 3:44 AM, 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. >> >> In this paper, a new allocator called TLSF (Two Level Segregated Fit) >> is presented. TLSF is designed and implemented to accommodate >> real-time constraints. The proposed allocator exhibits time-bounded >> behaviour, O(1), and maintains a very good execution time. This paper >> describes in detail the data structures and functions provided by >> TLSF. We also compare TLSF with a representative set of allocators >> regarding their temporal cost and fragmentation." >> >> <http://www.gii.upv.es/tlsf/files/jrts2008.pdf> > > Even so, if I'm designing a real-time system, I can either allocate all > the memory I'm going to need, either "dynamically" during > initialization, or statically. Then I don't have to concern my self with > trying to figure out whether the worst case execution time or worst case > fragmentation may break my program. And also step back in time to the 1950s wrt how flexible your design is. Say you have two processes each of which executes 50% of the time non-concurrently, and yet if you rely on static initialization all their resources are initialized at startup and exist forever to sit around and do nothing half their life.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | sci.electronics.design
csiph-web