首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"alloca“代表什么?

"alloca“代表什么?
EN

Software Engineering用户
提问于 2021-10-15 16:45:49
回答 1查看 364关注 0票数 3

alloca()函数在调用方的堆栈帧中分配内存。alloca最初代表什么?关于这个名字的词源有什么来源吗?

EN

回答 1

Software Engineering用户

回答已采纳

发布于 2021-10-15 16:45:49

我在历史上做过一些调查,似乎"alloca“中的"a”代表“自动”,就像自动释放一样。

GNU手册页说:

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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 return

spencer_pwb/sys/source/s4/util/alloca.s

代码语言:javascript
复制
/   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

代码语言:javascript
复制
/*      alloc - old-style memory allocator
 *      returns -1 on fail rather than 0
*/
alloc(n)
{
        register p;
        p = malloc(n);
        return(p?p:-1);
}
票数 8
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/432737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档