I heard of a scheme by Henry Baker a year or so ago that and I thought then that the scheme did not apply to this problem. Perhaps it does however.

Baker’s scheme, by word of mouth, would be a collection of C routines linked together but of these sorts:

Special routines would have no return statements and would not ”fall off the end”. The stack would grow with long contiguous stack frames of these special routines. Some magic, perhaps longjump, would occasionally abandon these useless frames and thus reclaim space. This requires that an upper limit be known of the number of invocations of special routines between running code that will clean the stack.

An alternative is to find a compiler that is guaranteed to do tail call optimization and insure that all special routines finish by calling another special routine only just before returning itself. This causes the special routine to undo its frame as it finishes.

In either case both special and ordinary routines can call ordinary routines. This means that faults in the stack segment cannot be taken as a signal to clean the stack. There is in general and often a few vital frames at the recent end of the stack.


Note that command:
gcc -O3 a.c -S -Wall
produces this:
	.file	"a.c"
	.text
	.p2align 4,,15
	.globl	b
	.type	b, @function
b:
.LFB0:
	.cfi_startproc
	movq	%rsi, %rax
	movq	8(%rsi), %rsi
	addl	16(%rax), %edi
	movq	(%rax), %rax
	jmp	*%rax
	.cfi_endproc
.LFE0:
	.size	b, .-b
	.ident	"GCC: (GNU) 4.6.4"
	.section	.note.GNU-stack,"",@progbits
This stream function adds a constant to each character. You can’t write much better code in assembler.