/* l_mem.c - Routines for memory allocation in C. Samuel A. Rebelsky Version 0.1 of 3 March 2007 */ #include #include #include "l.h" /* All of the available pairs. (Kept in a known area of memory so that we can * clean up when necessary.) */ static struct pair *mem; /* The index of the first free element. */ static int freemem; /* The number of pairs available in memory. */ static int memsize; /* Clean up memory to free space. */ static void gc() { /* STUB */ fprintf(stderr, "Out of memory. Exiting.\n"); exit(1); } /* gc() */ /* Set up the "memory" of the system to permit up to size dotted pairs. */ void init_mem(int size) { mem = (struct pair *) malloc(size * sizeof(struct pair)); memsize = size; freemem = 0; } /* init_mem() */ static pair palloc() { if (freemem >= memsize) { gc(); } return mem + freemem++; } pair allocate_pair(pointer left, pointer right) { pair result = palloc(); CAR(result) = left; CDR(result) = right; return result; } /* allocate_pair(pointer, pointer) */