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 ofallocfor storing characters.afree(p)releases the storage thus acquired so it can be reused later.- The routines are "redimentary" because the calls to
afreemust be made in the opposite order to the calls made onalloc. - The storage managed by
allocandafreeis a Stack.
- We use a pointer called
allocpthat points to the next free element. - When
allocis asked for n characters, it checks to see if there is enough room left inallocbuf. If so,allocreturns 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,
allocreturns zero. afree(p)merely setsallocptopifpif insideallocbuf.
- Arrays with pointers in C.
- Pointer arithmetics and comparison.
