1
/* @(#)buffer.h 19.1 (ESO-IPG) 02/25/03 13:49:26 */
2
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
.AUTHOR Francois Ochsenbein [ESO-IPG]
7
.CATEGORY Buffer, Stack
8
.COMMENTS Automatic processing of buffers.
10
.VERSION 1.0 23-Jun-1986: Extracted from STESODEF.
11
.VERSION 2.0 15-Apr-1988: Changed Names
12
.VERSION 2.1 07-Apr-1989: Added BUF_SaveString
13
------------------------------------------------------------*/
22
/*===========================================================================
24
*===========================================================================*/
26
typedef struct BUFFER_struct {
27
char *buf; /* Position of buffer in memory */
28
int allocated; /* Presently available memory */
29
int increment; /* Increment unit for expansion */
30
int used; /* Presently used memory */
31
int offset; /* For "locate" or stacking facilities */
33
#define NULL_BUFFER (BUFFER *)0
36
/*===========================================================================
38
*===========================================================================*/
39
BUFFER *mm_bopen (int size, int increment);
40
int mm_bfree (BUFFER *b);
41
int mm_bexp (BUFFER *b, int new_size);
42
char *mm_bst (BUFFER *b, char *record, int record_len); /* Stack */
43
char *mm_bunst (BUFFER *b); /* Unstack */
44
char *mm_bapp (BUFFER *b, char *record, int length); /* Append */
45
char *mm_ball (BUFFER *b, int length); /* Allocate */
46
char *mm_zfree (BUFFER *b, int index, int item_len); /* Free Item */
47
char *mm_zindex (BUFFER *b, int index, int item_len); /* Find Item */
48
char *mm_zloc (BUFFER *b, int item_len); /* Find Free Item */
52
char *mm_ball(), *mm_bapp();
53
char *mm_bst(), *mm_bunst();
54
char *mm_zfree(), *mm_zindex(), *mm_zloc();
57
/*======================================================================*
58
* Macros dealing with Buffers
59
*======================================================================*/
61
#define BUF_Init(type, incr) {(char *)0, 0, sizeof(type)*incr, 0, 0}
62
#define BUF_Open(type, n, incr) mm_bopen(sizeof(type)*(n), \
64
#define BUF_Expand(b, type, n) mm_bexp(b, sizeof(type)*n)
65
#define BUF_Close(b) mm_bfree(b)
66
#define BUF_Clear(b) (b)->used = 0, (b)->offset = 0
68
#define BUF_AllocateItems(b, type, n) (type *)mm_ball(b, sizeof(type)*(n))
69
#define BUF_AllocateItem(b, type) BUF_AllocateItems(b,type,1)
71
#define BUF_AppendItems(b, type, a, n) (type *)mm_bapp(b, (char *)a, \
73
#define BUF_AppendItem(b, type, a) BUF_AppendItems(b,type,a,1)
74
#define BUF_AppendString(b, s) BUF_AppendItems(b, char, s, strlen(s))
75
#define BUF_SaveString(b, s) BUF_AppendItems(b, char, s, 1+strlen(s))
77
#define BUF_StackItem(b, type, a) (type *)mm_bst(b, (char *)a, \
79
#define BUF_StackString(b, s) mm_bst(b, s, strlen(s))
81
#define BUF_UnstackItem(b, type) (type *)mm_bunst(b)
82
#define BUF_Unstack(b) mm_bunst(b)
84
#define BUF_ItemPosition(b, type) (type *)((b)->buf + (b)->offset)
85
#define BUF_Items(b,type) ((b)->used - (b)->offset)/sizeof(type)
87
/*======================================================================*
88
* Macros dealing with SETs (arrays of structures)
89
*======================================================================*/
91
#define SET_Init(type, incr) BUF_Init(type, incr)
92
#define SET_Open(type, n, incr) BUF_Open(type, n, incr)
93
#define SET_Close(b) BUF_Close(b)
95
#define SET_FindItem(b, type, i) (type *)mm_zindex(b, i, sizeof(type))
96
#define SET_FreeItem(b, type, i) (type *)mm_zfree(b, i, sizeof(type))
97
#define SET_FindFreeItem(b, type) (type *)mm_zloc(b, sizeof(type))
99
#define SET_Item(b, type) ((b)->offset / sizeof(type))
100
#define SET_Items(b, type) ((b)->used / sizeof(type))
102
#define SET_FirstItem(b, type) (type *)((b)->buf)
103
#define SET_EndItem(b, type) (type *)((b)->buf + (b)->used)
104
#define SET_LastItem(b, type) (type *)((b)->buf + (b)->used - sizeof(type))