Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jerry-core/ecma/base/ecma-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ ecma_init_gc_info (ecma_object_t *object_p) /**< object */
void
ecma_ref_object (ecma_object_t *object_p) /**< object */
{
if (object_p->type_flags_refs < ECMA_OBJECT_MAX_REF)
if (likely (object_p->type_flags_refs < ECMA_OBJECT_MAX_REF))
{
object_p->type_flags_refs = (uint16_t) (object_p->type_flags_refs + ECMA_OBJECT_REF_ONE);
}
Expand Down
112 changes: 14 additions & 98 deletions jerry-core/ecma/base/ecma-helpers-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,13 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */

if (str1_size == 0)
{
return ecma_copy_or_ref_ecma_string (string2_p);
ecma_ref_ecma_string (string2_p);
return string2_p;
}
else if (str2_size == 0)
{
return ecma_copy_or_ref_ecma_string (string1_p);
ecma_ref_ecma_string (string1_p);
return string1_p;
}

const lit_utf8_size_t new_size = str1_size + str2_size;
Expand Down Expand Up @@ -442,111 +444,25 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
return string_desc_p;
} /* ecma_concat_ecma_strings */

/**
* Copy ecma-string
*
* @return pointer to copy of ecma-string with reference counter set to 1
*/
static ecma_string_t *
ecma_copy_ecma_string (ecma_string_t *string_desc_p) /**< string descriptor */
{
JERRY_ASSERT (string_desc_p != NULL);
JERRY_ASSERT (string_desc_p->refs_and_container >= ECMA_STRING_REF_ONE);

ecma_string_t *new_str_p;

switch (ECMA_STRING_GET_CONTAINER (string_desc_p))
{
case ECMA_STRING_CONTAINER_LIT_TABLE:
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
case ECMA_STRING_CONTAINER_MAGIC_STRING:
case ECMA_STRING_CONTAINER_MAGIC_STRING_EX:
{
new_str_p = ecma_alloc_string ();

*new_str_p = *string_desc_p;

new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);

break;
}

case ECMA_STRING_CONTAINER_HEAP_UTF8_STRING:
{
new_str_p = ecma_alloc_string ();
*new_str_p = *string_desc_p;
new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);

const ecma_string_heap_header_t *data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t,
string_desc_p->u.utf8_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 *) jmem_heap_alloc_block (data_size);
memcpy (new_data_p, data_p, data_size);

ECMA_SET_NON_NULL_POINTER (new_str_p->u.utf8_collection_cp, new_data_p);

break;
}

case ECMA_STRING_CONTAINER_HEAP_ASCII_STRING:
{
new_str_p = ecma_alloc_string ();
*new_str_p = *string_desc_p;
new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);

const lit_utf8_byte_t *data_p = ECMA_GET_NON_NULL_POINTER (lit_utf8_byte_t,
string_desc_p->u.ascii_string.ascii_collection_cp);

JERRY_ASSERT (data_p != NULL);
const size_t data_size = string_desc_p->u.ascii_string.size;
lit_utf8_byte_t *new_data_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (data_size);
memcpy (new_data_p, data_p, data_size);

ECMA_SET_NON_NULL_POINTER (new_str_p->u.ascii_string.ascii_collection_cp, new_data_p);

break;
}

default:
{
JERRY_UNREACHABLE ();
}
}

JERRY_ASSERT (ecma_compare_ecma_strings (string_desc_p, new_str_p));

return new_str_p;
} /* ecma_copy_ecma_string */

/**
* Increase reference counter of ecma-string.
*
* @return pointer to same ecma-string descriptor with increased reference counter
* or the ecma-string's copy with reference counter set to 1
*/
ecma_string_t *
ecma_copy_or_ref_ecma_string (ecma_string_t *string_p) /**< string descriptor */
void
ecma_ref_ecma_string (ecma_string_t *string_p) /**< string descriptor */
{
JERRY_ASSERT (string_p != NULL);
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);

if (unlikely (string_p->refs_and_container >= ECMA_STRING_MAX_REF))
if (likely (string_p->refs_and_container < ECMA_STRING_MAX_REF))
{
/* First trying to free unreachable objects that maybe refer to the string */
ecma_gc_run ();

if (string_p->refs_and_container >= ECMA_STRING_MAX_REF)
{
/* reference counter was not changed during GC, copying string */
return ecma_copy_ecma_string (string_p);
}
/* Increase reference counter. */
string_p->refs_and_container = (uint16_t) (string_p->refs_and_container + ECMA_STRING_REF_ONE);
}

/* Increase reference counter. */
string_p->refs_and_container = (uint16_t) (string_p->refs_and_container + ECMA_STRING_REF_ONE);
return string_p;
} /* ecma_copy_or_ref_ecma_string */
else
{
jerry_fatal (ERR_REF_COUNT_LIMIT);
}
} /* ecma_ref_ecma_string */

/**
* Decrease reference counter and deallocate ecma-string
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/ecma/base/ecma-helpers-value.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ ecma_copy_value (ecma_value_t value) /**< value description */
}
case ECMA_TYPE_STRING:
{
return ecma_make_string_value (ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (value)));
ecma_ref_ecma_string (ecma_get_string_from_value (value));
return value;
}
case ECMA_TYPE_OBJECT:
{
Expand Down
4 changes: 2 additions & 2 deletions jerry-core/ecma/base/ecma-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ ecma_create_named_data_property (ecma_object_t *object_p, /**< object */

uint8_t type_and_flags = ECMA_PROPERTY_TYPE_NAMEDDATA | prop_attributes;

name_p = ecma_copy_or_ref_ecma_string (name_p);
ecma_ref_ecma_string (name_p);

ecma_property_value_t value;
value.value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
Expand All @@ -610,7 +610,7 @@ ecma_create_named_accessor_property (ecma_object_t *object_p, /**< object */

uint8_t type_and_flags = ECMA_PROPERTY_TYPE_NAMEDACCESSOR | prop_attributes;

name_p = ecma_copy_or_ref_ecma_string (name_p);
ecma_ref_ecma_string (name_p);

ecma_property_value_t value;
ECMA_SET_POINTER (value.getter_setter_pair.getter_p, get_p);
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/ecma/base/ecma-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ extern ecma_string_t *ecma_new_ecma_string_from_lit_cp (lit_cpointer_t);
extern ecma_string_t *ecma_new_ecma_string_from_magic_string_id (lit_magic_string_id_t);
extern ecma_string_t *ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_string_ex_id_t);
extern ecma_string_t *ecma_concat_ecma_strings (ecma_string_t *, ecma_string_t *);
extern ecma_string_t *ecma_copy_or_ref_ecma_string (ecma_string_t *);
extern void ecma_ref_ecma_string (ecma_string_t *);
extern void ecma_deref_ecma_string (ecma_string_t *);
extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ ecma_builtin_array_prototype_object_to_locale_string (const ecma_value_t this_ar
ecma_builtin_helper_get_to_locale_string_at_index (obj_p, 0),
ret_value);

ecma_string_t *return_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (first_value));
ecma_string_t *return_string_p = ecma_get_string_from_value (first_value);
ecma_ref_ecma_string (return_string_p);

/* 9-10. */
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
Expand Down Expand Up @@ -402,7 +403,8 @@ ecma_builtin_array_prototype_join (const ecma_value_t this_arg, /**< this argume
ecma_op_array_get_to_string_at_index (obj_p, 0),
ret_value);

ecma_string_t *return_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (first_value));
ecma_string_t *return_string_p = ecma_get_string_from_value (first_value);
ecma_ref_ecma_string (return_string_p);

/* 9-10. */
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this

if (ecma_string_get_length (name_string_p) == 0)
{
ret_str_p = ecma_copy_or_ref_ecma_string (msg_string_p);
ret_str_p = msg_string_p;
ecma_ref_ecma_string (ret_str_p);
}
else if (ecma_string_get_length (msg_string_p) == 0)
{
ret_str_p = ecma_copy_or_ref_ecma_string (name_string_p);
ret_str_p = name_string_p;
ecma_ref_ecma_string (ret_str_p);
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions jerry-core/ecma/builtin-objects/ecma-builtin-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,8 @@ ecma_builtin_json_stringify (ecma_value_t this_arg, /**< 'this' argument */

if (num_of_chars < 10)
{
context.gap_str_p = ecma_copy_or_ref_ecma_string (space_str_p);
ecma_ref_ecma_string (space_str_p);
context.gap_str_p = space_str_p;
}
else
{
Expand Down Expand Up @@ -1061,7 +1062,8 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
{
/* 1. */
ecma_string_t *quote_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR);
ecma_string_t *product_str_p = ecma_copy_or_ref_ecma_string (quote_str_p);
ecma_ref_ecma_string (quote_str_p);
ecma_string_t *product_str_p = quote_str_p;
ecma_string_t *tmp_str_p;

ECMA_STRING_TO_UTF8_STRING (string_p, string_buff, string_buff_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
}
else
{
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
pattern_string_p = ecma_get_string_from_value (regexp_str_value);
ecma_ref_ecma_string (pattern_string_p);
}

ECMA_FINALIZE (regexp_str_value);
Expand Down Expand Up @@ -380,8 +381,7 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume

ecma_string_t *src_sep_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SLASH_CHAR);
ecma_string_t *source_str_p = ecma_get_string_from_value (ecma_get_named_data_property_value (source_prop_p));
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, ecma_copy_or_ref_ecma_string (source_str_p));
ecma_deref_ecma_string (source_str_p);
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, source_str_p);

ecma_string_t *concat_p = ecma_concat_ecma_strings (output_str_p, src_sep_str_p);
ecma_deref_ecma_string (src_sep_str_p);
Expand Down
7 changes: 5 additions & 2 deletions jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
}
else
{
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
pattern_string_p = ecma_get_string_from_value (regexp_str_value);
ecma_ref_ecma_string (pattern_string_p);
}

ECMA_FINALIZE (regexp_str_value);
Expand All @@ -125,7 +126,9 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
ecma_op_to_string (flags_value),
ret_value);

flags_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (flags_str_value));
flags_string_p = ecma_get_string_from_value (flags_str_value);
ecma_ref_ecma_string (flags_string_p);

ECMA_FINALIZE (flags_str_value);
}

Expand Down
15 changes: 6 additions & 9 deletions jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ ecma_builtin_string_prototype_object_to_string (ecma_value_t this_arg) /**< this
ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_ECMA_VALUE);

ecma_string_t *prim_value_str_p;
prim_value_str_p = ecma_get_string_from_value (ecma_get_internal_property_value (prim_value_prop_p));

prim_value_str_p = ecma_copy_or_ref_ecma_string (prim_value_str_p);

return ecma_make_string_value (prim_value_str_p);
return ecma_copy_value (ecma_get_internal_property_value (prim_value_prop_p));
}
}

Expand Down Expand Up @@ -250,10 +245,11 @@ ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this ar
ret_value);

/* 3 */
// No copy performed
/* No copy performed */

/* 4 */
ecma_string_t *string_to_return = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (to_string_val));
ecma_string_t *string_to_return = ecma_get_string_from_value (to_string_val);
ecma_ref_ecma_string (string_to_return);

/* 5 */
for (uint32_t arg_index = 0;
Expand Down Expand Up @@ -659,7 +655,8 @@ ecma_builtin_string_prototype_object_replace_append_substr (ecma_string_t *base_
}
else
{
ret_string_p = ecma_copy_or_ref_ecma_string (base_string_p);
ret_string_p = base_string_p;
ecma_ref_ecma_string (ret_string_p);
}

return ret_string_p;
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/ecma/operations/ecma-conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ ecma_op_to_string (ecma_value_t value) /**< ecma value */
if (ecma_is_value_string (value))
{
res_p = ecma_get_string_from_value (value);
res_p = ecma_copy_or_ref_ecma_string (res_p);
ecma_ref_ecma_string (res_p);
}
else if (ecma_is_value_integer_number (value))
{
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/ecma/operations/ecma-exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ ecma_new_standard_error_with_message (ecma_standard_error_t error_type, /**< nat
message_magic_string_p,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE);

ecma_ref_ecma_string (message_string_p);
ecma_set_named_data_property_value (prop_p,
ecma_make_string_value (ecma_copy_or_ref_ecma_string (message_string_p)));
ecma_make_string_value (message_string_p));
ecma_deref_ecma_string (message_magic_string_p);

return new_error_obj_p;
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/ecma/operations/ecma-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */

JERRY_ASSERT (name_pos > 0
&& name_pos <= array_index_named_properties_count + string_named_properties_count);
names_p[--name_pos] = ecma_copy_or_ref_ecma_string (name_p);
ecma_ref_ecma_string (name_p);
names_p[--name_pos] = name_p;
}
}

Expand Down
2 changes: 1 addition & 1 deletion jerry-core/ecma/operations/ecma-reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ecma_make_reference (ecma_value_t base, /**< base value */
ecma_string_t *name_p, /**< referenced name */
bool is_strict) /**< strict reference flag */
{
name_p = ecma_copy_or_ref_ecma_string (name_p);
ecma_ref_ecma_string (name_p);

ecma_reference_t ref;
ref.base = ecma_copy_value (base);
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/ecma/operations/ecma-string-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ ecma_op_string_object_get_own_property (ecma_object_t *obj_p, /**< a String obje
{
uint32_index = property_name_p->u.uint32_number;

new_prop_name_p = ecma_copy_or_ref_ecma_string (property_name_p);
new_prop_name_p = property_name_p;
ecma_ref_ecma_string (new_prop_name_p);
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ jerry_acquire_string (jerry_string_t *string_p) /**< pointer passed to function
{
jerry_assert_api_available ();

return ecma_copy_or_ref_ecma_string (string_p);
ecma_ref_ecma_string (string_p);

return string_p;
} /* jerry_acquire_string */

/**
Expand Down
4 changes: 2 additions & 2 deletions jerry-core/parser/regexp/re-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ re_compile_bytecode (const re_compiled_code_t **out_bytecode_p, /**< [out] point

re_compiled_code.header.refs = 1;
re_compiled_code.header.status_flags = re_ctx.flags;
ECMA_SET_NON_NULL_POINTER (re_compiled_code.pattern_cp,
ecma_copy_or_ref_ecma_string (pattern_str_p));
ecma_ref_ecma_string (pattern_str_p);
ECMA_SET_NON_NULL_POINTER (re_compiled_code.pattern_cp, pattern_str_p);
re_compiled_code.num_of_captures = re_ctx.num_of_captures * 2;
re_compiled_code.num_of_non_captures = re_ctx.num_of_non_captures;

Expand Down