added Info.plist
[TestXSLT.git] / libsablot / src / engine / arena.h
1 /* 
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  * 
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  * 
12  * The Original Code is the Sablotron XSLT Processor.
13  * 
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  * 
18  * Contributor(s):
19  * 
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable 
23  * instead of those above.  If you wish to allow use of your 
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32
33 #ifndef ArenaHIncl
34 #define ArenaHIncl
35
36 // GP: clean
37
38 #include "base.h"
39
40 struct ArenaBlock
41 {
42     void *data;
43     ArenaBlock *next;
44     int freeSpace;
45 };
46
47 class SabArena
48 {
49 public:
50     SabArena(int bloxize_);
51     /*size of by Tim Crook release 0.51 _PH_ */
52     void* armalloc(int size, int alignment = sizeof (void*)); 
53     void initialize(int bloxize_);
54     void arfree(void *p);
55     void dispose();
56     ~SabArena();
57 private:
58     ArenaBlock* newBlock(int size);
59     int bloxize;
60     ArenaBlock *block0, *blockn;
61     int totalAsked, totalMalloced;
62 };
63
64 class SabArenaMember
65 {
66 public:
67     /* VC++ 4.2 doesn't take the following:
68     void operator delete(void *ptr, SabArena *arena_) {};
69     so far, VC++6 throws warnings
70     */
71
72     void *operator new(size_t size, SabArena *arena_ = NULL)
73     {
74         // shouldn't be called without an arena specified
75 //        assert(arena_);
76         return arena_? arena_ -> armalloc(size) : ::operator new(size);
77     };
78
79     void operator delete(void *ptr, size_t size)
80     {
81         // do nothing
82         // ::operator delete(ptr);
83     }
84 };
85
86 class SabArenaStr : public SabArenaMember, public Str
87 {
88 public:
89     SabArenaStr(SabArena *arena__)
90         : arena_(arena__) 
91     {};
92     SabArenaStr(Str& string, SabArena *arena__)
93         : arena_(arena__)
94     {
95         operator =(string);
96     };
97 /*
98     SabArenaStr(int num, SabArena *arena__)
99         : arena_(arena__)
100     {
101         operator =(num);
102     };
103
104     SabArenaStr(double num, SabArena *arena__)
105         : arena_(arena__)
106     {
107         operator =(num);
108     };
109
110     SabArenaStr(char c, SabArena *arena__)
111         : arena_(arena__)
112     {
113         operator =(c);
114     };
115 */
116     SabArenaStr(char *chars, SabArena *arena__)
117         : arena_(arena__)
118     {
119         operator =(chars);
120     };
121     SabArenaStr(DStr &dstring, SabArena *arena__)
122         : arena_(arena__)
123     {
124         operator =(dstring);
125     };
126
127     const SabArenaStr& operator= (const Str& string)
128     { nset((char*)string, string.length()); return *this; }
129
130     const SabArenaStr& operator= (const DStr& dstring)
131     { nset((char*)dstring, dstring.length()); return *this; }
132
133     const SabArenaStr& operator= (const char *s)
134     { nset(s, strlen(s)); return *this; }
135
136     ~SabArenaStr()
137     {
138         remove_();
139     }
140
141 protected:
142     virtual char* claimMemory( int nbytes ) const 
143     { 
144         // ask for memory with alignment 1 if from arena
145         return arena_ ? (char*)(arena_ -> armalloc(nbytes,1)) : Str::claimMemory(nbytes); 
146     }
147     virtual void returnMemory( char *&p ) const 
148     {
149         if (arena_)
150             arena_ -> arfree(p);
151         else
152             if (p) delete[] p; 
153         p = NULL; 
154     }
155     SabArena *arena_;
156 };
157
158 #endif // ArenaHIncl