From d47c36f1b4c635d31ca22504ba26af1e2be4499d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Thu, 11 Feb 2016 14:46:08 +0100 Subject: [PATCH] New Allocator and improved String handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- CMakeLists.txt | 3 + jerry-core/config.h | 10 +- jerry-core/ecma/base/ecma-globals.h | 5 +- jerry-core/ecma/base/ecma-helpers-string.c | 548 ++++----- jerry-core/ecma/base/ecma-helpers.c | 4 +- jerry-core/ecma/base/ecma-helpers.h | 1 + .../ecma/operations/ecma-objects-arguments.c | 2 +- jerry-core/ecma/operations/ecma-objects.c | 4 +- jerry-core/jerry.c | 56 +- jerry-core/lit/lit-cpointer.c | 61 + jerry-core/lit/lit-cpointer.h | 38 + jerry-core/lit/lit-globals.h | 16 - jerry-core/lit/lit-literal-storage.c | 204 ++-- jerry-core/lit/lit-literal-storage.h | 87 +- jerry-core/lit/lit-literal.c | 384 +++--- jerry-core/lit/lit-literal.h | 20 +- jerry-core/lit/lit-snapshot.c | 175 ++- jerry-core/lit/lit-snapshot.h | 7 +- jerry-core/mem/mem-allocator.c | 16 +- jerry-core/mem/mem-allocator.h | 5 + jerry-core/mem/mem-config.h | 5 + jerry-core/mem/mem-heap.c | 1054 ++++++----------- jerry-core/mem/mem-heap.h | 44 +- jerry-core/mem/mem-poolman.c | 689 ++--------- jerry-core/mem/mem-poolman.h | 20 +- jerry-core/parser/js/common.h | 8 +- jerry-core/parser/js/js-lexer.c | 4 +- jerry-core/parser/js/js-parser.c | 15 +- jerry-core/parser/regexp/re-compiler.c | 14 +- jerry-core/rcs/rcs-allocator.c | 395 ------ jerry-core/rcs/rcs-allocator.h | 28 - jerry-core/rcs/rcs-chunked-list.c | 340 ------ jerry-core/rcs/rcs-chunked-list.h | 68 -- jerry-core/rcs/rcs-cpointer.c | 104 -- jerry-core/rcs/rcs-cpointer.h | 53 - jerry-core/rcs/rcs-globals.h | 54 - jerry-core/rcs/rcs-iterator.c | 215 ---- jerry-core/rcs/rcs-iterator.h | 42 - jerry-core/rcs/rcs-records.c | 637 ---------- jerry-core/rcs/rcs-records.h | 169 --- jerry-core/vm/vm.c | 8 +- jerry-core/vm/vm.h | 1 + tests/unit/test-heap.c | 40 +- tests/unit/test-literal-storage.c | 4 - tests/unit/test-poolman.c | 14 +- 45 files changed, 1383 insertions(+), 4288 deletions(-) create mode 100644 jerry-core/lit/lit-cpointer.c create mode 100644 jerry-core/lit/lit-cpointer.h delete mode 100644 jerry-core/rcs/rcs-allocator.c delete mode 100644 jerry-core/rcs/rcs-allocator.h delete mode 100644 jerry-core/rcs/rcs-chunked-list.c delete mode 100644 jerry-core/rcs/rcs-chunked-list.h delete mode 100644 jerry-core/rcs/rcs-cpointer.c delete mode 100644 jerry-core/rcs/rcs-cpointer.h delete mode 100644 jerry-core/rcs/rcs-globals.h delete mode 100644 jerry-core/rcs/rcs-iterator.c delete mode 100644 jerry-core/rcs/rcs-iterator.h delete mode 100644 jerry-core/rcs/rcs-records.c delete mode 100644 jerry-core/rcs/rcs-records.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8791511f9..34383ad7f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,6 +213,9 @@ project (Jerry C ASM) # Compiler / Linker flags set(COMPILE_FLAGS_JERRY "-fno-builtin") + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") + set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -DMEM_HEAP_PTR_64") + endif() if(NOT ("${PLATFORM}" STREQUAL "DARWIN")) set(LINKER_FLAGS_COMMON "-Wl,-z,noexecstack") endif() diff --git a/jerry-core/config.h b/jerry-core/config.h index 95a9985b7..e4c085d81 100644 --- a/jerry-core/config.h +++ b/jerry-core/config.h @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -47,10 +48,15 @@ # error "Currently, maximum 256 kilobytes heap size is supported" #endif /* !CONFIG_MEM_HEAP_AREA_SIZE */ +/** + * Max heap usage limit + */ +#define CONFIG_MEM_HEAP_MAX_LIMIT 8192 + /** * Desired limit of heap usage */ -#define CONFIG_MEM_HEAP_DESIRED_LIMIT (CONFIG_MEM_HEAP_AREA_SIZE / 32) +#define CONFIG_MEM_HEAP_DESIRED_LIMIT (JERRY_MIN (CONFIG_MEM_HEAP_AREA_SIZE / 32, CONFIG_MEM_HEAP_MAX_LIMIT)) /** * Log2 of maximum possible offset in the heap @@ -80,7 +86,7 @@ * Also the option affects size of ECMA Object Model's data types. * In any case size of any of the types should not exceed CONFIG_MEM_POOL_CHUNK_SIZE. */ -#define CONFIG_ECMA_REFERENCE_COUNTER_WIDTH (10) +#define CONFIG_ECMA_REFERENCE_COUNTER_WIDTH (12) #define CONFIG_ECMA_REFERENCE_COUNTER_LIMIT ((1u << CONFIG_ECMA_REFERENCE_COUNTER_WIDTH) - 1u) diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index 4c5276ef5..6d1c7721f 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -25,7 +25,6 @@ #include "config.h" #include "jrt.h" -#include "lit-globals.h" #include "lit-magic-strings.h" #include "mem-allocator.h" @@ -384,7 +383,7 @@ typedef struct ecma_object_t */ #define ECMA_OBJECT_OBJ_TYPE_POS (ECMA_OBJECT_OBJ_EXTENSIBLE_POS + \ ECMA_OBJECT_OBJ_EXTENSIBLE_WIDTH) -#define ECMA_OBJECT_OBJ_TYPE_WIDTH (4) +#define ECMA_OBJECT_OBJ_TYPE_WIDTH (3) /** * Compressed pointer to prototype object (ecma_object_t) @@ -721,7 +720,7 @@ typedef struct ecma_string_t union { /** Index of string in literal table */ - lit_cpointer_t lit_cp; + mem_cpointer_t lit_cp; /** Compressed pointer to an ecma_collection_header_t */ __extension__ mem_cpointer_t collection_cp : ECMA_POINTER_FIELD_WIDTH; diff --git a/jerry-core/ecma/base/ecma-helpers-string.c b/jerry-core/ecma/base/ecma-helpers-string.c index 0dca7d1c9..2b42c97b6 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.c +++ b/jerry-core/ecma/base/ecma-helpers-string.c @@ -31,7 +31,7 @@ #include "lit-char-helpers.h" #include "lit-literal.h" #include "lit-magic-strings.h" -#include "rcs-records.h" +#include "lit-literal-storage.h" #include "vm.h" /** @@ -46,6 +46,15 @@ JERRY_STATIC_ASSERT ((int32_t) ECMA_STRING_MAX_CONCATENATION_LENGTH == ECMA_STRING_MAX_CONCATENATION_LENGTH, ECMA_STRING_MAX_CONCATENATION_LENGTH_should_be_representable_with_int32_t); +/** + * String header + */ +typedef struct +{ + uint16_t size; /* Size of string in bytes */ + uint16_t length; /* Number of characters in the string */ +} ecma_string_heap_header_t; + static void ecma_init_ecma_string_from_lit_cp (ecma_string_t *string_p, lit_cpointer_t lit_index); @@ -56,264 +65,6 @@ ecma_init_ecma_string_from_magic_string_id (ecma_string_t *string_p, static void ecma_init_ecma_string_from_magic_string_ex_id (ecma_string_t *string_p, lit_magic_string_ex_id_t magic_string_ex_id); -/** - * Allocate a collection of ecma-chars. - * - * @return pointer to the collection's header - */ -static ecma_collection_header_t * -ecma_new_chars_collection (const lit_utf8_byte_t chars_buffer[], /**< utf-8 chars */ - lit_utf8_size_t chars_size) /**< size of buffer with chars */ -{ - JERRY_ASSERT (chars_buffer != NULL); - JERRY_ASSERT (chars_size > 0); - - ecma_collection_header_t *collection_p = ecma_alloc_collection_header (); - - collection_p->unit_number = chars_size; - - mem_cpointer_t *next_chunk_cp_p = &collection_p->first_chunk_cp; - lit_utf8_byte_t *cur_char_buf_iter_p = NULL; - lit_utf8_byte_t *cur_char_buf_end_p = NULL; - - for (lit_utf8_size_t byte_index = 0; - byte_index < chars_size; - byte_index++) - { - if (cur_char_buf_iter_p == cur_char_buf_end_p) - { - ecma_collection_chunk_t *chunk_p = ecma_alloc_collection_chunk (); - ECMA_SET_NON_NULL_POINTER (*next_chunk_cp_p, chunk_p); - next_chunk_cp_p = &chunk_p->next_chunk_cp; - - cur_char_buf_iter_p = (lit_utf8_byte_t *) chunk_p->data; - cur_char_buf_end_p = cur_char_buf_iter_p + sizeof (chunk_p->data); - } - - JERRY_ASSERT (cur_char_buf_iter_p + 1 <= cur_char_buf_end_p); - - *cur_char_buf_iter_p++ = chars_buffer[byte_index]; - } - - *next_chunk_cp_p = ECMA_NULL_POINTER; - - return collection_p; -} /* ecma_new_chars_collection */ - -/** - * Get length of a collection of ecma-chars - * - * NOTE: - * While chars collection holds a string in utf-8 encoding, this function acts as if the string was encoded in - * UTF-16 and returns number of 16-bit characters (code units) required for string representation in this format. - * - * @return number of UTF-16 code units in a collecton - */ -static ecma_length_t -ecma_get_chars_collection_length (const ecma_collection_header_t *header_p) /**< collection's header */ -{ - JERRY_ASSERT (header_p != NULL); - - const ecma_length_t chars_number = header_p->unit_number; - - const lit_utf8_byte_t *cur_char_buf_iter_p = NULL; - const lit_utf8_byte_t *cur_char_buf_end_p = NULL; - - mem_cpointer_t next_chunk_cp = header_p->first_chunk_cp; - lit_utf8_size_t skip_bytes = 0; - ecma_length_t length = 0; - - ecma_length_t char_index; - for (char_index = 0; - char_index < chars_number; - char_index++) - { - if (cur_char_buf_iter_p == cur_char_buf_end_p) - { - const ecma_collection_chunk_t *chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t, next_chunk_cp); - - cur_char_buf_iter_p = (lit_utf8_byte_t *) chunk_p->data; - cur_char_buf_end_p = cur_char_buf_iter_p + sizeof (chunk_p->data); - - next_chunk_cp = chunk_p->next_chunk_cp; - } - - JERRY_ASSERT (cur_char_buf_iter_p + 1 <= cur_char_buf_end_p); - - if (skip_bytes == 0) - { - skip_bytes = lit_get_unicode_char_size_by_utf8_first_byte (*cur_char_buf_iter_p); - length++; - skip_bytes--; - } - else - { - skip_bytes--; - } - cur_char_buf_iter_p++; - } - - JERRY_ASSERT (char_index == chars_number); - - return length; -} /* ecma_get_chars_collection_length */ - -/** - * Compare two collection of ecma-chars. - * - * @return true - if collections are equal, - * false - otherwise. - */ -static bool -ecma_compare_chars_collection (const ecma_collection_header_t *header1_p, /**< first collection's header */ - const ecma_collection_header_t *header2_p) /**< second collection's header */ -{ - JERRY_ASSERT (header1_p != NULL && header2_p != NULL); - - if (header1_p->unit_number != header2_p->unit_number) - { - return false; - } - - const ecma_length_t chars_number = header1_p->unit_number; - - const lit_utf8_byte_t *cur_char_buf1_iter_p = NULL; - const lit_utf8_byte_t *cur_char_buf1_end_p = NULL; - const lit_utf8_byte_t *cur_char_buf2_iter_p = NULL; - const lit_utf8_byte_t *cur_char_buf2_end_p = NULL; - - mem_cpointer_t next_chunk1_cp = header1_p->first_chunk_cp; - mem_cpointer_t next_chunk2_cp = header2_p->first_chunk_cp; - - for (ecma_length_t char_index = 0; - char_index < chars_number; - char_index++) - { - if (cur_char_buf1_iter_p == cur_char_buf1_end_p) - { - JERRY_ASSERT (cur_char_buf2_iter_p == cur_char_buf2_end_p); - - const ecma_collection_chunk_t *chunk1_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t, next_chunk1_cp); - const ecma_collection_chunk_t *chunk2_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t, next_chunk2_cp); - - cur_char_buf1_iter_p = (lit_utf8_byte_t *) chunk1_p->data; - cur_char_buf1_end_p = cur_char_buf1_iter_p + sizeof (chunk1_p->data); - cur_char_buf2_iter_p = (lit_utf8_byte_t *) chunk2_p->data; - cur_char_buf2_end_p = cur_char_buf2_iter_p + sizeof (chunk2_p->data); - - next_chunk1_cp = chunk1_p->next_chunk_cp; - next_chunk2_cp = chunk2_p->next_chunk_cp; - } - - JERRY_ASSERT (cur_char_buf1_iter_p + 1 <= cur_char_buf1_end_p); - JERRY_ASSERT (cur_char_buf2_iter_p + 1 <= cur_char_buf2_end_p); - - if (*cur_char_buf1_iter_p++ != *cur_char_buf2_iter_p++) - { - return false; - } - } - - return true; -} /* ecma_compare_chars_collection */ - -/** - * Copy the collection of ecma-chars. - * - * @return pointer to collection copy - */ -static ecma_collection_header_t * -ecma_copy_chars_collection (const ecma_collection_header_t *collection_p) /**< collection's header */ -{ - JERRY_ASSERT (collection_p != NULL); - - ecma_collection_header_t *new_header_p = ecma_alloc_collection_header (); - *new_header_p = *collection_p; - - mem_cpointer_t *next_chunk_cp_p = &new_header_p->first_chunk_cp; - - ecma_collection_chunk_t *chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t, - collection_p->first_chunk_cp); - - while (chunk_p != NULL) - { - ecma_collection_chunk_t *new_chunk_p = ecma_alloc_collection_chunk (); - *new_chunk_p = *chunk_p; - - ECMA_SET_NON_NULL_POINTER (*next_chunk_cp_p, new_chunk_p); - next_chunk_cp_p = &new_chunk_p->next_chunk_cp; - - chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t, - chunk_p->next_chunk_cp); - } - - *next_chunk_cp_p = ECMA_NULL_POINTER; - - return new_header_p; -} /* ecma_copy_chars_collection */ - -/** - * Copy characters of the collection to buffer - */ -static void -ecma_copy_chars_collection_to_buffer (const ecma_collection_header_t *collection_p, /**< collection header */ - lit_utf8_byte_t chars_buffer[], /**< buffer for characters */ - lit_utf8_size_t buffer_size) /**< size of the buffer */ -{ - JERRY_ASSERT (collection_p != NULL); - - lit_utf8_byte_t *out_chars_buf_iter_p = chars_buffer; - - const lit_utf8_size_t chars_number = collection_p->unit_number; - - mem_cpointer_t next_chunk_cp = collection_p->first_chunk_cp; - const lit_utf8_byte_t *cur_char_buf_iter_p = NULL; - const lit_utf8_byte_t *cur_char_buf_end_p = NULL; - - for (lit_utf8_size_t char_index = 0; - char_index < chars_number; - char_index++) - { - if (cur_char_buf_iter_p == cur_char_buf_end_p) - { - const ecma_collection_chunk_t *chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t, next_chunk_cp); - - cur_char_buf_iter_p = (lit_utf8_byte_t *) chunk_p->data; - cur_char_buf_end_p = cur_char_buf_iter_p + sizeof (chunk_p->data); - - next_chunk_cp = chunk_p->next_chunk_cp; - } - - JERRY_ASSERT (cur_char_buf_iter_p + 1 <= cur_char_buf_end_p); - - *out_chars_buf_iter_p++ = *cur_char_buf_iter_p++; - } - - JERRY_ASSERT (out_chars_buf_iter_p - chars_buffer <= (ssize_t) buffer_size); -} /* ecma_copy_chars_collection_to_buffer */ - -/** - * Free the collection of ecma-chars. - */ -static void -ecma_free_chars_collection (ecma_collection_header_t *collection_p) /**< collection's header */ -{ - JERRY_ASSERT (collection_p != NULL); - - ecma_collection_chunk_t *chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t, - collection_p->first_chunk_cp); - - while (chunk_p != NULL) - { - ecma_collection_chunk_t *next_chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t, - chunk_p->next_chunk_cp); - ecma_dealloc_collection_chunk (chunk_p); - - chunk_p = next_chunk_p; - } - - ecma_dealloc_collection_header (collection_p); -} /* ecma_free_chars_collection */ /** * Initialize ecma-string descriptor with string described by index in literal table @@ -322,10 +73,9 @@ static void ecma_init_ecma_string_from_lit_cp (ecma_string_t *string_p, /**< descriptor to initialize */ lit_cpointer_t lit_cp) /**< compressed pointer to literal */ { - lit_literal_t lit = lit_get_literal_by_cp (lit_cp); - rcs_record_type_t type = rcs_record_get_type (lit); + lit_literal_t lit = lit_cpointer_decompress (lit_cp); - if (RCS_RECORD_TYPE_IS_MAGIC_STR (type)) + if (LIT_RECORD_IS_MAGIC_STR (lit)) { ecma_init_ecma_string_from_magic_string_id (string_p, lit_magic_literal_get_magic_str_id (lit)); @@ -333,14 +83,14 @@ ecma_init_ecma_string_from_lit_cp (ecma_string_t *string_p, /**< descriptor to i return; } - if (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (type)) + if (LIT_RECORD_IS_MAGIC_STR_EX (lit)) { ecma_init_ecma_string_from_magic_string_ex_id (string_p, - lit_magic_literal_ex_get_magic_str_id (lit)); + lit_magic_literal_get_magic_str_ex_id (lit)); return; } - JERRY_ASSERT (RCS_RECORD_TYPE_IS_CHARSET (type)); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); string_p->refs = 1; string_p->container = ECMA_STRING_CONTAINER_LIT_TABLE; @@ -416,8 +166,13 @@ ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, /**< utf-8 stri string_desc_p->hash = lit_utf8_string_calc_hash (string_p, string_size); string_desc_p->u.common_field = 0; - ecma_collection_header_t *collection_p = ecma_new_chars_collection (string_p, string_size); - ECMA_SET_NON_NULL_POINTER (string_desc_p->u.collection_cp, collection_p); + + const size_t data_size = string_size + sizeof (ecma_string_heap_header_t); + ecma_string_heap_header_t *data_p = (ecma_string_heap_header_t *) mem_heap_alloc_block (data_size); + data_p->size = (uint16_t) string_size; + data_p->length = (uint16_t) lit_utf8_string_length (string_p, string_size); + memcpy (data_p + 1, string_p, string_size); + ECMA_SET_NON_NULL_POINTER (string_desc_p->u.collection_cp, data_p); return string_desc_p; } /* ecma_new_ecma_string_from_utf8 */ @@ -562,8 +317,8 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */ JERRY_ASSERT (string1_p != NULL && string2_p != NULL); - lit_utf8_size_t str1_size = ecma_string_get_size (string1_p); - lit_utf8_size_t str2_size = ecma_string_get_size (string2_p); + const lit_utf8_size_t str1_size = ecma_string_get_size (string1_p); + const lit_utf8_size_t str2_size = ecma_string_get_size (string2_p); if (str1_size == 0) { @@ -574,30 +329,37 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */ return ecma_copy_or_ref_ecma_string (string1_p); } - int64_t length = (int64_t) str1_size + (int64_t) str2_size; + const lit_utf8_size_t new_size = str1_size + str2_size; - if (length > ECMA_STRING_MAX_CONCATENATION_LENGTH) - { - jerry_fatal (ERR_OUT_OF_MEMORY); - } + ecma_string_t *string_desc_p = ecma_alloc_string (); + string_desc_p->refs = 1; + string_desc_p->container = ECMA_STRING_CONTAINER_HEAP_CHUNKS; - lit_utf8_size_t buffer_size = str1_size + str2_size; - - lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) mem_heap_alloc_block (buffer_size, MEM_HEAP_ALLOC_SHORT_TERM); + string_desc_p->u.common_field = 0; + const size_t data_size = new_size + sizeof (ecma_string_heap_header_t); + ecma_string_heap_header_t *data_p = (ecma_string_heap_header_t *) mem_heap_alloc_block (data_size); ssize_t bytes_copied1, bytes_copied2; - bytes_copied1 = ecma_string_to_utf8_string (string1_p, str_p, (ssize_t) str1_size); + bytes_copied1 = ecma_string_to_utf8_string (string1_p, + (lit_utf8_byte_t *) (data_p + 1), + (ssize_t) str1_size); JERRY_ASSERT (bytes_copied1 > 0); - bytes_copied2 = ecma_string_to_utf8_string (string2_p, str_p + str1_size, (ssize_t) str2_size); + bytes_copied2 = ecma_string_to_utf8_string (string2_p, + (lit_utf8_byte_t *) (data_p + 1) + str1_size, + (ssize_t) str2_size); JERRY_ASSERT (bytes_copied2 > 0); - ecma_string_t *str_concat_p = ecma_new_ecma_string_from_utf8 (str_p, buffer_size); + data_p->size = (uint16_t) new_size; + data_p->length = (uint16_t) (ecma_string_get_length (string1_p) + ecma_string_get_length (string2_p)); + string_desc_p->hash = lit_utf8_string_hash_combine (string1_p->hash, + (lit_utf8_byte_t *) (data_p + 1) + str1_size, + (lit_utf8_size_t) str2_size); - mem_heap_free_block ((void *) str_p); + ECMA_SET_NON_NULL_POINTER (string_desc_p->u.collection_cp, data_p); - return str_concat_p; + return string_desc_p; } /* ecma_concat_ecma_strings */ /** @@ -643,12 +405,14 @@ ecma_copy_ecma_string (ecma_string_t *string_desc_p) /**< string descriptor */ new_str_p = ecma_alloc_string (); *new_str_p = *string_desc_p; - const ecma_collection_header_t *chars_collection_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string_desc_p->u.collection_cp); - JERRY_ASSERT (chars_collection_p != NULL); - ecma_collection_header_t *new_chars_collection_p = ecma_copy_chars_collection (chars_collection_p); + const ecma_string_heap_header_t *data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string_desc_p->u.collection_cp); + JERRY_ASSERT (data_p != NULL); + const size_t data_size = data_p->size + sizeof (ecma_string_heap_header_t); + ecma_string_heap_header_t *new_data_p = (ecma_string_heap_header_t *) mem_heap_alloc_block (data_size); + memcpy (new_data_p, data_p, data_p->size + sizeof (ecma_string_heap_header_t)); - ECMA_SET_NON_NULL_POINTER (new_str_p->u.collection_cp, new_chars_collection_p); + ECMA_SET_NON_NULL_POINTER (new_str_p->u.collection_cp, data_p); break; } @@ -725,10 +489,10 @@ ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */ { case ECMA_STRING_CONTAINER_HEAP_CHUNKS: { - ecma_collection_header_t *chars_collection_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string_p->u.collection_cp); + ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string_p->u.collection_cp); - ecma_free_chars_collection (chars_collection_p); + mem_heap_free_block (data_p, data_p->size + sizeof (ecma_string_heap_header_t)); break; } @@ -900,17 +664,16 @@ ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string { case ECMA_STRING_CONTAINER_HEAP_CHUNKS: { - const ecma_collection_header_t *chars_collection_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string_desc_p->u.collection_cp); - - ecma_copy_chars_collection_to_buffer (chars_collection_p, buffer_p, (lit_utf8_size_t) buffer_size); + const ecma_string_heap_header_t *data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string_desc_p->u.collection_cp); + memcpy (buffer_p, data_p + 1, (size_t) data_p->size); break; } case ECMA_STRING_CONTAINER_LIT_TABLE: { lit_literal_t lit = lit_get_literal_by_cp (string_desc_p->u.lit_cp); - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit)); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); lit_literal_to_utf8_string (lit, buffer_p, (size_t) required_buffer_size); break; } @@ -980,7 +743,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri { case ECMA_STRING_CONTAINER_LIT_TABLE: { - JERRY_ASSERT (string1_p->u.lit_cp.u.packed_value != string2_p->u.lit_cp.u.packed_value); + JERRY_ASSERT (string1_p->u.lit_cp != string2_p->u.lit_cp); return false; } case ECMA_STRING_CONTAINER_MAGIC_STRING: @@ -1042,37 +805,88 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri } case ECMA_STRING_CONTAINER_HEAP_CHUNKS: { - const ecma_collection_header_t *chars_collection1_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string1_p->u.collection_cp); - const ecma_collection_header_t *chars_collection2_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string2_p->u.collection_cp); + const ecma_string_heap_header_t *data1_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string1_p->u.collection_cp); + const ecma_string_heap_header_t *data2_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string2_p->u.collection_cp); - return ecma_compare_chars_collection (chars_collection1_p, chars_collection2_p); + if (data1_p->length != data2_p->length) + { + return false; + } + + return !strncmp ((char *) (data1_p + 1), (char *) (data2_p + 1), strings_size); } default: { - JERRY_ASSERT (false); + JERRY_UNREACHABLE (); break; } } } - bool is_equal = false; + lit_utf8_byte_t *utf8_string1_p, *utf8_string2_p; + bool is_utf8_string1_on_heap = false; + bool is_utf8_string2_on_heap = false; - MEM_DEFINE_LOCAL_ARRAY (string1_buf, strings_size, lit_utf8_byte_t); - MEM_DEFINE_LOCAL_ARRAY (string2_buf, strings_size, lit_utf8_byte_t); + if (string1_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS) + { + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string1_p->u.collection_cp); - ssize_t req_size; + utf8_string1_p = (lit_utf8_byte_t *) (data_p + 1); + } + else if (string1_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) + { + const lit_literal_t lit = lit_get_literal_by_cp (string1_p->u.lit_cp); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); - req_size = (ssize_t) ecma_string_to_utf8_string (string1_p, string1_buf, (ssize_t) strings_size); - JERRY_ASSERT (req_size > 0); - req_size = (ssize_t) ecma_string_to_utf8_string (string2_p, string2_buf, (ssize_t) strings_size); - JERRY_ASSERT (req_size > 0); + utf8_string1_p = (lit_utf8_byte_t *) lit_charset_literal_get_charset (lit); + } + else + { + utf8_string1_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) strings_size); - is_equal = (memcmp (string1_buf, string2_buf, (size_t) strings_size) == 0); + ssize_t bytes_copied = ecma_string_to_utf8_string (string1_p, utf8_string1_p, (ssize_t) strings_size); + JERRY_ASSERT (bytes_copied > 0); - MEM_FINALIZE_LOCAL_ARRAY (string2_buf); - MEM_FINALIZE_LOCAL_ARRAY (string1_buf); + is_utf8_string1_on_heap = true; + } + + if (string2_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS) + { + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string2_p->u.collection_cp); + + utf8_string2_p = (lit_utf8_byte_t *) (data_p + 1); + } + else if (string2_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) + { + const lit_literal_t lit = lit_get_literal_by_cp (string2_p->u.lit_cp); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); + + utf8_string2_p = (lit_utf8_byte_t *) lit_charset_literal_get_charset (lit); + } + else + { + utf8_string2_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) strings_size); + + ssize_t bytes_copied = ecma_string_to_utf8_string (string2_p, utf8_string2_p, (ssize_t) strings_size); + JERRY_ASSERT (bytes_copied > 0); + + is_utf8_string2_on_heap = true; + } + const bool is_equal = !strncmp ((char *) utf8_string1_p, (char *) utf8_string2_p, (size_t) strings_size); + + if (is_utf8_string1_on_heap) + { + mem_heap_free_block ((void *) utf8_string1_p, (size_t) strings_size); + } + + if (is_utf8_string2_on_heap) + { + mem_heap_free_block ((void *) utf8_string2_p, (size_t) strings_size); + } return is_equal; } /* ecma_compare_ecma_strings_longpath */ @@ -1158,46 +972,81 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma- lit_utf8_byte_t utf8_string2_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER]; lit_utf8_size_t utf8_string2_size; - ssize_t req_size = ecma_string_to_utf8_string (string1_p, utf8_string1_buffer, sizeof (utf8_string1_buffer)); - - if (req_size < 0) + if (string1_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS) { - lit_utf8_byte_t *heap_buffer_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) -req_size, - MEM_HEAP_ALLOC_SHORT_TERM); + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string1_p->u.collection_cp); - ssize_t bytes_copied = ecma_string_to_utf8_string (string1_p, heap_buffer_p, -req_size); - utf8_string1_size = (lit_utf8_size_t) bytes_copied; + utf8_string1_p = (lit_utf8_byte_t *) (data_p + 1); + utf8_string1_size = (lit_utf8_size_t) data_p->size; + } + else if (string1_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) + { + const lit_literal_t lit = lit_get_literal_by_cp (string1_p->u.lit_cp); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); - JERRY_ASSERT (bytes_copied > 0); - - utf8_string1_p = heap_buffer_p; - is_utf8_string1_on_heap = true; + utf8_string1_p = (lit_utf8_byte_t *) lit_charset_literal_get_charset (lit); + utf8_string1_size = (lit_utf8_size_t) lit_charset_literal_get_size (lit); } else { - utf8_string1_p = utf8_string1_buffer; - utf8_string1_size = (lit_utf8_size_t) req_size; + const ssize_t req_size = ecma_string_to_utf8_string (string1_p, utf8_string1_buffer, sizeof (utf8_string1_buffer)); + + if (req_size < 0) + { + lit_utf8_byte_t *heap_buffer_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) -req_size); + + ssize_t bytes_copied = ecma_string_to_utf8_string (string1_p, heap_buffer_p, -req_size); + utf8_string1_size = (lit_utf8_size_t) bytes_copied; + + JERRY_ASSERT (bytes_copied > 0); + + utf8_string1_p = heap_buffer_p; + is_utf8_string1_on_heap = true; + } + else + { + utf8_string1_p = utf8_string1_buffer; + utf8_string1_size = (lit_utf8_size_t) req_size; + } } - req_size = ecma_string_to_utf8_string (string2_p, utf8_string2_buffer, sizeof (utf8_string2_buffer)); - - if (req_size < 0) + if (string2_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS) { - lit_utf8_byte_t *heap_buffer_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) -req_size, - MEM_HEAP_ALLOC_SHORT_TERM); + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string2_p->u.collection_cp); - ssize_t bytes_copied = ecma_string_to_utf8_string (string2_p, heap_buffer_p, -req_size); - utf8_string2_size = (lit_utf8_size_t) bytes_copied; + utf8_string2_p = (lit_utf8_byte_t *) (data_p + 1); + utf8_string2_size = (lit_utf8_size_t) data_p->size; + } + else if (string2_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) + { + const lit_literal_t lit = lit_get_literal_by_cp (string2_p->u.lit_cp); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); - JERRY_ASSERT (bytes_copied > 0); - - utf8_string2_p = heap_buffer_p; - is_utf8_string2_on_heap = true; + utf8_string2_p = (lit_utf8_byte_t *) lit_charset_literal_get_charset (lit); + utf8_string2_size = (lit_utf8_size_t) lit_charset_literal_get_size (lit); } else { - utf8_string2_p = utf8_string2_buffer; - utf8_string2_size = (lit_utf8_size_t) req_size; + const ssize_t req_size = ecma_string_to_utf8_string (string2_p, utf8_string2_buffer, sizeof (utf8_string2_buffer)); + if (req_size < 0) + { + lit_utf8_byte_t *heap_buffer_p = (lit_utf8_byte_t *) mem_heap_alloc_block ((size_t) -req_size); + + ssize_t bytes_copied = ecma_string_to_utf8_string (string2_p, heap_buffer_p, -req_size); + utf8_string2_size = (lit_utf8_size_t) bytes_copied; + + JERRY_ASSERT (bytes_copied > 0); + + utf8_string2_p = heap_buffer_p; + is_utf8_string2_on_heap = true; + } + else + { + utf8_string2_p = utf8_string2_buffer; + utf8_string2_size = (lit_utf8_size_t) req_size; + } } bool is_first_less_than_second = lit_compare_utf8_strings_relational (utf8_string1_p, @@ -1207,12 +1056,12 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma- if (is_utf8_string1_on_heap) { - mem_heap_free_block ((void *) utf8_string1_p); + mem_heap_free_block ((void *) utf8_string1_p, (size_t) utf8_string1_size); } if (is_utf8_string2_on_heap) { - mem_heap_free_block ((void *) utf8_string2_p); + mem_heap_free_block ((void *) utf8_string2_p, (size_t) utf8_string2_size); } return is_first_less_than_second; @@ -1288,7 +1137,7 @@ ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */ case ECMA_STRING_CONTAINER_LIT_TABLE: { lit_literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp); - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit)); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); return lit_charset_literal_get_length (lit); } case ECMA_STRING_CONTAINER_MAGIC_STRING: @@ -1314,11 +1163,10 @@ ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */ default: { JERRY_ASSERT ((ecma_string_container_t) string_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS); + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string_p->u.collection_cp); - const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string_p->u.collection_cp); - - return ecma_get_chars_collection_length (collection_header_p); + return (ecma_length_t) data_p->length; } } } /* ecma_string_get_length */ @@ -1336,7 +1184,7 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */ case ECMA_STRING_CONTAINER_LIT_TABLE: { lit_literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp); - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit)); + JERRY_ASSERT (LIT_RECORD_IS_CHARSET (lit)); return lit_charset_literal_get_size (lit); } @@ -1359,10 +1207,10 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */ default: { JERRY_ASSERT ((ecma_string_container_t) string_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS); - const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, - string_p->u.collection_cp); + const ecma_string_heap_header_t *const data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t, + string_p->u.collection_cp); - return collection_header_p->unit_number; + return (lit_utf8_size_t) data_p->size; } } } /* ecma_string_get_size */ diff --git a/jerry-core/ecma/base/ecma-helpers.c b/jerry-core/ecma/base/ecma-helpers.c index b76948c31..ce9827e9b 100644 --- a/jerry-core/ecma/base/ecma-helpers.c +++ b/jerry-core/ecma/base/ecma-helpers.c @@ -1391,7 +1391,7 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */ for (uint32_t i = const_literal_end; i < literal_end; i++) { - mem_cpointer_t bytecode_cpointer = literal_start_p[i].u.value.base_cp; + mem_cpointer_t bytecode_cpointer = literal_start_p[i]; ecma_compiled_code_t *bytecode_literal_p = ECMA_GET_NON_NULL_POINTER (ecma_compiled_code_t, bytecode_cpointer); @@ -1411,7 +1411,7 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */ #endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */ } - mem_heap_free_block (bytecode_p); + mem_heap_free_block_size_stored (bytecode_p); } /* ecma_bytecode_deref */ /** diff --git a/jerry-core/ecma/base/ecma-helpers.h b/jerry-core/ecma/base/ecma-helpers.h index c46647434..3b015cd56 100644 --- a/jerry-core/ecma/base/ecma-helpers.h +++ b/jerry-core/ecma/base/ecma-helpers.h @@ -25,6 +25,7 @@ #define JERRY_ECMA_HELPERS_H #include "ecma-globals.h" +#include "lit-cpointer.h" #include "lit-strings.h" #include "mem-allocator.h" diff --git a/jerry-core/ecma/operations/ecma-objects-arguments.c b/jerry-core/ecma/operations/ecma-objects-arguments.c index 5fe8d7246..6ef94c150 100644 --- a/jerry-core/ecma/operations/ecma-objects-arguments.c +++ b/jerry-core/ecma/operations/ecma-objects-arguments.c @@ -138,7 +138,7 @@ ecma_op_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function indx++) { // i. - if (literal_p[indx].u.packed_value == MEM_CP_NULL) + if (literal_p[indx] == MEM_CP_NULL) { continue; } diff --git a/jerry-core/ecma/operations/ecma-objects.c b/jerry-core/ecma/operations/ecma-objects.c index 929ebcf66..2ce25e60b 100644 --- a/jerry-core/ecma/operations/ecma-objects.c +++ b/jerry-core/ecma/operations/ecma-objects.c @@ -755,9 +755,9 @@ ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */ JERRY_ASSERT (insertion_pos < array_index_name_pos); JERRY_ASSERT (index >= array_index_names_p[insertion_pos]); - uint32_t move_pos = ++array_index_name_pos; + uint32_t move_pos = array_index_name_pos++; - while (move_pos != insertion_pos) + while (move_pos > insertion_pos) { array_index_names_p[move_pos] = array_index_names_p[move_pos - 1u]; diff --git a/jerry-core/jerry.c b/jerry-core/jerry.c index ed1ee69e7..b1058067d 100644 --- a/jerry-core/jerry.c +++ b/jerry-core/jerry.c @@ -1,4 +1,5 @@ /* 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. @@ -1967,14 +1968,14 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ { lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p; - if (literal_start_p[i].u.packed_value != MEM_CP_NULL) + if (literal_start_p[i] != MEM_CP_NULL) { - while (current_p->literal_id.u.packed_value != literal_start_p[i].u.packed_value) + while (current_p->literal_id != literal_start_p[i]) { current_p++; } - literal_start_p[i].u.packed_value = (uint16_t) current_p->literal_offset; + literal_start_p[i] = (uint16_t) current_p->literal_offset; } } @@ -1982,13 +1983,13 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ { compiled_code_map_entry_t *current_p = snapshot_map_entries_p; - while (current_p->compiled_code_cp != literal_start_p[i].u.value.base_cp) + while (current_p->compiled_code_cp != literal_start_p[i]) { current_p = ECMA_GET_NON_NULL_POINTER (compiled_code_map_entry_t, current_p->next_cp); } - literal_start_p[i].u.packed_value = (uint16_t) current_p->offset; + literal_start_p[i] = (uint16_t) current_p->offset; } } @@ -2095,26 +2096,17 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t *source_p, /**< script sou } else { - if (header.lit_table_size > 0xffff) - { - /* Aligning literals could increase this range, but - * it is not a requirement for low-memory environments. */ - snapshot_buffer_write_offset = 0; - } - else - { - jerry_snapshot_set_offsets (buffer_p + compiled_code_start, - (uint32_t) compiled_code_size, - lit_map_p); + jerry_snapshot_set_offsets (buffer_p + compiled_code_start, + (uint32_t) compiled_code_size, + lit_map_p); - jrt_write_to_buffer_by_offset (buffer_p, - buffer_size, - &header_offset, - &header, - sizeof (header)); - } + jrt_write_to_buffer_by_offset (buffer_p, + buffer_size, + &header_offset, + &header, + sizeof (header)); - mem_heap_free_block (lit_map_p); + mem_heap_free_block_size_stored (lit_map_p); } ecma_bytecode_deref (bytecode_data_p); @@ -2212,8 +2204,7 @@ snapshot_load_compiled_code (const uint8_t *snapshot_data_p, /**< snapshot data if (copy_bytecode) { - bytecode_p = (ecma_compiled_code_t *) mem_heap_alloc_block (code_size, - MEM_HEAP_ALLOC_LONG_TERM); + bytecode_p = (ecma_compiled_code_t *) mem_heap_alloc_block_store_size (code_size); memcpy (bytecode_p, snapshot_data_p + offset + sizeof (uint32_t), code_size); } @@ -2223,8 +2214,7 @@ snapshot_load_compiled_code (const uint8_t *snapshot_data_p, /**< snapshot data uint8_t *real_bytecode_p = ((uint8_t *) bytecode_p) + code_size; - bytecode_p = (ecma_compiled_code_t *) mem_heap_alloc_block (code_size + 1 + sizeof (uint8_t *), - MEM_HEAP_ALLOC_LONG_TERM); + bytecode_p = (ecma_compiled_code_t *) mem_heap_alloc_block_store_size (code_size + 1 + sizeof (uint8_t *)); memcpy (bytecode_p, snapshot_data_p + offset + sizeof (uint32_t), code_size); @@ -2242,9 +2232,9 @@ snapshot_load_compiled_code (const uint8_t *snapshot_data_p, /**< snapshot data { lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p; - if (literal_start_p[i].u.packed_value != 0) + if (literal_start_p[i] != 0) { - while (current_p->literal_offset != literal_start_p[i].u.packed_value) + while (current_p->literal_offset != literal_start_p[i]) { current_p++; } @@ -2255,12 +2245,12 @@ snapshot_load_compiled_code (const uint8_t *snapshot_data_p, /**< snapshot data for (uint32_t i = const_literal_end; i < literal_end; i++) { - size_t literal_offset = ((size_t) literal_start_p[i].u.packed_value) << MEM_ALIGNMENT_LOG; + size_t literal_offset = ((size_t) literal_start_p[i]) << MEM_ALIGNMENT_LOG; if (literal_offset == offset) { /* Self reference */ - ECMA_SET_NON_NULL_POINTER (literal_start_p[i].u.value.base_cp, + ECMA_SET_NON_NULL_POINTER (literal_start_p[i], bytecode_p); } else @@ -2271,7 +2261,7 @@ snapshot_load_compiled_code (const uint8_t *snapshot_data_p, /**< snapshot data lit_map_p, copy_bytecode); - ECMA_SET_NON_NULL_POINTER (literal_start_p[i].u.value.base_cp, + ECMA_SET_NON_NULL_POINTER (literal_start_p[i], literal_bytecode_p); } } @@ -2357,7 +2347,7 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */ if (lit_map_p != NULL) { - mem_heap_free_block (lit_map_p); + mem_heap_free_block_size_stored (lit_map_p); } if (bytecode_p == NULL) diff --git a/jerry-core/lit/lit-cpointer.c b/jerry-core/lit/lit-cpointer.c new file mode 100644 index 000000000..fe532d02e --- /dev/null +++ b/jerry-core/lit/lit-cpointer.c @@ -0,0 +1,61 @@ +/* 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 + */ +lit_cpointer_t __attr_pure___ __attr_always_inline___ +lit_cpointer_compress (lit_record_t *pointer) /**< pointer to compress */ +{ + if (pointer == NULL) + { + return MEM_CP_NULL; + } + + return (lit_cpointer_t) mem_compress_pointer (pointer); +} /* lit_cpointer_compress */ + +/** + * Decompress extended compressed pointer. + * + * @return decompressed pointer + */ +lit_record_t * __attr_pure___ __attr_always_inline___ +lit_cpointer_decompress (lit_cpointer_t compressed_pointer) /**< recordset-specific compressed pointer */ +{ + if (compressed_pointer == MEM_CP_NULL) + { + return NULL; + } + + return (lit_record_t *) mem_decompress_pointer (compressed_pointer); +} /* lit_cpointer_decompress */ + +/** + * Create NULL compressed pointer. + * + * @return NULL compressed pointer + */ +lit_cpointer_t __attr_pure___ __attr_always_inline___ +lit_cpointer_null_cp (void) +{ + return MEM_CP_NULL; +} /* lit_cpointer_null_cp */ diff --git a/jerry-core/lit/lit-cpointer.h b/jerry-core/lit/lit-cpointer.h new file mode 100644 index 000000000..65f969910 --- /dev/null +++ b/jerry-core/lit/lit-cpointer.h @@ -0,0 +1,38 @@ +/* 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 "mem-allocator.h" + +#define LIT_CPOINTER_WIDTH (MEM_CP_WIDTH + MEM_ALIGNMENT_LOG - MEM_ALIGNMENT_LOG) + +/** + * Dynamic storage-specific extended compressed pointer + * + * Note: + * the pointer can represent addresses aligned by lit_DYN_STORAGE_LENGTH_UNIT, + * while mem_cpointer_t can only represent addresses aligned by MEM_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 */ diff --git a/jerry-core/lit/lit-globals.h b/jerry-core/lit/lit-globals.h index 1aff7084d..753fcedb7 100644 --- a/jerry-core/lit/lit-globals.h +++ b/jerry-core/lit/lit-globals.h @@ -17,7 +17,6 @@ #define LIT_GLOBALS_H #include "jrt.h" -#include "rcs-cpointer.h" /** * ECMAScript standard defines terms "code unit" and "character" as 16-bit unsigned value @@ -129,21 +128,6 @@ typedef uint32_t lit_code_point_t; */ typedef uint8_t lit_string_hash_t; -/** - * Literal type - */ -typedef rcs_record_t *lit_literal_t; - -/** - * Compressed pointer type - */ -typedef rcs_cpointer_t lit_cpointer_t; - -/** - * Invalid literal - */ -#define NOT_A_LITERAL (rcs_cpointer_null_cp ()) - /** * ECMA string hash value length, in bits */ diff --git a/jerry-core/lit/lit-literal-storage.c b/jerry-core/lit/lit-literal-storage.c index 167364d79..8c0d2738c 100644 --- a/jerry-core/lit/lit-literal-storage.c +++ b/jerry-core/lit/lit-literal-storage.c @@ -1,5 +1,5 @@ /* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 University of Szeged + * 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. @@ -15,85 +15,144 @@ */ #include "lit-literal-storage.h" +#include "lit-cpointer.h" #include "ecma-helpers.h" -#include "rcs-allocator.h" -#include "rcs-iterator.h" -#include "rcs-records.h" -rcs_record_set_t rcs_lit_storage; +lit_record_t *lit_storage = NULL; /** * Create charset record in the literal storage * * @return pointer to the created record */ -rcs_record_t * -lit_storage_create_charset_literal (rcs_record_set_t *rec_set_p, /**< recordset */ - const lit_utf8_byte_t *str_p, /**< string to be placed into the record */ - const lit_utf8_size_t buf_size) /**< size of the string (in bytes) */ +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 */ { - const size_t record_size = RCS_CHARSET_HEADER_SIZE + buf_size; - const size_t aligned_record_size = JERRY_ALIGNUP (record_size, RCS_DYN_STORAGE_LENGTH_UNIT); - const size_t alignment = aligned_record_size - record_size; + lit_charset_record_t *rec_p = (lit_charset_record_t *) mem_heap_alloc_block (buf_size + LIT_CHARSET_HEADER_SIZE); - rcs_record_t *rec_p = rcs_alloc_record (rec_set_p, RCS_RECORD_TYPE_CHARSET, aligned_record_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; - rcs_record_set_alignment_bytes_count (rec_p, (uint8_t) alignment); - rcs_record_set_charset (rec_set_p, rec_p, str_p, buf_size); - rcs_record_set_hash (rec_p, lit_utf8_string_calc_hash (str_p, rcs_record_get_length (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 rec_p; -} /* lit_storage_create_charset_literal */ + return (lit_record_t *) rec_p; +} /* lit_create_charset_literal */ /** * Create magic string record in the literal storage. * * @return pointer to the created record */ -rcs_record_t * -lit_storage_create_magic_literal (rcs_record_set_t *rec_set_p, /**< recordset */ - lit_magic_string_id_t id) /**< id of magic string */ +lit_record_t * +lit_create_magic_literal (const lit_magic_string_id_t id) /**< id of magic string */ { - rcs_record_t *rec_p = rcs_alloc_record (rec_set_p, RCS_RECORD_TYPE_MAGIC_STR, RCS_MAGIC_STR_HEADER_SIZE); - rcs_record_set_magic_str_id (rec_p, id); + lit_magic_record_t *rec_p = (lit_magic_record_t *) mem_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; - return rec_p; -} /* lit_storage_create_magic_literal */ + 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 */ -rcs_record_t * -lit_storage_create_magic_literal_ex (rcs_record_set_t *rec_set_p, /**< recordset */ - lit_magic_string_ex_id_t id) /**< id of magic string */ +lit_record_t * +lit_create_magic_literal_ex (const lit_magic_string_ex_id_t id) /**< id of magic string */ { - rcs_record_t *rec_p = rcs_alloc_record (rec_set_p, RCS_RECORD_TYPE_MAGIC_STR_EX, RCS_MAGIC_STR_HEADER_SIZE); - rcs_record_set_magic_str_ex_id (rec_p, id); + lit_magic_record_t *rec_p = (lit_magic_record_t *) mem_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; - return rec_p; -} /* lit_storage_create_magic_literal_ex */ + 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 */ -rcs_record_t * -lit_storage_create_number_literal (rcs_record_set_t *rec_set_p, /**< recordset */ - ecma_number_t num) /**< numeric value */ +lit_record_t * +lit_create_number_literal (const ecma_number_t num) /**< numeric value */ { - const size_t record_size = RCS_NUMBER_HEADER_SIZE + sizeof (ecma_number_t); - rcs_record_t *rec_p = rcs_alloc_record (rec_set_p, RCS_RECORD_TYPE_NUMBER, record_size); + lit_number_record_t *rec_p = (lit_number_record_t *) mem_heap_alloc_block (sizeof (lit_number_record_t)); - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_NUMBER_HEADER_SIZE); - rcs_iterator_write (&it_ctx, &num, sizeof (ecma_number_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; - return rec_p; -} /* lit_storage_create_number_literal */ + 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); + mem_heap_free_block (lit_p, lit_get_literal_size (lit_p)); + return ret_p; +} /* lit_free_literal */ /** * Count literal records in the storage @@ -101,85 +160,80 @@ lit_storage_create_number_literal (rcs_record_set_t *rec_set_p, /**< recordset * * @return number of literals */ uint32_t -lit_storage_count_literals (rcs_record_set_t *rec_set_p) /**< recordset */ +lit_count_literals () { uint32_t num = 0; - rcs_record_t *rec_p; + lit_record_t *rec_p; - for (rec_p = rcs_record_get_first (rec_set_p); + for (rec_p = lit_storage; rec_p != NULL; - rec_p = rcs_record_get_next (rec_set_p, rec_p)) + rec_p = lit_cpointer_decompress (rec_p->next)) { - if (rcs_record_get_type (rec_p) >= RCS_RECORD_TYPE_FIRST) + if (rec_p->type > LIT_RECORD_TYPE_FREE) { num++; } } return num; -} /* lit_storage_count_literals */ +} /* lit_count_literals */ /** * Dump the contents of the literal storage. */ void -lit_storage_dump_literals (rcs_record_set_t *rec_set_p) /**< recordset */ +lit_dump_literals () { - rcs_record_t *rec_p; + lit_record_t *rec_p; + size_t i; printf ("LITERALS:\n"); - for (rec_p = rcs_record_get_first (rec_set_p); + for (rec_p = lit_storage; rec_p != NULL; - rec_p = rcs_record_get_next (rec_set_p, rec_p)) + rec_p = lit_cpointer_decompress (rec_p->next)) { printf ("%p ", rec_p); - printf ("[%3zu] ", rcs_record_get_size (rec_p)); + printf ("[%3zu] ", lit_get_literal_size (rec_p)); - switch (rcs_record_get_type (rec_p)) + switch (rec_p->type) { - case RCS_RECORD_TYPE_CHARSET: + case LIT_RECORD_TYPE_CHARSET: { - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); - - size_t str_len = rcs_record_get_length (rec_p); - size_t i; - - for (i = 0; i < str_len; ++i) + 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) { FIXME ("Support proper printing of characters which occupy more than one byte.") - lit_utf8_byte_t chr; - rcs_iterator_read (&it_ctx, &chr, sizeof (lit_utf8_byte_t)); - rcs_iterator_skip (&it_ctx, sizeof (lit_utf8_byte_t)); - - printf ("%c", chr); + printf ("%c", *str); } printf (" : STRING"); break; } - case RCS_RECORD_TYPE_MAGIC_STR: + case LIT_RECORD_TYPE_MAGIC_STR: { - lit_magic_string_id_t id = rcs_record_get_magic_str_id (rec_p); + lit_magic_string_id_t id = (lit_magic_string_id_t) ((lit_magic_record_t *) rec_p)->magic_id; printf ("%s : MAGIC STRING", lit_get_magic_string_utf8 (id)); printf (" [id=%d] ", id); break; } - case RCS_RECORD_TYPE_MAGIC_STR_EX: + case LIT_RECORD_TYPE_MAGIC_STR_EX: { - lit_magic_string_ex_id_t id = rcs_record_get_magic_str_ex_id (rec_p); + lit_magic_string_ex_id_t id = ((lit_magic_record_t *) rec_p)->magic_id; printf ("%s : EXT MAGIC STRING", lit_get_magic_string_ex_utf8 (id)); printf (" [id=%d] ", id); break; } - case RCS_RECORD_TYPE_NUMBER: + case LIT_RECORD_TYPE_NUMBER: { - ecma_number_t value = rcs_record_get_number (rec_set_p, rec_p); + 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)) { @@ -200,10 +254,10 @@ lit_storage_dump_literals (rcs_record_set_t *rec_set_p) /**< recordset */ } default: { - printf (" : EMPTY RECORD"); + JERRY_UNREACHABLE (); } } printf ("\n"); } -} /* lit_storage_dump_literals */ +} /* lit_dump_literals */ diff --git a/jerry-core/lit/lit-literal-storage.h b/jerry-core/lit/lit-literal-storage.h index 8ba3f0f1f..0268a85db 100644 --- a/jerry-core/lit/lit-literal-storage.h +++ b/jerry-core/lit/lit-literal-storage.h @@ -1,5 +1,5 @@ /* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 University of Szeged + * 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. @@ -17,16 +17,87 @@ #ifndef LIT_LITERAL_STORAGE_H #define LIT_LITERAL_STORAGE_H +#include "lit-magic-strings.h" +#include "lit-globals.h" #include "ecma-globals.h" -extern rcs_record_set_t rcs_lit_storage; +/** + * 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; -extern rcs_record_t *lit_storage_create_charset_literal (rcs_record_set_t *, const lit_utf8_byte_t *, lit_utf8_size_t); -extern rcs_record_t *lit_storage_create_magic_literal (rcs_record_set_t *, lit_magic_string_id_t); -extern rcs_record_t *lit_storage_create_magic_literal_ex (rcs_record_set_t *, lit_magic_string_ex_id_t); -extern rcs_record_t *lit_storage_create_number_literal (rcs_record_set_t *, ecma_number_t); +/** + * Record header + */ +typedef struct +{ + uint16_t next; /* Compressed pointer to next record */ + uint8_t type; /* Type of record */ +} lit_record_t; -extern uint32_t lit_storage_count_literals (rcs_record_set_t *); -extern void lit_storage_dump_literals (rcs_record_set_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 (); +extern void lit_dump_literals (); + +#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 */ diff --git a/jerry-core/lit/lit-literal.c b/jerry-core/lit/lit-literal.c index e0d9cc43c..4531996a8 100644 --- a/jerry-core/lit/lit-literal.c +++ b/jerry-core/lit/lit-literal.c @@ -1,4 +1,5 @@ /* Copyright 2015 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. @@ -16,21 +17,17 @@ #include "lit-literal.h" #include "ecma-helpers.h" -#include "rcs-allocator.h" -#include "rcs-records.h" -#include "rcs-iterator.h" +#include "lit-cpointer.h" +#include "lit-magic-strings.h" #include "lit-literal-storage.h" + /** * Initialize literal storage */ void lit_init (void) { - JERRY_ASSERT (rcs_get_node_data_space_size () % RCS_DYN_STORAGE_LENGTH_UNIT == 0); - - rcs_chunked_list_init (&rcs_lit_storage); - lit_magic_strings_ex_init (); } /* lit_init */ @@ -40,19 +37,12 @@ lit_init (void) void lit_finalize (void) { - rcs_chunked_list_cleanup (&rcs_lit_storage); - rcs_chunked_list_free (&rcs_lit_storage); + while (lit_storage) + { + lit_storage = lit_free_literal (lit_storage); + } } /* lit_finalize */ -/** - * Dump records from the literal storage - */ -void -lit_dump_literals (void) -{ - lit_storage_dump_literals (&rcs_lit_storage); -} /* lit_dump_literals */ - /** * Create new literal in literal storage from characters buffer. * Don't check if the same literal already exists. @@ -78,7 +68,7 @@ lit_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string t if (!strncmp ((const char *) str_p, (const char *) lit_get_magic_string_utf8 (m_str_id), str_size)) { - return lit_storage_create_magic_literal (&rcs_lit_storage, m_str_id); + return lit_create_magic_literal (m_str_id); } } @@ -94,11 +84,11 @@ lit_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string t if (!strncmp ((const char *) str_p, (const char *) lit_get_magic_string_ex_utf8 (m_str_ex_id), str_size)) { - return lit_storage_create_magic_literal_ex (&rcs_lit_storage, m_str_ex_id); + return lit_create_magic_literal_ex (m_str_ex_id); } } - return lit_storage_create_charset_literal (&rcs_lit_storage, str_p, str_size); + return lit_create_charset_literal (str_p, str_size); } /* lit_create_literal_from_utf8_string */ /** @@ -117,57 +107,73 @@ lit_find_literal_by_utf8_string (const lit_utf8_byte_t *str_p, /**< a string to lit_literal_t lit; - for (lit = rcs_record_get_first (&rcs_lit_storage); + for (lit = lit_storage; lit != NULL; - lit = rcs_record_get_next (&rcs_lit_storage, lit)) + lit = lit_cpointer_decompress (lit->next)) { - rcs_record_type_t type = rcs_record_get_type (lit); + const lit_record_type_t type = (lit_record_type_t) lit->type; - if (RCS_RECORD_TYPE_IS_CHARSET (type)) + switch (type) { - if (rcs_record_get_hash (lit) != str_hash) + case LIT_RECORD_TYPE_CHARSET: { - continue; - } + const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit; - if (rcs_record_get_length (lit) != str_size) - { - continue; - } + if (rec_p->hash != str_hash) + { + continue; + } - if (rcs_record_is_equal_charset (&rcs_lit_storage, lit, str_p, str_size)) - { - return lit; - } - } - else if (RCS_RECORD_TYPE_IS_MAGIC_STR (type)) - { - lit_magic_string_id_t magic_id = rcs_record_get_magic_str_id (lit); - const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_utf8 (magic_id); + if (rec_p->size != str_size) + { + continue; + } - if (lit_get_magic_string_size (magic_id) != str_size) - { - continue; - } + if (!strncmp (rec_p + 1, (const char *) str_p, str_size)) + { + return lit; + } - if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size)) - { - return lit; + break; } - } - else if (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (type)) - { - lit_magic_string_ex_id_t magic_id = rcs_record_get_magic_str_ex_id (lit); - 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) + case LIT_RECORD_TYPE_MAGIC_STR: { - continue; + 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; } - - if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size)) + case LIT_RECORD_TYPE_MAGIC_STR_EX: { - return lit; + 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; } } } @@ -183,7 +189,7 @@ lit_find_literal_by_utf8_string (const lit_utf8_byte_t *str_p, /**< a string to */ lit_literal_t lit_find_or_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string, could be non-zero-terminated */ - lit_utf8_size_t str_size) /**< length of the string */ + 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); @@ -201,10 +207,10 @@ lit_find_or_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< * * @return pointer to a newly created record */ -lit_literal_t -lit_create_literal_from_num (ecma_number_t num) /**< number to initialize a new number literal */ +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_storage_create_number_literal (&rcs_lit_storage, num); + return lit_create_number_literal (num); } /* lit_create_literal_from_num */ /** @@ -231,21 +237,21 @@ lit_find_or_create_literal_from_num (ecma_number_t num) /**< number which a lite * @return pointer to existing or null */ lit_literal_t -lit_find_literal_by_num (ecma_number_t num) /**< a number to search for */ +lit_find_literal_by_num (const ecma_number_t num) /**< a number to search for */ { lit_literal_t lit; - for (lit = rcs_record_get_first (&rcs_lit_storage); + for (lit = lit_storage; lit != NULL; - lit = rcs_record_get_next (&rcs_lit_storage, lit)) + lit = lit_cpointer_decompress (lit->next)) { - rcs_record_type_t type = rcs_record_get_type (lit); + const lit_record_type_t type = (lit_record_type_t) lit->type; - if (!RCS_RECORD_TYPE_IS_NUMBER (type)) + if (type != LIT_RECORD_TYPE_NUMBER) { continue; } - ecma_number_t lit_num = rcs_record_get_number (&rcs_lit_storage, lit); + const ecma_number_t lit_num = lit_number_literal_get_number (lit); if (lit_num == num) { @@ -266,37 +272,37 @@ static bool lit_literal_equal_charset_rec (lit_literal_t lit, /**< literal to compare */ lit_literal_t record) /**< charset record to compare */ { - switch (rcs_record_get_type (lit)) + switch (lit->type) { - case RCS_RECORD_TYPE_CHARSET: + case LIT_RECORD_TYPE_CHARSET: { - return rcs_record_is_equal (&rcs_lit_storage, lit, record); + return lit_literal_equal_charset (lit, + lit_charset_literal_get_charset (record), + lit_charset_literal_get_size (record)); } - case RCS_RECORD_TYPE_MAGIC_STR: + case LIT_RECORD_TYPE_MAGIC_STR: { - lit_magic_string_id_t magic_string_id = rcs_record_get_magic_str_id (lit); - return rcs_record_is_equal_charset (&rcs_lit_storage, - record, - lit_get_magic_string_utf8 (magic_string_id), - lit_get_magic_string_size (magic_string_id)); + lit_magic_string_id_t magic_string_id = lit_magic_literal_get_magic_str_id (lit); + return lit_literal_equal_charset (record, + lit_get_magic_string_utf8 (magic_string_id), + lit_get_magic_string_size (magic_string_id)); } - case RCS_RECORD_TYPE_MAGIC_STR_EX: + case LIT_RECORD_TYPE_MAGIC_STR_EX: { - lit_magic_string_ex_id_t magic_string_id = rcs_record_get_magic_str_ex_id (lit); + lit_magic_string_ex_id_t magic_string_id = lit_magic_literal_get_magic_str_ex_id (lit); - return rcs_record_is_equal_charset (&rcs_lit_storage, - record, - lit_get_magic_string_ex_utf8 (magic_string_id), - lit_get_magic_string_ex_size (magic_string_id)); + return lit_literal_equal_charset (record, + lit_get_magic_string_ex_utf8 (magic_string_id), + lit_get_magic_string_ex_size (magic_string_id)); } - case RCS_RECORD_TYPE_NUMBER: + case LIT_RECORD_TYPE_NUMBER: { - ecma_number_t num = rcs_record_get_number (&rcs_lit_storage, lit); + ecma_number_t num = lit_number_literal_get_number (lit); lit_utf8_byte_t buff[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER]; lit_utf8_size_t copied = ecma_number_to_utf8_string (num, buff, sizeof (buff)); - return rcs_record_is_equal_charset (&rcs_lit_storage, record, buff, copied); + return lit_literal_equal_charset (record, buff, copied); } default: { @@ -317,25 +323,29 @@ lit_literal_equal_utf8 (lit_literal_t lit, /**< literal to compare */ const lit_utf8_byte_t *str_p, /**< utf-8 string to compare */ lit_utf8_size_t str_size) /**< string size in bytes */ { - switch (rcs_record_get_type (lit)) + switch (lit->type) { - case RCS_RECORD_TYPE_CHARSET: + case LIT_RECORD_TYPE_CHARSET: { - return rcs_record_is_equal_charset (&rcs_lit_storage, lit, str_p, str_size); + if (lit_charset_literal_get_size (lit) != str_size) + { + return 0; + } + return !strncmp ((const char *) lit_charset_literal_get_charset (lit), (const char *) str_p, str_size); } - case RCS_RECORD_TYPE_MAGIC_STR: + case LIT_RECORD_TYPE_MAGIC_STR: { - lit_magic_string_id_t magic_id = rcs_record_get_magic_str_id (lit); + lit_magic_string_id_t magic_id = lit_magic_literal_get_magic_str_id (lit); return lit_compare_utf8_string_and_magic_string (str_p, str_size, magic_id); } - case RCS_RECORD_TYPE_MAGIC_STR_EX: + case LIT_RECORD_TYPE_MAGIC_STR_EX: { - lit_magic_string_ex_id_t magic_id = rcs_record_get_magic_str_ex_id (lit); + lit_magic_string_ex_id_t magic_id = lit_magic_literal_get_magic_str_ex_id (lit); return lit_compare_utf8_string_and_magic_string_ex (str_p, str_size, magic_id); } - case RCS_RECORD_TYPE_NUMBER: + case LIT_RECORD_TYPE_NUMBER: { - ecma_number_t num = rcs_record_get_number (&rcs_lit_storage, lit); + ecma_number_t num = lit_number_literal_get_number (lit); lit_utf8_byte_t num_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER]; lit_utf8_size_t num_size = ecma_number_to_utf8_string (num, num_buf, sizeof (num_buf)); @@ -356,7 +366,7 @@ lit_literal_equal_utf8 (lit_literal_t lit, /**< literal to compare */ * false otherwise */ bool -lit_literal_equal_num (lit_literal_t lit, /**< literal to check */ +lit_literal_equal_num (lit_literal_t lit, /**< literal to check */ ecma_number_t num) /**< number to compare with */ { lit_utf8_byte_t buff[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER]; @@ -365,6 +375,29 @@ lit_literal_equal_num (lit_literal_t lit, /**< literal to check */ return lit_literal_equal_utf8 (lit, buff, copied); } /* lit_literal_equal_num */ + +/** + * Check if literal contains the string equal to the buffer + * + * @return true if equal + * false otherwise + */ +bool +lit_literal_equal_charset (lit_literal_t lit, /**< literal to checks */ + const lit_utf8_byte_t *buff, /**< string buffer */ + lit_utf8_size_t size) /**< buffer size */ +{ + JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET); + + if (size != lit_charset_literal_get_size (lit)) + { + return false; + } + + return !strncmp ((const char *) buff, (const char *) lit_charset_literal_get_charset (lit), size); +} /* lit_literal_equal_charset */ + + /** * Check if two literals are equal * @@ -375,38 +408,42 @@ bool lit_literal_equal (lit_literal_t lit1, /**< first literal */ lit_literal_t lit2) /**< second literal */ { - switch (rcs_record_get_type (lit2)) + switch (lit2->type) { - case RCS_RECORD_TYPE_CHARSET: + case LIT_RECORD_TYPE_CHARSET: { return lit_literal_equal_charset_rec (lit1, lit2); } - case RCS_RECORD_TYPE_MAGIC_STR: + case LIT_RECORD_TYPE_MAGIC_STR: { - lit_magic_string_id_t magic_str_id = rcs_record_get_magic_str_id (lit2); + lit_magic_string_id_t magic_str_id = lit_magic_literal_get_magic_str_id (lit2); return lit_literal_equal_utf8 (lit1, lit_get_magic_string_utf8 (magic_str_id), lit_get_magic_string_size (magic_str_id)); } - case RCS_RECORD_TYPE_MAGIC_STR_EX: + case LIT_RECORD_TYPE_MAGIC_STR_EX: { - lit_magic_string_ex_id_t magic_str_ex_id = rcs_record_get_magic_str_ex_id (lit2); + lit_magic_string_ex_id_t magic_str_ex_id = lit_magic_literal_get_magic_str_ex_id (lit2); return lit_literal_equal_utf8 (lit1, lit_get_magic_string_ex_utf8 (magic_str_ex_id), lit_get_magic_string_ex_size (magic_str_ex_id)); } - case RCS_RECORD_TYPE_NUMBER: + case LIT_RECORD_TYPE_NUMBER: { - ecma_number_t num = rcs_record_get_number (&rcs_lit_storage, lit2); + ecma_number_t num = lit_number_literal_get_number (lit2); return lit_literal_equal_num (lit1, num); } default: { JERRY_UNREACHABLE (); + break; } } + + JERRY_UNREACHABLE (); + return 0; } /* lit_literal_equal */ /** @@ -421,9 +458,9 @@ lit_literal_equal_type_utf8 (lit_literal_t lit, /**< literal to compare */ const lit_utf8_byte_t *str_p, /**< utf-8 string */ lit_utf8_size_t str_size) /**< string size */ { - rcs_record_type_t type = rcs_record_get_type (lit); + const lit_record_type_t type = (const lit_record_type_t) lit->type; - if (RCS_RECORD_TYPE_IS_NUMBER (type) || RCS_RECORD_TYPE_IS_FREE (type)) + if (type == LIT_RECORD_TYPE_NUMBER || type == LIT_RECORD_TYPE_FREE) { return false; } @@ -456,7 +493,7 @@ bool lit_literal_equal_type_num (lit_literal_t lit, /**< literal to check */ ecma_number_t num) /**< number to compare with */ { - if (!RCS_RECORD_IS_NUMBER (lit)) + if (lit->type != LIT_RECORD_TYPE_NUMBER) { return false; } @@ -475,7 +512,7 @@ bool lit_literal_equal_type (lit_literal_t lit1, /**< first literal */ lit_literal_t lit2) /**< second literal */ { - if (rcs_record_get_type (lit1) != rcs_record_get_type (lit2)) + if (lit1->type != lit2->type) { return false; } @@ -496,29 +533,32 @@ lit_literal_to_utf8_string (lit_literal_t lit, /**< literal to be processed */ size_t size) /**< size of the buffer */ { JERRY_ASSERT (buff_p != NULL && size > 0); - rcs_record_type_t type = rcs_record_get_type (lit); - if (RCS_RECORD_TYPE_IS_CHARSET (type)) + switch (lit->type) { - rcs_record_get_charset (&rcs_lit_storage, lit, buff_p, size); - return buff_p; + case LIT_RECORD_TYPE_CHARSET: + { + const size_t str_size = lit_charset_literal_get_size (lit); + memcpy (buff_p, lit_charset_literal_get_charset (lit), (size > str_size) ? str_size : size); + return buff_p; + } + case LIT_RECORD_TYPE_MAGIC_STR: + { + return lit_get_magic_string_utf8 (lit_magic_literal_get_magic_str_id (lit)); + } + case LIT_RECORD_TYPE_MAGIC_STR_EX: + { + return lit_get_magic_string_ex_utf8 (lit_magic_literal_get_magic_str_ex_id (lit)); + } + case LIT_RECORD_TYPE_NUMBER: + { + ecma_number_t number = lit_number_literal_get_number (lit); + ecma_number_to_utf8_string (number, buff_p, (ssize_t) size); + return buff_p; + } } - else if (RCS_RECORD_TYPE_IS_MAGIC_STR (type)) - { - return lit_get_magic_string_utf8 (rcs_record_get_magic_str_id (lit)); - } - else if (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (type)) - { - return lit_get_magic_string_ex_utf8 (rcs_record_get_magic_str_ex_id (lit)); - } - else - { - JERRY_ASSERT (RCS_RECORD_TYPE_IS_NUMBER (type)); - ecma_number_t number = rcs_record_get_number (&rcs_lit_storage, lit); - ecma_number_to_utf8_string (number, buff_p, (ssize_t) size); - return buff_p; - } + JERRY_UNREACHABLE (); } /* lit_literal_to_utf8_string */ /** @@ -548,9 +588,9 @@ lit_literal_exists (lit_literal_t lit) /**< literal to check for existence */ { lit_literal_t current_lit; - for (current_lit = rcs_record_get_first (&rcs_lit_storage); + for (current_lit = lit_storage; current_lit != NULL; - current_lit = rcs_record_get_next (&rcs_lit_storage, current_lit)) + current_lit = lit_cpointer_decompress (current_lit->next)) { if (current_lit == lit) { @@ -567,10 +607,9 @@ lit_literal_exists (lit_literal_t lit) /**< literal to check for existence */ * @return literal */ lit_literal_t -lit_get_literal_by_cp (rcs_cpointer_t lit_cp) /**< compressed pointer to literal */ +lit_get_literal_by_cp (lit_cpointer_t lit_cp) /**< compressed pointer to literal */ { - JERRY_ASSERT (lit_cp.u.packed_value != MEM_CP_NULL); - lit_literal_t lit = rcs_cpointer_decompress (lit_cp); + lit_literal_t lit = lit_cpointer_decompress (lit_cp); JERRY_ASSERT (lit_literal_exists (lit)); return lit; @@ -579,25 +618,41 @@ lit_get_literal_by_cp (rcs_cpointer_t lit_cp) /**< compressed pointer to literal lit_string_hash_t lit_charset_literal_get_hash (lit_literal_t lit) /**< literal */ { - return rcs_record_get_hash (lit); + 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 */ { - return rcs_record_get_magic_str_id (lit); + 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_ex_get_magic_str_id (lit_literal_t lit) /**< literal */ +lit_magic_literal_get_magic_str_ex_id (lit_literal_t lit) /**< literal */ { - return rcs_record_get_magic_str_ex_id (lit); -} /* lit_magic_literal_ex_get_magic_str_id */ + JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_MAGIC_STR_EX); -lit_utf8_size_t + 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 */ + +lit_utf8_size_t __attr_always_inline___ __attr_pure___ lit_charset_literal_get_size (lit_literal_t lit) /**< literal */ { - return rcs_record_get_length (lit); + 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 */ /** @@ -605,41 +660,32 @@ lit_charset_literal_get_size (lit_literal_t lit) /**< literal */ * * @return code units count */ -ecma_length_t +ecma_length_t __attr_always_inline___ __attr_pure___ lit_charset_literal_get_length (lit_literal_t lit) /**< literal */ { - TODO ("Add special case for literals which doesn't contain long characters"); + JERRY_ASSERT (lit->type == LIT_RECORD_TYPE_CHARSET); - rcs_iterator_t it_ctx = rcs_iterator_create (&rcs_lit_storage, lit); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); + const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit; - lit_utf8_size_t lit_utf8_str_size = rcs_record_get_length (lit); - ecma_length_t length = 0; - lit_utf8_size_t i = 0; - - while (i < lit_utf8_str_size) - { - lit_utf8_byte_t byte; - lit_utf8_size_t bytes_to_skip; - - rcs_iterator_read (&it_ctx, &byte, sizeof (lit_utf8_byte_t)); - bytes_to_skip = lit_get_unicode_char_size_by_utf8_first_byte (byte); - rcs_iterator_skip (&it_ctx, bytes_to_skip); - - i += bytes_to_skip; - length++; - } - -#ifndef JERRY_NDEBUG - rcs_iterator_skip (&it_ctx, rcs_record_get_alignment_bytes_count (lit)); - JERRY_ASSERT (rcs_iterator_finished (&it_ctx)); -#endif - - return length; + return (ecma_length_t) rec_p->length; } /* lit_charset_literal_get_length */ -ecma_number_t +ecma_number_t __attr_always_inline___ __attr_pure___ lit_number_literal_get_number (lit_literal_t lit) /**< literal */ { - return rcs_record_get_number (&rcs_lit_storage, lit); + 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 */ + +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 */ diff --git a/jerry-core/lit/lit-literal.h b/jerry-core/lit/lit-literal.h index 6eb2d2470..5a5a662fa 100644 --- a/jerry-core/lit/lit-literal.h +++ b/jerry-core/lit/lit-literal.h @@ -1,4 +1,5 @@ /* Copyright 2015 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. @@ -17,10 +18,18 @@ #define LIT_LITERAL_H #include "ecma-globals.h" +#include "lit-globals.h" +#include "lit-literal-storage.h" +#include "lit-cpointer.h" -extern void lit_init (void); -extern void lit_finalize (void); -extern void lit_dump_literals (void); +/** + * Invalid literal + */ +#define NOT_A_LITERAL (lit_cpointer_null_cp ()) + +extern void lit_init (); +extern void lit_finalize (); +extern void lit_dump_literals (); 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); @@ -38,6 +47,7 @@ extern bool lit_literal_equal_type_utf8 (lit_literal_t, const lit_utf8_byte_t *, extern bool lit_literal_equal_type_cstr (lit_literal_t, const char *); extern bool lit_literal_equal_type_num (lit_literal_t, ecma_number_t); extern bool lit_literal_equal_type (lit_literal_t, lit_literal_t); +extern bool lit_literal_equal_charset (lit_literal_t, const lit_utf8_byte_t *, lit_utf8_size_t); extern const lit_utf8_byte_t *lit_literal_to_utf8_string (lit_literal_t, lit_utf8_byte_t *, size_t); extern const char *lit_literal_to_str_internal_buf (lit_literal_t); @@ -48,8 +58,10 @@ 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_literal_t lit_literal_get_next (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_ex_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 */ diff --git a/jerry-core/lit/lit-snapshot.c b/jerry-core/lit/lit-snapshot.c index bb44a2c29..7772aa558 100644 --- a/jerry-core/lit/lit-snapshot.c +++ b/jerry-core/lit/lit-snapshot.c @@ -1,5 +1,5 @@ - /* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 University of Szeged +/* 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. @@ -18,9 +18,6 @@ #include "lit-literal.h" #include "lit-literal-storage.h" -#include "rcs-allocator.h" -#include "rcs-iterator.h" -#include "rcs-records.h" #ifdef JERRY_ENABLE_SNAPSHOT_SAVE @@ -36,91 +33,87 @@ lit_snapshot_dump (lit_literal_t lit, /**< literal to dump */ size_t buffer_size, /**< buffer size */ size_t *in_out_buffer_offset_p) /**< in-out: buffer write offset */ { - rcs_record_type_t record_type = rcs_record_get_type (lit); - - if (RCS_RECORD_TYPE_IS_NUMBER (record_type)) + const lit_record_type_t record_type = (lit_record_type_t) lit->type; + switch (record_type) { - double num_value = rcs_record_get_number (&rcs_lit_storage, lit); - 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)) + case LIT_RECORD_TYPE_CHARSET: { - return 0; - } - - return (uint32_t) size; - } - - if (RCS_RECORD_TYPE_IS_CHARSET (record_type)) - { - lit_utf8_size_t length = rcs_record_get_length (lit); - if (!jrt_write_to_buffer_by_offset (buffer_p, - buffer_size, - in_out_buffer_offset_p, - &length, - sizeof (length))) - { - return 0; - } - - rcs_iterator_t it_ctx = rcs_iterator_create (&rcs_lit_storage, lit); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); - - lit_utf8_size_t i; - for (i = 0; i < length; ++i) - { - lit_utf8_byte_t next_byte; - rcs_iterator_read (&it_ctx, &next_byte, sizeof (lit_utf8_byte_t)); + 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, - &next_byte, - sizeof (next_byte))) + &size, + sizeof (size))) { return 0; } - rcs_iterator_skip (&it_ctx, sizeof (lit_utf8_byte_t)); + + 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)); + } - - return (uint32_t) (sizeof (uint32_t) + length * sizeof (uint8_t)); - } - - if (RCS_RECORD_TYPE_IS_MAGIC_STR (record_type)) - { - lit_magic_string_id_t id = rcs_record_get_magic_str_id (lit); - - if (!jrt_write_to_buffer_by_offset (buffer_p, - buffer_size, - in_out_buffer_offset_p, - &id, - sizeof (id))) + case LIT_RECORD_TYPE_NUMBER: { - return 0; + 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; } - - return (uint32_t) sizeof (lit_magic_string_id_t); - } - - if (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (record_type)) - { - lit_magic_string_ex_id_t id = rcs_record_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))) + case LIT_RECORD_TYPE_MAGIC_STR: { - return 0; - } + lit_magic_string_id_t id = lit_magic_literal_get_magic_str_id (lit); - return (uint32_t) sizeof (lit_magic_string_ex_id_t); + 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 (); @@ -143,7 +136,7 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer * uint32_t *out_map_num_p, /**< out: number of literals */ uint32_t *out_lit_table_size_p) /**< out: number of bytes, dumped to snapshot buffer */ { - uint32_t literals_num = lit_storage_count_literals (&rcs_lit_storage); + uint32_t literals_num = lit_count_literals (); uint32_t lit_table_size = 0; *out_map_p = NULL; @@ -167,18 +160,18 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer * 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 *) mem_heap_alloc_block (id_map_size, MEM_HEAP_ALLOC_SHORT_TERM); + id_map_p = (lit_mem_to_snapshot_id_map_entry_t *) mem_heap_alloc_block_store_size (id_map_size); uint32_t literal_index = 0; lit_literal_t lit; - for (lit = rcs_record_get_first (&rcs_lit_storage); + for (lit = lit_storage; lit != NULL; - lit = rcs_record_get_next (&rcs_lit_storage, lit)) + lit = lit_cpointer_decompress (lit->next)) { - rcs_record_type_t record_type = rcs_record_get_type (lit); + lit_record_type_t record_type = (lit_record_type_t) lit->type; - if (RCS_RECORD_TYPE_IS_FREE (record_type)) + if (record_type == LIT_RECORD_TYPE_FREE) { continue; } @@ -202,7 +195,7 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer * break; } - rcs_cpointer_t lit_cp = rcs_cpointer_compress (lit); + 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; @@ -214,7 +207,7 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer * if (!is_ok) { - mem_heap_free_block (id_map_p); + mem_heap_free_block_size_stored (id_map_p); return false; } @@ -291,7 +284,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li 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 *) mem_heap_alloc_block (id_map_size, MEM_HEAP_ALLOC_SHORT_TERM); + id_map_p = (lit_mem_to_snapshot_id_map_entry_t *) mem_heap_alloc_block_store_size (id_map_size); bool is_ok = true; uint32_t lit_index; @@ -301,7 +294,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li uint32_t offset = (uint32_t) lit_table_read; JERRY_ASSERT (offset == lit_table_read); - rcs_record_type_t type; + lit_record_type_t type; if (!jrt_read_from_buffer_by_offset (lit_table_p, lit_table_size, &lit_table_read, @@ -314,7 +307,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li lit_literal_t lit; - if (RCS_RECORD_TYPE_IS_CHARSET (type)) + if (type == LIT_RECORD_TYPE_CHARSET) { lit_utf8_size_t length; if (!jrt_read_from_buffer_by_offset (lit_table_p, @@ -331,7 +324,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li 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 (RCS_RECORD_TYPE_IS_MAGIC_STR (type)) + else if (type == LIT_RECORD_TYPE_MAGIC_STR) { lit_magic_string_id_t id; if (!jrt_read_from_buffer_by_offset (lit_table_p, @@ -353,7 +346,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li */ lit = (lit_literal_t) lit_find_or_create_literal_from_utf8_string (magic_str_p, magic_str_sz); } - else if (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (type)) + 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, @@ -375,7 +368,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li */ lit = (lit_literal_t) lit_find_or_create_literal_from_utf8_string (magic_str_ex_p, magic_str_ex_sz); } - else if (RCS_RECORD_TYPE_IS_NUMBER (type)) + else if (type == LIT_RECORD_TYPE_NUMBER) { double num; if (!jrt_read_from_buffer_by_offset (lit_table_p, @@ -397,7 +390,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li } id_map_p[lit_index].literal_offset = offset; - id_map_p[lit_index].literal_id = rcs_cpointer_compress (lit); + id_map_p[lit_index].literal_id = lit_cpointer_compress (lit); } if (is_ok) @@ -408,7 +401,7 @@ lit_load_literals_from_snapshot (const uint8_t *lit_table_p, /**< buffer with li return true; } - mem_heap_free_block (id_map_p); + mem_heap_free_block_size_stored (id_map_p); return false; } /* lit_load_literals_from_snapshot */ diff --git a/jerry-core/lit/lit-snapshot.h b/jerry-core/lit/lit-snapshot.h index 61f8c6f9d..26676b84b 100644 --- a/jerry-core/lit/lit-snapshot.h +++ b/jerry-core/lit/lit-snapshot.h @@ -1,5 +1,5 @@ /* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 University of Szeged + * 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. @@ -17,11 +17,12 @@ #ifndef RCS_SNAPSHOT_H #define RCS_SNAPSHOT_H -#include "rcs-cpointer.h" +#include "lit-cpointer.h" +#include "ecma-globals.h" typedef struct { - rcs_cpointer_t literal_id; + lit_cpointer_t literal_id; uint32_t literal_offset; } lit_mem_to_snapshot_id_map_entry_t; diff --git a/jerry-core/mem/mem-allocator.c b/jerry-core/mem/mem-allocator.c index 5770e341c..22eac93ff 100644 --- a/jerry-core/mem/mem-allocator.c +++ b/jerry-core/mem/mem-allocator.c @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -143,23 +144,22 @@ mem_stats_reset_peak (void) void mem_stats_print (void) { - mem_heap_stats_print (); + mem_heap_print (); mem_pools_stats_t stats; mem_pools_get_stats (&stats); printf ("Pools stats:\n"); printf (" Chunk size: %zu\n" - " Pools: %zu\n" - " Allocated chunks: %zu\n" + " Pool chunks: %zu\n" + " Peak pool chunks: %zu\n" " Free chunks: %zu\n" - " Peak pools: %zu\n" - " Peak allocated chunks: %zu\n\n", + " Pool reuse ratio: %zu.%04zu\n", MEM_POOL_CHUNK_SIZE, stats.pools_count, - stats.allocated_chunks, - stats.free_chunks, stats.peak_pools_count, - stats.peak_allocated_chunks); + stats.free_chunks, + stats.reused_count / stats.new_alloc_count, + stats.reused_count % stats.new_alloc_count * 10000 / stats.new_alloc_count); } /* mem_stats_print */ #endif /* MEM_STATS */ diff --git a/jerry-core/mem/mem-allocator.h b/jerry-core/mem/mem-allocator.h index f9cb97955..d55e21bd9 100644 --- a/jerry-core/mem/mem-allocator.h +++ b/jerry-core/mem/mem-allocator.h @@ -43,6 +43,11 @@ typedef uint16_t mem_cpointer_t; */ #define MEM_ALIGNMENT (1u << MEM_ALIGNMENT_LOG) +/** + * Required alignment for allocated units/blocks + */ +#define MEM_POOL_ALIGNMENT (1 << MEM_POOL_ALIGNMENT_LOG) + /** * Width of compressed memory pointer */ diff --git a/jerry-core/mem/mem-config.h b/jerry-core/mem/mem-config.h index e2af8395e..5844eea79 100644 --- a/jerry-core/mem/mem-config.h +++ b/jerry-core/mem/mem-config.h @@ -43,4 +43,9 @@ */ #define MEM_ALIGNMENT_LOG 3 +/** + * Logarithm of required alignment for allocated pools + */ +#define MEM_POOL_ALIGNMENT_LOG 3 + #endif /* MEM_CONFIG_H */ diff --git a/jerry-core/mem/mem-heap.c b/jerry-core/mem/mem-heap.c index 5c2f7e015..6e95b8491 100644 --- a/jerry-core/mem/mem-heap.c +++ b/jerry-core/mem/mem-heap.c @@ -90,102 +90,55 @@ void mem_heap_valgrind_freya_mempool_request (void) # define VALGRIND_FREYA_FREELIKE_SPACE(p) #endif /* JERRY_VALGRIND_FREYA */ -/** - * Length type of the block - */ -typedef enum __attr_packed___ -{ - MEM_BLOCK_LENGTH_TYPE_GENERAL = 0, /**< general (may be multi-chunk) block - * - * Note: - * As zero is used for initialization in mem_heap_init, - * 0 value for the MEM_BLOCK_LENGTH_TYPE_GENERAL is necessary - */ - MEM_BLOCK_LENGTH_TYPE_ONE_CHUNKED = 1 /**< one-chunked block (See also: mem_heap_alloc_chunked_block) */ -} mem_block_length_type_t; - /** * Chunk size should satisfy the required alignment value */ JERRY_STATIC_ASSERT (MEM_HEAP_CHUNK_SIZE % MEM_ALIGNMENT == 0, MEM_HEAP_CHUNK_SIZE_must_be_multiple_of_MEM_ALIGNMENT); -typedef enum +/* Calculate heap area size, leaving space for a pointer to the free list */ +#define MEM_HEAP_AREA_SIZE (MEM_HEAP_SIZE - MEM_ALIGNMENT) +#define MEM_HEAP_END_OF_LIST ((mem_heap_free_t *const) ~((uint32_t) 0x0)) + +/** + * Free region node + */ +typedef struct { - MEM_HEAP_BITMAP_IS_ALLOCATED, /**< bitmap of 'chunk allocated' flags */ - MEM_HEAP_BITMAP_IS_FIRST_IN_BLOCK, /**< bitmap of 'chunk is first in allocated block' flags */ + uint32_t next_offset; /* Offset of next region in list */ + uint32_t size; /* Size of region */ +} mem_heap_free_t; - MEM_HEAP_BITMAP__COUNT /**< number of bitmaps */ -} mem_heap_bitmap_t; +#ifdef MEM_HEAP_PTR_64 +#define MEM_HEAP_GET_OFFSET_FROM_ADDR(p) ((uint32_t) ((uint8_t *) (p) - (uint8_t *) mem_heap.area)) +#define MEM_HEAP_GET_ADDR_FROM_OFFSET(u) ((mem_heap_free_t *) &mem_heap.area[u]) +#else +/* In this case we simply store the pointer, since it fits anyway. */ +#define MEM_HEAP_GET_OFFSET_FROM_ADDR(p) ((uint32_t) (p)) +#define MEM_HEAP_GET_ADDR_FROM_OFFSET(u) ((mem_heap_free_t *)(u)) +#endif /** - * Type of bitmap storage item, used to store one or several bitmap blocks + * Get end of region */ -typedef size_t mem_heap_bitmap_storage_item_t; - -/** - * Mask of a single bit at specified offset in a bitmap storage item - */ -#define MEM_HEAP_BITMAP_ITEM_BIT(offset) (((mem_heap_bitmap_storage_item_t) 1u) << (offset)) - -/** - * Number of bits in a bitmap storage item - */ -#define MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM (sizeof (mem_heap_bitmap_storage_item_t) * JERRY_BITSINBYTE) - -/** - * Full bit mask for a bitmap storage item - */ -#define MEM_HEAP_BITMAP_STORAGE_ALL_BITS_MASK ((mem_heap_bitmap_storage_item_t) -1) - -/** - * Number of chunks in heap - * - * bits_in_heap - * ALIGN_DOWN (-----------------------------------------, bits_in_bitmap_storage_item) - * bitmap_bits_per_chunk + bits_in_chunk - */ -#define MEM_HEAP_CHUNKS_NUM JERRY_ALIGNDOWN (JERRY_BITSINBYTE * MEM_HEAP_SIZE / \ - (MEM_HEAP_BITMAP__COUNT + JERRY_BITSINBYTE * MEM_HEAP_CHUNK_SIZE), \ - MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM) - -/** - * Size of heap data area - */ -#define MEM_HEAP_AREA_SIZE (MEM_HEAP_CHUNKS_NUM * MEM_HEAP_CHUNK_SIZE) - -/** - * Number of bits in heap's bitmap - */ -#define MEM_HEAP_BITMAP_BITS (MEM_HEAP_CHUNKS_NUM * 1u) - -/** - * Overall number of bitmap bits is multiple of number of bits in a bitmap storage item - */ -JERRY_STATIC_ASSERT (MEM_HEAP_BITMAP_BITS % MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM == 0, - MEM_HEAP_BITMAP_BITS_must_be_multiple_of_MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM); - -/** - * Number of bitmap storage items - */ -#define MEM_HEAP_BITMAP_STORAGE_ITEMS (MEM_HEAP_BITMAP_BITS / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM) +static mem_heap_free_t * __attr_always_inline___ __attr_pure___ +mem_heap_get_region_end (mem_heap_free_t *curr_p) /**< current region */ +{ + return (mem_heap_free_t *)((uint8_t *) curr_p + curr_p->size); +} /* mem_heap_get_region_end */ /** * Heap structure */ typedef struct { - /** - * Heap bitmaps - * - * The bitmaps consist of chunks with unique correspondence to the heap chunks - */ - mem_heap_bitmap_storage_item_t bitmaps[MEM_HEAP_BITMAP__COUNT][MEM_HEAP_BITMAP_STORAGE_ITEMS]; + /** First node in free region list */ + mem_heap_free_t first; /** * Heap area */ - uint8_t area[MEM_HEAP_AREA_SIZE] __attribute__ ((aligned (JERRY_MAX (MEM_ALIGNMENT, MEM_HEAP_CHUNK_SIZE)))); + uint8_t area[MEM_HEAP_AREA_SIZE] __attribute__ ((aligned (MEM_ALIGNMENT))); } mem_heap_t; /** @@ -204,58 +157,17 @@ JERRY_STATIC_ASSERT (sizeof (mem_heap) <= MEM_HEAP_SIZE, size_of_mem_heap_must_be_less_than_or_equal_to_MEM_HEAP_SIZE); /** - * Bitmap of 'is allocated' flags + * Size of allocated regions */ -#define MEM_HEAP_IS_ALLOCATED_BITMAP (mem_heap.bitmaps[MEM_HEAP_BITMAP_IS_ALLOCATED]) - -/** - * Bitmap of 'is first in block' flags - */ -#define MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP (mem_heap.bitmaps[MEM_HEAP_BITMAP_IS_FIRST_IN_BLOCK]) - -/** - * Total number of allocated heap chunks - */ -size_t mem_heap_allocated_chunks; +size_t mem_heap_allocated_size; /** * Current limit of heap usage, that is upon being reached, causes call of "try give memory back" callbacks */ size_t mem_heap_limit; -#if defined (JERRY_VALGRIND) || defined (MEM_STATS) || !defined (JERRY_DISABLE_HEAVY_DEBUG) - -# define MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY - -/** - * Number of bytes, allocated in heap block - * - * The array contains one entry per heap chunk with: - * - number of allocated bytes, if the chunk is at start of an allocated block; - * - 0, if the chunk is at start of free block; - * - -1, if the chunk is not at start of a block. - */ -ssize_t mem_heap_allocated_bytes[MEM_HEAP_CHUNKS_NUM]; -#else /* JERRY_VALGRIND || MEM_STATS || !JERRY_DISABLE_HEAVY_DEBUG */ -# ifdef MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY -# error "!" -# endif /* MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY */ -#endif /* !JERRY_VALGRIND && !MEM_STATS && JERRY_DISABLE_HEAVY_DEBUG */ - -#ifndef JERRY_NDEBUG -/** - * Length types for allocated chunks - * - * The array contains one entry per heap chunk with: - * - length type of corresponding block, if the chunk is at start of an allocated block; - * - MEM_BLOCK_LENGTH_TYPE_GENERAL length type for rest chunks. - */ -mem_block_length_type_t mem_heap_length_types[MEM_HEAP_CHUNKS_NUM]; -#endif /* !JERRY_NDEBUG */ - -static size_t mem_get_block_chunks_count_from_data_size (size_t block_allocated_size); - -static void mem_check_heap (void); +/* This is used to speed up deallocation. */ +mem_heap_free_t *mem_heap_list_skip_p; #ifdef MEM_STATS /** @@ -264,119 +176,60 @@ static void mem_check_heap (void); static mem_heap_stats_t mem_heap_stats; static void mem_heap_stat_init (void); -static void mem_heap_stat_alloc (size_t first_chunk_index, size_t chunks_num); -static void mem_heap_stat_free (size_t first_chunk_index, size_t chunks_num); +static void mem_heap_stat_alloc (size_t num); +static void mem_heap_stat_free (size_t num); +static void mem_heap_stat_skip (); +static void mem_heap_stat_nonskip (); +static void mem_heap_stat_alloc_iter (); +static void mem_heap_stat_free_iter (); # define MEM_HEAP_STAT_INIT() mem_heap_stat_init () -# define MEM_HEAP_STAT_ALLOC(v1, v2) mem_heap_stat_alloc (v1, v2) -# define MEM_HEAP_STAT_FREE(v1, v2) mem_heap_stat_free (v1, v2) +# define MEM_HEAP_STAT_ALLOC(v1) mem_heap_stat_alloc (v1) +# define MEM_HEAP_STAT_FREE(v1) mem_heap_stat_free (v1) +# define MEM_HEAP_STAT_SKIP() mem_heap_stat_skip () +# define MEM_HEAP_STAT_NONSKIP() mem_heap_stat_nonskip () +# define MEM_HEAP_STAT_ALLOC_ITER() mem_heap_stat_alloc_iter () +# define MEM_HEAP_STAT_FREE_ITER() mem_heap_stat_free_iter () #else /* !MEM_STATS */ # define MEM_HEAP_STAT_INIT() -# define MEM_HEAP_STAT_ALLOC(v1, v2) -# define MEM_HEAP_STAT_FREE(v1, v2) +# define MEM_HEAP_STAT_ALLOC(v1) +# define MEM_HEAP_STAT_FREE(v1) +# define MEM_HEAP_STAT_SKIP() +# define MEM_HEAP_STAT_NONSKIP() +# define MEM_HEAP_STAT_ALLOC_ITER() +# define MEM_HEAP_STAT_FREE_ITER() #endif /* !MEM_STATS */ -/** - * Calculate minimum chunks count needed for block with specified size of allocated data area. - * - * @return chunks count - */ -static size_t -mem_get_block_chunks_count_from_data_size (size_t block_allocated_size) /**< size of block's allocated area */ -{ - return JERRY_ALIGNUP (block_allocated_size, MEM_HEAP_CHUNK_SIZE) / MEM_HEAP_CHUNK_SIZE; -} /* mem_get_block_chunks_count_from_data_size */ - -/** - * Get index of a heap chunk from its starting address - * - * @return heap chunk index - */ -static size_t -mem_heap_get_chunk_from_address (const void *chunk_start_p) /**< address of a chunk's beginning */ -{ - uintptr_t heap_start_uintptr = (uintptr_t) mem_heap.area; - uintptr_t chunk_start_uintptr = (uintptr_t) chunk_start_p; - - uintptr_t chunk_offset = chunk_start_uintptr - heap_start_uintptr; - - JERRY_ASSERT (chunk_offset % MEM_HEAP_CHUNK_SIZE == 0); - - return (chunk_offset / MEM_HEAP_CHUNK_SIZE); -} /* mem_heap_get_chunk_from_address */ - -/** - * Mark specified chunk allocated - */ -static void -mem_heap_mark_chunk_allocated (size_t chunk_index, /**< index of the heap chunk (bitmap's chunk index is the same) */ - bool is_first_in_block) /**< is the chunk first in its block */ -{ - JERRY_ASSERT (chunk_index < MEM_HEAP_CHUNKS_NUM); - - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (chunk_index % MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM); - - JERRY_ASSERT ((MEM_HEAP_IS_ALLOCATED_BITMAP[chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM] & bit) == 0); - JERRY_ASSERT ((MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM] & bit) == 0); - - MEM_HEAP_IS_ALLOCATED_BITMAP[chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM] |= bit; - - if (is_first_in_block) - { - MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM] |= bit; - } -} /* mem_heap_mark_chunk_allocated */ - /** * Startup initialization of heap */ void mem_heap_init (void) { - JERRY_STATIC_ASSERT ((MEM_HEAP_CHUNK_SIZE & (MEM_HEAP_CHUNK_SIZE - 1u)) == 0, - MEM_HEAP_CHUNK_SIZE_must_be_power_of_2); JERRY_STATIC_ASSERT ((uintptr_t) mem_heap.area % MEM_ALIGNMENT == 0, mem_heap_area_must_be_multiple_of_MEM_ALIGNMENT); - JERRY_STATIC_ASSERT ((uintptr_t) mem_heap.area % MEM_HEAP_CHUNK_SIZE == 0, - mem_heap_area_must_be_multiple_of_MEM_HEAP_CHUNK_SIZE); - JERRY_STATIC_ASSERT (MEM_HEAP_AREA_SIZE % MEM_HEAP_CHUNK_SIZE == 0, - MEM_HEAP_AREA_SIZE_must_be_multiple_of_MEM_HEAP_CHUNK_SIZE); - - JERRY_ASSERT (MEM_HEAP_AREA_SIZE <= (1u << MEM_HEAP_OFFSET_LOG)); + mem_heap_allocated_size = 0; mem_heap_limit = CONFIG_MEM_HEAP_DESIRED_LIMIT; + mem_heap.first.size = 0; + mem_heap_free_t *const region_p = (mem_heap_free_t *) mem_heap.area; + mem_heap.first.next_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (region_p); + region_p->size = sizeof (mem_heap.area); + region_p->next_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (MEM_HEAP_END_OF_LIST); + + mem_heap_list_skip_p = &mem_heap.first; VALGRIND_NOACCESS_SPACE (mem_heap.area, MEM_HEAP_AREA_SIZE); - memset (MEM_HEAP_IS_ALLOCATED_BITMAP, 0, sizeof (MEM_HEAP_IS_ALLOCATED_BITMAP)); - memset (MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP, 0, sizeof (MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP)); - -#ifdef MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY - memset (mem_heap_allocated_bytes, -1, sizeof (mem_heap_allocated_bytes)); - - for (size_t i = 0; i < MEM_HEAP_CHUNKS_NUM; i++) - { -#ifndef JERRY_NDEBUG - JERRY_ASSERT (mem_heap_length_types[i] == MEM_BLOCK_LENGTH_TYPE_GENERAL); -#endif /* !JERRY_NDEBUG */ - - JERRY_ASSERT (mem_heap_allocated_bytes[i] == -1); - } -#endif /* MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY */ - - VALGRIND_NOACCESS_SPACE (&mem_heap, sizeof (mem_heap)); - MEM_HEAP_STAT_INIT (); } /* mem_heap_init */ /** * Finalize heap */ -void -mem_heap_finalize (void) +void mem_heap_finalize (void) { - JERRY_ASSERT (mem_heap_allocated_chunks == 0); - + JERRY_ASSERT (mem_heap_allocated_size == 0); VALGRIND_NOACCESS_SPACE (&mem_heap, sizeof (mem_heap)); } /* mem_heap_finalize */ @@ -389,174 +242,155 @@ mem_heap_finalize (void) * @return pointer to allocated memory block - if allocation is successful, * NULL - if there is not enough memory. */ -static void * -mem_heap_alloc_block_internal (size_t size_in_bytes, /**< size of region to allocate in bytes */ - mem_block_length_type_t length_type, /**< length type of the block - * (one-chunked or general) */ - mem_heap_alloc_term_t alloc_term) /**< expected allocation term */ +static __attribute__((hot)) +void *mem_heap_alloc_block_internal (const size_t size) { - JERRY_ASSERT (size_in_bytes != 0); - JERRY_ASSERT (length_type != MEM_BLOCK_LENGTH_TYPE_ONE_CHUNKED - || size_in_bytes == mem_heap_get_chunked_block_data_size ()); + // Align size + const size_t required_size = ((size + MEM_ALIGNMENT - 1) / MEM_ALIGNMENT) * MEM_ALIGNMENT; + mem_heap_free_t *data_space_p = NULL; - mem_check_heap (); + VALGRIND_DEFINED_SPACE (&mem_heap.first, sizeof (mem_heap_free_t)); - bool is_direction_forward = (alloc_term == MEM_HEAP_ALLOC_LONG_TERM); - - /* searching for appropriate free area, considering requested direction */ - - const size_t req_chunks_num = mem_get_block_chunks_count_from_data_size (size_in_bytes); - JERRY_ASSERT (req_chunks_num > 0); - - size_t found_chunks_num = 0; - size_t first_chunk = MEM_HEAP_CHUNKS_NUM; - - VALGRIND_DEFINED_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - for (size_t i = 0; - i < MEM_HEAP_BITMAP_STORAGE_ITEMS && found_chunks_num != req_chunks_num; - i++) + // Fast path for 8 byte chunks, first region is guaranteed to be sufficient + if (required_size == MEM_ALIGNMENT + && likely (mem_heap.first.next_offset != MEM_HEAP_GET_OFFSET_FROM_ADDR (MEM_HEAP_END_OF_LIST))) { - const size_t bitmap_item_index = (is_direction_forward ? i : MEM_HEAP_BITMAP_STORAGE_ITEMS - i - 1); + data_space_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (mem_heap.first.next_offset); + VALGRIND_DEFINED_SPACE (data_space_p, sizeof (mem_heap_free_t)); + mem_heap_allocated_size += MEM_ALIGNMENT; + MEM_HEAP_STAT_ALLOC_ITER (); - mem_heap_bitmap_storage_item_t item = MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index]; - - if (item != MEM_HEAP_BITMAP_STORAGE_ALL_BITS_MASK) + if (data_space_p->size == MEM_ALIGNMENT) { - for (size_t j = 0; j < MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; j++) - { - const size_t bit_index = (is_direction_forward ? j : MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM - j - 1); - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (bit_index); - - if ((item & bit) == 0) - { - found_chunks_num++; - - if (found_chunks_num == req_chunks_num) - { - first_chunk = bitmap_item_index * MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM + bit_index; - - if (is_direction_forward) - { - first_chunk -= req_chunks_num - 1u; - } - - break; - } - } - else - { - found_chunks_num = 0; - } - } + mem_heap.first.next_offset = data_space_p->next_offset; } else { - found_chunks_num = 0; + JERRY_ASSERT (MEM_HEAP_GET_ADDR_FROM_OFFSET (mem_heap.first.next_offset)->size > MEM_ALIGNMENT); + mem_heap_free_t *const remaining_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (mem_heap.first.next_offset) + 1; + + VALGRIND_DEFINED_SPACE (remaining_p, sizeof (mem_heap_free_t)); + remaining_p->size = data_space_p->size - MEM_ALIGNMENT; + remaining_p->next_offset = data_space_p->next_offset; + VALGRIND_NOACCESS_SPACE (remaining_p, sizeof (mem_heap_free_t)); + + mem_heap.first.next_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (remaining_p); + } + + VALGRIND_UNDEFINED_SPACE (data_space_p, sizeof (mem_heap_free_t)); + + if (unlikely (data_space_p == mem_heap_list_skip_p)) + { + mem_heap_list_skip_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (mem_heap.first.next_offset); + } + } + // Slow path for larger regions + else + { + mem_heap_free_t *current_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (mem_heap.first.next_offset); + mem_heap_free_t *prev_p = &mem_heap.first; + while (current_p != MEM_HEAP_END_OF_LIST) + { + VALGRIND_DEFINED_SPACE (current_p, sizeof (mem_heap_free_t)); + MEM_HEAP_STAT_ALLOC_ITER (); + const uint32_t next_offset = current_p->next_offset; + + if (current_p->size >= required_size) + { + // Region is sufficiently big, store address + data_space_p = current_p; + mem_heap_allocated_size += required_size; + + // Region was larger than necessary + if (current_p->size > required_size) + { + // Get address of remaining space + mem_heap_free_t *const remaining_p = (mem_heap_free_t *) ((uint8_t *) current_p + required_size); + + // Update metadata + VALGRIND_DEFINED_SPACE (remaining_p, sizeof (mem_heap_free_t)); + remaining_p->size = current_p->size - (uint32_t) required_size; + remaining_p->next_offset = next_offset; + VALGRIND_NOACCESS_SPACE (remaining_p, sizeof (mem_heap_free_t)); + + // Update list + VALGRIND_DEFINED_SPACE (prev_p, sizeof (mem_heap_free_t)); + prev_p->next_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (remaining_p); + VALGRIND_NOACCESS_SPACE (prev_p, sizeof (mem_heap_free_t)); + } + // Block is an exact fit + else + { + // Remove the region from the list + VALGRIND_DEFINED_SPACE (prev_p, sizeof (mem_heap_free_t)); + prev_p->next_offset = next_offset; + VALGRIND_NOACCESS_SPACE (prev_p, sizeof (mem_heap_free_t)); + } + + mem_heap_list_skip_p = prev_p; + + // Found enough space + break; + } + + VALGRIND_NOACCESS_SPACE (current_p, sizeof (mem_heap_free_t)); + // Next in list + prev_p = current_p; + current_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (next_offset); } } - VALGRIND_NOACCESS_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - if (found_chunks_num != req_chunks_num) + while (mem_heap_allocated_size >= mem_heap_limit) { - JERRY_ASSERT (found_chunks_num < req_chunks_num); + mem_heap_limit += CONFIG_MEM_HEAP_DESIRED_LIMIT; + } - /* not enough free space */ + VALGRIND_NOACCESS_SPACE (&mem_heap.first, sizeof (mem_heap_free_t)); + + if (unlikely (!data_space_p)) + { return NULL; } - JERRY_ASSERT (req_chunks_num <= found_chunks_num); - -#ifdef MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY - mem_heap_allocated_bytes[first_chunk] = (ssize_t) size_in_bytes; -#endif /* MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY */ - - mem_heap_allocated_chunks += req_chunks_num; - - JERRY_ASSERT (mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE <= MEM_HEAP_AREA_SIZE); - - if (mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE >= mem_heap_limit) - { - mem_heap_limit = JERRY_MIN (MEM_HEAP_AREA_SIZE, - JERRY_MAX (mem_heap_limit + CONFIG_MEM_HEAP_DESIRED_LIMIT, - mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE)); - JERRY_ASSERT (mem_heap_limit >= mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE); - } - - VALGRIND_DEFINED_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - mem_heap_mark_chunk_allocated (first_chunk, true); -#ifndef JERRY_NDEBUG - mem_heap_length_types[first_chunk] = length_type; -#endif /* !JERRY_NDEBUG */ - - for (size_t chunk_index = first_chunk + 1u; - chunk_index < first_chunk + req_chunks_num; - chunk_index++) - { - mem_heap_mark_chunk_allocated (chunk_index, false); - -#ifndef JERRY_NDEBUG - JERRY_ASSERT (length_type == MEM_BLOCK_LENGTH_TYPE_GENERAL - && mem_heap_length_types[chunk_index] == length_type); -#endif /* !JERRY_NDEBUG */ - } - - VALGRIND_NOACCESS_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - MEM_HEAP_STAT_ALLOC (first_chunk, req_chunks_num); - - /* return data space beginning address */ - uint8_t *data_space_p = (uint8_t *) mem_heap.area + (first_chunk * MEM_HEAP_CHUNK_SIZE); JERRY_ASSERT ((uintptr_t) data_space_p % MEM_ALIGNMENT == 0); + VALGRIND_UNDEFINED_SPACE (data_space_p, size); + MEM_HEAP_STAT_ALLOC (size); - VALGRIND_UNDEFINED_SPACE (data_space_p, size_in_bytes); - - mem_check_heap (); - - return data_space_p; -} /* mem_heap_alloc_block_internal */ + return (void *) data_space_p; +} /* mem_heap_finalize */ /** - * Allocation of memory region, running 'try to give memory back' callbacks, if there is not enough memory. + * Allocation of memory block, running 'try to give memory back' callbacks, if there is not enough memory. * * Note: * if after running the callbacks, there is still not enough memory, engine is terminated with ERR_OUT_OF_MEMORY. * - * Note: - * To reduce heap fragmentation there are two allocation modes - short-term and long-term. - * - * If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap. - * - * It is supposed, that all short-term allocation is used during relatively short discrete sessions. - * After end of the session all short-term allocated regions are supposed to be freed. - * * @return pointer to allocated memory block */ -static void * -mem_heap_alloc_block_try_give_memory_back (size_t size_in_bytes, /**< size of region to allocate in bytes */ - mem_block_length_type_t length_type, /**< length type of the block - * (one-chunked or general) */ - mem_heap_alloc_term_t alloc_term) /**< expected allocation term */ +void * __attribute__((hot)) +mem_heap_alloc_block (const size_t size) { + if (unlikely (size == 0)) + { + return NULL; + } + VALGRIND_FREYA_CHECK_MEMPOOL_REQUEST; #ifdef MEM_GC_BEFORE_EACH_ALLOC mem_run_try_to_give_memory_back_callbacks (MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH); #endif /* MEM_GC_BEFORE_EACH_ALLOC */ - size_t chunks = mem_get_block_chunks_count_from_data_size (size_in_bytes); - if ((mem_heap_allocated_chunks + chunks) * MEM_HEAP_CHUNK_SIZE >= mem_heap_limit) + if (mem_heap_allocated_size + size >= mem_heap_limit) { mem_run_try_to_give_memory_back_callbacks (MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW); } - void *data_space_p = mem_heap_alloc_block_internal (size_in_bytes, length_type, alloc_term); + void *data_space_p = mem_heap_alloc_block_internal (size); if (likely (data_space_p != NULL)) { - VALGRIND_FREYA_MALLOCLIKE_SPACE (data_space_p, size_in_bytes); + VALGRIND_FREYA_MALLOCLIKE_SPACE (data_space_p, size); return data_space_p; } @@ -566,11 +400,11 @@ mem_heap_alloc_block_try_give_memory_back (size_t size_in_bytes, /**< size of re { mem_run_try_to_give_memory_back_callbacks (severity); - data_space_p = mem_heap_alloc_block_internal (size_in_bytes, length_type, alloc_term); + data_space_p = mem_heap_alloc_block_internal (size); if (likely (data_space_p != NULL)) { - VALGRIND_FREYA_MALLOCLIKE_SPACE (data_space_p, size_in_bytes); + VALGRIND_FREYA_MALLOCLIKE_SPACE (data_space_p, size); return data_space_p; } } @@ -578,216 +412,144 @@ mem_heap_alloc_block_try_give_memory_back (size_t size_in_bytes, /**< size of re JERRY_ASSERT (data_space_p == NULL); jerry_fatal (ERR_OUT_OF_MEMORY); -} /* mem_heap_alloc_block_try_give_memory_back */ - -/** - * Allocation of memory region. - * - * Note: - * Please look at mem_heap_alloc_block_try_give_memory_back - * for description of allocation term and out-of-memory handling. - * - * @return pointer to allocated memory block - if allocation is successful, - * NULL - if requested region size is zero. - */ -void * -mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to allocate in bytes */ - mem_heap_alloc_term_t alloc_term) /**< expected allocation term */ -{ - if (unlikely (size_in_bytes == 0)) - { - return NULL; - } - else - { - return mem_heap_alloc_block_try_give_memory_back (size_in_bytes, - MEM_BLOCK_LENGTH_TYPE_GENERAL, - alloc_term); - } } /* mem_heap_alloc_block */ /** - * Allocation of one-chunked memory region, i.e. memory block that exactly fits one heap chunk. + * Allocate block and store block size. * - * Note: - * If there is any free space in the heap, it anyway can be allocated for one-chunked block. - * - * Contrariwise, there are cases, when block, requiring more than one chunk, - * cannot be allocated, because of heap fragmentation. - * - * Note: - * Please look at mem_heap_alloc_block_try_give_memory_back - * for description of allocation term and out-of-memory handling. - * - * @return pointer to allocated memory block + * Note: block will only be aligned to 4 bytes. */ -void * -mem_heap_alloc_chunked_block (mem_heap_alloc_term_t alloc_term) /**< expected allocation term */ +void * __attr_always_inline___ +mem_heap_alloc_block_store_size (size_t size) /**< required size */ { - return mem_heap_alloc_block_try_give_memory_back (mem_heap_get_chunked_block_data_size (), - MEM_BLOCK_LENGTH_TYPE_ONE_CHUNKED, - alloc_term); -} /* mem_heap_alloc_chunked_block */ + if (unlikely (size == 0)) + { + return NULL; + } + + size += sizeof (mem_heap_free_t); + + mem_heap_free_t *const data_space_p = (mem_heap_free_t *) mem_heap_alloc_block (size); + data_space_p->size = (uint32_t) size; + return (void *) (data_space_p + 1); +} /* mem_heap_alloc_block_store_size */ /** * Free the memory block. */ -void -mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the block */ +void __attribute__((hot)) +mem_heap_free_block (void *ptr, /**< pointer to beginning of data space of the block */ + const size_t size) /**< size of allocated region */ { VALGRIND_FREYA_CHECK_MEMPOOL_REQUEST; - uint8_t *uint8_ptr = (uint8_t *) ptr; - - /* checking that uint8_ptr points to the heap */ - JERRY_ASSERT (uint8_ptr >= mem_heap.area && uint8_ptr <= (uint8_t *) mem_heap.area + MEM_HEAP_AREA_SIZE); - - mem_check_heap (); - - JERRY_ASSERT (mem_heap_limit >= mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE); - - size_t chunk_index = mem_heap_get_chunk_from_address (ptr); - - size_t chunks = 0; - bool is_block_end_reached = false; - - VALGRIND_DEFINED_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - for (size_t bitmap_item_index = chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - bitmap_item_index < MEM_HEAP_BITMAP_STORAGE_ITEMS && !is_block_end_reached; - bitmap_item_index++) - { - mem_heap_bitmap_storage_item_t item_allocated = MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index]; - mem_heap_bitmap_storage_item_t item_first_in_block = MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[bitmap_item_index]; - - if (item_first_in_block == 0 - && item_allocated == MEM_HEAP_BITMAP_STORAGE_ALL_BITS_MASK) - { - JERRY_ASSERT (chunks != 0); - MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] = 0; - - chunks += MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - } - else - { - size_t bit_index; - - if (chunks == 0) - { - bit_index = chunk_index % MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (bit_index); - - JERRY_ASSERT ((item_first_in_block & bit) != 0); - item_first_in_block &= ~bit; - - MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[bitmap_item_index] = item_first_in_block; - } - else - { - bit_index = 0; - } - - while (bit_index < MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM) - { - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (bit_index); - - if ((item_allocated & bit) == 0 - || (item_first_in_block & bit) != 0) - { - is_block_end_reached = true; - break; - } - else - { - JERRY_ASSERT ((item_allocated & bit) != 0); - - item_allocated &= ~bit; - - chunks++; - bit_index++; - } - } - - MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] = item_allocated; - } - } - - VALGRIND_NOACCESS_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - -#ifdef JERRY_VALGRIND - VALGRIND_CHECK_MEM_IS_ADDRESSABLE (ptr, mem_heap_allocated_bytes[chunk_index]); -#endif /* JERRY_VALGRIND */ + /* checking that ptr points to the heap */ + JERRY_ASSERT ((uint8_t *) ptr >= mem_heap.area && (uint8_t *) ptr <= mem_heap.area + MEM_HEAP_AREA_SIZE); + JERRY_ASSERT (size > 0); + JERRY_ASSERT (mem_heap_limit >= mem_heap_allocated_size); VALGRIND_FREYA_FREELIKE_SPACE (ptr); - VALGRIND_NOACCESS_SPACE (ptr, chunks * MEM_HEAP_CHUNK_SIZE); + VALGRIND_NOACCESS_SPACE (ptr, size); + MEM_HEAP_STAT_FREE_ITER (); - JERRY_ASSERT (mem_heap_allocated_chunks >= chunks); - mem_heap_allocated_chunks -= chunks; + mem_heap_free_t *block_p = (mem_heap_free_t *) ptr; + mem_heap_free_t *prev_p; + mem_heap_free_t *next_p; - if (mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE * 3 <= mem_heap_limit) + VALGRIND_DEFINED_SPACE (&mem_heap.first, sizeof (mem_heap_free_t)); + + if (block_p > mem_heap_list_skip_p) { - mem_heap_limit /= 2; + prev_p = mem_heap_list_skip_p; + MEM_HEAP_STAT_SKIP (); } - else if (mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE + CONFIG_MEM_HEAP_DESIRED_LIMIT <= mem_heap_limit) + else + { + prev_p = &mem_heap.first; + MEM_HEAP_STAT_NONSKIP (); + } + + const uint32_t block_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (block_p); + VALGRIND_DEFINED_SPACE (prev_p, sizeof (mem_heap_free_t)); + // Find position of region in the list + while (prev_p->next_offset < block_offset) + { + mem_heap_free_t *const next_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (prev_p->next_offset); + VALGRIND_DEFINED_SPACE (next_p, sizeof (mem_heap_free_t)); + VALGRIND_NOACCESS_SPACE (prev_p, sizeof (mem_heap_free_t)); + prev_p = next_p; + MEM_HEAP_STAT_FREE_ITER (); + } + next_p = MEM_HEAP_GET_ADDR_FROM_OFFSET (prev_p->next_offset); + VALGRIND_DEFINED_SPACE (next_p, sizeof (mem_heap_free_t)); + + /* Realign size */ + const size_t aligned_size = (size + MEM_ALIGNMENT - 1) / MEM_ALIGNMENT * MEM_ALIGNMENT; + + VALGRIND_DEFINED_SPACE (block_p, sizeof (mem_heap_free_t)); + VALGRIND_DEFINED_SPACE (prev_p, sizeof (mem_heap_free_t)); + // Update prev + if (mem_heap_get_region_end (prev_p) == block_p) + { + // Can be merged + prev_p->size += (uint32_t) aligned_size; + VALGRIND_NOACCESS_SPACE (block_p, sizeof (mem_heap_free_t)); + block_p = prev_p; + } + else + { + block_p->size = (uint32_t) aligned_size; + prev_p->next_offset = block_offset; + } + + VALGRIND_DEFINED_SPACE (next_p, sizeof (mem_heap_free_t)); + // Update next + if (mem_heap_get_region_end (block_p) == next_p) + { + if (unlikely (next_p == mem_heap_list_skip_p)) + { + mem_heap_list_skip_p = block_p; + } + + // Can be merged + block_p->size += next_p->size; + block_p->next_offset = next_p->next_offset; + + } + else + { + block_p->next_offset = MEM_HEAP_GET_OFFSET_FROM_ADDR (next_p); + } + + mem_heap_list_skip_p = prev_p; + + VALGRIND_NOACCESS_SPACE (prev_p, sizeof (mem_heap_free_t)); + VALGRIND_NOACCESS_SPACE (block_p, size); + VALGRIND_NOACCESS_SPACE (next_p, sizeof (mem_heap_free_t)); + + JERRY_ASSERT (mem_heap_allocated_size > 0); + mem_heap_allocated_size -= aligned_size; + + while (mem_heap_allocated_size + CONFIG_MEM_HEAP_DESIRED_LIMIT <= mem_heap_limit) { mem_heap_limit -= CONFIG_MEM_HEAP_DESIRED_LIMIT; } - JERRY_ASSERT (mem_heap_limit >= mem_heap_allocated_chunks * MEM_HEAP_CHUNK_SIZE); - - MEM_HEAP_STAT_FREE (chunk_index, chunks); - -#ifdef MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY - mem_heap_allocated_bytes[chunk_index] = 0; -#endif /* MEM_HEAP_ENABLE_ALLOCATED_BYTES_ARRAY */ - -#ifndef JERRY_NDEBUG - mem_heap_length_types[chunk_index] = MEM_BLOCK_LENGTH_TYPE_GENERAL; -#endif /* !JERRY_NDEBUG */ - - mem_check_heap (); + VALGRIND_NOACCESS_SPACE (&mem_heap.first, sizeof (mem_heap_free_t)); + JERRY_ASSERT (mem_heap_limit >= mem_heap_allocated_size); + MEM_HEAP_STAT_FREE (size); } /* mem_heap_free_block */ /** - * Find beginning of user data in a one-chunked block from pointer, - * pointing into it, i.e. into [block_data_space_start; block_data_space_end) range. - * - * Note: - * Pointer must point to the one-chunked memory region which was previously allocated - * with mem_heap_alloc_chunked_block and is currently valid. - * - * Note: - * The interface should only be used for determining where the user space of heap-allocated block begins. - * Caller should never rely on some specific internals of heap implementation. - * - * @return beginning of user data space of block identified by the pointer + * Free block with stored size */ -void * -mem_heap_get_chunked_block_start (void *ptr) /**< pointer into a block */ +void __attr_always_inline___ +mem_heap_free_block_size_stored (void *ptr) /**< pointer to the memory block */ { - JERRY_ASSERT (mem_heap.area <= (uint8_t *) ptr && (uint8_t *) ptr < (uint8_t *) mem_heap.area + MEM_HEAP_AREA_SIZE); - - uintptr_t uintptr = (uintptr_t) ptr; - uintptr_t uintptr_chunk_aligned = JERRY_ALIGNDOWN (uintptr, MEM_HEAP_CHUNK_SIZE); - - JERRY_ASSERT (uintptr >= uintptr_chunk_aligned); - -#ifndef JERRY_NDEBUG - size_t chunk_index = mem_heap_get_chunk_from_address ((void *) uintptr_chunk_aligned); - JERRY_ASSERT (mem_heap_length_types[chunk_index] == MEM_BLOCK_LENGTH_TYPE_ONE_CHUNKED); -#endif /* !JERRY_NDEBUG */ - - return (void *) uintptr_chunk_aligned; -} /* mem_heap_get_chunked_block_start */ - -/** - * Get size of one-chunked block data space - */ -size_t -mem_heap_get_chunked_block_data_size (void) -{ - return MEM_HEAP_CHUNK_SIZE; -} /* mem_heap_get_chunked_block_data_size */ + mem_heap_free_t *const original_p = ((mem_heap_free_t *) ptr) - 1; + JERRY_ASSERT (original_p + 1 == ptr); + mem_heap_free_block (original_p, original_p->size); +} /* mem_heap_free_block_size_stored */ /** * Recommend allocation size based on chunk size. @@ -805,13 +567,13 @@ mem_heap_recommend_allocation_size (size_t minimum_allocation_size) /**< minimum * * @return packed heap pointer */ -uintptr_t +uintptr_t __attr_pure___ __attribute__((hot)) mem_heap_compress_pointer (const void *pointer_p) /**< pointer to compress */ { JERRY_ASSERT (pointer_p != NULL); uintptr_t int_ptr = (uintptr_t) pointer_p; - uintptr_t heap_start = (uintptr_t) &mem_heap; + const uintptr_t heap_start = (uintptr_t) &mem_heap; JERRY_ASSERT (int_ptr % MEM_ALIGNMENT == 0); @@ -830,13 +592,13 @@ mem_heap_compress_pointer (const void *pointer_p) /**< pointer to compress */ * * @return unpacked heap pointer */ -void * +void * __attr_pure___ __attribute__((hot)) mem_heap_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decompress */ { JERRY_ASSERT (compressed_pointer != MEM_CP_NULL); uintptr_t int_ptr = compressed_pointer; - uintptr_t heap_start = (uintptr_t) &mem_heap; + const uintptr_t heap_start = (uintptr_t) &mem_heap; int_ptr <<= MEM_ALIGNMENT_LOG; int_ptr += heap_start; @@ -857,146 +619,42 @@ mem_heap_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decom bool mem_is_heap_pointer (const void *pointer) /**< pointer */ { - uint8_t *uint8_pointer = (uint8_t *) pointer; - - return (uint8_pointer >= mem_heap.area && uint8_pointer <= ((uint8_t *) mem_heap.area + MEM_HEAP_AREA_SIZE)); + return ((uint8_t *) pointer >= mem_heap.area + && (uint8_t *) pointer <= ((uint8_t *) mem_heap.area + MEM_HEAP_AREA_SIZE)); } /* mem_is_heap_pointer */ #endif /* !JERRY_NDEBUG */ -/** - * Print heap block - */ -static void -mem_heap_print_block (bool dump_block_data, /**< print block with data (true) - * or print only block header (false) */ - size_t start_chunk, /**< starting chunk of block */ - size_t chunks_num, /**< number of chunks in the block */ - bool is_free) /**< is the block free? */ -{ - printf ("Block (%p): state=%s, size in chunks=%lu\n", - (uint8_t *) mem_heap.area + start_chunk * MEM_HEAP_CHUNK_SIZE, - is_free ? "free" : "allocated", - (unsigned long) chunks_num); - - if (dump_block_data) - { - uint8_t *block_data_p = (uint8_t *) mem_heap.area; - uint8_t *block_data_end_p = block_data_p + start_chunk * MEM_HEAP_CHUNK_SIZE; - -#ifdef JERRY_VALGRIND - VALGRIND_DISABLE_ERROR_REPORTING; -#endif /* JERRY_VALGRIND */ - - while (block_data_p != block_data_end_p) - { - printf ("0x%02x ", *block_data_p++); - } - -#ifdef JERRY_VALGRIND - VALGRIND_ENABLE_ERROR_REPORTING; -#endif /* JERRY_VALGRIND */ - - printf ("\n"); - } -} /* mem_heap_print_block */ - /** * Print heap */ void -mem_heap_print (bool dump_block_headers, /**< print block headers */ - bool dump_block_data, /**< print block with data (true) - or print only block header (false) */ - bool dump_stats) /**< print heap stats */ +mem_heap_print () { - mem_check_heap (); - - JERRY_ASSERT (!dump_block_data || dump_block_headers); - - if (dump_block_headers) - { - VALGRIND_DEFINED_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - printf ("Heap: start=%p size=%lu\n", - mem_heap.area, - (unsigned long) MEM_HEAP_AREA_SIZE); - - bool is_free = true; - size_t start_chunk = 0; - size_t chunk_index; - - for (chunk_index = 0; chunk_index < MEM_HEAP_CHUNKS_NUM; chunk_index++) - { - size_t bitmap_item_index = chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - size_t item_bit_index = chunk_index % MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (item_bit_index); - - if ((MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[bitmap_item_index] & bit) != 0 - || (((MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] & bit) == 0) != is_free)) - { - mem_heap_print_block (dump_block_data, start_chunk, chunk_index - start_chunk, is_free); - - start_chunk = chunk_index; - is_free = ((MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] & bit) == 0); - } - } - - VALGRIND_NOACCESS_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - mem_heap_print_block (dump_block_data, start_chunk, chunk_index - start_chunk, is_free); - } - #ifdef MEM_STATS - if (dump_stats) - { - mem_heap_stats_print (); - } -#else /* MEM_STATS */ - (void) dump_stats; -#endif /* !MEM_STATS */ - + printf ("Heap stats:\n"); + printf (" Heap size = %zu bytes\n" + " Allocated = %zu bytes\n" + " Waste = %zu bytes\n" + " Peak allocated = %zu bytes\n" + " Peak waste = %zu bytes\n" + " Skip-ahead ratio = %zu.%04zu\n" + " Average alloc iteration = %zu.%04zu\n" + " Average free iteration = %zu.%04zu\n", + mem_heap_stats.size, + mem_heap_stats.allocated_bytes, + mem_heap_stats.waste_bytes, + mem_heap_stats.peak_allocated_bytes, + mem_heap_stats.peak_waste_bytes, + mem_heap_stats.skip_count / mem_heap_stats.nonskip_count, + mem_heap_stats.skip_count % mem_heap_stats.nonskip_count * 10000 / mem_heap_stats.nonskip_count, + mem_heap_stats.alloc_iter_count / mem_heap_stats.alloc_count, + mem_heap_stats.alloc_iter_count % mem_heap_stats.alloc_count * 10000 / mem_heap_stats.alloc_count, + mem_heap_stats.free_iter_count / mem_heap_stats.free_count, + mem_heap_stats.free_iter_count % mem_heap_stats.free_count * 10000 / mem_heap_stats.free_count); printf ("\n"); +#endif /* !MEM_STATS */ } /* mem_heap_print */ -/** - * Check heap consistency - */ -static void -mem_check_heap (void) -{ -#ifndef JERRY_DISABLE_HEAVY_DEBUG - size_t allocated_chunks_num = 0; - - VALGRIND_DEFINED_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - for (size_t chunk_index = 0; chunk_index < MEM_HEAP_CHUNKS_NUM; chunk_index++) - { - size_t bitmap_item_index = chunk_index / MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - size_t item_bit_index = chunk_index % MEM_HEAP_BITMAP_BITS_IN_STORAGE_ITEM; - mem_heap_bitmap_storage_item_t bit = MEM_HEAP_BITMAP_ITEM_BIT (item_bit_index); - - if ((MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[bitmap_item_index] & bit) != 0) - { - JERRY_ASSERT ((MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] & bit) != 0); - } - - if ((MEM_HEAP_IS_ALLOCATED_BITMAP[bitmap_item_index] & bit) != 0) - { - if (mem_heap_length_types[chunk_index] == MEM_BLOCK_LENGTH_TYPE_ONE_CHUNKED) - { - JERRY_ASSERT ((MEM_HEAP_IS_FIRST_IN_BLOCK_BITMAP[bitmap_item_index] & bit) != 0); - } - - allocated_chunks_num++; - } - } - - VALGRIND_NOACCESS_SPACE (mem_heap.bitmaps, sizeof (mem_heap.bitmaps)); - - JERRY_ASSERT (allocated_chunks_num == mem_heap_allocated_chunks); -#endif /* !JERRY_DISABLE_HEAVY_DEBUG */ -} /* mem_check_heap */ - #ifdef MEM_STATS /** * Get heap memory usage statistics @@ -1013,7 +671,6 @@ mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p) /**< out: heap stats */ void mem_heap_stats_reset_peak (void) { - mem_heap_stats.peak_allocated_chunks = mem_heap_stats.allocated_chunks; mem_heap_stats.peak_allocated_bytes = mem_heap_stats.allocated_bytes; mem_heap_stats.peak_waste_bytes = mem_heap_stats.waste_bytes; } /* mem_heap_stats_reset_peak */ @@ -1033,25 +690,15 @@ mem_heap_stat_init () * Account allocation */ static void -mem_heap_stat_alloc (size_t first_chunk_index, /**< first chunk of the allocated area */ - size_t chunks_num) /**< number of chunks in the area */ +mem_heap_stat_alloc (size_t size) /**< Size of allocated block */ { - const size_t chunks = chunks_num; - const size_t bytes = (size_t) mem_heap_allocated_bytes[first_chunk_index]; - const size_t waste_bytes = chunks * MEM_HEAP_CHUNK_SIZE - bytes; + const size_t aligned_size = (size + MEM_ALIGNMENT - 1) / MEM_ALIGNMENT * MEM_ALIGNMENT; + const size_t waste_bytes = aligned_size - size; - mem_heap_stats.allocated_chunks += chunks; - mem_heap_stats.allocated_bytes += bytes; + mem_heap_stats.allocated_bytes += aligned_size; mem_heap_stats.waste_bytes += waste_bytes; + mem_heap_stats.alloc_count++; - if (mem_heap_stats.allocated_chunks > mem_heap_stats.peak_allocated_chunks) - { - mem_heap_stats.peak_allocated_chunks = mem_heap_stats.allocated_chunks; - } - if (mem_heap_stats.allocated_chunks > mem_heap_stats.global_peak_allocated_chunks) - { - mem_heap_stats.global_peak_allocated_chunks = mem_heap_stats.allocated_chunks; - } if (mem_heap_stats.allocated_bytes > mem_heap_stats.peak_allocated_bytes) { @@ -1070,58 +717,57 @@ mem_heap_stat_alloc (size_t first_chunk_index, /**< first chunk of the allocated { mem_heap_stats.global_peak_waste_bytes = mem_heap_stats.waste_bytes; } - - JERRY_ASSERT (mem_heap_stats.allocated_bytes <= mem_heap_stats.size); - JERRY_ASSERT (mem_heap_stats.allocated_chunks <= mem_heap_stats.size / MEM_HEAP_CHUNK_SIZE); } /* mem_heap_stat_alloc */ /** * Account freeing */ static void -mem_heap_stat_free (size_t first_chunk_index, /**< first chunk of the freed area */ - size_t chunks_num) /**< number of chunks in the area */ +mem_heap_stat_free (size_t size) /**< Size of freed block */ { - const size_t chunks = chunks_num; - const size_t bytes = (size_t) mem_heap_allocated_bytes[first_chunk_index]; - const size_t waste_bytes = chunks * MEM_HEAP_CHUNK_SIZE - bytes; + const size_t aligned_size = (size + MEM_ALIGNMENT - 1) / MEM_ALIGNMENT * MEM_ALIGNMENT; + const size_t waste_bytes = aligned_size - size; - JERRY_ASSERT (mem_heap_stats.allocated_bytes <= mem_heap_stats.size); - JERRY_ASSERT (mem_heap_stats.allocated_chunks <= mem_heap_stats.size / MEM_HEAP_CHUNK_SIZE); - - JERRY_ASSERT (mem_heap_stats.allocated_chunks >= chunks); - JERRY_ASSERT (mem_heap_stats.allocated_bytes >= bytes); - JERRY_ASSERT (mem_heap_stats.waste_bytes >= waste_bytes); - - mem_heap_stats.allocated_chunks -= chunks; - mem_heap_stats.allocated_bytes -= bytes; + mem_heap_stats.free_count++; + mem_heap_stats.allocated_bytes -= aligned_size; mem_heap_stats.waste_bytes -= waste_bytes; } /* mem_heap_stat_free */ /** - * Print heap statistics + * Counts number of skip-aheads during insertion of free block */ -void -mem_heap_stats_print (void) +static void +mem_heap_stat_skip () { - printf ("Heap stats:\n"); - printf (" Heap size = %zu bytes\n" - " Chunk size = %zu bytes\n" - " Allocated chunks count = %zu\n" - " Allocated = %zu bytes\n" - " Waste = %zu bytes\n" - " Peak allocated chunks count = %zu\n" - " Peak allocated = %zu bytes\n" - " Peak waste = %zu bytes\n", - mem_heap_stats.size, - MEM_HEAP_CHUNK_SIZE, - mem_heap_stats.allocated_chunks, - mem_heap_stats.allocated_bytes, - mem_heap_stats.waste_bytes, - mem_heap_stats.peak_allocated_chunks, - mem_heap_stats.peak_allocated_bytes, - mem_heap_stats.peak_waste_bytes); -} /* mem_heap_stats_print */ + mem_heap_stats.skip_count++; +} /* mem_heap_stat_skip */ + +/** + * Counts number of times we could not skip ahead during free block insertion + */ +static void +mem_heap_stat_nonskip () +{ + mem_heap_stats.nonskip_count++; +} /* mem_heap_stat_nonskip */ + +/** + * Count number of iterations required for allocations + */ +static void +mem_heap_stat_alloc_iter () +{ + mem_heap_stats.alloc_iter_count++; +} /* mem_heap_stat_alloc_iter */ + +/** + * Counts number of iterations required for inserting free blocks + */ +static void +mem_heap_stat_free_iter () +{ + mem_heap_stats.free_iter_count++; +} /* mem_heap_stat_free_iter */ #endif /* MEM_STATS */ /** diff --git a/jerry-core/mem/mem-heap.h b/jerry-core/mem/mem-heap.h index a5d6daa05..49c3f1224 100644 --- a/jerry-core/mem/mem-heap.h +++ b/jerry-core/mem/mem-heap.h @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -28,29 +29,18 @@ #include "jrt.h" -/** - * Type of allocation (argument of mem_Alloc) - * - * @see mem_heap_alloc_block - */ -typedef enum -{ - MEM_HEAP_ALLOC_SHORT_TERM, /**< allocated region will be freed soon */ - MEM_HEAP_ALLOC_LONG_TERM /**< allocated region most likely will not be freed soon */ -} mem_heap_alloc_term_t; extern void mem_heap_init (void); extern void mem_heap_finalize (void); -extern void *mem_heap_alloc_block (size_t, mem_heap_alloc_term_t); -extern void *mem_heap_alloc_chunked_block (mem_heap_alloc_term_t); -extern void mem_heap_free_block (void *); -extern void *mem_heap_get_chunked_block_start (void *); -extern size_t mem_heap_get_chunked_block_data_size (void); +extern void *mem_heap_alloc_block (const size_t); +extern void mem_heap_free_block (void *, const size_t); +extern void *mem_heap_alloc_block_store_size (size_t); +extern void mem_heap_free_block_size_stored (void *); extern uintptr_t mem_heap_compress_pointer (const void *); extern void *mem_heap_decompress_pointer (uintptr_t); extern bool mem_is_heap_pointer (const void *); extern size_t __attr_pure___ mem_heap_recommend_allocation_size (size_t); -extern void mem_heap_print (bool, bool, bool); +extern void mem_heap_print (); #ifdef MEM_STATS /** @@ -59,15 +49,6 @@ extern void mem_heap_print (bool, bool, bool); typedef struct { size_t size; /**< size */ - size_t blocks; /**< blocks count */ - - size_t allocated_chunks; /**< currently allocated chunks */ - size_t peak_allocated_chunks; /**< peak allocated chunks */ - size_t global_peak_allocated_chunks; /**< non-resettable peak allocated chunks */ - - size_t allocated_blocks; /**< currently allocated blocks */ - size_t peak_allocated_blocks; /**< peak allocated blocks */ - size_t global_peak_allocated_blocks; /**< non-resettable peak allocated blocks */ size_t allocated_bytes; /**< currently allocated bytes */ size_t peak_allocated_bytes; /**< peak allocated bytes */ @@ -77,6 +58,15 @@ typedef struct and due to block headers */ size_t peak_waste_bytes; /**< peak bytes waste */ size_t global_peak_waste_bytes; /**< non-resettable peak bytes waste */ + + size_t skip_count; + size_t nonskip_count; + + size_t alloc_count; + size_t alloc_iter_count; + + size_t free_count; + size_t free_iter_count; } mem_heap_stats_t; extern void mem_heap_get_stats (mem_heap_stats_t *); @@ -111,7 +101,7 @@ extern void mem_heap_valgrind_freya_mempool_request (void); #define MEM_DEFINE_LOCAL_ARRAY(var_name, number, type) \ { \ size_t var_name ## ___size = (size_t) (number) * sizeof (type); \ - type *var_name = (type *) (mem_heap_alloc_block (var_name ## ___size, MEM_HEAP_ALLOC_SHORT_TERM)); + type *var_name = (type *) (mem_heap_alloc_block (var_name ## ___size)); /** * Free the previously defined local array variable, freeing corresponding block on the heap, @@ -122,7 +112,7 @@ extern void mem_heap_valgrind_freya_mempool_request (void); { \ JERRY_ASSERT (var_name ## ___size != 0); \ \ - mem_heap_free_block (var_name); \ + mem_heap_free_block (var_name, var_name ## ___size); \ } \ else \ { \ diff --git a/jerry-core/mem/mem-poolman.c b/jerry-core/mem/mem-poolman.c index 0f304bdd1..e854c9bee 100644 --- a/jerry-core/mem/mem-poolman.c +++ b/jerry-core/mem/mem-poolman.c @@ -35,117 +35,45 @@ #include "mem-allocator-internal.h" /** - * Size of a pool + * Node for free chunk list */ -#define MEM_POOL_SIZE (mem_heap_get_chunked_block_data_size ()) - -/** - * Number of chunks in a pool - */ -#define MEM_POOL_CHUNKS_NUMBER (MEM_POOL_SIZE / MEM_POOL_CHUNK_SIZE) - -#ifndef JERRY_NDEBUG -size_t mem_free_chunks_number; -#endif /* !JERRY_NDEBUG */ - -/** - * Index of chunk in a pool - */ -typedef uint8_t mem_pool_chunk_index_t; - -/** - * Type for mem_pool_chunk - */ -typedef struct mem_pool_chunk mem_pool_chunk_t; - -/** - * Pool chunk - */ -struct mem_pool_chunk +typedef struct { - /** - * Union of possible free chunk layouts - * - * Allocated chunk represents raw data of MEM_POOL_CHUNK_SIZE bytes, - * and so, has no fixed layout. - */ - union - { - /** - * Structure of free pool chunks that are: - * - first in corresponding pool, while empty pool collector is not active; - * - not first in corresponding pool. - */ - struct - { - mem_pool_chunk_t *next_p; /**< global list of free pool chunks */ - } free; - - /** - * While empty pool collector is active, the following structure is used - * for first chunks of pools, in which first chunks are free - * - * See also: - * mem_pools_collect_empty - */ - struct - { - mem_cpointer_t next_first_cp; /**< list of first free chunks of - * pools with free first chunks */ - mem_cpointer_t free_list_cp; /**< list of free chunks - * in the pool containing this chunk */ - uint16_t hint_magic_num; /**< magic number that hints whether - * there is a probability that the chunk - * is an item (header) in a pool list */ - mem_pool_chunk_index_t free_chunks_num; /**< number of free chunks - * in the pool containing this chunk */ - uint8_t traversal_check_flag; /**< flag that is flipped between two non-first chunk lists traversals - * to determine whether the corresponding pool-first chunks are actually free */ - } pool_gc; - - /** - * The field is added to make sizeof (mem_pool_chunk_t) equal to MEM_POOL_CHUNK_SIZE - */ - uint8_t allocated_area[MEM_POOL_CHUNK_SIZE]; - } u; -} mem_pool_chunk; - -/** - * The condition is assumed when using pointer arithmetics on (mem_pool_chunk_t *) pointer type - */ -JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_t) == MEM_POOL_CHUNK_SIZE, - size_of_mem_pool_chunk_t_must_be_equal_to_MEM_POOL_CHUNK_SIZE); + struct mem_pools_chunk_t *next_p; /* pointer to next pool chunk */ +#ifndef MEM_HEAP_PTR_64 + uint32_t dummy; /* dummy member for alignment */ +#endif +} mem_pools_chunk_t; /** * List of free pool chunks */ -mem_pool_chunk_t *mem_free_chunk_p; - -static void mem_check_pools (void); +mem_pools_chunk_t *mem_free_chunk_p; #ifdef MEM_STATS + /** * Pools' memory usage statistics */ mem_pools_stats_t mem_pools_stats; static void mem_pools_stat_init (void); -static void mem_pools_stat_alloc_pool (void); static void mem_pools_stat_free_pool (void); -static void mem_pools_stat_alloc_chunk (void); -static void mem_pools_stat_free_chunk (void); +static void mem_pools_stat_new_alloc (void); +static void mem_pools_stat_reuse (void); +static void mem_pools_stat_dealloc (void); # define MEM_POOLS_STAT_INIT() mem_pools_stat_init () -# define MEM_POOLS_STAT_ALLOC_POOL() mem_pools_stat_alloc_pool () # define MEM_POOLS_STAT_FREE_POOL() mem_pools_stat_free_pool () -# define MEM_POOLS_STAT_ALLOC_CHUNK() mem_pools_stat_alloc_chunk () -# define MEM_POOLS_STAT_FREE_CHUNK() mem_pools_stat_free_chunk () +# define MEM_POOLS_STAT_NEW_ALLOC() mem_pools_stat_new_alloc () +# define MEM_POOLS_STAT_REUSE() mem_pools_stat_reuse () +# define MEM_POOLS_STAT_DEALLOC() mem_pools_stat_dealloc () #else /* !MEM_STATS */ # define MEM_POOLS_STAT_INIT() -# define MEM_POOLS_STAT_ALLOC_POOL() # define MEM_POOLS_STAT_FREE_POOL() -# define MEM_POOLS_STAT_ALLOC_CHUNK() -# define MEM_POOLS_STAT_FREE_CHUNK() +# define MEM_POOLS_STAT_NEW_ALLOC() +# define MEM_POOLS_STAT_REUSE() +# define MEM_POOLS_STAT_DEALLOC() #endif /* !MEM_STATS */ /* @@ -179,9 +107,8 @@ static void mem_pools_stat_free_chunk (void); void mem_pools_init (void) { -#ifndef JERRY_NDEBUG - mem_free_chunks_number = 0; -#endif /* !JERRY_NDEBUG */ + JERRY_STATIC_ASSERT (sizeof (mem_pools_chunk_t) == MEM_POOL_CHUNK_SIZE, + size_of_mem_pool_chunk_t_must_be_equal_to_MEM_POOL_CHUNK_SIZE); mem_free_chunk_p = NULL; @@ -196,423 +123,8 @@ mem_pools_finalize (void) { mem_pools_collect_empty (); -#ifndef JERRY_NDEBUG - JERRY_ASSERT (mem_free_chunks_number == 0); -#endif /* !JERRY_NDEBUG */ -} /* mem_pools_finalize */ - -/** - * Helper for reading magic number and traversal check flag fields of a pool-first chunk, - * that suppresses valgrind's warnings about undefined values. - * - * A pool-first chunk can be either allocated or free. - * - * As chunks are marked as undefined upon allocation, some of chunks can still be - * fully or partially marked as undefined. - * - * Nevertheless, the fields are read and their values are used to determine - * whether the chunk is actually free pool-first chunk. - * - * See also: - * Description of collection algorithm in mem_pools_collect_empty - */ -static void __attr_always_inline___ -mem_pools_collect_read_magic_num_and_flag (mem_pool_chunk_t *pool_first_chunk_p, /**< a pool-first chunk */ - uint16_t *out_magic_num_field_value_p, /**< out: value of magic num field, - * read from the chunk */ - bool *out_traversal_check_flag_p) /**< out: value of traversal check flag - * field, read from the chunk */ -{ - JERRY_ASSERT (pool_first_chunk_p != NULL); - JERRY_ASSERT (out_magic_num_field_value_p != NULL); - JERRY_ASSERT (out_traversal_check_flag_p != NULL); - -#ifdef JERRY_VALGRIND - /* - * If the chunk is not free, there may be undefined bytes at hint_magic_num and traversal_check_flag fields. - * - * Although, it is correct for the routine, valgrind issues warning about using uninitialized data - * in conditional expression. To suppress the false-positive warning, the chunk is temporarily marked - * as defined, and after reading hint magic number and list identifier, valgrind state of the chunk is restored. - */ - uint8_t vbits[MEM_POOL_CHUNK_SIZE]; - unsigned status; - - status = VALGRIND_GET_VBITS (pool_first_chunk_p, vbits, MEM_POOL_CHUNK_SIZE); - JERRY_ASSERT (status == 0 || status == 1); - - VALGRIND_DEFINED_SPACE (pool_first_chunk_p, MEM_POOL_CHUNK_SIZE); -#endif /* JERRY_VALGRIND */ - - uint16_t magic_num_field = pool_first_chunk_p->u.pool_gc.hint_magic_num; - bool traversal_check_flag = pool_first_chunk_p->u.pool_gc.traversal_check_flag; - -#ifdef JERRY_VALGRIND - status = VALGRIND_SET_VBITS (pool_first_chunk_p, vbits, MEM_POOL_CHUNK_SIZE); - JERRY_ASSERT (status == 0 || status == 1); -#endif /* JERRY_VALGRIND */ - - *out_magic_num_field_value_p = magic_num_field; - *out_traversal_check_flag_p = traversal_check_flag; -} /* mem_pools_collect_read_magic_num_and_flag */ - -/** - * Collect chunks from empty pools and free the pools - */ -void -mem_pools_collect_empty (void) -{ - /* - * Hint magic number in header of pools with free pool-first chunks - */ - const uint16_t hint_magic_num_value = 0x7e89; - - /* - * Collection-time chunk lists - */ - mem_pool_chunk_t *first_chunks_list_p = NULL; - mem_pool_chunk_t *non_first_chunks_list_p = NULL; - - /* - * At first stage collect free pool-first chunks to separate collection-time lists - * and change their layout from mem_pool_chunk_t::u::free to mem_pool_chunk_t::u::pool_gc - */ - { - mem_pool_chunk_t tmp_header; - tmp_header.u.free.next_p = mem_free_chunk_p; - - for (mem_pool_chunk_t *free_chunk_iter_p = tmp_header.u.free.next_p, - *prev_free_chunk_p = &tmp_header, - *next_free_chunk_p; - free_chunk_iter_p != NULL; - free_chunk_iter_p = next_free_chunk_p) - { - mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (free_chunk_iter_p); - - VALGRIND_DEFINED_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); - - next_free_chunk_p = free_chunk_iter_p->u.free.next_p; - - if (pool_start_p == free_chunk_iter_p) - { - /* - * The chunk is first at its pool - * - * Remove the chunk from common list of free chunks - */ - prev_free_chunk_p->u.free.next_p = next_free_chunk_p; - - /* - * Initialize pool-first chunk as pool header and it insert into list of free pool-first chunks - */ - free_chunk_iter_p->u.pool_gc.free_list_cp = MEM_CP_NULL; - free_chunk_iter_p->u.pool_gc.free_chunks_num = 1; /* the first chunk */ - free_chunk_iter_p->u.pool_gc.hint_magic_num = hint_magic_num_value; - free_chunk_iter_p->u.pool_gc.traversal_check_flag = false; - - MEM_CP_SET_POINTER (free_chunk_iter_p->u.pool_gc.next_first_cp, first_chunks_list_p); - first_chunks_list_p = free_chunk_iter_p; - } - else - { - prev_free_chunk_p = free_chunk_iter_p; - } - } - - mem_free_chunk_p = tmp_header.u.free.next_p; - } - - if (first_chunks_list_p == NULL) - { - /* there are no empty pools */ - - return; - } - - /* - * At second stage we collect all free non-pool-first chunks, for which corresponding pool-first chunks are free, - * and link them into the corresponding mem_pool_chunk_t::u::pool_gc::free_list_cp list, while also maintaining - * the corresponding mem_pool_chunk_t::u::pool_gc::free_chunks_num: - * - at first, for each non-pool-first free chunk we check whether traversal check flag is cleared in corresponding - * first chunk in the same pool, and move those chunks, for which the condition is true, - * to separate temporary list. - * - * - then, we flip the traversal check flags for each of free pool-first chunks. - * - * - at last, we perform almost the same as at first step, but check only non-pool-first chunks from the temporary - * list, and send the chunks, for which the corresponding traversal check flag is cleared, back to the common list - * of free chunks, and the rest chunks from the temporary list are linked to corresponding pool-first chunks. - * Also, counter of the linked free chunks is maintained in every free pool-first chunk. - */ - { - { - mem_pool_chunk_t tmp_header; - tmp_header.u.free.next_p = mem_free_chunk_p; - - for (mem_pool_chunk_t *free_chunk_iter_p = tmp_header.u.free.next_p, - *prev_free_chunk_p = &tmp_header, - *next_free_chunk_p; - free_chunk_iter_p != NULL; - free_chunk_iter_p = next_free_chunk_p) - { - mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (free_chunk_iter_p); - - next_free_chunk_p = free_chunk_iter_p->u.free.next_p; - - /* - * The magic number doesn't guarantee that the chunk is actually a free pool-first chunk, - * so we test the traversal check flag after flipping values of the flags in every - * free pool-first chunk. - */ - uint16_t magic_num_field; - bool traversal_check_flag; - - mem_pools_collect_read_magic_num_and_flag (pool_start_p, &magic_num_field, &traversal_check_flag); - - /* - * During this traversal the flag in the free header chunks is in cleared state - */ - if (!traversal_check_flag - && magic_num_field == hint_magic_num_value) - { - free_chunk_iter_p->u.free.next_p = non_first_chunks_list_p; - non_first_chunks_list_p = free_chunk_iter_p; - - prev_free_chunk_p->u.free.next_p = next_free_chunk_p; - } - else - { - prev_free_chunk_p = free_chunk_iter_p; - } - } - - mem_free_chunk_p = tmp_header.u.free.next_p; - } - - { - /* - * Now, flip the traversal check flag in free pool-first chunks - */ - for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p; - first_chunks_iter_p != NULL; - first_chunks_iter_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, - first_chunks_iter_p->u.pool_gc.next_first_cp)) - { - JERRY_ASSERT (!first_chunks_iter_p->u.pool_gc.traversal_check_flag); - - first_chunks_iter_p->u.pool_gc.traversal_check_flag = true; - } - } - - { - for (mem_pool_chunk_t *non_first_chunks_iter_p = non_first_chunks_list_p, *next_p; - non_first_chunks_iter_p != NULL; - non_first_chunks_iter_p = next_p) - { - next_p = non_first_chunks_iter_p->u.free.next_p; - - mem_pool_chunk_t *pool_start_p; - pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (non_first_chunks_iter_p); - - uint16_t magic_num_field; - bool traversal_check_flag; - - mem_pools_collect_read_magic_num_and_flag (pool_start_p, &magic_num_field, &traversal_check_flag); - - JERRY_ASSERT (magic_num_field == hint_magic_num_value); - -#ifndef JERRY_DISABLE_HEAVY_DEBUG - bool is_occured = false; - - for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p; - first_chunks_iter_p != NULL; - first_chunks_iter_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, - first_chunks_iter_p->u.pool_gc.next_first_cp)) - { - if (pool_start_p == first_chunks_iter_p) - { - is_occured = true; - break; - } - } - - JERRY_ASSERT (is_occured == traversal_check_flag); -#endif /* !JERRY_DISABLE_HEAVY_DEBUG */ - - /* - * During this traversal the flag in the free header chunks is in set state - * - * If the flag is set, it is guaranteed that the pool-first chunk, - * from the same pool, as the current non-pool-first chunk, is free - * and is placed in the corresponding list of free pool-first chunks. - */ - if (traversal_check_flag) - { - pool_start_p->u.pool_gc.free_chunks_num++; - - non_first_chunks_iter_p->u.free.next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, - pool_start_p->u.pool_gc.free_list_cp); - MEM_CP_SET_NON_NULL_POINTER (pool_start_p->u.pool_gc.free_list_cp, non_first_chunks_iter_p); - } - else - { - non_first_chunks_iter_p->u.free.next_p = mem_free_chunk_p; - mem_free_chunk_p = non_first_chunks_iter_p; - } - } - } - - non_first_chunks_list_p = NULL; - } - - /* - * At third stage we check each free pool-first chunk in collection-time list for counted - * number of free chunks in the pool, containing the chunk. - * - * If the number is equal to number of chunks in the pool - then the pool is empty, and so is freed, - * otherwise - free chunks of the pool are returned to the common list of free chunks. - */ - for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p, *next_p; - first_chunks_iter_p != NULL; - first_chunks_iter_p = next_p) - { - next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, - first_chunks_iter_p->u.pool_gc.next_first_cp); - - JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.hint_magic_num == hint_magic_num_value); - JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.traversal_check_flag); - JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.free_chunks_num <= MEM_POOL_CHUNKS_NUMBER); - - if (first_chunks_iter_p->u.pool_gc.free_chunks_num == MEM_POOL_CHUNKS_NUMBER) - { -#ifndef JERRY_NDEBUG - mem_free_chunks_number -= MEM_POOL_CHUNKS_NUMBER; -#endif /* !JERRY_NDEBUG */ - - MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); - mem_heap_free_block (first_chunks_iter_p); - - MEM_POOLS_STAT_FREE_POOL (); - } - else - { - mem_pool_chunk_t *first_chunk_p = first_chunks_iter_p; - - /* - * Convert layout of first chunk from collection-time pool-first chunk's layout to the common free chunk layout - */ - first_chunk_p->u.free.next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, - first_chunks_iter_p->u.pool_gc.free_list_cp); - - /* - * Link local pool's list of free chunks into the common list of free chunks - */ - for (mem_pool_chunk_t *pool_chunks_iter_p = first_chunk_p; - ; - pool_chunks_iter_p = pool_chunks_iter_p->u.free.next_p) - { - JERRY_ASSERT (pool_chunks_iter_p != NULL); - - if (pool_chunks_iter_p->u.free.next_p == NULL) - { - pool_chunks_iter_p->u.free.next_p = mem_free_chunk_p; - - break; - } - } - - mem_free_chunk_p = first_chunk_p; - } - } - -#ifdef JERRY_VALGRIND - /* - * Valgrind-mode specific pass that marks all free chunks inaccessible - */ - for (mem_pool_chunk_t *free_chunk_iter_p = mem_free_chunk_p, *next_free_chunk_p; - free_chunk_iter_p != NULL; - free_chunk_iter_p = next_free_chunk_p) - { - next_free_chunk_p = free_chunk_iter_p->u.free.next_p; - - VALGRIND_NOACCESS_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); - } -#endif /* JERRY_VALGRIND */ -} /* mem_pools_collect_empty */ - -/** - * Long path for mem_pools_alloc - */ -static void __attr_noinline___ -mem_pools_alloc_longpath (void) -{ - mem_check_pools (); - JERRY_ASSERT (mem_free_chunk_p == NULL); - - JERRY_ASSERT (MEM_POOL_SIZE <= mem_heap_get_chunked_block_data_size ()); - JERRY_ASSERT (MEM_POOL_CHUNKS_NUMBER >= 1); - - MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); - mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t *) mem_heap_alloc_chunked_block (MEM_HEAP_ALLOC_LONG_TERM); - - if (mem_free_chunk_p != NULL) - { - /* some chunks were freed due to GC invoked by heap allocator */ - MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); - mem_heap_free_block (pool_start_p); - - return; - } - -#ifndef JERRY_NDEBUG - mem_free_chunks_number += MEM_POOL_CHUNKS_NUMBER; -#endif /* !JERRY_NDEBUG */ - - JERRY_STATIC_ASSERT (MEM_POOL_CHUNK_SIZE % MEM_ALIGNMENT == 0, - MEM_POOL_CHUNK_SIZE_must_be_multiple_of_MEM_ALIGNMENT); - JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_index_t) <= MEM_POOL_CHUNK_SIZE, - size_of_mem_pool_chunk_index_t_must_be_less_than_or_equal_to_MEM_POOL_CHUNK_SIZE); - JERRY_ASSERT ((mem_pool_chunk_index_t) MEM_POOL_CHUNKS_NUMBER == MEM_POOL_CHUNKS_NUMBER); - JERRY_ASSERT (MEM_POOL_SIZE == MEM_POOL_CHUNKS_NUMBER * MEM_POOL_CHUNK_SIZE); - - JERRY_ASSERT (((uintptr_t) pool_start_p) % MEM_ALIGNMENT == 0); - - mem_pool_chunk_t *prev_free_chunk_p = NULL; - - for (mem_pool_chunk_index_t chunk_index = 0; - chunk_index < MEM_POOL_CHUNKS_NUMBER; - chunk_index++) - { - mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; - - if (prev_free_chunk_p != NULL) - { - prev_free_chunk_p->u.free.next_p = chunk_p; - } - - prev_free_chunk_p = chunk_p; - } - - prev_free_chunk_p->u.free.next_p = NULL; - -#ifdef JERRY_VALGRIND - for (mem_pool_chunk_index_t chunk_index = 0; - chunk_index < MEM_POOL_CHUNKS_NUMBER; - chunk_index++) - { - mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; - - VALGRIND_NOACCESS_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); - } -#endif /* JERRY_VALGRIND */ - - mem_free_chunk_p = pool_start_p; - - MEM_POOLS_STAT_ALLOC_POOL (); - - mem_check_pools (); -} /* mem_pools_alloc_longpath */ +} /* mem_pools_finalize */ /** * Allocate a chunk of specified size @@ -620,99 +132,69 @@ mem_pools_alloc_longpath (void) * @return pointer to allocated chunk, if allocation was successful, * or NULL - if not enough memory. */ -uint8_t *__attr_always_inline___ +void * __attribute__((hot)) __attr_always_inline___ mem_pools_alloc (void) { #ifdef MEM_GC_BEFORE_EACH_ALLOC mem_run_try_to_give_memory_back_callbacks (MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH); #endif /* MEM_GC_BEFORE_EACH_ALLOC */ - mem_check_pools (); - - do + if (mem_free_chunk_p != NULL) { - if (mem_free_chunk_p != NULL) - { - mem_pool_chunk_t *chunk_p = mem_free_chunk_p; + const mem_pools_chunk_t *const chunk_p = mem_free_chunk_p; - MEM_POOLS_STAT_ALLOC_CHUNK (); + MEM_POOLS_STAT_REUSE (); -#ifndef JERRY_NDEBUG - mem_free_chunks_number--; -#endif /* !JERRY_NDEBUG */ + VALGRIND_DEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); - VALGRIND_DEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); + mem_free_chunk_p = chunk_p->next_p; - mem_free_chunk_p = chunk_p->u.free.next_p; + VALGRIND_UNDEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); - VALGRIND_UNDEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); - - - mem_check_pools (); - - VALGRIND_FREYA_MALLOCLIKE_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); - return (uint8_t *) chunk_p; - } - else - { - mem_pools_alloc_longpath (); - - /* the assertion guarantees that there will be no more than two iterations */ - JERRY_ASSERT (mem_free_chunk_p != NULL); - } - } while (true); + return (void *) chunk_p; + } + else + { + MEM_POOLS_STAT_NEW_ALLOC (); + return (void *) mem_heap_alloc_block (MEM_POOL_CHUNK_SIZE); + } } /* mem_pools_alloc */ /** * Free the chunk */ -void __attr_always_inline___ -mem_pools_free (uint8_t *chunk_p) /**< pointer to the chunk */ +void __attribute__((hot)) +mem_pools_free (void *chunk_p) /**< pointer to the chunk */ { - mem_check_pools (); + mem_pools_chunk_t *const chunk_to_free_p = (mem_pools_chunk_t *) chunk_p; - mem_pool_chunk_t *chunk_to_free_p = (mem_pool_chunk_t *) chunk_p; + VALGRIND_DEFINED_SPACE (chunk_to_free_p, MEM_POOL_CHUNK_SIZE); - chunk_to_free_p->u.free.next_p = mem_free_chunk_p; + chunk_to_free_p->next_p = mem_free_chunk_p; mem_free_chunk_p = chunk_to_free_p; - VALGRIND_FREYA_FREELIKE_SPACE (chunk_to_free_p); VALGRIND_NOACCESS_SPACE (chunk_to_free_p, MEM_POOL_CHUNK_SIZE); -#ifndef JERRY_NDEBUG - mem_free_chunks_number++; -#endif /* !JERRY_NDEBUG */ - - MEM_POOLS_STAT_FREE_CHUNK (); - - mem_check_pools (); + MEM_POOLS_STAT_FREE_POOL (); } /* mem_pools_free */ /** - * Check correctness of pool allocator state + * Collect empty pool chunks */ -static void -mem_check_pools (void) +void +mem_pools_collect_empty () { -#ifndef JERRY_DISABLE_HEAVY_DEBUG - size_t free_chunks_met = 0; - - for (mem_pool_chunk_t *free_chunk_iter_p = mem_free_chunk_p, *next_free_chunk_p; - free_chunk_iter_p != NULL; - free_chunk_iter_p = next_free_chunk_p) + while (mem_free_chunk_p) { - VALGRIND_DEFINED_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); + VALGRIND_DEFINED_SPACE (mem_free_chunk_p, sizeof (mem_pools_chunk_t)); + mem_pools_chunk_t *const next_p = mem_free_chunk_p->next_p; + VALGRIND_NOACCESS_SPACE (mem_free_chunk_p, sizeof (mem_pools_chunk_t)); - next_free_chunk_p = free_chunk_iter_p->u.free.next_p; - - VALGRIND_NOACCESS_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); - - free_chunks_met++; + mem_heap_free_block (mem_free_chunk_p, MEM_POOL_CHUNK_SIZE); + MEM_POOLS_STAT_DEALLOC (); + mem_free_chunk_p = next_p; } - - JERRY_ASSERT (free_chunks_met == mem_free_chunks_number); -#endif /* !JERRY_DISABLE_HEAVY_DEBUG */ -} /* mem_check_pools */ +} /* mem_pools_collect_empty */ #ifdef MEM_STATS /** @@ -733,7 +215,6 @@ void mem_pools_stats_reset_peak (void) { mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count; - mem_pools_stats.peak_allocated_chunks = mem_pools_stats.allocated_chunks; } /* mem_pools_stats_reset_peak */ /** @@ -746,12 +227,13 @@ mem_pools_stat_init (void) } /* mem_pools_stat_init */ /** - * Account allocation of a pool + * Account for allocation of new pool chunk */ static void -mem_pools_stat_alloc_pool (void) +mem_pools_stat_new_alloc (void) { mem_pools_stats.pools_count++; + mem_pools_stats.new_alloc_count++; if (mem_pools_stats.pools_count > mem_pools_stats.peak_pools_count) { @@ -761,57 +243,50 @@ mem_pools_stat_alloc_pool (void) { mem_pools_stats.global_peak_pools_count = mem_pools_stats.pools_count; } +} /* mem_pools_stat_new_alloc */ - mem_pools_stats.free_chunks += MEM_POOL_CHUNKS_NUMBER; -} /* mem_pools_stat_alloc_pool */ /** - * Account freeing of a pool + * Account for reuse of pool chunk + */ +static void +mem_pools_stat_reuse (void) +{ + mem_pools_stats.pools_count++; + mem_pools_stats.free_chunks--; + mem_pools_stats.reused_count++; + + if (mem_pools_stats.pools_count > mem_pools_stats.peak_pools_count) + { + mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count; + } + if (mem_pools_stats.pools_count > mem_pools_stats.global_peak_pools_count) + { + mem_pools_stats.global_peak_pools_count = mem_pools_stats.pools_count; + } +} /* mem_pools_stat_reuse */ + + +/** + * Account for freeing a chunk */ static void mem_pools_stat_free_pool (void) { - JERRY_ASSERT (mem_pools_stats.free_chunks >= MEM_POOL_CHUNKS_NUMBER); - - mem_pools_stats.free_chunks -= MEM_POOL_CHUNKS_NUMBER; - JERRY_ASSERT (mem_pools_stats.pools_count > 0); mem_pools_stats.pools_count--; + mem_pools_stats.free_chunks++; } /* mem_pools_stat_free_pool */ /** - * Account allocation of chunk in a pool + * Account for freeing a chunk */ static void -mem_pools_stat_alloc_chunk (void) +mem_pools_stat_dealloc (void) { - JERRY_ASSERT (mem_pools_stats.free_chunks > 0); - - mem_pools_stats.allocated_chunks++; mem_pools_stats.free_chunks--; - - if (mem_pools_stats.allocated_chunks > mem_pools_stats.peak_allocated_chunks) - { - mem_pools_stats.peak_allocated_chunks = mem_pools_stats.allocated_chunks; - } - if (mem_pools_stats.allocated_chunks > mem_pools_stats.global_peak_allocated_chunks) - { - mem_pools_stats.global_peak_allocated_chunks = mem_pools_stats.allocated_chunks; - } -} /* mem_pools_stat_alloc_chunk */ - -/** - * Account freeing of chunk in a pool - */ -static void -mem_pools_stat_free_chunk (void) -{ - JERRY_ASSERT (mem_pools_stats.allocated_chunks > 0); - - mem_pools_stats.allocated_chunks--; - mem_pools_stats.free_chunks++; -} /* mem_pools_stat_free_chunk */ +} /* mem_pools_stat_dealloc */ #endif /* MEM_STATS */ /** diff --git a/jerry-core/mem/mem-poolman.h b/jerry-core/mem/mem-poolman.h index 3f762fe07..df4d950ed 100644 --- a/jerry-core/mem/mem-poolman.h +++ b/jerry-core/mem/mem-poolman.h @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -31,8 +32,8 @@ extern void mem_pools_init (void); extern void mem_pools_finalize (void); -extern uint8_t *mem_pools_alloc (void); -extern void mem_pools_free (uint8_t *); +extern void *mem_pools_alloc (void); +extern void mem_pools_free (void *); extern void mem_pools_collect_empty (void); #ifdef MEM_STATS @@ -50,17 +51,14 @@ typedef struct /** non-resettable peak pools' count */ size_t global_peak_pools_count; - /** allocated chunks count */ - size_t allocated_chunks; - - /** peak allocated chunks count */ - size_t peak_allocated_chunks; - - /** non-resettable peak allocated chunks count */ - size_t global_peak_allocated_chunks; - /** free chunks count */ size_t free_chunks; + + /* Number of newly allocated pool chunks */ + size_t new_alloc_count; + + /* Number of reused pool chunks */ + size_t reused_count; } mem_pools_stats_t; extern void mem_pools_get_stats (mem_pools_stats_t *); diff --git a/jerry-core/parser/js/common.h b/jerry-core/parser/js/common.h index 471d3dd89..51df8a8c2 100644 --- a/jerry-core/parser/js/common.h +++ b/jerry-core/parser/js/common.h @@ -52,11 +52,11 @@ /* Malloc functions. */ -#define PARSER_MALLOC(size) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM) -#define PARSER_FREE(ptr) mem_heap_free_block ((void *) ptr) +#define PARSER_MALLOC(size) mem_heap_alloc_block_store_size (size) +#define PARSER_FREE(ptr) mem_heap_free_block_size_stored ((void *) ptr) -#define PARSER_MALLOC_LOCAL(size) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM) -#define PARSER_FREE_LOCAL(ptr) mem_heap_free_block (ptr) +#define PARSER_MALLOC_LOCAL(size) mem_heap_alloc_block_store_size (size) +#define PARSER_FREE_LOCAL(ptr) mem_heap_free_block_size_stored (ptr) /* UTF character management. Only ASCII characters are * supported for simplicity. */ diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 1315214f6..fc0e7cc41 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -1566,13 +1566,13 @@ lexer_construct_number_object (parser_context_t *context_p, /**< context */ num = -num; } - lit_cp = rcs_cpointer_compress (lit_find_or_create_literal_from_num (num)); + lit_cp = lit_cpointer_compress (lit_find_or_create_literal_from_num (num)); parser_list_iterator_init (&context_p->literal_pool, &literal_iterator); while ((literal_p = (lexer_literal_t *) parser_list_iterator_next (&literal_iterator)) != NULL) { if (literal_p->type == LEXER_NUMBER_LITERAL - && literal_p->u.value.u.value.base_cp == lit_cp.u.value.base_cp) + && literal_p->u.value == lit_cp) { context_p->lit_object.literal_p = literal_p; context_p->lit_object.index = (uint16_t) literal_index; diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 4ee1221dd..3f4222b3b 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -18,6 +18,7 @@ #include "jerry-snapshot.h" #include "js-parser-internal.h" #include "lit-literal.h" +#include "lit-cpointer.h" #ifdef PARSER_DUMP_BYTE_CODE static int parser_show_instrs = PARSER_FALSE; @@ -100,7 +101,7 @@ parser_compute_indicies (parser_context_t *context_p, /**< context */ { lit_literal_t lit = lit_find_or_create_literal_from_utf8_string (char_p, literal_p->prop.length); - literal_p->u.value = rcs_cpointer_compress (lit); + literal_p->u.value = lit_cpointer_compress (lit); if (!(literal_p->status_flags & LEXER_FLAG_SOURCE_PTR)) { @@ -521,7 +522,7 @@ parser_generate_initializers (parser_context_t *context_p, /**< context */ #ifdef PARSER_DUMP_BYTE_CODE lit_literal_t lit = lit_find_or_create_literal_from_utf8_string (literal_p->u.char_p, literal_p->prop.length); - literal_pool_p[literal_p->prop.index] = rcs_cpointer_compress (lit); + literal_pool_p[literal_p->prop.index] = lit_cpointer_compress (lit); if (!context_p->is_show_opcodes && !(literal_p->status_flags & LEXER_FLAG_SOURCE_PTR)) @@ -535,7 +536,7 @@ parser_generate_initializers (parser_context_t *context_p, /**< context */ else if ((literal_p->type == LEXER_FUNCTION_LITERAL) || (literal_p->type == LEXER_REGEXP_LITERAL)) { - ECMA_SET_NON_NULL_POINTER (literal_pool_p[literal_p->prop.index].u.value.base_cp, + ECMA_SET_NON_NULL_POINTER (literal_pool_p[literal_p->prop.index], literal_p->u.bytecode_p); } else @@ -579,7 +580,7 @@ parser_generate_initializers (parser_context_t *context_p, /**< context */ JERRY_ASSERT (literal_p != NULL && literal_p->type == LEXER_FUNCTION_LITERAL); init_index = literal_p->prop.index; - ECMA_SET_NON_NULL_POINTER (literal_pool_p[literal_p->prop.index].u.value.base_cp, + ECMA_SET_NON_NULL_POINTER (literal_pool_p[literal_p->prop.index], literal_p->u.bytecode_p); } @@ -1723,7 +1724,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */ const uint8_t *char_p = context_p->source_end_p - (source_data & 0xfffff); lit_literal_t lit = lit_find_or_create_literal_from_utf8_string (char_p, source_data >> 20); - literal_pool_p[literal_p->prop.index] = rcs_cpointer_compress (lit); + literal_pool_p[literal_p->prop.index] = lit_cpointer_compress (lit); } } } @@ -1753,7 +1754,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */ { if (literal_p->u.char_p == NULL) { - literal_pool_p[argument_count] = rcs_cpointer_null_cp (); + literal_pool_p[argument_count] = lit_cpointer_null_cp (); argument_count++; continue; } @@ -1775,7 +1776,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */ if (context_p->status_flags & PARSER_NAMED_FUNCTION_EXP) { - ECMA_SET_NON_NULL_POINTER (literal_pool_p[const_literal_end].u.value.base_cp, + ECMA_SET_NON_NULL_POINTER (literal_pool_p[const_literal_end], compiled_code_p); } diff --git a/jerry-core/parser/regexp/re-compiler.c b/jerry-core/parser/regexp/re-compiler.c index ab9d2edee..e3ec8fcc8 100644 --- a/jerry-core/parser/regexp/re-compiler.c +++ b/jerry-core/parser/regexp/re-compiler.c @@ -72,12 +72,12 @@ re_realloc_regexp_bytecode_block (re_bytecode_ctx_t *bc_ctx_p) /**< RegExp bytec JERRY_ASSERT (bc_ctx_p->current_p >= bc_ctx_p->block_start_p); size_t current_ptr_offset = (size_t) (bc_ctx_p->current_p - bc_ctx_p->block_start_p); - uint8_t *new_block_start_p = (uint8_t *) mem_heap_alloc_block (new_block_size, - MEM_HEAP_ALLOC_SHORT_TERM); + uint8_t *new_block_start_p = (uint8_t *) mem_heap_alloc_block_store_size (new_block_size); + if (bc_ctx_p->current_p) { memcpy (new_block_start_p, bc_ctx_p->block_start_p, (size_t) (current_ptr_offset)); - mem_heap_free_block (bc_ctx_p->block_start_p); + mem_heap_free_block_size_stored (bc_ctx_p->block_start_p); } bc_ctx_p->block_start_p = new_block_start_p; bc_ctx_p->block_end_p = new_block_start_p + new_block_size; @@ -128,11 +128,11 @@ re_bytecode_list_insert (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode conte { uint8_t *dest_p = src_p + length; uint8_t *tmp_block_start_p; - tmp_block_start_p = (uint8_t *) mem_heap_alloc_block ((re_get_bytecode_length (bc_ctx_p) - offset), - MEM_HEAP_ALLOC_SHORT_TERM); + tmp_block_start_p = (uint8_t *) mem_heap_alloc_block_store_size (re_get_bytecode_length (bc_ctx_p) - offset); + memcpy (tmp_block_start_p, src_p, (size_t) (re_get_bytecode_length (bc_ctx_p) - offset)); memcpy (dest_p, tmp_block_start_p, (size_t) (re_get_bytecode_length (bc_ctx_p) - offset)); - mem_heap_free_block (tmp_block_start_p); + mem_heap_free_block_size_stored (tmp_block_start_p); } memcpy (src_p, bytecode_p, length); @@ -698,7 +698,7 @@ re_compile_bytecode (re_compiled_code_t **out_bytecode_p, /**< out:pointer to by if (!ecma_is_value_empty (ret_value)) { /* Compilation failed, free bytecode. */ - mem_heap_free_block (bc_ctx.block_start_p); + mem_heap_free_block_size_stored (bc_ctx.block_start_p); *out_bytecode_p = NULL; } else diff --git a/jerry-core/rcs/rcs-allocator.c b/jerry-core/rcs/rcs-allocator.c deleted file mode 100644 index 16664b56e..000000000 --- a/jerry-core/rcs/rcs-allocator.c +++ /dev/null @@ -1,395 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 "rcs-allocator.h" -#include "rcs-records.h" - -/** - * Assert that recordset state is correct. - */ -static void -rcs_assert_state_is_correct (rcs_record_set_t *rec_set_p __attr_unused___) /**< recordset */ -{ -#ifndef JERRY_DISABLE_HEAVY_DEBUG - size_t node_size_sum = 0; - size_t record_size_sum = 0; - - rcs_record_t *last_record_p = NULL; - rcs_record_t *rec_p; - rcs_record_t *next_rec_p; - - for (rec_p = rcs_record_get_first (rec_set_p); - rec_p != NULL; - last_record_p = rec_p, rec_p = next_rec_p) - { - JERRY_ASSERT (rcs_record_get_size (rec_p) > 0); - record_size_sum += rcs_record_get_size (rec_p); - - rcs_chunked_list_node_t *node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, rec_p); - next_rec_p = rcs_record_get_next (rec_set_p, rec_p); - rcs_chunked_list_node_t *next_node_p = NULL; - - if (next_rec_p != NULL) - { - next_node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, next_rec_p); - } - - while (node_p != next_node_p) - { - node_p = rcs_chunked_list_get_next (node_p); - node_size_sum += rcs_get_node_data_space_size (); - } - } - - JERRY_ASSERT (node_size_sum == record_size_sum); - - record_size_sum = 0; - - for (rec_p = last_record_p; - rec_p != NULL; - rec_p = rcs_record_get_prev (rec_set_p, rec_p)) - { - record_size_sum += rcs_record_get_size (rec_p); - } - - JERRY_ASSERT (node_size_sum == record_size_sum); -#endif /* !JERRY_DISABLE_HEAVY_DEBUG */ -} /* rcs_assert_state_is_correct */ - -/** - * Initialize specified record as free record. - */ -static void -rcs_init_free_record (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *rec_p, /**< record to init as free record */ - rcs_record_t *prev_rec_p, /**< previous record (or NULL) */ - size_t size) /**< size, including header */ -{ - rcs_record_set_type (rec_p, RCS_RECORD_TYPE_FREE); - rcs_record_set_prev (rec_set_p, rec_p, prev_rec_p); - rcs_record_set_size (rec_p, size); -} /* rcs_init_free_record */ - -/** - * Check the alignment of the record. - */ -void -rcs_check_record_alignment (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (rec_p != NULL); - - uintptr_t ptr = (uintptr_t) rec_p; - - JERRY_ASSERT (JERRY_ALIGNUP (ptr, RCS_DYN_STORAGE_LENGTH_UNIT) == ptr); -} /* rcs_check_record_alignment */ - -/** - * Get size of a node's data space. - * - * @return size - */ -size_t -rcs_get_node_data_space_size (void) -{ - return JERRY_ALIGNDOWN (rcs_chunked_list_get_node_data_space_size (), RCS_DYN_STORAGE_LENGTH_UNIT); -} /* rcs_get_node_data_space_size */ - -/** - * Get the node's data space. - * - * @return pointer to beginning of the node's data space - */ -uint8_t * -rcs_get_node_data_space (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_chunked_list_node_t *node_p) /**< the node */ -{ - uintptr_t unaligned_data_space_start = (uintptr_t) rcs_chunked_list_get_node_data_space (rec_set_p, node_p); - uintptr_t aligned_data_space_start = JERRY_ALIGNUP (unaligned_data_space_start, RCS_DYN_STORAGE_LENGTH_UNIT); - - JERRY_ASSERT (unaligned_data_space_start + rcs_chunked_list_get_node_data_space_size () - == aligned_data_space_start + rcs_chunked_list_get_node_data_space_size ()); - - return (uint8_t *) aligned_data_space_start; -} /* rcs_get_node_data_space */ - -/** - * Initialize record in specified place, and, if there is free space - * before next record, initialize free record for the space. - */ -static void -rcs_alloc_record_in_place (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *place_p, /**< where to initialize the record */ - rcs_record_t *next_record_p, /**< next allocated record */ - size_t free_size) /**< size of free part between the allocated record - and the next allocated record */ -{ - const size_t node_data_space_size = rcs_get_node_data_space_size (); - - if (next_record_p != NULL) - { - if (free_size == 0) - { - rcs_record_set_prev (rec_set_p, next_record_p, place_p); - } - else - { - rcs_chunked_list_node_t *node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, next_record_p); - uint8_t *node_data_space_p = rcs_get_node_data_space (rec_set_p, node_p); - - JERRY_ASSERT ((uint8_t *) next_record_p < node_data_space_p + node_data_space_size); - - rcs_record_t *free_rec_p; - - if ((uint8_t *) next_record_p >= node_data_space_p + free_size) - { - free_rec_p = (rcs_record_t *) ((uint8_t *) next_record_p - free_size); - } - else - { - size_t size_passed_back = (size_t) ((uint8_t *) next_record_p - node_data_space_p); - JERRY_ASSERT (size_passed_back < free_size && size_passed_back + node_data_space_size > free_size); - - node_p = rcs_chunked_list_get_prev (node_p); - node_data_space_p = rcs_get_node_data_space (rec_set_p, node_p); - - free_rec_p = (rcs_record_t *) (node_data_space_p + node_data_space_size - \ - (free_size - size_passed_back)); - } - - rcs_init_free_record (rec_set_p, free_rec_p, place_p, free_size); - } - } - else if (free_size != 0) - { - rcs_chunked_list_node_t *node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, place_p); - JERRY_ASSERT (node_p != NULL); - - rcs_chunked_list_node_t *next_node_p = rcs_chunked_list_get_next (node_p); - - while (next_node_p != NULL) - { - node_p = next_node_p; - next_node_p = rcs_chunked_list_get_next (node_p); - } - - uint8_t *node_data_space_p = rcs_get_node_data_space (rec_set_p, node_p); - const size_t node_data_space_size = rcs_get_node_data_space_size (); - - rcs_record_t *free_rec_p = (rcs_record_t *) (node_data_space_p + node_data_space_size - free_size); - rcs_init_free_record (rec_set_p, free_rec_p, place_p, free_size); - } -} /* rcs_alloc_record_in_place */ - -/** - * Allocate record of specified size. - * - * @return record identifier - */ -static rcs_record_t * -rcs_alloc_space_for_record (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t **out_prev_rec_p, /**< out: pointer to record, previous to the allocated, - * or NULL if the allocated record is the first */ - size_t bytes) /**< size */ -{ - rcs_assert_state_is_correct (rec_set_p); - - JERRY_ASSERT (JERRY_ALIGNUP (bytes, RCS_DYN_STORAGE_LENGTH_UNIT) == bytes); - JERRY_ASSERT (out_prev_rec_p != NULL); - - rcs_record_t *rec_p = NULL; - *out_prev_rec_p = NULL; - - const size_t node_data_space_size = rcs_get_node_data_space_size (); - - for (rec_p = rcs_record_get_first (rec_set_p); - rec_p != NULL; - *out_prev_rec_p = rec_p, rec_p = rcs_record_get_next (rec_set_p, rec_p)) - { - if (RCS_RECORD_IS_FREE (rec_p)) - { - rcs_record_t *next_rec_p = rcs_record_get_next (rec_set_p, rec_p); - size_t record_size = rcs_record_get_size (rec_p); - - if (record_size >= bytes) - { - /* Record size is sufficient. */ - rcs_alloc_record_in_place (rec_set_p, rec_p, next_rec_p, record_size - bytes); - return rec_p; - } - - rcs_chunked_list_node_t *node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, rec_p); - uint8_t *node_data_space_p = rcs_get_node_data_space (rec_set_p, node_p); - uint8_t *node_data_space_end_p = node_data_space_p + node_data_space_size; - uint8_t *rec_space_p = (uint8_t *) rec_p; - - if (rec_space_p + record_size >= node_data_space_end_p) - { - /* Record lies up to end of node's data space size, - * thus it can be extended up to necessary size. */ - while (record_size < bytes) - { - node_p = rcs_chunked_list_insert_new (rec_set_p, node_p); - record_size += node_data_space_size; - } - - rcs_alloc_record_in_place (rec_set_p, rec_p, next_rec_p, record_size - bytes); - return rec_p; - } - - if (next_rec_p == NULL) - { - /* There are no more records in the storage, - * so we should append a new record. */ - break; - } - - JERRY_ASSERT (!RCS_RECORD_IS_FREE (rec_p)); - } - } - - /* Free record of sufficient size was not found. */ - rcs_chunked_list_node_t *node_p = rcs_chunked_list_append_new (rec_set_p); - rcs_record_t *new_rec_p = (rcs_record_t *) rcs_get_node_data_space (rec_set_p, node_p); - - size_t allocated_size = node_data_space_size; - - while (allocated_size < bytes) - { - allocated_size += node_data_space_size; - rcs_chunked_list_append_new (rec_set_p); - } - - rcs_alloc_record_in_place (rec_set_p, new_rec_p, NULL, allocated_size - bytes); - - return new_rec_p; -} /* rcs_alloc_space_for_record */ - -/** - * Allocate and initialize a new record. - * - * @return pointer to the new record - */ -rcs_record_t * -rcs_alloc_record (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_type_t type, /**< type for the new record */ - size_t size) /**< allocation size */ -{ - JERRY_ASSERT (type >= RCS_RECORD_TYPE_FIRST && type <= RCS_RECORD_TYPE_LAST); - - rcs_record_t *prev_rec_p; - rcs_record_t *rec_p = (rcs_record_t *) rcs_alloc_space_for_record (rec_set_p, &prev_rec_p, size); - - rcs_record_set_type (rec_p, type); - rcs_record_set_size (rec_p, size); - rcs_record_set_prev (rec_set_p, rec_p, prev_rec_p); - - rcs_assert_state_is_correct (rec_set_p); - - return rec_p; -} /* rcs_alloc_record */ - -/** - * Free the specified record. - */ -void -rcs_free_record (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *record_p) /**< record to free */ -{ - JERRY_ASSERT (record_p != NULL); - - rcs_assert_state_is_correct (rec_set_p); - - rcs_record_t *prev_rec_p = rcs_record_get_prev (rec_set_p, record_p); - - rcs_init_free_record (rec_set_p, record_p, prev_rec_p, rcs_record_get_size (record_p)); - - /* Merge adjacent free records, if there are any, - * and free nodes of chunked list that became unused. */ - rcs_record_t *rec_from_p = record_p; - rcs_record_t *rec_to_p = rcs_record_get_next (rec_set_p, record_p); - - if (prev_rec_p != NULL && RCS_RECORD_IS_FREE (prev_rec_p)) - { - rec_from_p = prev_rec_p; - prev_rec_p = rcs_record_get_prev (rec_set_p, rec_from_p); - } - - if (rec_to_p != NULL && RCS_RECORD_IS_FREE (rec_to_p)) - { - rec_to_p = rcs_record_get_next (rec_set_p, rec_to_p); - } - - JERRY_ASSERT (rec_from_p != NULL && RCS_RECORD_IS_FREE (rec_from_p)); - JERRY_ASSERT (rec_to_p == NULL || !RCS_RECORD_IS_FREE (rec_to_p)); - - rcs_chunked_list_node_t *node_from_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, rec_from_p); - rcs_chunked_list_node_t *node_to_p = NULL; - - if (rec_to_p != NULL) - { - node_to_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, rec_to_p); - } - - const size_t node_data_space_size = rcs_get_node_data_space_size (); - - uint8_t *rec_from_beg_p = (uint8_t *) rec_from_p; - uint8_t *rec_to_beg_p = (uint8_t *) rec_to_p; - size_t free_size; - - if (node_from_p == node_to_p) - { - JERRY_ASSERT (rec_from_beg_p + rcs_record_get_size (rec_from_p) <= rec_to_beg_p); - free_size = (size_t) (rec_to_beg_p - rec_from_beg_p); - } - else - { - rcs_chunked_list_node_t *iter_node_p; - rcs_chunked_list_node_t *iter_next_node_p; - - for (iter_node_p = rcs_chunked_list_get_next (node_from_p); - iter_node_p != node_to_p; - iter_node_p = iter_next_node_p) - { - iter_next_node_p = rcs_chunked_list_get_next (iter_node_p); - rcs_chunked_list_remove (rec_set_p, iter_node_p); - } - - JERRY_ASSERT (rcs_chunked_list_get_next (node_from_p) == node_to_p); - - size_t node_from_space = (size_t) (rcs_get_node_data_space (rec_set_p, node_from_p) \ - + node_data_space_size - rec_from_beg_p); - size_t node_to_space = (size_t) (node_to_p != NULL ? \ - (rec_to_beg_p - rcs_get_node_data_space (rec_set_p, node_to_p)) : 0); - - free_size = node_from_space + node_to_space; - } - - rcs_init_free_record (rec_set_p, rec_from_p, prev_rec_p, free_size); - - if (rec_to_p != NULL) - { - rcs_record_set_prev (rec_set_p, rec_to_p, rec_from_p); - } - else if (prev_rec_p == NULL) - { - rcs_chunked_list_remove (rec_set_p, node_from_p); - - JERRY_ASSERT (node_to_p == NULL); - JERRY_ASSERT (rcs_chunked_list_get_first (rec_set_p) == NULL); - } - - rcs_assert_state_is_correct (rec_set_p); -} /* rcs_free_record */ diff --git a/jerry-core/rcs/rcs-allocator.h b/jerry-core/rcs/rcs-allocator.h deleted file mode 100644 index 4d58d3cb3..000000000 --- a/jerry-core/rcs/rcs-allocator.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 RCS_ALLOCATOR_H -#define RCS_ALLOCATOR_H - -#include "rcs-globals.h" - -extern void rcs_check_record_alignment (rcs_record_t *); -extern void rcs_free_record (rcs_record_set_t *, rcs_record_t *); -extern size_t rcs_get_node_data_space_size (void); -extern uint8_t *rcs_get_node_data_space (rcs_record_set_t *, rcs_chunked_list_node_t *); -extern rcs_record_t *rcs_alloc_record (rcs_record_set_t *, rcs_record_type_t, size_t); - -#endif /* !RCS_ALLOCATOR_H */ diff --git a/jerry-core/rcs/rcs-chunked-list.c b/jerry-core/rcs/rcs-chunked-list.c deleted file mode 100644 index 2ad6b2dad..000000000 --- a/jerry-core/rcs/rcs-chunked-list.c +++ /dev/null @@ -1,340 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * - * 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 "rcs-chunked-list.h" - - - -/** - * Set previous node for the specified node - */ -static void -rcs_chunked_list_set_prev (rcs_chunked_list_node_t *node_p, /**< node to set previous for */ - rcs_chunked_list_node_t *prev_node_p) /**< the previous node */ -{ - JERRY_ASSERT (node_p != NULL); - - MEM_CP_SET_POINTER (node_p->prev_cp, prev_node_p); -} /* rcs_chunked_list_set_prev */ - -/** - * Set next node for the specified node - */ -static void -rcs_chunked_list_set_next (rcs_chunked_list_node_t *node_p, /**< node to set next for */ - rcs_chunked_list_node_t *next_node_p) /**< the next node */ -{ - JERRY_ASSERT (node_p != NULL); - - MEM_CP_SET_POINTER (node_p->next_cp, next_node_p); -} /* rcs_chunked_list_set_next */ - -/** - * Get size of the node - * - * @return size of node, including header and data space - */ -static size_t -rcs_chunked_list_get_node_size (void) -{ - size_t size = mem_heap_recommend_allocation_size (sizeof (rcs_chunked_list_node_t) + 1u); - - JERRY_ASSERT (size != 0 && size >= sizeof (rcs_chunked_list_node_t)); - - return size; -} /* rcs_chunked_list_get_node_size */ - -/** - * Assert that the list state is correct - */ -static void -rcs_assert_chunked_list_is_correct (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ -#ifndef JERRY_DISABLE_HEAVY_DEBUG - for (rcs_chunked_list_node_t *node_iter_p = rcs_chunked_list_get_first (cl_p); - node_iter_p != NULL; - node_iter_p = rcs_chunked_list_get_next (node_iter_p)) - { - rcs_chunked_list_node_t *prev_node_p = rcs_chunked_list_get_prev (node_iter_p); - rcs_chunked_list_node_t *next_node_p = rcs_chunked_list_get_next (node_iter_p); - - JERRY_ASSERT ((node_iter_p == cl_p->head_p - && prev_node_p == NULL) - || (node_iter_p != cl_p->head_p - && prev_node_p != NULL - && rcs_chunked_list_get_next (prev_node_p) == node_iter_p)); - JERRY_ASSERT ((node_iter_p == cl_p->tail_p - && next_node_p == NULL) - || (node_iter_p != cl_p->tail_p - && next_node_p != NULL - && rcs_chunked_list_get_prev (next_node_p) == node_iter_p)); - } -#else - (void) cl_p; -#endif /* !JERRY_DISABLE_HEAVY_DEBUG */ -} /* rcs_assert_chunked_list_is_correct */ - -/** - * Assert that state of specified node is correct - */ -static void -rcs_assert_chunked_list_node_is_correct (rcs_chunked_list_t *cl_p, /**< the chunked_list */ - rcs_chunked_list_node_t *node_p) /**< the node */ -{ -#ifndef JERRY_DISABLE_HEAVY_DEBUG - JERRY_ASSERT (node_p != NULL); - - rcs_assert_chunked_list_is_correct (cl_p); - - bool is_in_list = false; - for (rcs_chunked_list_node_t *node_iter_p = rcs_chunked_list_get_first (cl_p); - node_iter_p != NULL; - node_iter_p = rcs_chunked_list_get_next (node_iter_p)) - { - if (node_iter_p == node_p) - { - is_in_list = true; - - break; - } - } - - JERRY_ASSERT (is_in_list); -#else /* !JERRY_DISABLE_HEAVY_DEBUG */ - (void) node_p; - (void) cl_p; -#endif /* JERRY_DISABLE_HEAVY_DEBUG */ -} /* rcs_assert_chunked_list_node_is_correct */ - - -/** - * Initializarion - */ -void -rcs_chunked_list_init (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - cl_p->head_p = NULL; - cl_p->tail_p = NULL; -} /* rcs_chunked_list_init */ - -/** - * Free - */ -void -rcs_chunked_list_free (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - JERRY_ASSERT (cl_p->head_p == NULL); - JERRY_ASSERT (cl_p->tail_p == NULL); -} /* rcs_chunked_list_free */ - -void -rcs_chunked_list_cleanup (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - while (cl_p->head_p) - { - rcs_chunked_list_remove (cl_p, cl_p->head_p); - } -} /* rcs_chunked_list_cleanup */ - - - -/** - * Get first node of the list - * - * @return pointer to the first node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_get_first (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - return cl_p->head_p; -} /* rcs_chunked_list_get_first */ - - -/** - * Get last node of the list - * - * @return pointer to the last node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_get_last (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - return cl_p->tail_p; -} /* rcs_chunked_list_get_last */ - -/** - * Get node, previous to specified - * - * @return pointer to previous node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_get_prev (rcs_chunked_list_node_t *node_p) /**< the node in the chunked_list */ -{ - JERRY_ASSERT (node_p != NULL); - return MEM_CP_GET_POINTER (rcs_chunked_list_node_t, node_p->prev_cp); -} /* rcs_chunked_list_get_prev */ - - -/** - * Get node, next to specified - * - * @return pointer to next node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_get_next (rcs_chunked_list_node_t *node_p) /**< the node in the chunked_list */ -{ - JERRY_ASSERT (node_p != NULL); - return MEM_CP_GET_POINTER (rcs_chunked_list_node_t, node_p->next_cp); -} /* rcs_chunked_list_get_next */ - -/** - * Append new node to end of the list - * - * @return pointer to the new node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_append_new (rcs_chunked_list_t *cl_p) /**< the chunked_list */ -{ - rcs_assert_chunked_list_is_correct (cl_p); - rcs_chunked_list_node_t *node_p = - (rcs_chunked_list_node_t *) mem_heap_alloc_chunked_block (MEM_HEAP_ALLOC_LONG_TERM); - rcs_chunked_list_set_prev (node_p, cl_p->tail_p); - rcs_chunked_list_set_next (node_p, NULL); - - if (cl_p->head_p == NULL) - { - JERRY_ASSERT (cl_p->tail_p == NULL); - cl_p->head_p = node_p; - cl_p->tail_p = node_p; - } - else - { - JERRY_ASSERT (cl_p->tail_p != NULL); - rcs_chunked_list_set_next (cl_p->tail_p, node_p); - cl_p->tail_p = node_p; - } - rcs_assert_chunked_list_node_is_correct (cl_p, node_p); - - return node_p; -} /* rcs_chunked_list_append_new */ - -/** - * Insert new node after the specified node - * - * @return pointer to the new node - */ -rcs_chunked_list_node_t * -rcs_chunked_list_insert_new (rcs_chunked_list_t *cl_p, /**< the chunked_list */ - rcs_chunked_list_node_t *after_p) /**< the node to insert the new node after */ -{ - rcs_assert_chunked_list_is_correct (cl_p); - rcs_chunked_list_node_t *node_p = - (rcs_chunked_list_node_t *) mem_heap_alloc_chunked_block (MEM_HEAP_ALLOC_LONG_TERM); - JERRY_ASSERT (cl_p->head_p != NULL); - JERRY_ASSERT (cl_p->tail_p != NULL); - rcs_assert_chunked_list_node_is_correct (cl_p, after_p); - - rcs_chunked_list_set_next (after_p, node_p); - if (cl_p->tail_p == after_p) - { - cl_p->tail_p = node_p; - } - rcs_chunked_list_set_prev (node_p, after_p); - rcs_chunked_list_set_next (node_p, NULL); - - rcs_assert_chunked_list_node_is_correct (cl_p, node_p); - return node_p; -} /* rcs_chunked_list_insert_new */ - -/** - * Remove specified node - */ -void -rcs_chunked_list_remove (rcs_chunked_list_t *cl_p, /**< the chunked_list */ - rcs_chunked_list_node_t *node_p) /**< the node to remove */ -{ - JERRY_ASSERT (cl_p->head_p != NULL); - JERRY_ASSERT (cl_p->tail_p != NULL); - - rcs_assert_chunked_list_node_is_correct (cl_p, node_p); - rcs_chunked_list_node_t *prev_node_p, *next_node_p; - prev_node_p = rcs_chunked_list_get_prev (node_p); - next_node_p = rcs_chunked_list_get_next (node_p); - - if (prev_node_p == NULL) - { - JERRY_ASSERT (cl_p->head_p == node_p); - cl_p->head_p = next_node_p; - } - else - { - rcs_chunked_list_set_next (prev_node_p, next_node_p); - } - - if (next_node_p == NULL) - { - JERRY_ASSERT (cl_p->tail_p == node_p); - cl_p->tail_p = prev_node_p; - } - else - { - rcs_chunked_list_set_prev (next_node_p, prev_node_p); - } - - mem_heap_free_block (node_p); - - rcs_assert_chunked_list_is_correct (cl_p); -} /* rcs_chunked_list_remove */ - -/** - * Find node containing space, pointed by specified pointer - * - * @return pointer to the node that contains the pointed area - */ -rcs_chunked_list_node_t * -rcs_chunked_list_get_node_from_pointer (rcs_chunked_list_t *cl_p, /**< the chunked_list */ - void *ptr) /**< the pointer value */ -{ - rcs_chunked_list_node_t *node_p = (rcs_chunked_list_node_t *) mem_heap_get_chunked_block_start (ptr); - - rcs_assert_chunked_list_node_is_correct (cl_p, node_p); - - return node_p; -} /* rcs_chunked_list_get_node_from_pointer */ - -/** - * Get the node's data space - * - * @return pointer to beginning of the node's data space - */ -uint8_t * -rcs_chunked_list_get_node_data_space (rcs_chunked_list_t *cl_p, /**< the chunked_list */ - rcs_chunked_list_node_t *node_p) /**< the node */ -{ - rcs_assert_chunked_list_node_is_correct (cl_p, node_p); - - return (uint8_t *) (node_p + 1); -} /* rcs_chunked_list_get_node_data_space */ - -/** - * Get size of a node's data space - * - * @return size - */ -size_t -rcs_chunked_list_get_node_data_space_size (void) -{ - return rcs_chunked_list_get_node_size () - sizeof (rcs_chunked_list_node_t); -} /* rcs_chunked_list_get_node_data_space_size */ - diff --git a/jerry-core/rcs/rcs-chunked-list.h b/jerry-core/rcs/rcs-chunked-list.h deleted file mode 100644 index 33e8ea223..000000000 --- a/jerry-core/rcs/rcs-chunked-list.h +++ /dev/null @@ -1,68 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * - * 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 RCS_CHUNKED_LIST_H -#define RCS_CHUNKED_LIST_H - -#include "mem-allocator.h" - -/** \addtogroup recordset Recordset - * @{ - * - * \addtogroup chunkedlist Chunked list - * - * List of nodes with size exactly fit to one memory heap's chunk. - * - * @{ - */ - -/** - * List node - */ -typedef struct -{ - mem_cpointer_t prev_cp; /**< prev list's node */ - mem_cpointer_t next_cp; /**< next list's node */ -} rcs_chunked_list_node_t; - -/** - * Chunked list - */ -typedef struct -{ - rcs_chunked_list_node_t *head_p; /**< head node of list */ - rcs_chunked_list_node_t *tail_p; /**< tail node of list */ -} rcs_chunked_list_t; - -extern void rcs_chunked_list_init (rcs_chunked_list_t *); -extern void rcs_chunked_list_free (rcs_chunked_list_t *); -extern void rcs_chunked_list_cleanup (rcs_chunked_list_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_get_first (rcs_chunked_list_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_get_last (rcs_chunked_list_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_get_prev (rcs_chunked_list_node_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_get_next (rcs_chunked_list_node_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_append_new (rcs_chunked_list_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_insert_new (rcs_chunked_list_t *, rcs_chunked_list_node_t *); -extern void rcs_chunked_list_remove (rcs_chunked_list_t *, rcs_chunked_list_node_t *); -extern rcs_chunked_list_node_t *rcs_chunked_list_get_node_from_pointer (rcs_chunked_list_t *, void *); -extern uint8_t *rcs_chunked_list_get_node_data_space (rcs_chunked_list_t *, rcs_chunked_list_node_t *); -extern size_t rcs_chunked_list_get_node_data_space_size (void); - -/** - * @} - * @} - */ - -#endif /* RCS_CHUNKED_LIST_H */ diff --git a/jerry-core/rcs/rcs-cpointer.c b/jerry-core/rcs/rcs-cpointer.c deleted file mode 100644 index 7d5206cbe..000000000 --- a/jerry-core/rcs/rcs-cpointer.c +++ /dev/null @@ -1,104 +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 "rcs-cpointer.h" -#include "jrt-bit-fields.h" - -/** - * Compress pointer to extended compressed pointer. - * - * @return dynamic storage-specific extended compressed pointer - */ -rcs_cpointer_t -rcs_cpointer_compress (rcs_record_t *pointer) /**< pointer to compress */ -{ - rcs_cpointer_t cpointer; - cpointer.u.packed_value = 0; - - uintptr_t base_pointer = JERRY_ALIGNDOWN ((uintptr_t) pointer, MEM_ALIGNMENT); - - if ((void *) base_pointer == NULL) - { - cpointer.u.value.base_cp = MEM_CP_NULL; - } - else - { - cpointer.u.value.base_cp = mem_compress_pointer ((void *) base_pointer) & MEM_CP_MASK; - } - -#if MEM_ALIGNMENT_LOG > RCS_DYN_STORAGE_LENGTH_UNIT_LOG - /* - * If alignment of a unit in recordset storage is less than required by MEM_ALIGNMENT_LOG, - * then mem_cpointer_t can't store pointer to the unit, and so, rcs_cpointer_t stores - * mem_cpointer_t to block, aligned to MEM_ALIGNMENT, and also extension with difference - * between positions of the MEM_ALIGNMENT-aligned block and the unit. - */ - uintptr_t diff = (uintptr_t) pointer - base_pointer; - - JERRY_ASSERT (diff < MEM_ALIGNMENT); - JERRY_ASSERT (JRT_EXTRACT_BIT_FIELD (uintptr_t, diff, 0, RCS_DYN_STORAGE_LENGTH_UNIT_LOG) == 0); - - uintptr_t ext_part = (uintptr_t) JRT_EXTRACT_BIT_FIELD (uintptr_t, diff, - RCS_DYN_STORAGE_LENGTH_UNIT_LOG, - MEM_ALIGNMENT_LOG - RCS_DYN_STORAGE_LENGTH_UNIT_LOG); - - cpointer.u.value.ext = ext_part & ((1ull << (MEM_ALIGNMENT_LOG - RCS_DYN_STORAGE_LENGTH_UNIT_LOG)) - 1); -#endif /* MEM_ALIGNMENT > RCS_DYN_STORAGE_LENGTH_UNIT_LOG */ - JERRY_ASSERT (rcs_cpointer_decompress (cpointer) == pointer); - - return cpointer; -} /* rcs_cpointer_compress */ - -/** - * Decompress extended compressed pointer. - * - * @return decompressed pointer - */ -rcs_record_t * -rcs_cpointer_decompress (rcs_cpointer_t compressed_pointer) /**< recordset-specific compressed pointer */ -{ - uint8_t *base_pointer = NULL; - - if (compressed_pointer.u.value.base_cp != MEM_CP_NULL) - { - base_pointer = (uint8_t *) mem_decompress_pointer (compressed_pointer.u.value.base_cp); - } - - uintptr_t diff = 0; -#if MEM_ALIGNMENT_LOG > RCS_DYN_STORAGE_LENGTH_UNIT_LOG - /* - * See also: - * rcs_cpointer_compress - */ - - diff = (uintptr_t) compressed_pointer.u.value.ext << RCS_DYN_STORAGE_LENGTH_UNIT_LOG; -#endif /* MEM_ALIGNMENT_LOG > RCS_DYN_STORAGE_LENGTH_UNIT_LOG */ - rcs_record_t *rec_p = (rcs_record_t *) (base_pointer + diff); - - return rec_p; -} /* rcs_cpointer_decompress */ - -/** - * Create NULL compressed pointer. - * - * @return NULL compressed pointer - */ -rcs_cpointer_t rcs_cpointer_null_cp (void) -{ - rcs_cpointer_t cp; - cp.u.packed_value = MEM_CP_NULL; - return cp; -} /* rcs_cpointer_null_cp */ diff --git a/jerry-core/rcs/rcs-cpointer.h b/jerry-core/rcs/rcs-cpointer.h deleted file mode 100644 index f8ba22ee6..000000000 --- a/jerry-core/rcs/rcs-cpointer.h +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 RCS_CPOINTER_H -#define RCS_CPOINTER_H - -#include "rcs-globals.h" - -#define RCS_CPOINTER_WIDTH (MEM_CP_WIDTH + MEM_ALIGNMENT_LOG - RCS_DYN_STORAGE_LENGTH_UNIT_LOG) - -/** - * Dynamic storage-specific extended compressed pointer - * - * Note: - * the pointer can represent addresses aligned by RCS_DYN_STORAGE_LENGTH_UNIT, - * while mem_cpointer_t can only represent addresses aligned by MEM_ALIGNMENT. - */ -typedef struct -{ - union - { - struct - { - __extension__ mem_cpointer_t base_cp : MEM_CP_WIDTH; /**< pointer to base of addressed area */ -#if MEM_ALIGNMENT_LOG > RCS_DYN_STORAGE_LENGTH_UNIT_LOG - __extension__ uint16_t ext : (MEM_ALIGNMENT_LOG - RCS_DYN_STORAGE_LENGTH_UNIT_LOG); /**< extension of the basic - * compressed pointer - * used for more detailed - * addressing */ -#endif /* MEM_ALIGNMENT_LOG > RCS_DYN_STORAGE_LENGTH_UNIT_LOG */ - } value; - uint16_t packed_value; - } u; -} rcs_cpointer_t; - -extern rcs_cpointer_t rcs_cpointer_compress (rcs_record_t *); -extern rcs_record_t *rcs_cpointer_decompress (rcs_cpointer_t); -extern rcs_cpointer_t rcs_cpointer_null_cp (); - -#endif /* !RCS_CPOINTER_H */ diff --git a/jerry-core/rcs/rcs-globals.h b/jerry-core/rcs/rcs-globals.h deleted file mode 100644 index 0196fcade..000000000 --- a/jerry-core/rcs/rcs-globals.h +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 RCS_GLOBALS_H -#define RCS_GLOBALS_H - -#include "rcs-chunked-list.h" - -/** - * Represents the type of the record. - */ -typedef enum -{ - RCS_RECORD_TYPE_FREE = 0, /**< Free record that marks an empty space. It doesn't hold any values. */ - RCS_RECORD_TYPE_CHARSET = 1, /**< Charset record that holds characters. */ - RCS_RECORD_TYPE_MAGIC_STR = 2, /**< Magic string record that holds a magic string id. */ - RCS_RECORD_TYPE_MAGIC_STR_EX = 3, /**< External magic string record that holds an extrernal magic string id. */ - RCS_RECORD_TYPE_NUMBER = 4 /**< Number record that holds a numeric value. */ -} rcs_record_type_t; - -/** - * Record type - */ -typedef uint8_t rcs_record_t; - -/** - * Recordset type - */ -typedef rcs_chunked_list_t rcs_record_set_t; - -/** - * Logarithm of a dynamic storage unit alignment - */ -#define RCS_DYN_STORAGE_LENGTH_UNIT_LOG (2u) - -/** - * Unit of length - */ -#define RCS_DYN_STORAGE_LENGTH_UNIT ((size_t) (1ull << RCS_DYN_STORAGE_LENGTH_UNIT_LOG)) - -#endif /* !RCS_GLOBALS_H */ diff --git a/jerry-core/rcs/rcs-iterator.c b/jerry-core/rcs/rcs-iterator.c deleted file mode 100644 index 31bf0b737..000000000 --- a/jerry-core/rcs/rcs-iterator.c +++ /dev/null @@ -1,215 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 "rcs-iterator.h" -#include "rcs-allocator.h" -#include "rcs-records.h" - -/** - * Represents the memory access on the literal storage. - */ -typedef enum -{ - RCS_ITERATOR_ACCESS_WRITE = 0, /**< Write 'size' bytes from 'data' buffer to the record. */ - RCS_ITERATOR_ACCESS_READ = 1, /**< Read 'size' bytes from the record and write to the 'data' buffer. */ - RCS_ITERATOR_ACCESS_SKIP = 2 /**< Increment current position so that 'size' bytes would be skipped. */ -} rcs_access_t; - -/** - * Create an iterator context. - * - * @return an initialized iterator context - */ -rcs_iterator_t -rcs_iterator_create (rcs_record_set_t *recordset_p, /**< recordset */ - rcs_record_t *record_p) /**< start record */ -{ - rcs_iterator_t ctx; - { - ctx.recordset_p = recordset_p; - ctx.record_start_p = record_p; - - rcs_iterator_reset (&ctx); - } - - return ctx; -} /* rcs_iterator_create */ - -/** - * Perform general access to the record - * - * Warning: This function is implemented in assumption that `size` is not more than `2 * node_data_space_size`. - */ -static void -rcs_iterator_access (rcs_iterator_t *ctx_p, /**< iterator context */ - void *data, /**< iterator context */ - size_t size, /**< iterator context */ - rcs_access_t access_type) /**< access type */ -{ - const size_t node_data_space_size = rcs_get_node_data_space_size (); - JERRY_ASSERT (2 * node_data_space_size >= size); - const size_t record_size = rcs_record_get_size (ctx_p->record_start_p); - - JERRY_ASSERT (!rcs_iterator_finished (ctx_p)); - - rcs_chunked_list_node_t *current_node_p = - rcs_chunked_list_get_node_from_pointer (ctx_p->recordset_p, ctx_p->current_pos_p); - uint8_t *current_node_data_space_p = rcs_get_node_data_space (ctx_p->recordset_p, current_node_p); - size_t left_in_node = node_data_space_size - (size_t) (ctx_p->current_pos_p - current_node_data_space_p); - - JERRY_ASSERT (ctx_p->current_offset + size <= record_size); - - /* - * Read the data and increase the current position pointer. - */ - if (left_in_node >= size) - { - /* all data is placed inside single node */ - if (access_type == RCS_ITERATOR_ACCESS_READ) - { - memcpy (data, ctx_p->current_pos_p, size); - } - else if (access_type == RCS_ITERATOR_ACCESS_WRITE) - { - memcpy (ctx_p->current_pos_p, data, size); - } - else - { - JERRY_ASSERT (access_type == RCS_ITERATOR_ACCESS_SKIP); - - if (left_in_node > size) - { - ctx_p->current_pos_p += size; - } - else if (ctx_p->current_offset + size < record_size) - { - current_node_p = rcs_chunked_list_get_next (current_node_p); - JERRY_ASSERT (current_node_p); - ctx_p->current_pos_p = rcs_get_node_data_space (ctx_p->recordset_p, current_node_p); - } - else - { - JERRY_ASSERT (ctx_p->current_offset + size == record_size); - } - } - } - else - { - /* Data is distributed between two nodes. */ - const size_t first_chunk_size = node_data_space_size - (size_t) (ctx_p->current_pos_p - current_node_data_space_p); - - if (access_type == RCS_ITERATOR_ACCESS_READ) - { - memcpy (data, ctx_p->current_pos_p, first_chunk_size); - } - else if (access_type == RCS_ITERATOR_ACCESS_WRITE) - { - memcpy (ctx_p->current_pos_p, data, first_chunk_size); - } - - rcs_chunked_list_node_t *next_node_p = rcs_chunked_list_get_next (current_node_p); - JERRY_ASSERT (next_node_p != NULL); - uint8_t *next_node_data_space_p = rcs_get_node_data_space (ctx_p->recordset_p, next_node_p); - - if (access_type == RCS_ITERATOR_ACCESS_READ) - { - memcpy ((uint8_t *) data + first_chunk_size, next_node_data_space_p, size - first_chunk_size); - } - else if (access_type == RCS_ITERATOR_ACCESS_WRITE) - { - memcpy (next_node_data_space_p, (uint8_t *) data + first_chunk_size, size - first_chunk_size); - } - else - { - JERRY_ASSERT (access_type == RCS_ITERATOR_ACCESS_SKIP); - ctx_p->current_pos_p = next_node_data_space_p + size - first_chunk_size; - } - } - - /* Check if we reached the end. */ - if (access_type == RCS_ITERATOR_ACCESS_SKIP) - { - ctx_p->current_offset += size; - JERRY_ASSERT (ctx_p->current_offset <= record_size); - - if (ctx_p->current_offset == record_size) - { - ctx_p->current_pos_p = NULL; - ctx_p->current_offset = 0; - } - } -} /* rcs_iterator_access */ - -/** - * Read a value from the record. - * After reading iterator doesn't change its position. - * - * @return read value - */ -void -rcs_iterator_read (rcs_iterator_t *ctx_p, /**< iterator context */ - void *out_data, /**< value to read */ - size_t size) /**< size to read */ -{ - rcs_iterator_access (ctx_p, out_data, size, RCS_ITERATOR_ACCESS_READ); -} /* rcs_iterator_read */ - -/** - * Write a value to the record. - * After writing, iterator doesn't change its position. - */ -void -rcs_iterator_write (rcs_iterator_t *ctx_p, /**< iterator context */ - void *value, /**< value to write */ - size_t size) /**< size to write */ -{ - rcs_iterator_access (ctx_p, value, size, RCS_ITERATOR_ACCESS_WRITE); -} /* rcs_iterator_write */ - -/** - * Increment current position to skip 'size' bytes. - */ -void -rcs_iterator_skip (rcs_iterator_t *ctx_p, /**< iterator context */ - size_t size) /**< size to skip */ -{ - if (size) - { - rcs_iterator_access (ctx_p, NULL, size, RCS_ITERATOR_ACCESS_SKIP); - } -} /* rcs_iterator_skip */ - -/** - * Reset the iterator, so that it points to the beginning of the record. - */ -void -rcs_iterator_reset (rcs_iterator_t *ctx_p) /**< iterator context */ -{ - ctx_p->current_pos_p = ctx_p->record_start_p; - ctx_p->current_offset = 0; -} /* rcs_iterator_reset */ - -/** - * Check if the end of the record was reached. - * - * @return true if the whole record was iterated - * false otherwise - */ -bool -rcs_iterator_finished (rcs_iterator_t *ctx_p) /**< iterator context */ -{ - return ctx_p->current_pos_p == NULL; -} /* rcs_iterator_finished */ diff --git a/jerry-core/rcs/rcs-iterator.h b/jerry-core/rcs/rcs-iterator.h deleted file mode 100644 index 46a8fc73c..000000000 --- a/jerry-core/rcs/rcs-iterator.h +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 RCS_ITERATOR_H -#define RCS_ITERATOR_H - -#include "ecma-globals.h" - -/** - * Represents a context for the iterator. - */ -typedef struct -{ - rcs_record_set_t *recordset_p; /**< recordset, containing the records */ - rcs_record_t *record_start_p; /**< start of current record */ - uint8_t *current_pos_p; /**< pointer to current offset in current record */ - size_t current_offset; /**< current offset */ -} rcs_iterator_t; - -extern rcs_iterator_t rcs_iterator_create (rcs_record_set_t *, rcs_record_t *); - -extern void rcs_iterator_write (rcs_iterator_t *, void *, size_t); -extern void rcs_iterator_read (rcs_iterator_t *, void *, size_t); -extern void rcs_iterator_skip (rcs_iterator_t *, size_t); - -extern void rcs_iterator_reset (rcs_iterator_t *); -extern bool rcs_iterator_finished (rcs_iterator_t *); - -#endif /* !RCS_ITERATOR_H */ diff --git a/jerry-core/rcs/rcs-records.c b/jerry-core/rcs/rcs-records.c deleted file mode 100644 index 593561a7d..000000000 --- a/jerry-core/rcs/rcs-records.c +++ /dev/null @@ -1,637 +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 "rcs-records.h" - -#include "rcs-allocator.h" -#include "rcs-cpointer.h" -#include "rcs-iterator.h" -#include "jrt-bit-fields.h" - -/** - * Set value of the record's field with specified offset and width. - */ -static void -rcs_record_set_field (rcs_record_t *rec_p, /**< record */ - uint32_t field_pos, /**< offset, in bits */ - uint32_t field_width, /**< width, in bits */ - size_t value) /**< 32-bit unsigned integer value */ -{ - rcs_check_record_alignment (rec_p); - - JERRY_ASSERT (sizeof (uint32_t) <= RCS_DYN_STORAGE_LENGTH_UNIT); - JERRY_ASSERT (field_pos + field_width <= RCS_DYN_STORAGE_LENGTH_UNIT * JERRY_BITSINBYTE); - - uint32_t prev_value = *(uint32_t *) rec_p; - *(uint32_t *) rec_p = JRT_SET_BIT_FIELD_VALUE (uint32_t, prev_value, value, field_pos, field_width); -} /* rcs_record_set_field */ - -/** - * Set the record's type identifier. - */ -void -rcs_record_set_type (rcs_record_t *rec_p, /**< record */ - rcs_record_type_t type) /**< record type */ -{ - JERRY_ASSERT (RCS_RECORD_TYPE_IS_VALID (type)); - - rcs_record_set_field (rec_p, RCS_HEADER_TYPE_POS, RCS_HEADER_TYPE_WIDTH, type); -} /* rcs_record_set_type */ - -/** - * Set previous record for the records. - */ -void -rcs_record_set_prev (rcs_record_set_t *rec_sec_p, /**< recordset */ - rcs_record_t *rec_p, /**< record */ - rcs_record_t *prev_p) /**< prev record */ -{ - uint8_t begin_pos; - - switch (rcs_record_get_type (rec_p)) - { - case RCS_RECORD_TYPE_CHARSET: - { - rcs_cpointer_t prev_cpointer = rcs_cpointer_compress (prev_p); - rcs_iterator_t it_ctx = rcs_iterator_create (rec_sec_p, rec_p); - - rcs_iterator_skip (&it_ctx, RCS_DYN_STORAGE_LENGTH_UNIT); - rcs_iterator_write (&it_ctx, &prev_cpointer.u.packed_value, sizeof (uint16_t)); - - return; - } - case RCS_RECORD_TYPE_FREE: - { - begin_pos = RCS_FREE_HEADER_PREV_POS; - break; - } - case RCS_RECORD_TYPE_MAGIC_STR: - case RCS_RECORD_TYPE_MAGIC_STR_EX: - { - begin_pos = RCS_MAGIC_STR_HEADER_PREV_POS; - break; - } - case RCS_RECORD_TYPE_NUMBER: - { - begin_pos = RCS_NUMBER_HEADER_PREV_POS; - break; - } - default: - { - JERRY_UNREACHABLE (); - } - } - - rcs_cpointer_t cpointer = rcs_cpointer_compress (prev_p); - rcs_record_set_field (rec_p, begin_pos, RCS_CPOINTER_WIDTH, cpointer.u.packed_value); -} /* rcs_record_set_prev */ - -/** - * Set size of the records. - */ -void -rcs_record_set_size (rcs_record_t *rec_p, /**< recordset */ - size_t size) /**< record size */ -{ - rcs_record_type_t type = rcs_record_get_type (rec_p); - - if (RCS_RECORD_TYPE_IS_CHARSET (type)) - { - JERRY_ASSERT (JERRY_ALIGNUP (size, RCS_DYN_STORAGE_LENGTH_UNIT) == size); - - rcs_record_set_field (rec_p, - RCS_CHARSET_HEADER_LENGTH_POS, - RCS_CHARSET_HEADER_LENGTH_WIDTH, - size >> RCS_DYN_STORAGE_LENGTH_UNIT_LOG); - return; - } - - if (RCS_RECORD_TYPE_IS_FREE (type)) - { - JERRY_ASSERT (JERRY_ALIGNUP (size, RCS_DYN_STORAGE_LENGTH_UNIT) == size); - - rcs_record_set_field (rec_p, - RCS_FREE_HEADER_LENGTH_POS, - RCS_FREE_HEADER_LENGTH_WIDTH, - size >> RCS_DYN_STORAGE_LENGTH_UNIT_LOG); - return; - } - - JERRY_ASSERT (rcs_record_get_size (rec_p) == size); -} /* rcs_record_set_size */ - -/** - * Set the count of the alignment bytes at the end of record. - */ -void -rcs_record_set_alignment_bytes_count (rcs_record_t *rec_p, /**< record */ - size_t count) /**< align bytes */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - - rcs_record_set_field (rec_p, RCS_CHARSET_HEADER_ALIGN_POS, RCS_CHARSET_HEADER_ALIGN_WIDTH, count); -} /* rcs_record_set_alignment_bytes_count */ - -/** - * Set the hash value of the record. - */ -void -rcs_record_set_hash (rcs_record_t *rec_p, /**< record */ - lit_string_hash_t hash) /**< hash value */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - - rcs_record_set_field (rec_p, RCS_CHARSET_HEADER_HASH_POS, RCS_CHARSET_HEADER_HASH_WIDTH, hash); -} /* rcs_record_set_hash */ - -/** - * Set the charset of the record. - */ -void -rcs_record_set_charset (rcs_record_set_t *rec_set_p, /**< recordset containing the records */ - rcs_record_t *rec_p, /**< record */ - const lit_utf8_byte_t *str_p, /**< buffer containing characters to set */ - lit_utf8_size_t size) /**< size of the buffer in bytes */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - JERRY_ASSERT (RCS_CHARSET_HEADER_SIZE + size - == rcs_record_get_size (rec_p) - rcs_record_get_alignment_bytes_count (rec_p)); - - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); - - lit_utf8_size_t str_len = rcs_record_get_length (rec_p); - lit_utf8_size_t i; - - for (i = 0; i < str_len; ++i) - { - rcs_iterator_write (&it_ctx, (void *)(str_p + i), sizeof (lit_utf8_byte_t)); - rcs_iterator_skip (&it_ctx, sizeof (lit_utf8_byte_t)); - } -} /* rcs_record_set_charset */ - -/** - * Set the magic string id of the record. - */ -void -rcs_record_set_magic_str_id (rcs_record_t *rec_p, /**< record */ - lit_magic_string_id_t id) /**< magic string id */ -{ - JERRY_ASSERT (RCS_RECORD_IS_MAGIC_STR (rec_p)); - - rcs_record_set_field (rec_p, RCS_MAGIC_STR_HEADER_ID_POS, RCS_MAGIC_STR_HEADER_ID_WIDTH, id); -} /* rcs_record_set_magic_str_id */ - -/** - * Set the external magic string id of the record. - */ -void -rcs_record_set_magic_str_ex_id (rcs_record_t *rec_p, /**< record */ - lit_magic_string_ex_id_t id) /**< external magic string id */ -{ - JERRY_ASSERT (RCS_RECORD_IS_MAGIC_STR_EX (rec_p)); - - rcs_record_set_field (rec_p, RCS_MAGIC_STR_HEADER_ID_POS, RCS_MAGIC_STR_HEADER_ID_WIDTH, id); -} /* rcs_record_set_magic_str_ex_id */ - -/** - * Get value of the record's field with specified offset and width. - * - * @return field's 32-bit unsigned integer value - */ -static uint32_t -rcs_record_get_field (rcs_record_t *rec_p, /**< record */ - uint32_t field_pos, /**< offset, in bits */ - uint32_t field_width) /**< width, in bits */ -{ - rcs_check_record_alignment (rec_p); - - JERRY_ASSERT (sizeof (uint32_t) <= RCS_DYN_STORAGE_LENGTH_UNIT); - JERRY_ASSERT (field_pos + field_width <= RCS_DYN_STORAGE_LENGTH_UNIT * JERRY_BITSINBYTE); - - uint32_t value = *(uint32_t *) rec_p; - return JRT_EXTRACT_BIT_FIELD (uint32_t, value, field_pos, field_width); -} /* rcs_record_get_field */ - -/** - * Get value of the record's pointer field with specified offset and width. - * - * @return pointer to record - */ -static rcs_record_t * -rcs_record_get_pointer (rcs_record_t *rec_p, /**< record */ - uint32_t field_pos, /**< offset, in bits */ - uint32_t field_width) /**< width, in bits */ -{ - rcs_cpointer_t cpointer; - - uint16_t value = (uint16_t) rcs_record_get_field (rec_p, field_pos, field_width); - - JERRY_ASSERT (sizeof (cpointer) == sizeof (cpointer.u.value)); - JERRY_ASSERT (sizeof (value) == sizeof (cpointer.u.value)); - - cpointer.u.packed_value = value; - - return rcs_cpointer_decompress (cpointer); -} /* rcs_record_get_pointer */ - -/** - * Get the record's type identifier. - * - * @return record type identifier - */ -rcs_record_type_t -rcs_record_get_type (rcs_record_t *rec_p) /**< record */ -{ - JERRY_STATIC_ASSERT (sizeof (rcs_record_type_t) * JERRY_BITSINBYTE >= RCS_HEADER_TYPE_WIDTH, - bits_in_rcs_record_type_t_must_be_greater_than_or_equal_to_RCS_HEADER_TYPE_WIDTH); - - return (rcs_record_type_t) rcs_record_get_field (rec_p, RCS_HEADER_TYPE_POS, RCS_HEADER_TYPE_WIDTH); -} /* rcs_record_get_type */ - -/** - * Get previous record for the records. - * - * @return previous record - */ -rcs_record_t * -rcs_record_get_prev (rcs_record_set_t *rec_sec_p, /**< recordset */ - rcs_record_t *rec_p) /**< record */ -{ - uint8_t begin_pos; - - switch (rcs_record_get_type (rec_p)) - { - case RCS_RECORD_TYPE_CHARSET: - { - rcs_cpointer_t cpointer; - rcs_iterator_t it_ctx = rcs_iterator_create (rec_sec_p, rec_p); - - rcs_iterator_skip (&it_ctx, RCS_DYN_STORAGE_LENGTH_UNIT); - rcs_iterator_read (&it_ctx, &cpointer.u.packed_value, sizeof (uint16_t)); - - return rcs_cpointer_decompress (cpointer); - } - case RCS_RECORD_TYPE_FREE: - { - begin_pos = RCS_FREE_HEADER_PREV_POS; - break; - } - case RCS_RECORD_TYPE_MAGIC_STR: - case RCS_RECORD_TYPE_MAGIC_STR_EX: - { - begin_pos = RCS_MAGIC_STR_HEADER_PREV_POS; - break; - } - case RCS_RECORD_TYPE_NUMBER: - { - begin_pos = RCS_NUMBER_HEADER_PREV_POS; - break; - } - default: - { - JERRY_UNREACHABLE (); - } - } - - return rcs_record_get_pointer (rec_p, begin_pos, RCS_CPOINTER_WIDTH); -} /* rcs_record_get_prev */ - -/** - * Get the count of the alignment bytes at the end of record. - * These bytes are needed to align the record to RCS_DYN_STORAGE_ALIGNMENT. - * - * @return alignment bytes count - */ -size_t -rcs_record_get_alignment_bytes_count (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - - return rcs_record_get_field (rec_p, RCS_CHARSET_HEADER_ALIGN_POS, RCS_CHARSET_HEADER_ALIGN_WIDTH); -} /* rcs_record_get_alignment_bytes_count */ - -/** - * Get hash value of the record's charset. - * - * @return hash value of the string - */ -lit_string_hash_t -rcs_record_get_hash (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - - return (lit_string_hash_t) rcs_record_get_field (rec_p, RCS_CHARSET_HEADER_HASH_POS, RCS_CHARSET_HEADER_HASH_WIDTH); -} /* rcs_record_get_hash */ - -/** - * Get header size of the records. - * - * @return size of the header in bytes - */ -size_t -rcs_header_get_size (rcs_record_t *rec_p) /**< record */ -{ - if (RCS_RECORD_IS_CHARSET (rec_p)) - { - return RCS_CHARSET_HEADER_SIZE; - } - - return RCS_DYN_STORAGE_LENGTH_UNIT; -} /* rcs_header_get_size */ - -/** - * Get size of the records. - * - * @return size of the record in bytes - */ -size_t -rcs_record_get_size (rcs_record_t *rec_p) /**< record */ -{ - switch (rcs_record_get_type (rec_p)) - { - case RCS_RECORD_TYPE_CHARSET: - { - size_t size = rcs_record_get_field (rec_p, RCS_CHARSET_HEADER_LENGTH_POS, RCS_CHARSET_HEADER_LENGTH_WIDTH); - return (size * RCS_DYN_STORAGE_LENGTH_UNIT); - } - case RCS_RECORD_TYPE_FREE: - { - size_t size = rcs_record_get_field (rec_p, RCS_FREE_HEADER_LENGTH_POS, RCS_FREE_HEADER_LENGTH_WIDTH); - return (size * RCS_DYN_STORAGE_LENGTH_UNIT); - } - case RCS_RECORD_TYPE_NUMBER: - { - return (RCS_DYN_STORAGE_LENGTH_UNIT + sizeof (ecma_number_t)); - } - case RCS_RECORD_TYPE_MAGIC_STR: - case RCS_RECORD_TYPE_MAGIC_STR_EX: - { - return RCS_DYN_STORAGE_LENGTH_UNIT; - } - default: - { - JERRY_UNREACHABLE (); - return 0; - } - } -} /* rcs_record_get_size */ - -/** - * Get the length of the string, which is contained inside the record. - * - * @return length of the string (bytes count) - */ -lit_utf8_size_t -rcs_record_get_length (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - - size_t record_size = rcs_record_get_size (rec_p); - size_t align_count = rcs_record_get_alignment_bytes_count (rec_p); - - return (lit_utf8_size_t) (record_size - RCS_CHARSET_HEADER_SIZE - align_count); -} /* rcs_record_get_length */ - -/** - * Get magic string id which is held by the record. - * - * @return magic string id - */ -lit_magic_string_id_t -rcs_record_get_magic_str_id (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_MAGIC_STR (rec_p)); - - return (lit_magic_string_id_t) rcs_record_get_field (rec_p, - RCS_MAGIC_STR_HEADER_ID_POS, - RCS_MAGIC_STR_HEADER_ID_WIDTH); -} /* rcs_record_get_magic_str_id */ - -/** - * Get external magic string id which is held by the record. - * - * @return external magic string id - */ -lit_magic_string_ex_id_t -rcs_record_get_magic_str_ex_id (rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_MAGIC_STR_EX (rec_p)); - - return (lit_magic_string_ex_id_t) rcs_record_get_field (rec_p, - RCS_MAGIC_STR_HEADER_ID_POS, - RCS_MAGIC_STR_HEADER_ID_WIDTH); -} /* rcs_record_get_magic_str_ex_id */ - -/** - * Get the number which is held by the record. - * - * @return number - */ -ecma_number_t -rcs_record_get_number (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *rec_p) /**< record */ -{ - JERRY_ASSERT (RCS_RECORD_IS_NUMBER (rec_p)); - - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_NUMBER_HEADER_SIZE); - - ecma_number_t value; - rcs_iterator_read (&it_ctx, &value, sizeof (ecma_number_t)); - - return value; -} /* rcs_record_get_number */ - -/** - * Get the characters which are stored to the record. - * - * @return number of code units written to the buffer - */ -lit_utf8_size_t -rcs_record_get_charset (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *rec_p, /**< record */ - const lit_utf8_byte_t *buff_p, /**< output buffer */ - size_t buff_size) /**< size of the output buffer in bytes */ -{ - JERRY_ASSERT (RCS_RECORD_IS_CHARSET (rec_p)); - JERRY_ASSERT (buff_p && buff_size >= sizeof (lit_utf8_byte_t)); - - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); - - lit_utf8_size_t str_len = rcs_record_get_length (rec_p); - lit_utf8_size_t i; - - for (i = 0; i < str_len && buff_size > 0; ++i) - { - rcs_iterator_read (&it_ctx, (void *)(buff_p + i), sizeof (lit_utf8_byte_t)); - rcs_iterator_skip (&it_ctx, sizeof (lit_utf8_byte_t)); - buff_size -= sizeof (lit_utf8_byte_t); - } - - return i; -} /* rcs_record_get_charset */ - -/** - * Get the first record of the recordset. - * - * @return pointer of the first record of the recordset - */ -rcs_record_t * -rcs_record_get_first (rcs_record_set_t *rec_set_p) /**< recordset */ -{ - rcs_chunked_list_node_t *first_node_p = rcs_chunked_list_get_first (rec_set_p); - - if (first_node_p == NULL) - { - return NULL; - } - - return (rcs_record_t *) rcs_get_node_data_space (rec_set_p, first_node_p); -} /* rcs_record_get_first */ - -/** - * Get record, next to the specified. - * - * @return pointer to the next record - */ -rcs_record_t * -rcs_record_get_next (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *rec_p) /**< record */ -{ - rcs_chunked_list_node_t *node_p = rcs_chunked_list_get_node_from_pointer (rec_set_p, rec_p); - - const uint8_t *data_space_begin_p = rcs_get_node_data_space (rec_set_p, node_p); - const size_t data_space_size = rcs_get_node_data_space_size (); - - const uint8_t *record_start_p = (const uint8_t *) rec_p; - const size_t record_size = rcs_record_get_size (rec_p); - - const size_t record_offset_in_node = (size_t) (record_start_p - data_space_begin_p); - const size_t node_size_left = data_space_size - record_offset_in_node; - - if (node_size_left > record_size) - { - return (rcs_record_t *) (record_start_p + record_size); - } - - node_p = rcs_chunked_list_get_next (node_p); - JERRY_ASSERT (node_p != NULL || record_size == node_size_left); - - size_t record_size_left = record_size - node_size_left; - while (record_size_left >= data_space_size) - { - JERRY_ASSERT (node_p != NULL); - - node_p = rcs_chunked_list_get_next (node_p); - record_size_left -= data_space_size; - } - - if (node_p == NULL) - { - JERRY_ASSERT (record_size_left == 0); - return NULL; - } - - return (rcs_record_t *) (rcs_get_node_data_space (rec_set_p, node_p) + record_size_left); -} /* rcs_record_get_next */ - -/** - * Compares two charset records for equality. - * - * @return true if strings inside records are equal - * false otherwise - */ -bool -rcs_record_is_equal (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *l_rec_p, /**< left record */ - rcs_record_t *r_rec_p) /**< rigth record */ -{ - size_t l_rec_length = rcs_record_get_length (l_rec_p); - size_t r_rec_length = rcs_record_get_length (r_rec_p); - - if (l_rec_length != r_rec_length) - { - return false; - } - - rcs_iterator_t l_rec_it_ctx = rcs_iterator_create (rec_set_p, l_rec_p); - rcs_iterator_t r_rec_it_ctx = rcs_iterator_create (rec_set_p, r_rec_p); - - rcs_iterator_skip (&l_rec_it_ctx, RCS_CHARSET_HEADER_SIZE); - rcs_iterator_skip (&r_rec_it_ctx, RCS_CHARSET_HEADER_SIZE); - - lit_utf8_size_t i; - for (i = 0; i < l_rec_length; ++i) - { - lit_utf8_byte_t l_chr; - lit_utf8_byte_t r_chr; - - rcs_iterator_read (&l_rec_it_ctx, &l_chr, sizeof (lit_utf8_byte_t)); - rcs_iterator_read (&r_rec_it_ctx, &r_chr, sizeof (lit_utf8_byte_t)); - - if (l_chr != r_chr) - { - return false; - } - - rcs_iterator_skip (&l_rec_it_ctx, sizeof (lit_utf8_byte_t)); - rcs_iterator_skip (&r_rec_it_ctx, sizeof (lit_utf8_byte_t)); - } - - return true; -} /* rcs_record_is_equal */ - -/** - * Compare a record with a string (which could contain '\0' characters) for equality. - * - * @return true if compared instances are equal - * false otherwise - */ -bool -rcs_record_is_equal_charset (rcs_record_set_t *rec_set_p, /**< recordset */ - rcs_record_t *rec_p, /**< record */ - const lit_utf8_byte_t *str_p, /**< string to compare with */ - lit_utf8_size_t str_size) /**< length of the string */ -{ - JERRY_ASSERT (str_p != NULL); - - size_t rec_length = rcs_record_get_length (rec_p); - - if (rec_length != str_size) - { - return false; - } - - rcs_iterator_t it_ctx = rcs_iterator_create (rec_set_p, rec_p); - rcs_iterator_skip (&it_ctx, RCS_CHARSET_HEADER_SIZE); - - lit_utf8_size_t i; - for (i = 0; i < rec_length; ++i) - { - lit_utf8_byte_t chr; - rcs_iterator_read (&it_ctx, &chr, sizeof (lit_utf8_byte_t)); - - if (chr != str_p[i]) - { - return false; - } - - rcs_iterator_skip (&it_ctx, sizeof (lit_utf8_byte_t)); - } - - return true; -} /* rcs_record_is_equal_charset */ diff --git a/jerry-core/rcs/rcs-records.h b/jerry-core/rcs/rcs-records.h deleted file mode 100644 index a6f059f27..000000000 --- a/jerry-core/rcs/rcs-records.h +++ /dev/null @@ -1,169 +0,0 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. - * Copyright 2015 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 RCS_RECORDS_H -#define RCS_RECORDS_H - -#include "ecma-globals.h" - -#define RCS_RECORD_TYPE_FIRST RCS_RECORD_TYPE_CHARSET -#define RCS_RECORD_TYPE_LAST RCS_RECORD_TYPE_NUMBER -#define RCS_RECORD_TYPE_MIN RCS_RECORD_TYPE_FREE -#define RCS_RECORD_TYPE_MAX RCS_RECORD_TYPE_NUMBER - -#define RCS_RECORD_TYPE_IS_FREE(type) ((type) == RCS_RECORD_TYPE_FREE) -#define RCS_RECORD_TYPE_IS_NUMBER(type) ((type) == RCS_RECORD_TYPE_NUMBER) -#define RCS_RECORD_TYPE_IS_CHARSET(type) ((type) == RCS_RECORD_TYPE_CHARSET) -#define RCS_RECORD_TYPE_IS_MAGIC_STR(type) ((type) == RCS_RECORD_TYPE_MAGIC_STR) -#define RCS_RECORD_TYPE_IS_MAGIC_STR_EX(type) ((type) == RCS_RECORD_TYPE_MAGIC_STR_EX) -#define RCS_RECORD_TYPE_IS_VALID(type) ((type) <= RCS_RECORD_TYPE_MAX) - -#define RCS_RECORD_IS_FREE(rec) (RCS_RECORD_TYPE_IS_FREE (rcs_record_get_type (rec))) -#define RCS_RECORD_IS_NUMBER(rec) (RCS_RECORD_TYPE_IS_NUMBER (rcs_record_get_type (rec))) -#define RCS_RECORD_IS_CHARSET(rec) (RCS_RECORD_TYPE_IS_CHARSET (rcs_record_get_type (rec))) -#define RCS_RECORD_IS_MAGIC_STR(rec) (RCS_RECORD_TYPE_IS_MAGIC_STR (rcs_record_get_type (rec))) -#define RCS_RECORD_IS_MAGIC_STR_EX(rec) (RCS_RECORD_TYPE_IS_MAGIC_STR_EX (rcs_record_get_type (rec))) - -/** - * Common header informations. - */ -#define RCS_HEADER_TYPE_POS 0u -#define RCS_HEADER_TYPE_WIDTH 4u - -#define RCS_HEADER_FIELD_BEGIN_POS (RCS_HEADER_TYPE_POS + RCS_HEADER_TYPE_WIDTH) - -/** - * Number record - * Doesn't hold any characters, holds a number. - * Numbers from source code are represented as number literals. - * - * Layout: - * ------- header ----------------------- - * type (4 bits) - * padding (12 bits) - * pointer to prev (16 bits) - * -------------------------------------- - * ecma_number_t - */ -#define RCS_NUMBER_HEADER_SIZE RCS_DYN_STORAGE_LENGTH_UNIT -#define RCS_NUMBER_HEADER_PREV_POS (RCS_HEADER_FIELD_BEGIN_POS + 12u) - -/** - * Charset record - * - * layout: - * ------- header ----------------------- - * type (4 bits) - * alignment (2 bits) - * unused (2 bits) - * hash (8 bits) - * length (16 bits) - * pointer to prev (16 bits) - * ------- characters ------------------- - * ... - * chars - * .... - * ------- alignment bytes -------------- - * unused bytes (their count is specified - * by 'alignment' field in header) - * -------------------------------------- - */ -#define RCS_CHARSET_HEADER_SIZE (RCS_DYN_STORAGE_LENGTH_UNIT + RCS_DYN_STORAGE_LENGTH_UNIT / 2) - -#define RCS_CHARSET_HEADER_ALIGN_POS RCS_HEADER_FIELD_BEGIN_POS -#define RCS_CHARSET_HEADER_ALIGN_WIDTH RCS_DYN_STORAGE_LENGTH_UNIT_LOG - -#define RCS_CHARSET_HEADER_UNUSED_POS (RCS_CHARSET_HEADER_ALIGN_POS + RCS_CHARSET_HEADER_ALIGN_WIDTH) -#define RCS_CHARSET_HEADER_UNUSED_WIDTH 2u - -#define RCS_CHARSET_HEADER_HASH_POS (RCS_CHARSET_HEADER_UNUSED_POS + RCS_CHARSET_HEADER_UNUSED_WIDTH) -#define RCS_CHARSET_HEADER_HASH_WIDTH 8u - -#define RCS_CHARSET_HEADER_LENGTH_POS (RCS_CHARSET_HEADER_HASH_POS + RCS_CHARSET_HEADER_HASH_WIDTH) -#define RCS_CHARSET_HEADER_LENGTH_WIDTH 16u - -#define RCS_CHARSET_HEADER_PREV_POS (RCS_CHARSET_HEADER_LENGTH_POS + RCS_CHARSET_HEADER_LENGTH_WIDTH) - -/** - * Magic string record - * Doesn't hold any characters. Corresponding string is identified by its id. - * - * Layout: - * ------- header ----------------------- - * type (4 bits) - * magic string id (12 bits) - * pointer to prev (16 bits) - * -------------------------------------- - */ -#define RCS_MAGIC_STR_HEADER_SIZE RCS_DYN_STORAGE_LENGTH_UNIT - -#define RCS_MAGIC_STR_HEADER_ID_POS RCS_HEADER_FIELD_BEGIN_POS -#define RCS_MAGIC_STR_HEADER_ID_WIDTH 12u - -#define RCS_MAGIC_STR_HEADER_PREV_POS (RCS_MAGIC_STR_HEADER_ID_POS + RCS_MAGIC_STR_HEADER_ID_WIDTH) - -/** - * Free record - * Doesn't hold any data. - * - * Layout: - * ------- header ----------------------- - * type (4 bits) - * length (12 bits) - * pointer to prev (16 bits) - * -------------------------------------- - */ -#define RCS_FREE_HEADER_SIZE RCS_DYN_STORAGE_LENGTH_UNIT - -#define RCS_FREE_HEADER_LENGTH_POS RCS_HEADER_FIELD_BEGIN_POS -#define RCS_FREE_HEADER_LENGTH_WIDTH (14u - RCS_DYN_STORAGE_LENGTH_UNIT_LOG) - -#define RCS_FREE_HEADER_PREV_POS (RCS_FREE_HEADER_LENGTH_POS + RCS_FREE_HEADER_LENGTH_WIDTH) - -/* - * Setters - */ -extern void rcs_record_set_type (rcs_record_t *, rcs_record_type_t); -extern void rcs_record_set_prev (rcs_record_set_t *, rcs_record_t *, rcs_record_t *); -extern void rcs_record_set_size (rcs_record_t *, size_t); -extern void rcs_record_set_alignment_bytes_count (rcs_record_t *, size_t); -extern void rcs_record_set_hash (rcs_record_t *, lit_string_hash_t); -extern void rcs_record_set_charset (rcs_record_set_t *, rcs_record_t *, const lit_utf8_byte_t *, lit_utf8_size_t); -extern void rcs_record_set_magic_str_id (rcs_record_t *, lit_magic_string_id_t); -extern void rcs_record_set_magic_str_ex_id (rcs_record_t *, lit_magic_string_ex_id_t); - -/* - * Getters - */ -extern rcs_record_type_t rcs_record_get_type (rcs_record_t *); -extern rcs_record_t *rcs_record_get_prev (rcs_record_set_t *, rcs_record_t *); -extern size_t rcs_record_get_size (rcs_record_t *); -extern size_t rcs_header_get_size (rcs_record_t *); -extern size_t rcs_record_get_alignment_bytes_count (rcs_record_t *); -extern lit_string_hash_t rcs_record_get_hash (rcs_record_t *); -extern lit_utf8_size_t rcs_record_get_length (rcs_record_t *); -extern lit_utf8_size_t rcs_record_get_charset (rcs_record_set_t *, rcs_record_t *, const lit_utf8_byte_t *, size_t); -extern lit_magic_string_id_t rcs_record_get_magic_str_id (rcs_record_t *); -extern lit_magic_string_ex_id_t rcs_record_get_magic_str_ex_id (rcs_record_t *); -extern ecma_number_t rcs_record_get_number (rcs_record_set_t *, rcs_record_t *); - -extern rcs_record_t *rcs_record_get_first (rcs_record_set_t *); -extern rcs_record_t *rcs_record_get_next (rcs_record_set_t *, rcs_record_t *); - -extern bool rcs_record_is_equal (rcs_record_set_t *, rcs_record_t *, rcs_record_t *); -extern bool rcs_record_is_equal_charset (rcs_record_set_t *, rcs_record_t *, const lit_utf8_byte_t *, lit_utf8_size_t); - -#endif /* !RCS_RECORDS_H */ diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index 0e051c9f6..5ba78ff27 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -29,8 +29,8 @@ #include "ecma-objects-general.h" #include "ecma-regexp-object.h" #include "ecma-try-catch-macro.h" +#include "lit-literal-storage.h" #include "opcodes.h" -#include "rcs-records.h" #include "vm.h" #include "vm-stack.h" @@ -269,7 +269,7 @@ vm_construct_literal_object (vm_frame_ctx_t *frame_ctx_p, /**< frame context */ lit_cpointer_t lit_cp) /**< literal */ { ecma_compiled_code_t *bytecode_p = ECMA_GET_NON_NULL_POINTER (ecma_compiled_code_t, - lit_cp.u.value.base_cp); + lit_cp); bool is_function = ((bytecode_p->status_flags & CBC_CODE_FLAGS_FUNCTION) != 0); if (is_function) @@ -390,8 +390,8 @@ enum else if (literal_index < const_literal_end) \ { \ lit_cpointer_t lit_cpointer = literal_start_p[literal_index]; \ - lit_literal_t lit = rcs_cpointer_decompress (lit_cpointer); \ - if (unlikely (RCS_RECORD_IS_NUMBER (lit))) \ + lit_literal_t lit = lit_cpointer_decompress (lit_cpointer); \ + if (unlikely (LIT_RECORD_IS_NUMBER (lit))) \ { \ ecma_number_t *number_p = ecma_alloc_number (); \ *number_p = lit_number_literal_get_number (lit); \ diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index bf356fb39..efa8826fe 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -18,6 +18,7 @@ #define VM_H #include "ecma-globals.h" +#include "lit-cpointer.h" #include "jrt.h" #include "vm-defines.h" diff --git a/tests/unit/test-heap.c b/tests/unit/test-heap.c index 26671f4df..6d03f74a7 100644 --- a/tests/unit/test-heap.c +++ b/tests/unit/test-heap.c @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -60,7 +61,7 @@ test_heap_give_some_memory_back (mem_try_give_memory_back_severity_t severity) JERRY_ASSERT (ptrs[i][k] == 0); } - mem_heap_free_block (ptrs[i]); + mem_heap_free_block_size_stored (ptrs[i]); ptrs[i] = NULL; } } @@ -77,37 +78,18 @@ main (int __attr_unused___ argc, mem_register_a_try_give_memory_back_callback (test_heap_give_some_memory_back); - mem_heap_print (true, false, true); + mem_heap_print (); for (uint32_t i = 0; i < test_iters; i++) { for (uint32_t j = 0; j < test_sub_iters; j++) { - if (rand () % 2) - { - size_t size = (size_t) rand () % test_threshold_block_size; - ptrs[j] = (uint8_t*) mem_heap_alloc_block (size, - (rand () % 2) ? - MEM_HEAP_ALLOC_LONG_TERM : MEM_HEAP_ALLOC_SHORT_TERM); - sizes[j] = size; - is_one_chunked[j] = false; - } - else - { - ptrs[j] = (uint8_t*) mem_heap_alloc_chunked_block ((rand () % 2) ? - MEM_HEAP_ALLOC_LONG_TERM : MEM_HEAP_ALLOC_SHORT_TERM); - sizes[j] = mem_heap_get_chunked_block_data_size (); - is_one_chunked[j] = true; - } + size_t size = (size_t) rand () % test_threshold_block_size; + ptrs[j] = (uint8_t*) mem_heap_alloc_block_store_size (size); + sizes[j] = size; JERRY_ASSERT (sizes[j] == 0 || ptrs[j] != NULL); memset (ptrs[j], 0, sizes[j]); - - if (is_one_chunked[j]) - { - JERRY_ASSERT (ptrs[j] != NULL - && mem_heap_get_chunked_block_start (ptrs[j] + (size_t) rand () % sizes[j]) == ptrs[j]); - } } // mem_heap_print (true); @@ -121,20 +103,14 @@ main (int __attr_unused___ argc, JERRY_ASSERT (ptrs[j][k] == 0); } - if (is_one_chunked[j]) - { - JERRY_ASSERT (sizes[j] == 0 - || mem_heap_get_chunked_block_start (ptrs[j] + (size_t) rand () % sizes[j]) == ptrs[j]); - } - - mem_heap_free_block (ptrs[j]); + mem_heap_free_block_size_stored (ptrs[j]); ptrs[j] = NULL; } } } - mem_heap_print (true, false, true); + mem_heap_print (); return 0; } /* main */ diff --git a/tests/unit/test-literal-storage.c b/tests/unit/test-literal-storage.c index 91869ebe8..4eb4fec12 100644 --- a/tests/unit/test-literal-storage.c +++ b/tests/unit/test-literal-storage.c @@ -16,7 +16,6 @@ #include "ecma-helpers.h" #include "lit-literal.h" #include "lit-literal-storage.h" -#include "rcs-records.h" #include "test-common.h" // Iterations count @@ -132,9 +131,6 @@ main (int __attr_unused___ argc, // Check empty string exists JERRY_ASSERT (lit_find_literal_by_utf8_string (NULL, 0)); - - rcs_chunked_list_cleanup (&rcs_lit_storage); - JERRY_ASSERT (rcs_record_get_first (&rcs_lit_storage) == NULL); } lit_finalize (); diff --git a/tests/unit/test-poolman.c b/tests/unit/test-poolman.c index 58f0e1369..0a9ffa8c4 100644 --- a/tests/unit/test-poolman.c +++ b/tests/unit/test-poolman.c @@ -1,4 +1,5 @@ /* Copyright 2014-2015 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. @@ -47,8 +48,7 @@ main (int __attr_unused___ argc, for (size_t j = 0; j < subiters; j++) { - ptrs[j] = mem_pools_alloc (); - // JERRY_ASSERT (ptrs[j] != NULL); + ptrs[j] = (uint8_t *) mem_pools_alloc (); if (ptrs[j] != NULL) { @@ -85,11 +85,11 @@ main (int __attr_unused___ argc, printf ("Pools stats:\n"); printf (" Chunk size: %u\n" - " Pools: %lu\n" - " Allocated chunks: %lu\n" - " Free chunks: %lu\n" - " Peak pools: %lu\n" - " Peak allocated chunks: %lu\n\n", + " Pools: %zu\n" + " Allocated chunks: %zu\n" + " Free chunks: %zu\n" + " Peak pools: %zu\n" + " Peak allocated chunks: %zu\n\n", MEM_POOL_CHUNK_SIZE, stats.pools_count, stats.allocated_chunks,