Monday, July 27, 2020

Digest for comp.lang.c++@googlegroups.com - 14 updates in 3 topics

Ben Bacarisse <ben.usenet@bsb.me.uk>: Jul 27 01:05AM +0100


> Yes, sure - but are these systems using function stack frames that are
> allocated in lumps from the heap for each function call, or are they
> using conventional stacks?
 
The choice is more between allocating them on the heap for each function
call or statically allocating one for each function. If the compiler
can determine that it's safe (basically no recursive re-entry) the
register save and parameter space can be a statically allocated block.
 
<cut>
--
Ben.
Bonita Montero <Bonita.Montero@gmail.com>: Jul 27 07:38AM +0200

For what do you need this ability to decide whether an object has been
allocated on the stack or the heap ? I don't see any sense in this.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jul 26 11:13PM -0700

On 7/26/2020 10:38 PM, Bonita Montero wrote:
> For what do you need this ability to decide whether an object has been
> allocated on the stack or the heap ? I don't see any sense in this.
 
 
Sorry for interjecting, however, imvho, this is an interesting question.
There is a way to create a full blown memory allocator using memory on
threads stacks only.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jul 26 11:15PM -0700

On 7/26/2020 11:13 PM, Chris M. Thomasson wrote:
 
> Sorry for interjecting, however, imvho, this is an interesting question.
> There is a way to create a full blown memory allocator using memory on
> threads stacks only.
 
In my case I did not care if where the memory came from. If a thread
frees something it did not itself create, well, it would use an atomic
XCHG, or CAS. The creator thread, in other words, the one that
allocated, would never die until all of its allocations were
deallocated. It used a little "fancy" pointer stealing to store a little
meta data in the atomic pointer swaps. Iirc, it was only a bit.
Bonita Montero <Bonita.Montero@gmail.com>: Jul 27 08:16AM +0200

> Sorry for interjecting, however, imvho, this is an interesting question.
> There is a way to create a full blown memory allocator using memory on
> threads stacks only.
 
That's possible without what the OP wanted. Simply open your own heap
-arena on the stack with alloca and divide it into smaller parts after-
wards. But that's also useless.
Bonita Montero <Bonita.Montero@gmail.com>: Jul 27 08:25AM +0200

I just had the idea that the in_our_stack could be applied to the this
pointer on construction and thereby detemining if the object has been
allocated on the stack or heap.
 
Something like this:
 
#include <intrin.h>
 
inline
bool in_our_stack( void *addr )
{
void *stackBottom,
*stackTop;
#if defined _MSC_VER
#if defined(_M_IX86)
stackBottom = (void *)__readfsdword( 0x08 );
stackTop = (void *)__readfsdword( 0x04 );
#elif defined(_M_X64)
stackBottom = (void *)__readgsqword( 0x10 );
stackTop = (void *)__readgsqword( 0x08 );
#else
#error "unsupported MSC-CPU"

No comments: