1 /*--------------------------------------------------------------------------
2
3 REVERSEM :
4
5 UNIT : HEAP CLASS.
6
7 Since the Borland heap SUCKS, I had to add this.
8
9 COPYRIGHT (C) 1994 Erich P. Gatejen ALL RIGHTS RESERVED
10 This is Freeware. You may distribute it as you wish. However, you may not
11 distribute a modified version without my consent.
12
13 Feel free to cut and paste any algorthm.
14
15 NOTE: XTILE is (C) 1992, 1994 by myself. It is NOT freeware. You may use
16 it for personal projects, but otherwise, it is not to be distributed
17 outside this REVERSEM package. (Contact me if you wish to do
18 otherwise)
19
20 ---------------------------------------------------------------------------*/
21
22 // ------------------------------------------------------------------------
23 // --- INCLUDES
24 // ------------------------------------------------------------------------
25 #include<stdlib.h>
26 #include<alloc.h>
27 #include"heap.hpp"
28
29 // ------------------------------------------------------------------------
30 // --- CONSTRUCTOR/DESTRUCTOR
31 // ------------------------------------------------------------------------
32
33 Heap::Heap( unsigned int Size ) {
34
35 unsigned int Temp;
36
37 // Clear the zone pointers
38 for ( Temp = 0; Temp < MAX_ZONES; Temp++ ) {
39 Zones[Temp] = NULL;
40 }
41
42 // Figure the zones needed. And allocate them.
43 // We are going to double word align these boys.
44 Temp = Size & 0x0003;
45 if ( Temp ) {
46 Temp = 4 - Temp;
47 ObjectSize = Size + Temp;
48 } else
49 ObjectSize = Size;
50
51
52 SlotsLeft = MAX_ZONESIZE / ObjectSize;
53
54 CurrentZone = 0;
55 ZoneSlots = SlotsLeft;
56 Zones[CurrentZone] = (char *) farmalloc( ZoneSlots * ObjectSize );
57
58 // Set next slot
59 NextSlot = Zones[CurrentZone];
60
61 };
62
63
64 Heap::~Heap( void ) {
65
66 unsigned int Temp;
67
68 // Kill of zones
69 for( Temp = 0; Temp < MAX_ZONES; Temp++ ) {
70
71 if ( Zones[Temp] != NULL )
72 farfree( Zones[Temp] );
73 }
74
75 };
76
77 // ------------------------------------------------------------------------
78 // --- PUBLIC MEMBERS
79 // ------------------------------------------------------------------------
80
81 void* Heap::Allocate( void ) {
82
83 void* ReturnPointer;
84
85 void* MemDump;
86
87 // If a slot is readily available, allocate it. Else, get a zone for it
88 if ( SlotsLeft ) {
89
90 ReturnPointer = (void *) NextSlot;
91 SlotsLeft--;
92 if ( SlotsLeft ) {
93 NextSlot = NextSlot + ObjectSize;
94 };
95
96 } else {
97
98 // Can a new zone be had?
99 CurrentZone++;
100 if( CurrentZone == MAX_ZONES ) {
101
102 // No!!! return a null pointer
103 CurrentZone--;
104 ReturnPointer = NULL;
105
106 } else {
107
108 // Yes!! Build it if we have to or can
109 if ( Zones[CurrentZone] == NULL ) {
110 if ( (long)(farcoreleft()+4) <= (long)(ZoneSlots * ObjectSize) ) {
111 // Couldn't get the memory, return a nul pointer
112 CurrentZone--;
113 ReturnPointer = NULL;
114 return ReturnPointer;
115
116 } else {
117
118 Zones[CurrentZone] =
119 (char *) farmalloc( ZoneSlots * ObjectSize );
120
121 }
122
123 }
124
125 // Allocate it
126 NextSlot = Zones[CurrentZone] + ObjectSize; // remember, we are
127 SlotsLeft = ZoneSlots - 1; // allocating one
128
129 ReturnPointer = (void *) Zones[CurrentZone];
130
131 }
132 } // end if slots left
133
134 return ReturnPointer;
135
136 };
137
138
139 void Heap::Reset( void ) {
140
141 // Reset back to first zone. Don't dump any of the zones.
142 CurrentZone = 0;
143 NextSlot = Zones[CurrentZone];
144 SlotsLeft = ZoneSlots;
145
146 };
147
148
|