src/DataBuffer.cc

changeset 112
def56932f938
parent 108
6409ece8297c
child 115
9be16e1c1e44
equal deleted inserted replaced
111:87d9ebd3ef34 112:def56932f938
31 // ============================================================================ 31 // ============================================================================
32 // 32 //
33 DataBuffer::DataBuffer (int size) 33 DataBuffer::DataBuffer (int size)
34 { 34 {
35 SetBuffer (new char[size]); 35 SetBuffer (new char[size]);
36 SetPosition (&GetBuffer()[0]); 36 SetPosition (&Buffer()[0]);
37 SetAllocatedSize (size); 37 SetAllocatedSize (size);
38 } 38 }
39 39
40 // ============================================================================ 40 // ============================================================================
41 // 41 //
42 DataBuffer::~DataBuffer() 42 DataBuffer::~DataBuffer()
43 { 43 {
44 assert (CountMarks() == 0 && CountReferences() == 0); 44 assert (Marks().Size() == 0 && References().Size() == 0);
45 delete GetBuffer(); 45 delete Buffer();
46 } 46 }
47 47
48 // ============================================================================ 48 // ============================================================================
49 // 49 //
50 void DataBuffer::MergeAndDestroy (DataBuffer* other) 50 void DataBuffer::MergeAndDestroy (DataBuffer* other)
75 75
76 // ============================================================================ 76 // ============================================================================
77 // 77 //
78 void DataBuffer::CopyBuffer (const DataBuffer* buf) 78 void DataBuffer::CopyBuffer (const DataBuffer* buf)
79 { 79 {
80 CheckSpace (buf->GetWrittenSize()); 80 CheckSpace (buf->WrittenSize());
81 memcpy (mPosition, buf->GetBuffer(), buf->GetWrittenSize()); 81 memcpy (mPosition, buf->Buffer(), buf->WrittenSize());
82 mPosition += buf->GetWrittenSize(); 82 mPosition += buf->WrittenSize();
83 } 83 }
84 84
85 // ============================================================================ 85 // ============================================================================
86 // 86 //
87 void DataBuffer::TransferMarks (DataBuffer* other) 87 void DataBuffer::TransferMarks (DataBuffer* other)
88 { 88 {
89 int offset = other->GetWrittenSize(); 89 int offset = other->WrittenSize();
90 90
91 for (ByteMark* mark : GetMarks()) 91 for (ByteMark* mark : Marks())
92 { 92 {
93 mark->pos += offset; 93 mark->pos += offset;
94 other->PushToMarks (mark); 94 other->mMarks << mark;
95 } 95 }
96 96
97 for (MarkReference* ref : GetReferences()) 97 for (MarkReference* ref : References())
98 { 98 {
99 ref->pos += offset; 99 ref->pos += offset;
100 other->PushToReferences (ref); 100 other->mReferences << ref;
101 } 101 }
102 102
103 ClearMarks(); 103 mMarks.Clear();
104 ClearReferences(); 104 mReferences.Clear();
105 } 105 }
106 106
107 // ============================================================================ 107 // ============================================================================
108 // 108 //
109 ByteMark* DataBuffer::AddMark (String name) 109 ByteMark* DataBuffer::AddMark (String name)
110 { 110 {
111 ByteMark* mark = new ByteMark; 111 ByteMark* mark = new ByteMark;
112 mark->name = name; 112 mark->name = name;
113 mark->pos = GetWrittenSize(); 113 mark->pos = WrittenSize();
114 PushToMarks (mark); 114 mMarks << mark;
115 return mark; 115 return mark;
116 } 116 }
117 117
118 // ============================================================================ 118 // ============================================================================
119 // 119 //
120 MarkReference* DataBuffer::AddReference (ByteMark* mark) 120 MarkReference* DataBuffer::AddReference (ByteMark* mark)
121 { 121 {
122 MarkReference* ref = new MarkReference; 122 MarkReference* ref = new MarkReference;
123 ref->target = mark; 123 ref->target = mark;
124 ref->pos = GetWrittenSize(); 124 ref->pos = WrittenSize();
125 PushToReferences (ref); 125 mReferences << ref;
126 126
127 // Write a dummy placeholder for the reference 127 // Write a dummy placeholder for the reference
128 WriteDWord (0xBEEFCAFE); 128 WriteDWord (0xBEEFCAFE);
129 129
130 return ref; 130 return ref;
132 132
133 // ============================================================================ 133 // ============================================================================
134 // 134 //
135 void DataBuffer::AdjustMark (ByteMark* mark) 135 void DataBuffer::AdjustMark (ByteMark* mark)
136 { 136 {
137 mark->pos = GetWrittenSize(); 137 mark->pos = WrittenSize();
138 } 138 }
139 139
140 // ============================================================================ 140 // ============================================================================
141 // 141 //
142 void DataBuffer::OffsetMark (ByteMark* mark, int offset) 142 void DataBuffer::OffsetMark (ByteMark* mark, int offset)
147 // ============================================================================ 147 // ============================================================================
148 // 148 //
149 void DataBuffer::WriteStringIndex (const String& a) 149 void DataBuffer::WriteStringIndex (const String& a)
150 { 150 {
151 WriteDWord (DH_PushStringIndex); 151 WriteDWord (DH_PushStringIndex);
152 WriteDWord (GetStringTableIndex (a)); 152 WriteDWord (StringTableIndex (a));
153 } 153 }
154 154
155 // ============================================================================ 155 // ============================================================================
156 // 156 //
157 void DataBuffer::Dump() 157 void DataBuffer::Dump()
158 { 158 {
159 for (int i = 0; i < GetWrittenSize(); ++i) 159 for (int i = 0; i < WrittenSize(); ++i)
160 printf ("%d. [0x%X]\n", i, GetBuffer()[i]); 160 printf ("%d. [0x%X]\n", i, Buffer()[i]);
161 } 161 }
162 162
163 // ============================================================================ 163 // ============================================================================
164 // 164 //
165 void DataBuffer::CheckSpace (int bytes) 165 void DataBuffer::CheckSpace (int bytes)
166 { 166 {
167 int writesize = GetWrittenSize(); 167 int writesize = WrittenSize();
168 168
169 if (writesize + bytes < GetAllocatedSize()) 169 if (writesize + bytes < AllocatedSize())
170 return; 170 return;
171 171
172 // We don't have enough space in the buffer to write 172 // We don't have enough space in the buffer to write
173 // the stuff - thus resize. First, store the old 173 // the stuff - thus resize. First, store the old
174 // buffer temporarily: 174 // buffer temporarily:
175 char* copy = new char[GetAllocatedSize()]; 175 char* copy = new char[AllocatedSize()];
176 memcpy (copy, GetBuffer(), GetAllocatedSize()); 176 memcpy (copy, Buffer(), AllocatedSize());
177 177
178 // Remake the buffer with the new size. Have enough space 178 // Remake the buffer with the new size. Have enough space
179 // for the stuff we're going to write, as well as a bit 179 // for the stuff we're going to write, as well as a bit
180 // of leeway so we don't have to resize immediately again. 180 // of leeway so we don't have to resize immediately again.
181 int newsize = GetAllocatedSize() + bytes + 512; 181 int newsize = AllocatedSize() + bytes + 512;
182 182
183 delete GetBuffer(); 183 delete Buffer();
184 SetBuffer (new char[newsize]); 184 SetBuffer (new char[newsize]);
185 SetAllocatedSize (newsize); 185 SetAllocatedSize (newsize);
186 186
187 // Now, copy the stuff back. 187 // Now, copy the stuff back.
188 memcpy (mBuffer, copy, GetAllocatedSize()); 188 memcpy (mBuffer, copy, AllocatedSize());
189 SetPosition (GetBuffer() + writesize); 189 SetPosition (Buffer() + writesize);
190 delete copy; 190 delete copy;
191 } 191 }
192 192
193 // ============================================================================= 193 // =============================================================================
194 // 194 //
232 232
233 // ============================================================================= 233 // =============================================================================
234 // 234 //
235 ByteMark* DataBuffer::FindMarkByName (const String& target) 235 ByteMark* DataBuffer::FindMarkByName (const String& target)
236 { 236 {
237 for (ByteMark* mark : GetMarks()) 237 for (ByteMark* mark : Marks())
238 if (mark->name == target) 238 if (mark->name == target)
239 return mark; 239 return mark;
240 240
241 return null; 241 return null;
242 } 242 }

mercurial