Rudimentary storage allocator using the Stack data structure. Section 5.4 of The C Programming Language.
alloc(n)
returns a pointer p to n consecutive character positions which can be used by the caller ofalloc
for storing characters.afree(p)
releases the storage thus acquired so it can be reused later.- The routines are "redimentary" because the calls to
afree
must be made in the opposite order to the calls made onalloc
. - The storage managed by
alloc
andafree
is a Stack.
- We use a pointer called
allocp
that points to the next free element. - When
alloc
is asked for n characters, it checks to see if there is enough room left inallocbuf
. If so,alloc
returns the currect value ofallocp
(the beginning of the free block), then increments it by n to point to the next free area. - If there is no room,
alloc
returns zero. afree(p)
merely setsallocp
top
ifp
if insideallocbuf
.
- Arrays with pointers in C.
- Pointer arithmetics and comparison.