1 /* stkmem.h
2  *
3  * $Id$
4  *
5  * Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
6  * Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
7  *
8  * This software was derived from Elk 1.2, which was Copyright 1987, 1988,
9  * 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
10  * by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
11  * between TELES and Nixdorf Microprocessor Engineering, Berlin).
12  *
13  * Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
14  * owners or individual owners of copyright in this software, grant to any
15  * person or company a worldwide, royalty free, license to
16  *
17  *    i) copy this software,
18  *   ii) prepare derivative works based on this software,
19  *  iii) distribute copies of this software or derivative works,
20  *   iv) perform this software, or
21  *    v) display this software,
22  *
23  * provided that this notice is not removed and that neither Oliver Laumann
24  * nor Teles nor Nixdorf are deemed to have made any representations as to
25  * the suitability of this software for any purpose nor are held responsible
26  * for any defects of this software.
27  *
28  * THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
29  */
30 
31 #ifdef HAVE_ALLOCA
32 
33 #ifdef HAVE_ALLOCA_H
34 #  include <alloca.h>
35 #endif
36 
37 /* #pragma must be indented to prevent some C-compilers from complaining
38  * about "undefined control".
39  */
40 #ifdef PRAGMA_ALLOCA
41    #pragma alloca
42 #endif
43 
44 #if !defined(alloca) && !defined(__GNUC__)
45 C_LINKAGE_BEGIN
46 extern char *alloca (int);
47 C_LINKAGE_END
48 #endif
49 
50 /* MIPS cc under Ultrix 4.2 requires argument to alloca() to be
51  * parenthesized if it's a compound expression.
52  *
53  * Declare variable in Alloca_Begin and reference it in Alloca to make
54  * sure users won't forget Alloca_Begin when using macros like
55  * Get_String_Stack().
56  */
57 #define Alloca_Begin int _Check_Alloca_Begin
58 #define Alloca(ret,type,size) (_Check_Alloca_Begin = 0,\
59     (ret) = (type)alloca((size)))
60 #define Alloca_End
61 
62 #else /* HAVE_ALLOCA */
63 
64 extern MEM_NODE *Mem_List;
65 extern char *Mem_Alloc (unsigned);
66 
67 #define Alloca_Begin MEM_NODE *_mem_first = 0
68 #define Alloca(ret,type,size) {\
69     register MEM_NODE *_p;\
70     _p = (MEM_NODE*)Mem_Alloc ((unsigned)(size) + sizeof(MEM_NODE));\
71     _p->next = Mem_List;\
72     _p->len = (size);\
73     _p->refcnt = 1;\
74     Mem_List = _p;\
75     if (_mem_first == 0) _mem_first = _p;\
76     (ret) = (type)(_p+1);\
77 }
78 #define Alloca_End {\
79     register MEM_NODE *_p, *_q;\
80     if (_mem_first != 0) {\
81         _p = Mem_List;\
82         do {\
83             _q = _p;\
84             _p = _p->next;\
85             if (--_q->refcnt == 0)\
86                 free ((char *)_q);\
87         } while (_q != _mem_first);\
88         Mem_List = _p;\
89     }\
90 }
91 
92 #endif
93