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 #include "arena.h"
34 #include "base.h"
35 #include "proc.h"
36 
37 // GP: clean (no E())
38 
SabArena(int bloxize_)39 SabArena::SabArena(int bloxize_)
40 {
41     initialize(bloxize_);
42 }
43 
initialize(int bloxize_)44 void SabArena::initialize(int bloxize_)
45 {
46     totalAsked = totalMalloced = 0;
47     bloxize = bloxize_ & ~15;    // qword-align the ends of the blocks
48     //    delaying: block0 = blockn = newBlock(bloxize);
49     block0 = blockn = NULL;
50 }
51 
newBlock(int size)52 ArenaBlock* SabArena::newBlock(int size)
53 {
54     totalMalloced += size;
55     ArenaBlock *b = new ArenaBlock;
56     b -> next = NULL;
57     b -> data = malloc(b -> freeSpace = size); // guaranteed to be well-aligned
58     b -> blockSize = size;
59     return b;
60 }
61 
dispose()62 void SabArena::dispose()
63 {
64     if (!block0) return;
65     ArenaBlock *b, *b_was;
66     for (b = block0; b; )
67     {
68         free(b -> data);
69         b = (b_was = b) -> next;
70         delete b_was;
71     };
72     // temporarily removing this to avoid the need for a Situation
73     // Log2(proc -> situation, L2_DISP_ARENA, Str(totalAsked), Str(totalMalloced));
74     initialize(bloxize);
75 }
76 
~SabArena()77 SabArena::~SabArena()
78 {
79     // printf("Arena deleted --- asked %d malloced %d", totalAsked, totalMalloced);
80     dispose();
81 }
82 
armalloc(int size,int alignment)83 void* SabArena::armalloc(int size, int alignment /* = sizeof(void*) */)
84 {
85     totalAsked += size;
86     int thisBloxize = bloxize;
87     if (!block0)
88         block0 = blockn = newBlock(bloxize);
89     thisBloxize = blockn -> blockSize;
90     if ((blockn -> freeSpace &= ~(alignment - 1)) < size) // align as necessary
91     {
92         if (size > bloxize)
93             thisBloxize = size | 15 + 1;
94         blockn = blockn -> next = newBlock(thisBloxize);
95     }
96     blockn -> freeSpace -= size;
97     return ((char*)(blockn -> data)) + (thisBloxize - blockn -> freeSpace - size);
98 }
99 
arfree(void * p)100 void SabArena::arfree(void *p)
101 {
102 }
103 
104