Skip to content

Commit 5dd6993

Browse files
author
Roland Takacs
committed
Refactor jrt_read_from_buffer_by_offset and jrt_write_to_buffer_by_offset to eliminate template.
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
1 parent 8af8720 commit 5dd6993

File tree

7 files changed

+129
-78
lines changed

7 files changed

+129
-78
lines changed

jerry-core/jerry.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,8 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
16081608
if (!jrt_write_to_buffer_by_offset (buffer_p,
16091609
buffer_size,
16101610
&buffer_write_offset,
1611-
version))
1611+
&version,
1612+
sizeof (version)))
16121613
{
16131614
return 0;
16141615
}
@@ -1657,7 +1658,7 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
16571658
return 0;
16581659
}
16591660

1660-
is_ok = jrt_write_to_buffer_by_offset (buffer_p, buffer_size, &header_offset, header);
1661+
is_ok = jrt_write_to_buffer_by_offset (buffer_p, buffer_size, &header_offset, &header, sizeof (header));
16611662
JERRY_ASSERT (is_ok && header_offset < buffer_write_offset);
16621663

16631664
return buffer_write_offset;
@@ -1702,7 +1703,8 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
17021703
if (!jrt_read_from_buffer_by_offset (snapshot_data_p,
17031704
snapshot_size,
17041705
&snapshot_read,
1705-
&version))
1706+
&version,
1707+
sizeof (version)))
17061708
{
17071709
return JERRY_COMPLETION_CODE_INVALID_SNAPSHOT_FORMAT;
17081710
}

jerry-core/jrt/jrt.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include <string.h>
17+
18+
#include "jrt.h"
19+
20+
/**
21+
* Read data of specified type from specified buffer
22+
*
23+
* Note:
24+
* Offset is in-out and is incremented if the read operation completes successfully.
25+
*
26+
* @return true, if read was successful, i.e. offset + sizeof (data) doesn't exceed buffer size,
27+
* false - otherwise.
28+
*/
29+
30+
bool __attr_always_inline___
31+
jrt_read_from_buffer_by_offset (const uint8_t *buffer_p, /**< buffer */
32+
size_t buffer_size, /**< size of buffer */
33+
size_t *in_out_buffer_offset_p, /**< in: offset to read from,
34+
* out: offset, incremented on sizeof (T) */
35+
void *out_data_p, /**< out: data */
36+
size_t out_data_size) /**< size of the out_data */
37+
{
38+
if (*in_out_buffer_offset_p + out_data_size > buffer_size)
39+
{
40+
return false;
41+
}
42+
43+
memcpy (out_data_p, buffer_p + *in_out_buffer_offset_p, out_data_size);
44+
*in_out_buffer_offset_p += out_data_size;
45+
46+
return true;
47+
} /* jrt_read_from_buffer_by_offset */
48+
49+
/**
50+
* Write data of specified type to specified buffer
51+
*
52+
* Note:
53+
* Offset is in-out and is incremented if the write operation completes successfully.
54+
*
55+
* @return true, if write was successful, i.e. offset + sizeof (data) doesn't exceed buffer size,
56+
* false - otherwise.
57+
*/
58+
bool __attr_always_inline___
59+
jrt_write_to_buffer_by_offset (uint8_t *buffer_p, /**< buffer */
60+
size_t buffer_size, /**< size of buffer */
61+
size_t *in_out_buffer_offset_p, /**< in: offset to read from,
62+
* out: offset, incremented on sizeof (T) */
63+
void *data_p, /**< data */
64+
size_t data_size) /**< size of data */
65+
{
66+
if (*in_out_buffer_offset_p + data_size > buffer_size)
67+
{
68+
return false;
69+
}
70+
71+
memcpy (buffer_p + *in_out_buffer_offset_p, data_p, data_size);
72+
*in_out_buffer_offset_p += data_size;
73+
74+
return true;
75+
} /* jrt_write_to_buffer_by_offset */

jerry-core/jrt/jrt.h

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define JERRY_GLOBALS_H
1818

1919
#include <stdio.h>
20-
#include <string.h>
2120

2221
#include "jerry.h"
2322
#include "jrt-types.h"
@@ -226,60 +225,10 @@ inline void *operator new (size_t, void *where)
226225
return where;
227226
} /* operator new */
228227

229-
/**
230-
* Read data of specified type (T) from specified buffer
231-
*
232-
* Note:
233-
* Offset is in-out and is incremented if the read operation completes successfully.
234-
*
235-
* @return true, if read was successful, i.e. offset + sizeof (T) doesn't exceed buffer size,
236-
* false - otherwise.
237-
*/
238-
template<typename T>
239-
bool __attr_always_inline___
240-
jrt_read_from_buffer_by_offset (const uint8_t *buffer_p, /**< buffer */
241-
size_t buffer_size, /**< size of buffer */
242-
size_t *in_out_buffer_offset_p, /**< in: offset to read from,
243-
* out: offset, incremented on sizeof (T) */
244-
T *out_data_p) /**< out: data */
245-
{
246-
if (*in_out_buffer_offset_p + sizeof (T) > buffer_size)
247-
{
248-
return false;
249-
}
250-
251-
memcpy (out_data_p, buffer_p + *in_out_buffer_offset_p, sizeof (T));
252-
*in_out_buffer_offset_p += sizeof (T);
253-
254-
return true;
255-
} /* jrt_read_from_buffer_by_offset */
256-
257-
/**
258-
* Write data of specified type (T) to specified buffer
259-
*
260-
* Note:
261-
* Offset is in-out and is incremented if the write operation completes successfully.
262-
*
263-
* @return true, if write was successful, i.e. offset + sizeof (T) doesn't exceed buffer size,
264-
* false - otherwise.
265-
*/
266-
template<typename T>
267-
bool __attr_always_inline___
268-
jrt_write_to_buffer_by_offset (uint8_t *buffer_p, /**< buffer */
269-
size_t buffer_size, /**< size of buffer */
270-
size_t *in_out_buffer_offset_p, /**< in: offset to read from,
271-
* out: offset, incremented on sizeof (T) */
272-
T data) /**< data */
273-
{
274-
if (*in_out_buffer_offset_p + sizeof (T) > buffer_size)
275-
{
276-
return false;
277-
}
278-
279-
memcpy (buffer_p + *in_out_buffer_offset_p, &data, sizeof (T));
280-
*in_out_buffer_offset_p += sizeof (T);
228+
extern bool
229+
jrt_read_from_buffer_by_offset (const uint8_t *, size_t, size_t *, void *, size_t);
281230

282-
return true;
283-
} /* jrt_write_to_buffer_by_offset */
231+
extern bool
232+
jrt_write_to_buffer_by_offset (uint8_t *, size_t, size_t *, void *, size_t);
284233

285234
#endif /* !JERRY_GLOBALS_H */

jerry-core/lit/lit-literal-storage.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ lit_charset_record_t::dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump
232232
size_t *in_out_buffer_offset_p) /**< in-out: buffer write offset */
233233
{
234234
lit_utf8_size_t length = get_length ();
235-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, length))
235+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &length, sizeof (length)))
236236
{
237237
return 0;
238238
}
@@ -246,7 +246,7 @@ lit_charset_record_t::dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump
246246
lit_utf8_byte_t next_byte = it_this.read<lit_utf8_byte_t> ();
247247
it_this.skip<lit_utf8_byte_t> ();
248248

249-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, next_byte))
249+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &next_byte, sizeof (next_byte)))
250250
{
251251
return 0;
252252
}
@@ -269,7 +269,7 @@ lit_number_record_t::dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump t
269269
/* dumping as double (not ecma_number_t), because ecma_number_t can be float or double,
270270
* depending on engine compile-time configuration */
271271
double num = get_number ();
272-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, num))
272+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &num, sizeof (num)))
273273
{
274274
return 0;
275275
}
@@ -566,7 +566,11 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer *
566566
*out_map_num_p = 0;
567567
*out_lit_table_size_p = 0;
568568

569-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, literals_num))
569+
if (!jrt_write_to_buffer_by_offset (buffer_p,
570+
buffer_size,
571+
in_out_buffer_offset_p,
572+
&literals_num,
573+
sizeof (literals_num)))
570574
{
571575
return false;
572576
}
@@ -594,7 +598,7 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer *
594598
continue;
595599
}
596600

597-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, type))
601+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &type, sizeof (type)))
598602
{
599603
is_ok = false;
600604
break;
@@ -677,7 +681,7 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer *
677681

678682
for (uint32_t i = 0; i < padding_bytes_num; i++)
679683
{
680-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, padding))
684+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &padding, sizeof (padding)))
681685
{
682686
return false;
683687
}
@@ -721,7 +725,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
721725
if (!jrt_read_from_buffer_by_offset (lit_table_p,
722726
lit_table_size,
723727
&lit_table_read,
724-
&literals_num))
728+
&literals_num,
729+
sizeof (literals_num)))
725730
{
726731
return false;
727732
}
@@ -746,7 +751,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
746751
if (!jrt_read_from_buffer_by_offset (lit_table_p,
747752
lit_table_size,
748753
&lit_table_read,
749-
&type))
754+
&type,
755+
sizeof (type)))
750756
{
751757
is_ok = false;
752758
break;
@@ -760,7 +766,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
760766
if (!jrt_read_from_buffer_by_offset (lit_table_p,
761767
lit_table_size,
762768
&lit_table_read,
763-
&length)
769+
&length,
770+
sizeof (length))
764771
|| (lit_table_read + length > lit_table_size))
765772
{
766773
is_ok = false;
@@ -776,7 +783,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
776783
if (!jrt_read_from_buffer_by_offset (lit_table_p,
777784
lit_table_size,
778785
&lit_table_read,
779-
&id))
786+
&id,
787+
sizeof (id)))
780788
{
781789
is_ok = false;
782790
break;
@@ -797,7 +805,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
797805
if (!jrt_read_from_buffer_by_offset (lit_table_p,
798806
lit_table_size,
799807
&lit_table_read,
800-
&id))
808+
&id,
809+
sizeof (id)))
801810
{
802811
is_ok = false;
803812
break;
@@ -818,7 +827,8 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li
818827
if (!jrt_read_from_buffer_by_offset (lit_table_p,
819828
lit_table_size,
820829
&lit_table_read,
821-
&num))
830+
&num,
831+
sizeof (num)))
822832
{
823833
is_ok = false;
824834
break;

jerry-core/lit/lit-literal-storage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class lit_magic_record_t : public rcs_record_t
257257
size_t *in_out_buffer_offset_p) const /**< in-out: buffer write offset */
258258
{
259259
magic_string_id_t id = get_magic_str_id<magic_string_id_t> ();
260-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, id))
260+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &id, sizeof (id)))
261261
{
262262
return 0;
263263
}

jerry-core/parser/js/collections/lit-id-hash-table.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to *
148148
uint32_t idx_num_total = (uint32_t) table_p->current_bucket_pos;
149149
JERRY_ASSERT (idx_num_total == table_p->current_bucket_pos);
150150

151-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_total))
151+
if (!jrt_write_to_buffer_by_offset (buffer_p,
152+
buffer_size,
153+
in_out_buffer_offset_p,
154+
&idx_num_total,
155+
sizeof (idx_num_total)))
152156
{
153157
return 0;
154158
}
@@ -183,7 +187,11 @@ lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to *
183187
idx_num_in_block = 0;
184188
}
185189

186-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_in_block))
190+
if (!jrt_write_to_buffer_by_offset (buffer_p,
191+
buffer_size,
192+
in_out_buffer_offset_p,
193+
&idx_num_in_block,
194+
sizeof (idx_num_in_block)))
187195
{
188196
return 0;
189197
}
@@ -205,7 +213,7 @@ lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to *
205213
JERRY_ASSERT (lit_index < literals_num);
206214

207215
uint32_t offset = lit_map_p[lit_index].literal_offset;
208-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, offset))
216+
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, &offset, sizeof (offset)))
209217
{
210218
return 0;
211219
}
@@ -215,7 +223,11 @@ lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to *
215223
{
216224
idx_num_in_block = 0;
217225

218-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_in_block))
226+
if (!jrt_write_to_buffer_by_offset (buffer_p,
227+
buffer_size,
228+
in_out_buffer_offset_p,
229+
&idx_num_in_block,
230+
sizeof (idx_num_in_block)))
219231
{
220232
return 0;
221233
}
@@ -263,7 +275,8 @@ lit_id_hash_table_load_from_snapshot (size_t blocks_count, /**< number of byte-c
263275
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
264276
idx_to_lit_map_size,
265277
&idx_to_lit_map_offset,
266-
&idx_num_in_block))
278+
&idx_num_in_block,
279+
sizeof (idx_num_in_block)))
267280
{
268281
return false;
269282
}
@@ -287,7 +300,8 @@ lit_id_hash_table_load_from_snapshot (size_t blocks_count, /**< number of byte-c
287300
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
288301
idx_to_lit_map_size,
289302
&idx_to_lit_map_offset,
290-
&lit_offset_from_snapshot))
303+
&lit_offset_from_snapshot,
304+
sizeof (lit_offset_from_snapshot)))
291305
{
292306
return false;
293307
}

jerry-core/parser/js/serializer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ serializer_load_bytecode_with_idx_map (const uint8_t *bytecode_and_idx_map_p, /*
420420
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
421421
idx_to_lit_map_size,
422422
&idx_to_lit_map_offset,
423-
&idx_num_total))
423+
&idx_num_total,
424+
sizeof (idx_num_total)))
424425
{
425426
return NULL;
426427
}

0 commit comments

Comments
 (0)