summaryrefslogtreecommitdiff
path: root/unproto/tok_pool.c
diff options
context:
space:
mode:
authorRobert de Bath <rdebath@poboxes.com>1996-03-24 17:45:55 +0100
committerLubomir Rintel <lkundrak@v3.sk>2013-10-23 23:29:43 +0200
commitfe22c37817ce338fbbc90b239320248c270957fa (patch)
treed9550410c4a20bdd382fcc58d2d3d7c5e04e5245 /unproto/tok_pool.c
parenta7aba15e8efffb1c5d3097656f1a93955a64f01f (diff)
parent42192453ea219b80d0bf9f41e51e36d3d4d0740b (diff)
downloaddev86-fe22c37817ce338fbbc90b239320248c270957fa.tar.gz
Import Dev86-0.0.4.tar.gzv0.0.4
Diffstat (limited to 'unproto/tok_pool.c')
-rw-r--r--unproto/tok_pool.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/unproto/tok_pool.c b/unproto/tok_pool.c
new file mode 100644
index 0000000..c2a9665
--- /dev/null
+++ b/unproto/tok_pool.c
@@ -0,0 +1,104 @@
+/*++
+/* NAME
+/* tok_pool 3
+/* SUMMARY
+/* maintain pool of unused token structures
+/* PACKAGE
+/* unproto
+/* SYNOPSIS
+/* #include "token.h"
+/*
+/* struct token *tok_alloc()
+/*
+/* void tok_free(t)
+/* struct token *t;
+/* DESCRIPTION
+/* tok_alloc() and tok_free() maintain a pool of unused token
+/* structures.
+/*
+/* tok_alloc() takes the first free token structure from the pool
+/* or allocates a new one if the pool is empty.
+/*
+/* tok_free() adds a (possibly composite) token structure to the pool.
+/* BUGS
+/* The pool never shrinks.
+/* AUTHOR(S)
+/* Wietse Venema
+/* Eindhoven University of Technology
+/* Department of Mathematics and Computer Science
+/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
+/* LAST MODIFICATION
+/* 91/09/01 23:08:36
+/* VERSION/RELEASE
+/* 1.1
+/*--*/
+
+static char pool_sccsid[] = "@(#) tok_pool.c 1.1 91/09/01 23:08:36";
+
+/* C library */
+
+extern char *malloc();
+
+/* Application-specific stuff */
+
+#include "token.h"
+#include "vstring.h"
+
+extern void error();
+
+#define TOKLEN 5 /* initial string buffer length */
+
+struct token *tok_pool = 0; /* free token pool */
+
+/* tok_alloc - allocate token structure from pool or heap */
+
+struct token *tok_alloc()
+{
+ register struct token *t;
+
+ if (tok_pool) { /* re-use an old one */
+ t = tok_pool;
+ tok_pool = t->next;
+ } else { /* create a new one */
+ if ((t = (struct token *) malloc(sizeof(struct token))) == 0
+ || (t->vstr = vs_alloc(TOKLEN)) == 0)
+ error(1, "out of memory");
+ }
+ t->next = t->head = t->tail = 0;
+#ifdef DEBUG
+ strcpy(t->vstr->str, "BUSY");
+#endif
+ return (t);
+}
+
+/* tok_free - return (possibly composite) token to pool of free tokens */
+
+void tok_free(t)
+register struct token *t;
+{
+#ifdef DEBUG
+ /* Check if we are freeing free token */
+
+ register struct token *p;
+
+ for (p = tok_pool; p; p = p->next)
+ if (p == t)
+ error(1, "freeing free token");
+#endif
+
+ /* Free neighbours and subordinates first */
+
+ if (t->next)
+ tok_free(t->next);
+ if (t->head)
+ tok_free(t->head);
+
+ /* Free self */
+
+ t->next = tok_pool;
+ t->head = t->tail = 0;
+ tok_pool = t;
+#ifdef DEBUG
+ strcpy(t->vstr->str, "FREE");
+#endif
+}