Re: Runtime stack allocation



kid joe wrote:
I'm using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I'd rather not waste space or dynamically allocate on the heap (since
it doesn't need to persist). Now I'm planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.

Here's an example:

void doSomeStuff(unsigned size) {
char* rawData;
//INLINED (Intel p4 assembly)
//sub esp, dword ptr [size]
//mov dword ptr [rawData], esp

//do some stuff with the temporary buffer, using rawData to access
it

//INLINED (Intel p4 assembly)
//add esp, dword ptr [size]

}

This should just work, right? Or are there any odd quirks that I should be
aware of that I may not have thought of?

Well, assuming you want to limit your code to only working on a particular platform or processor, and you know your code won't conflict with the compiler's optimizations...

malloc()/free() should be fast enough, even if it doesn't "feel" like the right tool for the job. If not, can you use a VLA? If not, does your system offer alloca()? Any of those are better than subverting the compiler (and portability) and confusing people who will read your code later by using inline assembly.

S
.



Relevant Pages

  • Re: memory fragmentation
    ... My application have to allocate small buffer for sending data ... I doubt the heap algorithms are O, but I also doubt they are O. ...
    (microsoft.public.vc.language)
  • [PATCH] cpm_uart: Fix dpram allocation and non-console uarts
    ... Makes non-console UART work on both 8xx and 82xx ... static unsigned int cpm_uart_tx_empty(struct uart_port *port) ... /* Write back buffer pointer */ ... * Allocate DP-Ram and memory buffers. ...
    (Linux-Kernel)
  • Re: How to PostMessage with a string parameter?
    ... > How do I post a message with a string parameter? ... You can allocate a buffer on the heap and send the pointer cast to ...
    (microsoft.public.vc.atl)
  • Re: Access violation with heap memory
    ... I'm getting a runtime access violation using heap memory that was ... Now, when I try to access the buffer in the main function, I ... Why are you using 'char'? ... Note that there is no need to allocate storage until you are in the FillBuf, ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 0/3]HTLB mapping for drivers (take 2)
    ... It sounds like this patch set working towards the same goal as my ... so the buffer is in normal memory. ... is responsible for populating a buffer for sending to a device. ... Allocate memory. ...
    (Linux-Kernel)