运行时堆栈分配 [英] Runtime stack allocation

查看:79
本文介绍了运行时堆栈分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用一个临时缓冲区来传输一些数据,而宁愿将
分配到堆上。问题是

缓冲区的大小只有在进入使用它的函数时才知道。

我宁可不浪费空间或动态分配堆(因为

它不需要坚持)。现在我正在计划利用内联

汇编来直接修改堆栈指针以分配我需要的空间。


这是一个例子:


void doSomeStuff(无符号大小){

char * rawData;

// INLINED (英特尔p4组装)

// sub esp,dword ptr [size]

// mov dword ptr [rawData],esp


//使用临时缓冲区做一些事情,使用rawData访问




// INLINED(英特尔p4程序集)

//添加esp,dword ptr [size]


}


这应该可行,对吗?或者是否有任何奇怪的怪癖我应该知道我可能没有想过?

解决方案

kid joe写道:


大家好,


我正在使用临时缓冲区来传输一些数据并且宁愿

也不会在堆上分配它。问题是

缓冲区的大小只有在进入使用它的函数时才知道。

我宁可不浪费空间或动态分配堆(因为

它不需要坚持)。现在我正计划利用内联

汇编来直接修改堆栈指针以分配我需要的空间。



使用VLA或alloca()的恕我直言,可以直接用堆栈指针搞乱

。在这两个中,当它可用时,我会选择VLA。至少它是标准的。如果不是,我会先拿出alloca()甚至

,然后再去说明你所说的那种诡计。


< blockquote> kid joe写道:


大家好,


我正在使用临时缓冲区来传输一些数据将

而不是在堆上分配它。问题是缓冲区的大小只有在进入使用它的函数

时才知道,我宁愿不浪费空间或动态

在堆上分配(因为它不需要持久化)。现在

我打算利用内联汇编来直接修改堆栈

指针来分配我需要的空间。



如果有一些便携式功能,则不需要内联汇编程序,

即可完成。但是我只能建议,不要使用

吧。 malloc的大多数实现都足够高效,以便与b
分享性能......


alloca


Wolfgang Draxinger

-

电子邮件地址有效,Jabber:他*** ***@jabber.org ,ICQ:134682867


kid joe< sp ****** @ spamtrap.invalidwrites:


我正在使用临时缓冲区来传输一些数据,而宁愿将
分配到堆上。问题是

缓冲区的大小只有在进入使用它的函数时才知道。

我宁可不浪费空间或动态分配堆(因为

它不需要坚持)。现在我正计划利用内联

汇编来直接修改堆栈指针以分配我需要的空间。



[...]


我的建议:使用malloc()(在堆上)分配它无论如何,当你完成它的时候打电话给

free()。​​


没有便携和可靠的方法在
stack。 (C标准甚至不保证你可能意味着有一个

堆栈。)


使用汇编语言你把自己限制在一个平台上。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

Nokia

" We必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Hi all,

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?

解决方案

kid joe wrote:

Hi all,

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.

IMHO using either a VLA or alloca() is infinitely preferable to messing
with the stack pointer directly. Of the two I''d chose VLAs when it is
available. At least it is standard. If not I''d take alloca() or even
use the heap before going for the sort of trickery you illustrate.


kid joe wrote:

Hi all,

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.

No need for inline assembler, if there''s some portable function,
that will do the trick. However I can only advise, not to use
it. Most implementations of malloc are efficient enough to
compete in performance with...

alloca

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867


kid joe <sp******@spamtrap.invalidwrites:

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.

[...]

My advice: allocate it using malloc() (on the "heap") anyway, and call
free() when you''re done with it.

There is no portable and reliable way to do temporary allocations on
the "stack". (The C standard doesn''t even guarantee that there is a
"stack" in the sense you probably mean.)

By using assembly language, you limit yourself to a single platform.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于运行时堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆