|
|
1.1 root 1: #include <stdio.h>
2: #include <sys/mdata.h>
3: #include "tsort.h"
4:
5:
6: #define BUMP 2048 /* twice minimum space to get from malloc */
7:
8:
9: /*
10: * Blockp is a pointer to a block of memory from which the
11: * various allocation routines will get memory. Blockc is
12: * the number of bytes left in the block. When these bytes
13: * are all used up, then malloc is called for BUMP more
14: * bytes. This save malloc from going over all the small
15: * blocks.
16: */
17:
18: static char *blockp;
19: static int blockc = 0;
20:
21:
22: /*
23: * Alloc functions exactly like malloc with two exceptions
24: * 1. It tries to get memory from blockp/blockc if
25: * possible.
26: * 2. It never returns NULL, but instead exits with
27: * an error message if there is no memory left.
28: */
29:
30: static char *
31: alloc(size)
32: register unsigned size;
33: {
34: register char *result;
35: char *malloc();
36:
37: /*
38: * for alignment constraints.
39: */
40: size += ALSTRUCT - 1;
41: size -= size % ALSTRUCT;
42: if (blockc >= size) {
43: blockc -= size;
44: result = blockp;
45: blockp += size;
46: return (result);
47: }
48: if (size < BUMP / 2) {
49: result = (char *)malloc(size + BUMP);
50: blockp = result + size;
51: blockc = BUMP;
52: } else
53: result = (char *)malloc(size);
54: if (result == NULL)
55: die("out of memory");
56: return (result);
57: }
58:
59:
60:
61: /*
62: * Newword returns a pointer to a word with the ancestors
63: * field set to NULL and the name field set to "str".
64: */
65:
66: struct word *
67: newword(str)
68: register char *str;
69: {
70: register struct word *result;
71:
72: result = (struct word *) alloc( sizeof(struct word));
73: result->name = alloc(strlen(str) + 1);
74: strcpy(result->name, str);
75: result->ancestors = NULL;
76: return (result);
77: }
78:
79:
80: /*
81: * Newwordl returns a pointer to a wordlist with the element
82: * field set to "wrd".
83: */
84:
85: struct wordlist *
86: newwordl(wrd)
87: register struct word *wrd;
88: {
89: register struct wordlist *result;
90:
91: result = (struct wordlist *) alloc( sizeof(struct wordlist));
92: result->element = wrd;
93: return (result);
94: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.