src/data_buffer.h

changeset 73
1ee9b312dc18
child 75
bf8c57437231
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /*
2 Copyright (c) 2013-2014, Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of the <organization> nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef BOTC_DATABUFFER_H
32 #define BOTC_DATABUFFER_H
33
34 #include <stdio.h>
35 #include <string.h>
36 #include "main.h"
37 #include "stringtable.h"
38
39 #define MAX_MARKS 512
40
41 extern int g_NextMark;
42
43 // ============================================================================
44 // DataBuffer: A dynamic data buffer.
45 class data_buffer
46 {
47 public:
48 // The actual buffer
49 byte* buffer;
50
51 // Allocated size of the buffer
52 int allocsize;
53
54 // Written size of the buffer
55 int writesize;
56
57 // Marks and references
58 ScriptMark* marks[MAX_MARKS];
59 ScriptMarkReference* refs[MAX_MARKS];
60
61 data_buffer (int size = 128);
62 ~data_buffer ();
63
64 // ====================================================================
65 // Write stuff to the buffer
66 // TODO: un-template and remove the union, move to source file
67 template<class T> void write (T stuff)
68 {
69 if (writesize + sizeof (T) >= allocsize)
70 {
71 // We don't have enough space in the buffer to write
72 // the stuff - thus resize. First, store the old
73 // buffer temporarily:
74 char* copy = new char[allocsize];
75 memcpy (copy, buffer, allocsize);
76
77 // Remake the buffer with the new size. Have enough space
78 // for the stuff we're going to write, as well as a bit
79 // of leeway so we don't have to resize immediately again.
80 size_t newsize = allocsize + sizeof (T) + 128;
81
82 delete buffer;
83 buffer = new unsigned char[newsize];
84 allocsize = newsize;
85
86 // Now, copy the stuff back.
87 memcpy (buffer, copy, allocsize);
88 delete copy;
89 }
90
91 // Buffer is now guaranteed to have enough space.
92 // Write the stuff one byte at a time.
93 generic_union<T> uni;
94 uni.as_t = stuff;
95
96 for (int x = 0; x < sizeof (T); x++)
97 {
98 if (writesize >= allocsize) // should NEVER happen because resizing is done above
99 error ("DataBuffer: written size exceeds allocated size!\n");
100
101 buffer[writesize] = uni.b[x];
102 writesize++;
103 }
104 }
105
106 // ====================================================================
107 // Merge another data buffer into this one.
108 void merge (data_buffer* other);
109
110 // Clones this databuffer to a new one and returns it.
111 data_buffer* clone ();
112
113 // ====================================================================
114 // Adds a mark to the buffer. A mark is a "pointer" to a particular
115 // position in the bytecode. The actual permanent position cannot
116 // be predicted in any way or form, thus these things will be used
117 // to "mark" a position like that for future use.
118 int add_mark (string name);
119
120 // ====================================================================
121 // A ref is another "mark" that references a mark. When the bytecode
122 // is written to file, they are changed into their marks' current
123 // positions. Marks themselves are never written to files, only refs are
124 int add_reference (int marknum, bool placeholder = true);
125
126 // Delete a mark and all references to it.
127 void delete_mark (int marknum);
128
129 // Adjusts a mark to the current position
130 void move_mark(int i);
131
132 void offset_mark (int mark, int offset);
133
134 // Dump the buffer (for debugging purposes)
135 void dump();
136
137 // Count the amount of marks
138 int count_marks ();
139
140 // Count the amount of refs
141 int count_references ();
142
143 // Write a float into the buffer
144 void write_float (string floatstring);
145
146 void write_string (string a);
147 };
148
149 #endif // BOTC_DATABUFFER_H

mercurial