3

The alloca() function allocates memory in the stack frame of the caller. What did alloca originally stand for? Are there any sources regarding the etymology of the name?

2
  • 2
    This kind of question might be a better fit for Retrocomputing?
    – amon
    Commented Oct 15, 2021 at 18:28
  • @amon I wasn't sure... Answers to this meta question suggest Software Engineering, Retrocomputing or Computer Science.
    – Jo Liss
    Commented Oct 15, 2021 at 21:55

1 Answer 1

8

I did some sleuthing in historic sources, and it seems that the "a" in "alloca" stands for "automatic", as in automatically freed.

The GNU man page says:

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.

Linked from this commit message I found archives of UNIX/32V and PWB/UNIX sources, each of which contains an alloca.s assembly file:

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 return

spencer_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>

I believe the alloc function referenced would be a predecessor of the modern malloc, based on this function definition in PWB:

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);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.