1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 1989-2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s): ECRC GmbH
20 *
21 * END LICENSE BLOCK */
22
23/*
24 * SEPIA INCLUDE FILE
25 *
26 * VERSION	$Id: mem.h,v 1.1 2008/06/30 17:43:57 jschimpf Exp $
27 *
28 * IDENTIFICATION	mem.h
29 *
30 * DESCRIPTION		Definitions for heap memory management
31 *
32 * AUTHORS:		Joachim Schimpf, Emmanuel van Rossum, Micha Meier
33 *
34 ********************************************************************/
35
36#ifndef NULL
37#define		NULL	0
38#endif
39
40/*---------------------------------------------------------------------
41 * Abstract data type "Unbounded Stack" (description see mem.c)
42 *---------------------------------------------------------------------*/
43
44#define Stack_Declare(Stack)			unbounded_stack Stack;
45
46#define Stack_Create(Stack, MinimumWords)	stack_create(&(Stack), MinimumWords)
47
48#define Stack_Destroy(Stack)			stack_destroy(&(Stack))
49
50#define StackEmpty(Stack)			((Stack)->top == (Stack)->limit)
51
52#define StackTop(Stack)				((Stack)->top)
53
54#define Stack_Pop_To(Stack, OldTop)		stack_pop_to(&(Stack), OldTop)
55
56#define Stack_Push(Stack, WordsNeeded) {				\
57	if (((Stack)->top -= (WordsNeeded)) < (uword *)((Stack) + 1))	\
58	    stack_push(&(Stack), WordsNeeded);				\
59	}
60
61#define Stack_Pop(Stack, NumberWords) {					\
62	if (((Stack)->top += (NumberWords)) >= (Stack)->limit)		\
63	    stack_pop(&(Stack), NumberWords);				\
64	}
65
66#define Stack_Reset(Stack)				\
67	for (;;) {					\
68	    (Stack)->top = (Stack)->limit;		\
69	    if (!(Stack)->down) break;			\
70	    (Stack) = (Stack)->down;			\
71	}
72
73
74typedef struct stack_header {
75	struct stack_header	*down,		/* previous segment */
76				*up;		/* next segment */
77	uword			*top,		/* stack top inside this segment */
78				*limit;		/* end of this segment */
79} *unbounded_stack;
80
81
82extern void stack_create ARGS((struct stack_header **pstack, uword words_needed));
83extern void stack_pop_to ARGS((struct stack_header **pstack, uword *old_top));
84extern void stack_push ARGS((struct stack_header **pstack, uword words_needed));
85extern void stack_pop ARGS((struct stack_header **pstack, uword word_offset));
86extern void stack_destroy ARGS((struct stack_header **pstack));
87
88/*---------------------------------------------------------------------
89 * Abstract data type "Temporary Memory" (description see mem.c)
90 *---------------------------------------------------------------------*/
91
92#define Temp_Create(Temp, MinimumBytes)	temp_create(&(Temp), MinimumBytes)
93
94#define Temp_Destroy(Temp)		temp_destroy(&(Temp))
95
96#define TempAlloc(Temp, BytesNeeded)	temp_alloc(&(Temp), BytesNeeded)
97
98#define Temp_Align(Temp, Size)		temp_align(&(Temp), Size)
99
100#define Temp_Reset(Temp) {			\
101	    (Temp) = (Temp)->first;		\
102	    (Temp)->top = (char *)((Temp)+1);	\
103	}
104
105
106typedef struct temp_header {
107	struct temp_header	*next,		/* next segment	*/
108				*first;		/* first segment */
109	char			*top,
110				*limit;
111} *temp_area;
112
113
114extern void	temp_create ARGS((struct temp_header **ptemp, uword bytes_needed));
115extern void	temp_align ARGS((struct temp_header **ptemp, uword size));
116extern void	temp_destroy ARGS((struct temp_header **ptemp));
117extern char	*temp_alloc ARGS((struct temp_header **ptemp, uword bytes_needed));
118
119/*---------------------------------------------------------------------
120 * Abstract data type "Unbounded Buffer"
121 * (description see mem.c)
122 *---------------------------------------------------------------------*/
123
124#define Buf_Declare(Buf)		unbounded_buffer Buf
125
126#define Buf_Create(Buf, MinWords)	buffer_create(&(Buf), MinWords)
127
128#define Buf_Reinit(Buf)			buffer_reinit(&(Buf))
129
130#define Buf_Destroy(Buf)		buffer_destroy(&(Buf))
131
132#define BufWriteZ(Buf)			(Buf).write_block->top
133
134#define BufWriteR(Buf, RdPtr)					\
135	((Buf).write_block = (Buf).read_block,			\
136	 (Buf).write_block_end = (Buf).write_block->limit,	\
137	 (Buf).write_block->top = (RdPtr))
138
139#define Buf_Alloc(Buf, WrPtr, Words) {				\
140	if ((WrPtr) + (Words) > (Buf).write_block_end) 		\
141		(WrPtr) = buffer_alloc(&(Buf), WrPtr, Words);	\
142	}
143
144#define Buf_Flush(Buf, WrPtr)		{ (Buf).write_block->top = (WrPtr); }
145
146#define BufReadA(Buf)	\
147	((uword *)(((Buf).read_block = (Buf).write_block->first) + 1))
148
149#define BufReadW(Buf, WrPtr)	\
150	(((Buf).read_block = (Buf).write_block), (WrPtr))
151
152#define BufReadZ(Buf)	\
153	(((Buf).read_block = (Buf).write_block), (Buf).write_block->top)
154
155#define Buf_Set_Read(Buf, RdPtr) {				\
156	if ((RdPtr) < (uword *)((Buf).read_block + 1)		\
157		|| (Buf).read_block->limit < (RdPtr))		\
158	    buffer_setread(&(Buf), RdPtr);			\
159	}
160
161#define Buf_Check(Buf, RdPtr) {				\
162	if ((RdPtr) == (Buf).read_block->top)		\
163	    (RdPtr) = buffer_next(&(Buf), RdPtr);	\
164	}
165
166#define BufPos(Buf, AnyPtr)	buffer_pos(&(Buf), AnyPtr)
167
168
169typedef struct {
170	struct buffer_block_header	*read_block;
171	struct buffer_block_header	*write_block;
172	uword				*write_block_end;
173	int				has_nonpage_buffers;
174} unbounded_buffer;
175
176struct buffer_block_header {
177	struct buffer_block_header	*next;
178	struct buffer_block_header	*first;
179	uword				*top;
180	uword				*limit;
181};
182
183
184extern void	buffer_create ARGS((unbounded_buffer *bd, uword minwords));
185extern void	buffer_reinit ARGS((unbounded_buffer *bd));
186extern void	buffer_setread ARGS((unbounded_buffer *bd, uword *ptr));
187extern void	buffer_destroy ARGS((unbounded_buffer *bd));
188
189extern uword	buffer_pos ARGS((unbounded_buffer *bd, uword *ptr));
190
191extern uword	*buffer_alloc ARGS((unbounded_buffer *bd, uword *ptr, uword words));
192extern uword	*buffer_next ARGS((unbounded_buffer *bd, uword *ptr));
193
194
195/*---------------------------------------------------------------------
196 * Simplified shared heap interface
197 *---------------------------------------------------------------------*/
198
199extern struct heap_descriptor global_heap;
200
201#ifdef __STDC__
202generic_ptr     hg_alloc_size(word size);
203void            hg_free_size(generic_ptr, word size);
204generic_ptr     hg_realloc_size(generic_ptr, word, word);
205generic_ptr     hg_alloc(word size);
206void            hg_free(generic_ptr);
207generic_ptr     hg_resize(generic_ptr, word);
208int             hg_statistics(int what);
209#else /* __STDC__ */
210generic_ptr     hg_alloc_size();
211void            hg_free_size();
212generic_ptr     hg_realloc_size();
213generic_ptr     hg_alloc();
214void            hg_free();
215generic_ptr     hg_resize();
216int             hg_statistics();
217#endif /* __STDC__ */
218