Rework literal storage.
The new literal storage keeps ecma strings rather than having a custom string implementation which duplicates the string management routines. Conversions between string implementations are eliminated which improved the performance by 4%. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
/* Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "lit-cpointer.h"
|
||||
#include "jrt-bit-fields.h"
|
||||
|
||||
/**
|
||||
* Compress pointer to extended compressed pointer.
|
||||
*
|
||||
* @return dynamic storage-specific extended compressed pointer
|
||||
*/
|
||||
inline lit_cpointer_t __attr_pure___ __attr_always_inline___
|
||||
lit_cpointer_compress (lit_record_t *pointer) /**< pointer to compress */
|
||||
{
|
||||
if (pointer == NULL)
|
||||
{
|
||||
return JMEM_CP_NULL;
|
||||
}
|
||||
|
||||
return (lit_cpointer_t) jmem_compress_pointer (pointer);
|
||||
} /* lit_cpointer_compress */
|
||||
|
||||
/**
|
||||
* Decompress extended compressed pointer.
|
||||
*
|
||||
* @return decompressed pointer
|
||||
*/
|
||||
inline lit_record_t * __attr_pure___ __attr_always_inline___
|
||||
lit_cpointer_decompress (lit_cpointer_t compressed_pointer) /**< recordset-specific compressed pointer */
|
||||
{
|
||||
if (compressed_pointer == JMEM_CP_NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (lit_record_t *) jmem_decompress_pointer (compressed_pointer);
|
||||
} /* lit_cpointer_decompress */
|
||||
|
||||
/**
|
||||
* Create NULL compressed pointer.
|
||||
*
|
||||
* @return NULL compressed pointer
|
||||
*/
|
||||
inline lit_cpointer_t __attr_pure___ __attr_always_inline___
|
||||
lit_cpointer_null_cp (void)
|
||||
{
|
||||
return JMEM_CP_NULL;
|
||||
} /* lit_cpointer_null_cp */
|
||||
@@ -1,38 +0,0 @@
|
||||
/* Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LIT_CPOINTER_H
|
||||
#define LIT_CPOINTER_H
|
||||
|
||||
#include "lit-literal-storage.h"
|
||||
#include "jmem-allocator.h"
|
||||
|
||||
#define LIT_CPOINTER_WIDTH (JMEM_CP_WIDTH + JMEM_ALIGNMENT_LOG - JMEM_ALIGNMENT_LOG)
|
||||
|
||||
/**
|
||||
* Dynamic storage-specific extended compressed pointer
|
||||
*
|
||||
* Note:
|
||||
* the pointer can represent addresses aligned by lit_DYN_STORAGE_LENGTH_UNIT,
|
||||
* while jmem_cpointer_t can only represent addresses aligned by JMEM_ALIGNMENT.
|
||||
*/
|
||||
typedef uint16_t lit_cpointer_t;
|
||||
|
||||
extern lit_cpointer_t lit_cpointer_compress (lit_record_t *);
|
||||
extern lit_record_t *lit_cpointer_decompress (lit_cpointer_t);
|
||||
extern lit_cpointer_t lit_cpointer_null_cp ();
|
||||
|
||||
#endif /* !LIT_CPOINTER_H */
|
||||
@@ -1,265 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "lit-literal-storage.h"
|
||||
#include "lit-cpointer.h"
|
||||
|
||||
#include "ecma-helpers.h"
|
||||
|
||||
lit_record_t *lit_storage = NULL;
|
||||
|
||||
/**
|
||||
* Create charset record in the literal storage
|
||||
*
|
||||
* @return pointer to the created record
|
||||
*/
|
||||
lit_record_t *
|
||||
lit_create_charset_literal (const lit_utf8_byte_t *str_p, /**< string to be placed into the record */
|
||||
const lit_utf8_size_t buf_size) /**< size in bytes of the buffer which holds the string */
|
||||
{
|
||||
lit_charset_record_t *rec_p = (lit_charset_record_t *) jmem_heap_alloc_block (buf_size + LIT_CHARSET_HEADER_SIZE);
|
||||
|
||||
rec_p->type = LIT_RECORD_TYPE_CHARSET;
|
||||
rec_p->next = (uint16_t) lit_cpointer_compress (lit_storage);
|
||||
lit_storage = (lit_record_t *) rec_p;
|
||||
|
||||
rec_p->hash = (uint8_t) lit_utf8_string_calc_hash (str_p, buf_size);
|
||||
rec_p->size = (uint16_t) buf_size;
|
||||
rec_p->length = (uint16_t) lit_utf8_string_length (str_p, buf_size);
|
||||
memcpy (rec_p + 1, str_p, buf_size);
|
||||
|
||||
return (lit_record_t *) rec_p;
|
||||
} /* lit_create_charset_literal */
|
||||
|
||||
/**
|
||||
* Create magic string record in the literal storage.
|
||||
*
|
||||
* @return pointer to the created record
|
||||
*/
|
||||
lit_record_t *
|
||||
lit_create_magic_literal (const lit_magic_string_id_t id) /**< id of magic string */
|
||||
{
|
||||
lit_magic_record_t *rec_p = (lit_magic_record_t *) jmem_heap_alloc_block (sizeof (lit_magic_record_t));
|
||||
rec_p->type = LIT_RECORD_TYPE_MAGIC_STR;
|
||||
rec_p->next = (uint16_t) lit_cpointer_compress (lit_storage);
|
||||
lit_storage = (lit_record_t *) rec_p;
|
||||
|
||||
rec_p->magic_id = (uint32_t) id;
|
||||
|
||||
return (lit_record_t *) rec_p;
|
||||
} /* lit_create_magic_literal */
|
||||
|
||||
/**
|
||||
* Create external magic string record in the literal storage.
|
||||
*
|
||||
* @return pointer to the created record
|
||||
*/
|
||||
lit_record_t *
|
||||
lit_create_magic_literal_ex (const lit_magic_string_ex_id_t id) /**< id of magic string */
|
||||
{
|
||||
lit_magic_record_t *rec_p = (lit_magic_record_t *) jmem_heap_alloc_block (sizeof (lit_magic_record_t));
|
||||
rec_p->type = LIT_RECORD_TYPE_MAGIC_STR_EX;
|
||||
rec_p->next = (uint16_t) lit_cpointer_compress (lit_storage);
|
||||
lit_storage = (lit_record_t *) rec_p;
|
||||
|
||||
rec_p->magic_id = (uint32_t) id;
|
||||
|
||||
return (lit_record_t *) rec_p;
|
||||
} /* lit_create_magic_literal_ex */
|
||||
|
||||
/**
|
||||
* Create number record in the literal storage.
|
||||
*
|
||||
* @return pointer to the created record
|
||||
*/
|
||||
lit_record_t *
|
||||
lit_create_number_literal (const ecma_number_t num) /**< numeric value */
|
||||
{
|
||||
lit_number_record_t *rec_p = (lit_number_record_t *) jmem_heap_alloc_block (sizeof (lit_number_record_t));
|
||||
|
||||
rec_p->type = (uint8_t) LIT_RECORD_TYPE_NUMBER;
|
||||
rec_p->next = (uint16_t) lit_cpointer_compress (lit_storage);
|
||||
lit_storage = (lit_record_t *) rec_p;
|
||||
|
||||
rec_p->number = num;
|
||||
|
||||
return (lit_record_t *) rec_p;
|
||||
} /* lit_create_number_literal */
|
||||
|
||||
/**
|
||||
* Get size of stored literal
|
||||
*
|
||||
* @return size of literal
|
||||
*/
|
||||
size_t __attr_pure___
|
||||
lit_get_literal_size (const lit_record_t *lit_p) /**< literal record */
|
||||
{
|
||||
const lit_record_type_t type = (const lit_record_type_t) lit_p->type;
|
||||
size_t size = 0;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case LIT_RECORD_TYPE_NUMBER:
|
||||
{
|
||||
size = sizeof (lit_number_record_t);
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_CHARSET:
|
||||
{
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit_p;
|
||||
size = rec_p->size + LIT_CHARSET_HEADER_SIZE;
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR:
|
||||
case LIT_RECORD_TYPE_MAGIC_STR_EX:
|
||||
{
|
||||
size = sizeof (lit_magic_record_t);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
JERRY_UNREACHABLE ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT (size > 0);
|
||||
return size;
|
||||
} /* lit_get_literal_size */
|
||||
|
||||
|
||||
/**
|
||||
* Free stored literal
|
||||
*
|
||||
* @return pointer to the next literal in the list
|
||||
*/
|
||||
lit_record_t *
|
||||
lit_free_literal (lit_record_t *lit_p) /**< literal record */
|
||||
{
|
||||
lit_record_t *const ret_p = lit_cpointer_decompress (lit_p->next);
|
||||
jmem_heap_free_block (lit_p, lit_get_literal_size (lit_p));
|
||||
return ret_p;
|
||||
} /* lit_free_literal */
|
||||
|
||||
/**
|
||||
* Count literal records in the storage
|
||||
*
|
||||
* @return number of literals
|
||||
*/
|
||||
uint32_t
|
||||
lit_count_literals ()
|
||||
{
|
||||
uint32_t num = 0;
|
||||
lit_record_t *rec_p;
|
||||
|
||||
for (rec_p = lit_storage;
|
||||
rec_p != NULL;
|
||||
rec_p = lit_cpointer_decompress (rec_p->next))
|
||||
{
|
||||
if (rec_p->type > LIT_RECORD_TYPE_FREE)
|
||||
{
|
||||
num++;
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
} /* lit_count_literals */
|
||||
|
||||
#ifdef JERRY_ENABLE_LOG
|
||||
/**
|
||||
* Dump the contents of the literal storage.
|
||||
*/
|
||||
void
|
||||
lit_dump_literals ()
|
||||
{
|
||||
lit_record_t *rec_p;
|
||||
size_t i;
|
||||
|
||||
JERRY_DLOG ("LITERALS:\n");
|
||||
|
||||
for (rec_p = lit_storage;
|
||||
rec_p != NULL;
|
||||
rec_p = lit_cpointer_decompress (rec_p->next))
|
||||
{
|
||||
JERRY_DLOG ("%p ", rec_p);
|
||||
JERRY_DLOG ("[%3zu] ", lit_get_literal_size (rec_p));
|
||||
|
||||
switch (rec_p->type)
|
||||
{
|
||||
case LIT_RECORD_TYPE_CHARSET:
|
||||
{
|
||||
const lit_charset_record_t *const record_p = (const lit_charset_record_t *) rec_p;
|
||||
char *str = (char *) (record_p + 1);
|
||||
for (i = 0; i < record_p->size; ++i, ++str)
|
||||
{
|
||||
/* TODO: Support proper printing of characters which occupy more than one byte. */
|
||||
|
||||
JERRY_DLOG ("%c", *str);
|
||||
}
|
||||
|
||||
JERRY_DLOG (" : STRING");
|
||||
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR:
|
||||
{
|
||||
lit_magic_string_id_t id = (lit_magic_string_id_t) ((lit_magic_record_t *) rec_p)->magic_id;
|
||||
JERRY_DLOG ("%s : MAGIC STRING", lit_get_magic_string_utf8 (id));
|
||||
JERRY_DLOG (" [id=%d] ", id);
|
||||
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR_EX:
|
||||
{
|
||||
lit_magic_string_ex_id_t id = ((lit_magic_record_t *) rec_p)->magic_id;
|
||||
JERRY_DLOG ("%s : EXT MAGIC STRING", lit_get_magic_string_ex_utf8 (id));
|
||||
JERRY_DLOG (" [id=%d] ", id);
|
||||
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_NUMBER:
|
||||
{
|
||||
const lit_number_record_t *const record_p = (const lit_number_record_t *) rec_p;
|
||||
ecma_number_t value;
|
||||
memcpy (&value, &record_p->number, sizeof (ecma_number_t));
|
||||
|
||||
if (ecma_number_is_nan (value))
|
||||
{
|
||||
JERRY_DLOG ("%s : NUMBER", "NaN");
|
||||
}
|
||||
else
|
||||
{
|
||||
lit_utf8_byte_t buff[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER + 1u];
|
||||
memset (buff, 0, sizeof (buff));
|
||||
|
||||
lit_utf8_size_t sz = ecma_number_to_utf8_string (value, buff, sizeof (buff));
|
||||
JERRY_ASSERT (sz <= ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER);
|
||||
|
||||
JERRY_DLOG ("%s : NUMBER", buff);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
JERRY_UNREACHABLE ();
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_DLOG ("\n");
|
||||
}
|
||||
} /* lit_dump_literals */
|
||||
#endif /* JERRY_ENABLE_LOG */
|
||||
@@ -1,106 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LIT_LITERAL_STORAGE_H
|
||||
#define LIT_LITERAL_STORAGE_H
|
||||
|
||||
#include "lit-magic-strings.h"
|
||||
#include "lit-globals.h"
|
||||
#include "ecma-globals.h"
|
||||
|
||||
/**
|
||||
* Represents the type of the record.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
LIT_RECORD_TYPE_FREE = 0, /**< Free record that marks an empty space. It doesn't hold any values. */
|
||||
LIT_RECORD_TYPE_CHARSET = 1, /**< Charset record that holds characters. */
|
||||
LIT_RECORD_TYPE_MAGIC_STR = 2, /**< Magic string record that holds a magic string id. */
|
||||
LIT_RECORD_TYPE_MAGIC_STR_EX = 3, /**< External magic string record that holds an extrernal magic string id. */
|
||||
LIT_RECORD_TYPE_NUMBER = 4 /**< Number record that holds a numeric value. */
|
||||
} lit_record_type_t;
|
||||
|
||||
/**
|
||||
* Record header
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t next; /* Compressed pointer to next record */
|
||||
uint8_t type; /* Type of record */
|
||||
} lit_record_t;
|
||||
|
||||
/**
|
||||
* Head pointer to literal storage
|
||||
*/
|
||||
extern lit_record_t *lit_storage;
|
||||
|
||||
typedef lit_record_t *lit_literal_t;
|
||||
|
||||
/**
|
||||
* Charset record header
|
||||
*
|
||||
* String is stored after the header.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t next; /* Compressed pointer to next record */
|
||||
uint8_t type; /* Type of record */
|
||||
uint8_t hash; /* Hash of the string */
|
||||
uint16_t size; /* Size of the string in bytes */
|
||||
uint16_t length; /* Number of character in the string */
|
||||
} lit_charset_record_t;
|
||||
|
||||
/**
|
||||
* Number record header
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t next; /* Compressed pointer to next record */
|
||||
uint8_t type; /* Type of record */
|
||||
ecma_number_t number; /* Number stored in the record */
|
||||
} lit_number_record_t;
|
||||
|
||||
/**
|
||||
* Magic record header
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t next; /* Compressed pointer to next record */
|
||||
uint8_t type; /* Type of record */
|
||||
uint32_t magic_id; /* Magic ID stored in the record */
|
||||
} lit_magic_record_t;
|
||||
|
||||
#define LIT_CHARSET_HEADER_SIZE (sizeof(lit_charset_record_t))
|
||||
|
||||
extern lit_record_t *lit_create_charset_literal (const lit_utf8_byte_t *, const lit_utf8_size_t);
|
||||
extern lit_record_t *lit_create_magic_literal (const lit_magic_string_id_t);
|
||||
extern lit_record_t *lit_create_magic_literal_ex (const lit_magic_string_ex_id_t);
|
||||
extern lit_record_t *lit_create_number_literal (const ecma_number_t);
|
||||
extern lit_record_t *lit_free_literal (lit_record_t *);
|
||||
extern size_t lit_get_literal_size (const lit_record_t *);
|
||||
|
||||
extern uint32_t lit_count_literals ();
|
||||
|
||||
#ifdef JERRY_ENABLE_LOG
|
||||
extern void lit_dump_literals ();
|
||||
#endif /* JERRY_ENABLE_LOG */
|
||||
|
||||
#define LIT_RECORD_IS_CHARSET(lit) (((lit_record_t *) lit)->type == LIT_RECORD_TYPE_CHARSET)
|
||||
#define LIT_RECORD_IS_MAGIC_STR(lit) (((lit_record_t *) lit)->type == LIT_RECORD_TYPE_MAGIC_STR)
|
||||
#define LIT_RECORD_IS_MAGIC_STR_EX(lit) (((lit_record_t *) lit)->type == LIT_RECORD_TYPE_MAGIC_STR_EX)
|
||||
#define LIT_RECORD_IS_NUMBER(lit) (((lit_record_t *) lit)->type == LIT_RECORD_TYPE_NUMBER)
|
||||
|
||||
#endif /* !LIT_LITERAL_STORAGE_H */
|
||||
@@ -1,384 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2016 University of Szeged.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "lit-literal.h"
|
||||
|
||||
#include "ecma-helpers.h"
|
||||
#include "lit-cpointer.h"
|
||||
#include "lit-magic-strings.h"
|
||||
#include "lit-literal-storage.h"
|
||||
|
||||
|
||||
/**
|
||||
* Initialize literal storage
|
||||
*/
|
||||
void
|
||||
lit_init (void)
|
||||
{
|
||||
lit_magic_strings_ex_init ();
|
||||
} /* lit_init */
|
||||
|
||||
/**
|
||||
* Finalize literal storage
|
||||
*/
|
||||
void
|
||||
lit_finalize (void)
|
||||
{
|
||||
#ifdef JERRY_ENABLE_LOG
|
||||
lit_dump_literals ();
|
||||
#endif /* JERRY_ENABLE_LOG */
|
||||
|
||||
while (lit_storage)
|
||||
{
|
||||
lit_storage = lit_free_literal (lit_storage);
|
||||
}
|
||||
} /* lit_finalize */
|
||||
|
||||
/**
|
||||
* Create new literal in literal storage from characters buffer.
|
||||
* Don't check if the same literal already exists.
|
||||
*
|
||||
* @return pointer to created record
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string to initialize the record,
|
||||
* could be non-zero-terminated */
|
||||
lit_utf8_size_t str_size) /**< length of the string */
|
||||
{
|
||||
JERRY_ASSERT (str_p || !str_size);
|
||||
|
||||
lit_magic_string_id_t m_str_id;
|
||||
for (m_str_id = (lit_magic_string_id_t) 0;
|
||||
m_str_id < LIT_MAGIC_STRING__COUNT;
|
||||
m_str_id = (lit_magic_string_id_t) (m_str_id + 1))
|
||||
{
|
||||
if (lit_get_magic_string_size (m_str_id) != str_size)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strncmp ((const char *) str_p, (const char *) lit_get_magic_string_utf8 (m_str_id), str_size))
|
||||
{
|
||||
return lit_create_magic_literal (m_str_id);
|
||||
}
|
||||
}
|
||||
|
||||
lit_magic_string_ex_id_t m_str_ex_id;
|
||||
for (m_str_ex_id = (lit_magic_string_ex_id_t) 0;
|
||||
m_str_ex_id < lit_get_magic_string_ex_count ();
|
||||
m_str_ex_id = (lit_magic_string_ex_id_t) (m_str_ex_id + 1))
|
||||
{
|
||||
if (lit_get_magic_string_ex_size (m_str_ex_id) != str_size)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strncmp ((const char *) str_p, (const char *) lit_get_magic_string_ex_utf8 (m_str_ex_id), str_size))
|
||||
{
|
||||
return lit_create_magic_literal_ex (m_str_ex_id);
|
||||
}
|
||||
}
|
||||
|
||||
return lit_create_charset_literal (str_p, str_size);
|
||||
} /* lit_create_literal_from_utf8_string */
|
||||
|
||||
/**
|
||||
* Find a literal in literal storage.
|
||||
* Only charset and magic string records are checked during search.
|
||||
*
|
||||
* @return pointer to a literal or NULL if no corresponding literal exists
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_find_literal_by_utf8_string (const lit_utf8_byte_t *str_p, /**< a string to search for */
|
||||
lit_utf8_size_t str_size) /**< length of the string */
|
||||
{
|
||||
JERRY_ASSERT (str_p || !str_size);
|
||||
|
||||
lit_string_hash_t str_hash = lit_utf8_string_calc_hash (str_p, str_size);
|
||||
|
||||
lit_literal_t lit;
|
||||
|
||||
for (lit = lit_storage;
|
||||
lit != NULL;
|
||||
lit = lit_cpointer_decompress (lit->next))
|
||||
{
|
||||
const lit_record_type_t type = (lit_record_type_t) lit->type;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case LIT_RECORD_TYPE_CHARSET:
|
||||
{
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
if (rec_p->hash != str_hash)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rec_p->size != str_size)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strncmp ((const char *) (rec_p + 1), (const char *) str_p, str_size))
|
||||
{
|
||||
return lit;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR:
|
||||
{
|
||||
lit_magic_string_id_t magic_id = (lit_magic_string_id_t) ((lit_magic_record_t *) lit)->magic_id;
|
||||
const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_utf8 (magic_id);
|
||||
|
||||
if (lit_get_magic_string_size (magic_id) != str_size)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size))
|
||||
{
|
||||
return lit;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR_EX:
|
||||
{
|
||||
lit_magic_string_ex_id_t magic_id = ((lit_magic_record_t *) lit)->magic_id;
|
||||
const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_ex_utf8 (magic_id);
|
||||
|
||||
if (lit_get_magic_string_ex_size (magic_id) != str_size)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size))
|
||||
{
|
||||
return lit;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
JERRY_ASSERT (type == LIT_RECORD_TYPE_NUMBER);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
} /* lit_find_literal_by_utf8_string */
|
||||
|
||||
/**
|
||||
* Check if a literal which holds the passed string exists.
|
||||
* If it doesn't exist, create a new one.
|
||||
*
|
||||
* @return pointer to existing or newly created record
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_find_or_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string, could be non-zero-terminated */
|
||||
const lit_utf8_size_t str_size) /**< length of the string */
|
||||
{
|
||||
lit_literal_t lit = lit_find_literal_by_utf8_string (str_p, str_size);
|
||||
|
||||
if (lit == NULL)
|
||||
{
|
||||
lit = lit_create_literal_from_utf8_string (str_p, str_size);
|
||||
}
|
||||
|
||||
return lit;
|
||||
} /* lit_find_or_create_literal_from_utf8_string */
|
||||
|
||||
|
||||
/**
|
||||
* Create new literal in literal storage from number.
|
||||
*
|
||||
* @return pointer to a newly created record
|
||||
*/
|
||||
inline lit_literal_t __attr_always_inline___
|
||||
lit_create_literal_from_num (const ecma_number_t num) /**< number to initialize a new number literal */
|
||||
{
|
||||
return lit_create_number_literal (num);
|
||||
} /* lit_create_literal_from_num */
|
||||
|
||||
/**
|
||||
* Find existing or create new number literal in literal storage.
|
||||
*
|
||||
* @return pointer to existing or a newly created record
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_find_or_create_literal_from_num (ecma_number_t num) /**< number which a literal should contain */
|
||||
{
|
||||
lit_literal_t lit = lit_find_literal_by_num (num);
|
||||
|
||||
if (lit == NULL)
|
||||
{
|
||||
lit = lit_create_literal_from_num (num);
|
||||
}
|
||||
|
||||
return lit;
|
||||
} /* lit_find_or_create_literal_from_num */
|
||||
|
||||
/**
|
||||
* Find an existing number literal which contains the passed number
|
||||
*
|
||||
* @return pointer to existing or null
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_find_literal_by_num (const ecma_number_t num) /**< a number to search for */
|
||||
{
|
||||
lit_literal_t lit;
|
||||
for (lit = lit_storage;
|
||||
lit != NULL;
|
||||
lit = lit_cpointer_decompress (lit->next))
|
||||
{
|
||||
const lit_record_type_t type = (lit_record_type_t) lit->type;
|
||||
|
||||
if (type != LIT_RECORD_TYPE_NUMBER)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const ecma_number_t lit_num = lit_number_literal_get_number (lit);
|
||||
|
||||
if (lit_num == num)
|
||||
{
|
||||
return lit;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
} /* lit_find_literal_by_num */
|
||||
|
||||
#ifndef JERRY_NDEBUG
|
||||
/**
|
||||
* Check if literal really exists in the storage
|
||||
*
|
||||
* @return true if literal exists in the storage
|
||||
* false otherwise
|
||||
*/
|
||||
static bool
|
||||
lit_literal_exists (lit_literal_t lit) /**< literal to check for existence */
|
||||
{
|
||||
lit_literal_t current_lit;
|
||||
|
||||
for (current_lit = lit_storage;
|
||||
current_lit != NULL;
|
||||
current_lit = lit_cpointer_decompress (current_lit->next))
|
||||
{
|
||||
if (current_lit == lit)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} /* lit_literal_exists */
|
||||
#endif /* !JERRY_NDEBUG */
|
||||
|
||||
/**
|
||||
* Convert compressed pointer to literal
|
||||
*
|
||||
* @return literal
|
||||
*/
|
||||
lit_literal_t
|
||||
lit_get_literal_by_cp (lit_cpointer_t lit_cp) /**< compressed pointer to literal */
|
||||
{
|
||||
lit_literal_t lit = lit_cpointer_decompress (lit_cp);
|
||||
#ifndef JERRY_NDEBUG
|
||||
JERRY_ASSERT (lit_literal_exists (lit));
|
||||
#endif /* !JERRY_NDEBUG */
|
||||
|
||||
return lit;
|
||||
} /* lit_get_literal_by_cp */
|
||||
|
||||
lit_string_hash_t
|
||||
lit_charset_literal_get_hash (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET);
|
||||
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
return (lit_string_hash_t) rec_p->hash;
|
||||
} /* lit_charset_literal_get_hash */
|
||||
|
||||
lit_magic_string_id_t
|
||||
lit_magic_literal_get_magic_str_id (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_MAGIC_STR);
|
||||
|
||||
const lit_magic_record_t *const rec_p = (const lit_magic_record_t *) lit;
|
||||
|
||||
return (lit_magic_string_id_t) rec_p->magic_id;
|
||||
} /* lit_magic_literal_get_magic_str_id */
|
||||
|
||||
lit_magic_string_ex_id_t
|
||||
lit_magic_literal_get_magic_str_ex_id (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_MAGIC_STR_EX);
|
||||
|
||||
const lit_magic_record_t *const rec_p = (const lit_magic_record_t *) lit;
|
||||
|
||||
return (lit_magic_string_ex_id_t) rec_p->magic_id;
|
||||
} /* lit_magic_literal_get_magic_str_ex_id */
|
||||
|
||||
inline lit_utf8_size_t __attr_always_inline___ __attr_pure___
|
||||
lit_charset_literal_get_size (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET);
|
||||
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
return (lit_utf8_size_t) rec_p->size;
|
||||
} /* lit_charset_literal_get_size */
|
||||
|
||||
/**
|
||||
* Get length of the literal
|
||||
*
|
||||
* @return code units count
|
||||
*/
|
||||
inline ecma_length_t __attr_always_inline___ __attr_pure___
|
||||
lit_charset_literal_get_length (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET);
|
||||
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
return (ecma_length_t) rec_p->length;
|
||||
} /* lit_charset_literal_get_length */
|
||||
|
||||
inline ecma_number_t __attr_always_inline___ __attr_pure___
|
||||
lit_number_literal_get_number (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_NUMBER);
|
||||
|
||||
const lit_number_record_t *const rec_p = (const lit_number_record_t *) lit;
|
||||
|
||||
return rec_p->number;
|
||||
} /* lit_number_literal_get_number */
|
||||
|
||||
inline lit_utf8_byte_t * __attr_always_inline___ __attr_pure___
|
||||
lit_charset_literal_get_charset (lit_literal_t lit) /**< literal */
|
||||
{
|
||||
JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET);
|
||||
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
return (lit_utf8_byte_t *) (rec_p + 1);
|
||||
} /* lit_charset_literal_get_charset */
|
||||
@@ -1,52 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2016 University of Szeged.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LIT_LITERAL_H
|
||||
#define LIT_LITERAL_H
|
||||
|
||||
#include "ecma-globals.h"
|
||||
#include "lit-globals.h"
|
||||
#include "lit-literal-storage.h"
|
||||
#include "lit-cpointer.h"
|
||||
|
||||
/**
|
||||
* Invalid literal
|
||||
*/
|
||||
#define NOT_A_LITERAL (lit_cpointer_null_cp ())
|
||||
|
||||
extern void lit_init ();
|
||||
extern void lit_finalize ();
|
||||
|
||||
extern lit_literal_t lit_create_literal_from_utf8_string (const lit_utf8_byte_t *, lit_utf8_size_t);
|
||||
extern lit_literal_t lit_find_literal_by_utf8_string (const lit_utf8_byte_t *, lit_utf8_size_t);
|
||||
extern lit_literal_t lit_find_or_create_literal_from_utf8_string (const lit_utf8_byte_t *, lit_utf8_size_t);
|
||||
|
||||
extern lit_literal_t lit_create_literal_from_num (ecma_number_t);
|
||||
extern lit_literal_t lit_find_literal_by_num (ecma_number_t);
|
||||
extern lit_literal_t lit_find_or_create_literal_from_num (ecma_number_t);
|
||||
|
||||
extern lit_literal_t lit_get_literal_by_cp (lit_cpointer_t);
|
||||
|
||||
extern ecma_number_t lit_number_literal_get_number (lit_literal_t);
|
||||
extern lit_string_hash_t lit_charset_literal_get_hash (lit_literal_t);
|
||||
extern lit_utf8_size_t lit_charset_literal_get_size (lit_literal_t);
|
||||
extern ecma_length_t lit_charset_literal_get_length (lit_literal_t);
|
||||
extern lit_utf8_byte_t *lit_charset_literal_get_charset (lit_literal_t);
|
||||
|
||||
extern lit_magic_string_id_t lit_magic_literal_get_magic_str_id (lit_literal_t);
|
||||
extern lit_magic_string_ex_id_t lit_magic_literal_get_magic_str_ex_id (lit_literal_t);
|
||||
|
||||
#endif /* !LIT_LITERAL_H */
|
||||
@@ -1,402 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "lit-snapshot.h"
|
||||
|
||||
#include "lit-literal.h"
|
||||
#include "lit-literal-storage.h"
|
||||
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT_SAVE
|
||||
|
||||
/**
|
||||
* Save a record to specified snapshot buffer.
|
||||
*
|
||||
* @return number of bytes saved,
|
||||
* or 0 - upon save failure
|
||||
*/
|
||||
static uint32_t
|
||||
lit_snapshot_save (lit_literal_t lit, /**< literal to save */
|
||||
uint8_t *buffer_p, /**< buffer to save to */
|
||||
size_t buffer_size, /**< buffer size */
|
||||
size_t *in_out_buffer_offset_p) /**< [in,out] buffer write offset */
|
||||
{
|
||||
const lit_record_type_t record_type = (lit_record_type_t) lit->type;
|
||||
switch (record_type)
|
||||
{
|
||||
case LIT_RECORD_TYPE_CHARSET:
|
||||
{
|
||||
const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit;
|
||||
|
||||
lit_utf8_size_t size = lit_charset_literal_get_size (lit);
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&size,
|
||||
sizeof (size)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
(void *) (rec_p + 1),
|
||||
size))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint32_t) (sizeof (uint32_t) + size * sizeof (uint8_t));
|
||||
|
||||
}
|
||||
case LIT_RECORD_TYPE_NUMBER:
|
||||
{
|
||||
double num_value = lit_number_literal_get_number (lit);
|
||||
const size_t size = sizeof (num_value);
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&num_value,
|
||||
size))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint32_t) size;
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR:
|
||||
{
|
||||
lit_magic_string_id_t id = lit_magic_literal_get_magic_str_id (lit);
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&id,
|
||||
sizeof (id)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint32_t) sizeof (lit_magic_string_id_t);
|
||||
}
|
||||
case LIT_RECORD_TYPE_MAGIC_STR_EX:
|
||||
{
|
||||
lit_magic_string_ex_id_t id = lit_magic_literal_get_magic_str_ex_id (lit);
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&id,
|
||||
sizeof (id)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint32_t) sizeof (lit_magic_string_ex_id_t);
|
||||
}
|
||||
default:
|
||||
{
|
||||
JERRY_UNREACHABLE ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_UNREACHABLE ();
|
||||
return 0;
|
||||
} /* lit_snapshot_save */
|
||||
|
||||
/**
|
||||
* Save literals to specified snapshot buffer.
|
||||
*
|
||||
* @return true, if save was performed successfully (i.e. buffer size is sufficient),
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
lit_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot buffer */
|
||||
size_t buffer_size, /**< size of the buffer */
|
||||
size_t *in_out_buffer_offset_p, /**< [in,out] write position in the buffer */
|
||||
lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal identifiers
|
||||
* to the literal offsets
|
||||
* in snapshot */
|
||||
uint32_t *out_map_num_p, /**< [out] number of literals */
|
||||
uint32_t *out_lit_table_size_p) /**< [out] number of bytes, saved to snapshot buffer */
|
||||
{
|
||||
uint32_t literals_num = lit_count_literals ();
|
||||
uint32_t lit_table_size = 0;
|
||||
|
||||
*out_map_p = NULL;
|
||||
*out_map_num_p = 0;
|
||||
*out_lit_table_size_p = 0;
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&literals_num,
|
||||
sizeof (literals_num)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
lit_table_size += (uint32_t) sizeof (literals_num);
|
||||
|
||||
if (literals_num != 0)
|
||||
{
|
||||
bool is_ok = true;
|
||||
|
||||
size_t id_map_size = sizeof (lit_mem_to_snapshot_id_map_entry_t) * literals_num;
|
||||
lit_mem_to_snapshot_id_map_entry_t *id_map_p;
|
||||
id_map_p = (lit_mem_to_snapshot_id_map_entry_t *) jmem_heap_alloc_block_store_size (id_map_size);
|
||||
|
||||
uint32_t literal_index = 0;
|
||||
lit_literal_t lit;
|
||||
|
||||
for (lit = lit_storage;
|
||||
lit != NULL;
|
||||
lit = lit_cpointer_decompress (lit->next))
|
||||
{
|
||||
lit_record_type_t record_type = (lit_record_type_t) lit->type;
|
||||
|
||||
if (record_type == LIT_RECORD_TYPE_FREE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&record_type,
|
||||
sizeof (record_type)))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t bytes = lit_snapshot_save (lit, buffer_p, buffer_size, in_out_buffer_offset_p);
|
||||
|
||||
if (bytes == 0)
|
||||
{
|
||||
/* write failed */
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
lit_cpointer_t lit_cp = lit_cpointer_compress (lit);
|
||||
id_map_p[literal_index].literal_id = lit_cp;
|
||||
id_map_p[literal_index].literal_offset = lit_table_size;
|
||||
|
||||
lit_table_size += (uint32_t) sizeof (record_type);
|
||||
lit_table_size += bytes;
|
||||
|
||||
literal_index++;
|
||||
}
|
||||
|
||||
if (!is_ok)
|
||||
{
|
||||
jmem_heap_free_block_size_stored (id_map_p);
|
||||
return false;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (literal_index == literals_num);
|
||||
*out_map_p = id_map_p;
|
||||
}
|
||||
|
||||
uint32_t aligned_size = JERRY_ALIGNUP (lit_table_size, JMEM_ALIGNMENT);
|
||||
|
||||
if (aligned_size != lit_table_size)
|
||||
{
|
||||
JERRY_ASSERT (aligned_size > lit_table_size);
|
||||
|
||||
uint8_t padding = 0;
|
||||
uint32_t padding_bytes_num = (uint32_t) (aligned_size - lit_table_size);
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < padding_bytes_num; i++)
|
||||
{
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
&padding,
|
||||
sizeof (uint8_t)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*out_map_num_p = literals_num;
|
||||
*out_lit_table_size_p = aligned_size;
|
||||
return true;
|
||||
} /* lit_save_literals_for_snapshot */
|
||||
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT_SAVE */
|
||||
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT_EXEC
|
||||
|
||||
/**
|
||||
* Load literals from snapshot.
|
||||
*
|
||||
* @return true, if load was performed successfully (i.e. literals saved in the snapshot are consistent),
|
||||
* false - otherwise (i.e. snapshot is incorrect).
|
||||
*/
|
||||
bool
|
||||
lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with literal table in snapshot */
|
||||
uint32_t lit_table_size, /**< size of literal table in snapshot */
|
||||
lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal offsets
|
||||
* in snapshot to identifiers
|
||||
* of loaded literals in literal
|
||||
* storage */
|
||||
uint32_t *out_map_num_p) /**< [out] literals number */
|
||||
{
|
||||
*out_map_p = NULL;
|
||||
*out_map_num_p = 0;
|
||||
|
||||
size_t lit_table_read = 0;
|
||||
|
||||
uint32_t literals_num;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&literals_num,
|
||||
sizeof (literals_num)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (literals_num == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t id_map_size = sizeof (lit_mem_to_snapshot_id_map_entry_t) * literals_num;
|
||||
lit_mem_to_snapshot_id_map_entry_t *id_map_p;
|
||||
id_map_p = (lit_mem_to_snapshot_id_map_entry_t *) jmem_heap_alloc_block_store_size (id_map_size);
|
||||
|
||||
bool is_ok = true;
|
||||
uint32_t lit_index;
|
||||
|
||||
for (lit_index = 0; lit_index < literals_num; ++lit_index)
|
||||
{
|
||||
uint32_t offset = (uint32_t) lit_table_read;
|
||||
JERRY_ASSERT (offset == lit_table_read);
|
||||
|
||||
lit_record_type_t type;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&type,
|
||||
sizeof (type)))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
lit_literal_t lit;
|
||||
|
||||
if (type == LIT_RECORD_TYPE_CHARSET)
|
||||
{
|
||||
lit_utf8_size_t length;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&length,
|
||||
sizeof (length))
|
||||
|| (lit_table_read + length > lit_table_size))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
lit = (lit_literal_t) lit_find_or_create_literal_from_utf8_string (lit_table_p + lit_table_read, length);
|
||||
lit_table_read += length;
|
||||
}
|
||||
else if (type == LIT_RECORD_TYPE_MAGIC_STR)
|
||||
{
|
||||
lit_magic_string_id_t id;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&id,
|
||||
sizeof (id)))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_utf8 (id);
|
||||
lit_utf8_size_t magic_str_sz = lit_get_magic_string_size (id);
|
||||
|
||||
/* TODO: Consider searching literal storage by magic string identifier instead of by its value */
|
||||
lit = (lit_literal_t) lit_find_or_create_literal_from_utf8_string (magic_str_p, magic_str_sz);
|
||||
}
|
||||
else if (type == LIT_RECORD_TYPE_MAGIC_STR_EX)
|
||||
{
|
||||
lit_magic_string_ex_id_t id;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&id,
|
||||
sizeof (id)))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
const lit_utf8_byte_t *magic_str_ex_p = lit_get_magic_string_ex_utf8 (id);
|
||||
lit_utf8_size_t magic_str_ex_sz = lit_get_magic_string_ex_size (id);
|
||||
|
||||
/* TODO: Consider searching literal storage by magic string identifier instead of by its value */
|
||||
lit = (lit_literal_t) lit_find_or_create_literal_from_utf8_string (magic_str_ex_p, magic_str_ex_sz);
|
||||
}
|
||||
else if (type == LIT_RECORD_TYPE_NUMBER)
|
||||
{
|
||||
double num;
|
||||
if (!jrt_read_from_buffer_by_offset (lit_table_p,
|
||||
lit_table_size,
|
||||
&lit_table_read,
|
||||
&num,
|
||||
sizeof (num)))
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
lit = (lit_literal_t) lit_find_or_create_literal_from_num ((ecma_number_t) num);
|
||||
}
|
||||
else
|
||||
{
|
||||
is_ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
id_map_p[lit_index].literal_offset = offset;
|
||||
id_map_p[lit_index].literal_id = lit_cpointer_compress (lit);
|
||||
}
|
||||
|
||||
if (is_ok)
|
||||
{
|
||||
*out_map_p = id_map_p;
|
||||
*out_map_num_p = literals_num;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
jmem_heap_free_block_size_stored (id_map_p);
|
||||
return false;
|
||||
} /* lit_load_literals_from_snapshot */
|
||||
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT_EXEC */
|
||||
@@ -1,47 +0,0 @@
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 University of Szeged
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LIT_SNAPSHOT_H
|
||||
#define LIT_SNAPSHOT_H
|
||||
|
||||
#include "lit-cpointer.h"
|
||||
#include "ecma-globals.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
lit_cpointer_t literal_id;
|
||||
uint32_t literal_offset;
|
||||
} lit_mem_to_snapshot_id_map_entry_t;
|
||||
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT_SAVE
|
||||
extern bool
|
||||
lit_save_literals_for_snapshot (uint8_t *,
|
||||
size_t,
|
||||
size_t *,
|
||||
lit_mem_to_snapshot_id_map_entry_t **,
|
||||
uint32_t *,
|
||||
uint32_t *);
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT_SAVE */
|
||||
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT_EXEC
|
||||
extern bool
|
||||
lit_load_literals_from_snapshot (const uint8_t *,
|
||||
uint32_t,
|
||||
lit_mem_to_snapshot_id_map_entry_t **,
|
||||
uint32_t *);
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT_EXEC */
|
||||
|
||||
#endif /* !LIT_SNAPSHOT_H */
|
||||
Reference in New Issue
Block a user