alloca()函数在调用方的堆栈帧中分配内存。alloca最初代表什么?关于这个名字的词源有什么来源吗?
发布于 2021-10-15 16:45:49
我在历史上做过一些调查,似乎"alloca“中的"a”代表“自动”,就像自动释放一样。
GNU手册页说:
There is evidence that the alloca() function appeared in 32V,
PWB, PWB.2, 3BSD, and 4BSD. There is a man page for it in
4.3BSD. Linux uses the GNU version.从此提交消息链接,我找到了UNIX/32V和PWB/UNIX源代码的档案,每个源都包含一个alloca.s程序集文件:
32v/usr/src/libc/sys/alloca.s# like alloc, but automatic
# automatic free in return
.globl _alloca
_alloca:
.word 0x0000
subl2 4(ap),sp # crude allocation
movl 16(fp),r1 # pc
movq 8(fp),ap # new (old) ap and fp
bicl2 $3,sp # 4-byte align
addl2 $7*4,sp # reuse space of mscp
movl sp,r0 # return value
jmp (r1) # funny returnspencer_pwb/sys/source/s4/util/alloca.s/ wdptr = alloca(nbytes);
/ like alloc, but automatic
/ automatic free upon return from calling function
.globl _alloca
_alloca:
mov (sp)+,r1 / return
mov (sp),r0 /count
inc r0
bic $1,r0 / round up
sub r0,sp / take core
mov sp,r0
tst (r0)+ / returned value; will generate
/ a memory fault if there isn't enough core
jmp (r1)
.data
<@(#)alloca 1.1\0>我相信引用的alloc函数将是基于PWB中的函数定义的现代malloc的前身:
spencer_pwb/sys/source/s4/alloc.c/* alloc - old-style memory allocator
* returns -1 on fail rather than 0
*/
alloc(n)
{
register p;
p = malloc(n);
return(p?p:-1);
}https://softwareengineering.stackexchange.com/questions/432737
复制相似问题