1: # include <sccs.h>
2:
3: SCCSID(@(#)need.c 8.2 12/18/85)
4:
5: /*
6: ** NEED.C -- general buffer allocation routines
7: **
8: ** allow buffers with LIFO de-allocation
9: */
10:
11:
12:
13:
14:
15:
16: /* structure that the routines use to allocate space */
17: struct nodbuffer
18: {
19: int nleft; /* bytes left */
20: int err_num; /* error code on overflow */
21: int (*err_func)(); /* error function on overflow */
22: char *xfree; /* next free byte */
23: char buffer[1]; /*beginning of buffer area */
24: };
25:
26: /*
27: ** NEED -- allocate space from a buffer
28: **
29: ** On buffer overflow, calls err_func from that field
30: ** in the buffer with the error code err_code from that field
31: ** in the buffer, then returns 0.
32: ** need() guarantees an even adress on return.
33: **
34: ** Parameters:
35: ** bf -- buffer
36: ** nbytes -- number of bytes desired
37: **
38: ** Returns:
39: ** pointer to allocated area
40: ** on buffer overflow returns 0.
41: **
42: ** Side Effects:
43: ** adjusts buffer structure to reflect allocation.
44: */
45:
46: char *
47: need(bf, nbytes)
48: struct nodbuffer *bf;
49: int nbytes;
50: {
51: register char *x;
52: register struct nodbuffer *buf;
53: register int i;
54:
55: buf = bf;
56: i = nbytes;
57: if (i > buf->nleft)
58: {
59: (*buf->err_func)(buf->err_num, 0);
60: return (0);
61: }
62: i = (i + 3) & ~03;
63: x = buf->xfree;
64: buf->xfree += i;
65: buf->nleft -= i;
66: clrmem(x, i);
67: return(x);
68: }
69:
70: /*
71: ** INITBUF -- initialize a buffer
72: **
73: ** Must be called before the first need() call on the buffer.
74: **
75: ** Parameters:
76: ** bf -- buffer
77: ** size -- size fo buffer area
78: ** err_num -- error code for overflow
79: ** err_func -- function to call with err_code on error
80: **
81: ** Returns:
82: ** none
83: **
84: ** Side Effects:
85: ** initializes buffer structure
86: **
87: ** Diagnostics:
88: ** "initbuf : odd buffer adress 0%o" -- buffers must start
89: ** at an even address.
90: */
91:
92: initbuf(bf, size, err_num, err_func)
93: char *bf;
94: int size;
95: int err_num;
96: int (*err_func)();
97: {
98: register struct nodbuffer *buf;
99: register i;
100:
101: buf = (struct nodbuffer *) bf;
102: i = (int) buf;
103: if (i & 03)
104: syserr("initbuf : odd buffer adress 0%o", buf);
105: buf->nleft = size - sizeof *buf;
106: buf->xfree = buf->buffer;
107: buf->err_num = err_num;
108: buf->err_func = err_func;
109: }
Defined functions
need
defined in line
46;
never used
Defined struct's