@@ -232,13 +232,11 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
232
232
lit_mem_to_snapshot_id_map_entry_t * * out_map_p , /**< [out] map from literal identifiers
233
233
* to the literal offsets
234
234
* in snapshot */
235
- uint32_t * out_map_len_p , /**< [out] number of literals */
236
- uint32_t * out_lit_table_size_p ) /**< [out] number of bytes, saved to snapshot buffer */
235
+ uint32_t * out_map_len_p ) /**< [out] number of literals */
237
236
{
238
237
/* Count literals and literal space. */
239
- uint32_t string_count = 0 ;
240
- uint32_t number_count = 0 ;
241
- uint32_t lit_table_size = 2 * sizeof (uint32_t );
238
+ uint32_t lit_table_size = sizeof (uint32_t );
239
+ uint32_t total_count = 0 ;
242
240
243
241
ecma_lit_storage_item_t * string_list_p = JERRY_CONTEXT (string_list_first_p );
244
242
@@ -253,13 +251,15 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
253
251
254
252
lit_table_size += (uint32_t ) JERRY_ALIGNUP (sizeof (uint16_t ) + ecma_string_get_size (string_p ),
255
253
JERRY_SNAPSHOT_LITERAL_ALIGNMENT );
256
- string_count ++ ;
254
+ total_count ++ ;
257
255
}
258
256
}
259
257
260
258
string_list_p = JMEM_CP_GET_POINTER (ecma_lit_storage_item_t , string_list_p -> next_cp );
261
259
}
262
260
261
+ uint32_t number_offset = lit_table_size ;
262
+
263
263
ecma_lit_storage_item_t * number_list_p = JERRY_CONTEXT (number_list_first_p );
264
264
265
265
while (number_list_p != NULL )
@@ -269,7 +269,7 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
269
269
if (number_list_p -> values [i ] != JMEM_CP_NULL )
270
270
{
271
271
lit_table_size += (uint32_t ) sizeof (ecma_number_t );
272
- number_count ++ ;
272
+ total_count ++ ;
273
273
}
274
274
}
275
275
@@ -288,7 +288,6 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
288
288
return false;
289
289
}
290
290
291
- uint32_t total_count = string_count + number_count ;
292
291
lit_mem_to_snapshot_id_map_entry_t * map_p ;
293
292
294
293
map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t ));
@@ -299,17 +298,14 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
299
298
* in_out_buffer_offset_p += lit_table_size ;
300
299
* out_map_p = map_p ;
301
300
* out_map_len_p = total_count ;
302
- * out_lit_table_size_p = lit_table_size ;
303
301
304
302
/* Write data into the buffer. */
305
303
306
304
/* The zero value is reserved for NULL (no literal)
307
305
* constant so the first literal must have offset one. */
308
306
uint32_t literal_offset = JERRY_SNAPSHOT_LITERAL_ALIGNMENT ;
309
307
310
- buffer_p [0 ] = string_count ;
311
- buffer_p [1 ] = number_count ;
312
- buffer_p += 2 ;
308
+ * buffer_p ++ = number_offset ;
313
309
314
310
string_list_p = JERRY_CONTEXT (string_list_first_p );
315
311
@@ -385,132 +381,47 @@ ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot
385
381
#ifdef JERRY_ENABLE_SNAPSHOT_EXEC
386
382
387
383
/**
388
- * Helper function for ecma_load_literals_from_snapshot.
389
- *
390
- * Note: always inline because it is used only once.
384
+ * Computes the base pointer of the literals and starting offset of numbers.
391
385
*
392
- * @return true - if load was performed successfully
393
- * false - otherwise (i.e. buffer length is incorrect)
386
+ * @return the base pointer of the literals
394
387
*/
395
- static inline bool __attr_always_inline___
396
- ecma_load_literals_from_buffer (const uint16_t * buffer_p , /**< buffer with literal table in snapshot */
397
- uint32_t lit_table_size , /**< size of literal table in snapshot */
398
- lit_mem_to_snapshot_id_map_entry_t * map_p , /**< literal map */
399
- uint32_t string_count , /**< number of strings */
400
- uint32_t number_count ) /**< number of numbers */
388
+ const uint8_t *
389
+ ecma_snapshot_get_literals_base (uint32_t * buffer_p , /**< literal buffer start */
390
+ const uint8_t * * number_base_p ) /**< [out] literal number start */
401
391
{
402
- /* The zero value is reserved for NULL (no literal)
403
- * constant so the first literal must have offset one. */
404
- uint32_t literal_offset = JERRY_SNAPSHOT_LITERAL_ALIGNMENT ;
405
-
406
- /* Load strings first. */
407
- while (string_count > 0 )
408
- {
409
- if (lit_table_size < literal_offset + sizeof (uint32_t ))
410
- {
411
- /* Buffer is not sufficent. */
412
- return false;
413
- }
414
-
415
- lit_utf8_size_t length = * buffer_p ;
416
- lit_utf8_size_t aligned_length = JERRY_ALIGNUP (sizeof (uint16_t ) + length ,
417
- JERRY_SNAPSHOT_LITERAL_ALIGNMENT );
392
+ * number_base_p = ((uint8_t * ) buffer_p ) + buffer_p [0 ];
418
393
419
- if (lit_table_size < literal_offset + aligned_length )
420
- {
421
- /* Buffer is not sufficent. */
422
- return false;
423
- }
424
-
425
- map_p -> literal_id = ecma_find_or_create_literal_string (((lit_utf8_byte_t * ) buffer_p ) + sizeof (uint16_t ), length );
426
- map_p -> literal_offset = (jmem_cpointer_t ) (literal_offset >> JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG );
427
- map_p ++ ;
428
-
429
- JERRY_ASSERT ((aligned_length % sizeof (uint16_t )) == 0 );
430
- buffer_p += aligned_length / sizeof (uint16_t );
431
- literal_offset += aligned_length ;
432
-
433
- string_count -- ;
434
- }
435
-
436
- /* Load numbers. */
437
- while (number_count > 0 )
438
- {
439
- if (lit_table_size < literal_offset + sizeof (ecma_number_t ))
440
- {
441
- /* Buffer is not sufficent. */
442
- return false;
443
- }
444
-
445
- ecma_number_t num ;
446
- memcpy (& num , buffer_p , sizeof (ecma_number_t ));
447
-
448
- map_p -> literal_id = ecma_find_or_create_literal_number (num );
449
- map_p -> literal_offset = (jmem_cpointer_t ) (literal_offset >> JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG );
450
- map_p ++ ;
451
-
452
- ecma_length_t length = JERRY_ALIGNUP (sizeof (ecma_number_t ),
453
- JERRY_SNAPSHOT_LITERAL_ALIGNMENT );
454
-
455
- JERRY_ASSERT ((length % sizeof (uint16_t )) == 0 );
456
- buffer_p += length / sizeof (uint16_t );
457
- literal_offset += length ;
458
-
459
- number_count -- ;
460
- }
461
-
462
- return (lit_table_size == (literal_offset + 2 * sizeof (uint32_t ) - JERRY_SNAPSHOT_LITERAL_ALIGNMENT ));
463
- } /* ecma_load_literals_from_buffer */
394
+ return ((uint8_t * ) (buffer_p + 1 )) - JERRY_SNAPSHOT_LITERAL_ALIGNMENT ;
395
+ } /* ecma_snapshot_get_literals_base */
464
396
465
397
/**
466
- * Load literals from snapshot .
398
+ * Get the compressed pointer of a given literal .
467
399
*
468
- * @return true - if load was performed successfully (i.e. literals saved in the snapshot are consistent),
469
- * false - otherwise (i.e. snapshot is incorrect)
400
+ * @return literal compressed pointer
470
401
*/
471
- bool
472
- ecma_load_literals_from_snapshot (const uint32_t * buffer_p , /**< buffer with literal table in snapshot */
473
- uint32_t lit_table_size , /**< size of literal table in snapshot */
474
- lit_mem_to_snapshot_id_map_entry_t * * out_map_p , /**< [out] map from literal offsets
475
- * in snapshot to identifiers
476
- * of loaded literals in literal
477
- * storage */
478
- uint32_t * out_map_len_p ) /**< [out] literals number */
402
+ jmem_cpointer_t
403
+ ecma_snapshot_get_literal (const uint8_t * literal_base_p , /**< literal start */
404
+ const uint8_t * number_base_p , /**< literal number start */
405
+ jmem_cpointer_t offset )
479
406
{
480
- * out_map_p = NULL ;
481
-
482
- if (lit_table_size < 2 * sizeof (uint32_t ))
407
+ if (offset == 0 )
483
408
{
484
- /* Buffer is not sufficent. */
485
- return false;
409
+ return ECMA_NULL_POINTER ;
486
410
}
487
411
488
- uint32_t string_count = buffer_p [0 ];
489
- uint32_t number_count = buffer_p [1 ];
490
- buffer_p += 2 ;
412
+ const uint8_t * literal_p = literal_base_p + (((size_t ) offset ) << JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG );
491
413
492
- uint32_t total_count = string_count + number_count ;
493
- lit_mem_to_snapshot_id_map_entry_t * map_p ;
494
-
495
- * out_map_len_p = total_count ;
496
-
497
- if (total_count == 0 )
414
+ if (literal_p >= number_base_p )
498
415
{
499
- return true;
416
+ ecma_number_t num ;
417
+ memcpy (& num , literal_p , sizeof (ecma_number_t ));
418
+ return ecma_find_or_create_literal_number (num );
500
419
}
501
420
502
- map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t ));
503
- * out_map_p = map_p ;
504
-
505
- if (ecma_load_literals_from_buffer ((uint16_t * ) buffer_p , lit_table_size , map_p , string_count , number_count ))
506
- {
507
- return true;
508
- }
421
+ uint16_t length = * (const uint16_t * ) literal_p ;
509
422
510
- jmem_heap_free_block (map_p , total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t ));
511
- * out_map_p = NULL ;
512
- return false;
513
- } /* ecma_load_literals_from_snapshot */
423
+ return ecma_find_or_create_literal_string (literal_p + sizeof (uint16_t ), length );
424
+ } /* ecma_snapshot_get_literal */
514
425
515
426
#endif /* JERRY_ENABLE_SNAPSHOT_EXEC */
516
427
0 commit comments