source: dev/trunk/abdev/BasicCompiler_Common/include/NativeCode.h@ 224

Last change on this file since 224 was 224, checked in by dai_9181, 17 years ago
File size: 2.7 KB
Line 
1#pragma once
2
3#include <vector>
4
5class Schedule
6{
7public:
8 enum Type
9 {
10 None,
11 };
12
13private:
14 Type type;
15 int offset;
16
17 // XMLシリアライズ用
18private:
19 friend class boost::serialization::access;
20 template<class Archive> void serialize(Archive& ar, const unsigned int version)
21 {
22 ar & BOOST_SERIALIZATION_NVP( type );
23 ar & BOOST_SERIALIZATION_NVP( offset );
24 }
25
26public:
27 Schedule()
28 {
29 }
30 Schedule( Type type, int offset )
31 : type( type )
32 , offset( offset )
33 {
34 }
35 ~Schedule()
36 {
37 }
38};
39typedef std::vector<Schedule> Schedules;
40
41class NativeCode
42{
43 int allocateSize;
44 char *codeBuffer;
45 int size;
46
47 Schedules schedules;
48
49 // XMLシリアライズ用
50private:
51 friend class boost::serialization::access;
52 BOOST_SERIALIZATION_SPLIT_MEMBER();
53 template<class Archive> void load(Archive& ar, const unsigned int version)
54 {
55 std::string code;
56 ar & BOOST_SERIALIZATION_NVP( code );
57 ar & BOOST_SERIALIZATION_NVP( size );
58 ar & BOOST_SERIALIZATION_NVP( schedules );
59
60 // 読み込み後の処理
61 Realloc( size );
62 for( int i=0; i<size; i++ )
63 {
64 char c;
65 sscanf( code.c_str() + i*3, "%02x,", &c );
66 codeBuffer[i] = c;
67 }
68 }
69 template<class Archive> void save(Archive& ar, const unsigned int version) const
70 {
71 // 保存準備
72 char *tempCode = (char *)malloc( (size+1) * 3 );
73 for( int i=0; i<size; i++ )
74 {
75 char temp[32];
76 sprintf( temp, "%02x,", codeBuffer[i] );
77 tempCode[i*3] = temp[0];
78 tempCode[i*3+1] = temp[1];
79 tempCode[i*3+2] = temp[2];
80 }
81
82 std::string code = tempCode;
83 free( tempCode );
84
85 ar & BOOST_SERIALIZATION_NVP( code );
86 ar & BOOST_SERIALIZATION_NVP( size );
87 ar & BOOST_SERIALIZATION_NVP( schedules );
88 }
89
90
91 void Realloc( int additionSize = 0 )
92 {
93 if( allocateSize < size + 8192 + additionSize )
94 {
95 while( allocateSize < size + 8192 + additionSize )
96 {
97 allocateSize += 8192;
98 }
99 codeBuffer = (char *)realloc( codeBuffer, allocateSize );
100 }
101 }
102
103public:
104 NativeCode()
105 : allocateSize( 8192 )
106 , codeBuffer( (char *)malloc( allocateSize ) )
107 , size( 0 )
108 {
109 }
110 ~NativeCode()
111 {
112 free( codeBuffer );
113 }
114 void Clear()
115 {
116 size = 0;
117 }
118
119 void Put( const char *codeBuffer, int size )
120 {
121 Realloc( size );
122
123 memcpy( this->codeBuffer + this->size, codeBuffer, size );
124 this->size += size;
125 }
126 void Put( const NativeCode &nativeCode )
127 {
128 Put( nativeCode.codeBuffer, nativeCode.size );
129 }
130 void Put( char c )
131 {
132 codeBuffer[size++] = c;
133 Realloc();
134 }
135 void Put( long l, Schedule::Type scheduleType = Schedule::None )
136 {
137 if( scheduleType != Schedule::None )
138 {
139 schedules.push_back( Schedule( scheduleType, size ) );
140 }
141
142 *((long *)(codeBuffer+size))=l;
143 size += sizeof(long);
144 }
145};
Note: See TracBrowser for help on using the repository browser.