src/data_buffer.h

changeset 88
5def6ff8b466
parent 87
8f65914e7046
child 89
029a330a9bef
equal deleted inserted replaced
87:8f65914e7046 88:5def6ff8b466
1 /*
2 Copyright 2012-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
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. 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 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef BOTC_DATABUFFER_H
30 #define BOTC_DATABUFFER_H
31
32 #include <stdio.h>
33 #include <string.h>
34 #include "main.h"
35 #include "stringtable.h"
36
37 #define MAX_MARKS 512
38
39 // ============================================================================
40 // data_buffer: A dynamic data buffer.
41 //
42 // Notes:
43 //
44 // - A mark is a "pointer" to a particular position in the bytecode. The actual
45 // permanent position cannot be predicted in any way or form, thus these things
46 // are used to "mark" a position like that for future use.
47 //
48 // - A reference is another "mark" that references a mark. When the bytecode
49 // is written to file, they are changed into their marks' current
50 // positions. Marks themselves are never written to files, only refs are
51 //
52 class data_buffer
53 {
54 PROPERTY (private, byte*, buffer, NO_OPS, STOCK_WRITE)
55 PROPERTY (private, int, allocated_size, NUM_OPS, STOCK_WRITE)
56 PROPERTY (private, byte*, writepos, NO_OPS, STOCK_WRITE)
57 PROPERTY (private, list<byte_mark*>, marks, LIST_OPS, STOCK_WRITE)
58 PROPERTY (private, list<mark_reference*>, refs, LIST_OPS, STOCK_WRITE)
59
60 public:
61 data_buffer (int size = 128);
62 ~data_buffer();
63
64 // ====================================================================
65 // Merge another data buffer into this one.
66 // Note: @other is destroyed in the process!
67 void merge_and_destroy (data_buffer* other);
68
69 // Clones this databuffer to a new one and returns it.
70 data_buffer* clone ();
71
72 byte_mark* add_mark (string name);
73
74 mark_reference* add_reference (byte_mark* mark, bool write_placeholder = true);
75 void check_space (int bytes);
76 void delete_mark (int marknum);
77 void adjust_mark(byte_mark* mark);
78 void offset_mark (byte_mark* mark, int offset);
79 byte_mark* find_mark_by_name (const string& target);
80 void dump();
81 void write_float (float a);
82 void write_string_index (const string& a);
83 void write_string (const string& a);
84 void write_byte (int8_t data);
85 void write_word (int16_t data);
86 void write_dword (int32_t data);
87 void copy_buffer (const data_buffer* buf);
88
89 inline int get_write_size() const
90 {
91 return m_writepos - get_buffer();
92 }
93 };
94
95 #endif // BOTC_DATABUFFER_H

mercurial