Generational mark and sweep GC.

This commit is contained in:
Ruben Ayrapetyan 2014-08-07 14:54:32 +04:00
parent ba6713e295
commit e3cd8ee942
15 changed files with 475 additions and 180 deletions

View File

@ -231,7 +231,9 @@ GIT_HASH=$(shell git rev-parse HEAD)
BUILD_DATE=$(shell date +'%d/%m/%Y')
CFLAGS_JERRY = $(CFLAGS_WARNINGS) $(CFLAGS_WERROR) $(CFLAGS_WFATAL_ERRORS)
DEFINES_JERRY = -DMEM_HEAP_CHUNK_SIZE=$$((64)) -DMEM_HEAP_AREA_SIZE=$$((2 * 1024 + 512)) -DMEM_STATS
DEFINES_JERRY = -DMEM_HEAP_CHUNK_SIZE=$$((64)) -DMEM_HEAP_AREA_SIZE=$$((2 * 1024 + 512)) -DMEM_STATS \
-DCONFIG_ECMA_REFERENCE_COUNTER_WIDTH=$$((10))
DEFINES_JERRY += -DJERRY_BUILD_DATE="\"$(BUILD_DATE)\"" \
-DJERRY_COMMIT_HASH="\"$(GIT_HASH)\"" \
-DJERRY_BRANCH_NAME="\"$(GIT_BRANCH)\""

View File

@ -147,7 +147,7 @@ run_int_from_pos (opcode_counter_t start_pos,
reg_index < regs_num;
reg_index++ )
{
ecma_free_value( regs[ reg_index ] );
ecma_free_value( regs[ reg_index ], true);
}
return completion;

View File

@ -168,7 +168,7 @@ get_variable_value(struct __int_data *int_data, /**< interpreter context */
JERRY_ASSERT( !ecma_is_value_empty( reg_value) );
ret_value = ecma_make_completion_value (ECMA_COMPLETION_TYPE_NORMAL,
ecma_copy_value( reg_value),
ecma_copy_value( reg_value, true),
ECMA_TARGET_ID_RESERVED);
}
else
@ -218,10 +218,10 @@ set_variable_value(struct __int_data *int_data, /**< interpreter context */
if ( !ecma_is_value_empty( reg_value) )
{
ecma_free_value( reg_value);
ecma_free_value( reg_value, true);
}
int_data->regs_p[ var_idx - int_data->min_reg_num ] = ecma_copy_value( value);
int_data->regs_p[ var_idx - int_data->min_reg_num ] = ecma_copy_value( value, true);
ret_value = ecma_make_empty_completion_value();
}
@ -1375,7 +1375,7 @@ opfunc_func_decl_0(OPCODE opdata, /**< operation data */
// f.
ecma_completion_value_t ret_value = ecma_op_set_mutable_binding( int_data->lex_env_p, fn, fo_value, is_strict);
ecma_free_value( fo_value);
ecma_free_value( fo_value, true);
free_string_literal_copy( &function_name);
@ -1421,7 +1421,7 @@ opfunc_call_0( OPCODE opdata, /**< operation data */
ret_value = set_variable_value( int_data, lhs_var_idx, returned_value);
ecma_free_value( returned_value);
ecma_free_value( returned_value, true);
}
ECMA_FINALIZE( this_value);

View File

@ -40,7 +40,10 @@
JERRY_STATIC_ASSERT( sizeof (ecma_value_t) <= sizeof (uint16_t) );
JERRY_STATIC_ASSERT( sizeof (ecma_property_t) <= sizeof (uint64_t) );
JERRY_STATIC_ASSERT( sizeof (ecma_object_t) <= sizeof (uint64_t) );
FIXME( Pack ecma_object_t )
JERRY_STATIC_ASSERT( sizeof (ecma_object_t) <= 2 * sizeof (uint64_t) );
JERRY_STATIC_ASSERT( sizeof (ecma_array_header_t) <= sizeof (uint32_t) );
JERRY_STATIC_ASSERT( sizeof (ecma_array_first_chunk_t) == ECMA_ARRAY_CHUNK_SIZE_IN_BYTES );
JERRY_STATIC_ASSERT( sizeof (ecma_array_non_first_chunk_t) == ECMA_ARRAY_CHUNK_SIZE_IN_BYTES );
@ -54,13 +57,25 @@ JERRY_STATIC_ASSERT( sizeof (ecma_completion_value_t) == sizeof(uint32_t) );
#define ALLOC( ecma_type) ecma_ ## ecma_type ## _t * \
ecma_alloc_ ## ecma_type (void) \
{ \
ecma_ ## ecma_type ## _t *p ## ecma_type = (ecma_ ## ecma_type ## _t *) \
ecma_ ## ecma_type ## _t *p ## ecma_type = (ecma_ ## ecma_type ## _t *) \
mem_pools_alloc( mem_size_to_pool_chunk_type( sizeof(ecma_ ## ecma_type ## _t))); \
\
if ( likely( p ## ecma_type != NULL ) ) \
{ \
return p ## ecma_type; \
} \
\
ecma_gc_run( ECMA_GC_GEN_0 ); \
\
p ## ecma_type = (ecma_ ## ecma_type ## _t *) \
mem_pools_alloc( mem_size_to_pool_chunk_type( sizeof(ecma_ ## ecma_type ## _t))); \
\
ecma_gc_run(); \
JERRY_ASSERT( p ## ecma_type != NULL ); \
\
return p ## ecma_type; \
\
if ( likely( p ## ecma_type != NULL ) ) \
{ \
return p ## ecma_type; \
} \
\
JERRY_UNREACHABLE(); \
}
/**

View File

@ -24,35 +24,38 @@
* Garbage collector implementation
*/
#include "globals.h"
#include "ecma-alloc.h"
#include "ecma-globals.h"
#include "ecma-gc.h"
#include "ecma-helpers.h"
#include "globals.h"
#include "jerry-libc.h"
/**
* Queue of objects, awaiting for GC
* Global lists of objects sorted by generation identifier.
*/
static ecma_object_t *ecma_gc_objs_to_free_queue;
static ecma_object_t *ecma_gc_objects_lists[ ECMA_GC_GEN_COUNT ];
static void ecma_gc_mark( ecma_object_t *object_p, ecma_gc_gen_t maximum_gen_to_traverse);
static void ecma_gc_sweep( ecma_object_t *object_p);
/**
* Queue object for GC.
*
* Warning:
* After this operation the object is not longer valid for general use.
* Initialize GC information for the object
*/
static void
ecma_gc_queue( ecma_object_t *object_p) /**< object */
void
ecma_init_gc_info(ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT( object_p != NULL );
JERRY_ASSERT( object_p->GCInfo.is_object_valid );
JERRY_ASSERT( object_p->GCInfo.u.refs == 0 );
object_p->gc_info.refs = 1;
object_p->GCInfo.is_object_valid = false;
ecma_set_pointer( object_p->GCInfo.u.next_queued_for_gc, ecma_gc_objs_to_free_queue);
object_p->gc_info.generation = ECMA_GC_GEN_0;
ecma_set_pointer( object_p->gc_info.next, ecma_gc_objects_lists[ ECMA_GC_GEN_0 ]);
ecma_gc_objects_lists[ ECMA_GC_GEN_0 ] = object_p;
ecma_gc_objs_to_free_queue = object_p;
} /* ecma_gc_queue */
/* Should be set to false at the beginning of garbage collection */
object_p->gc_info.visited = true;
object_p->gc_info.may_ref_younger_objects = false;
} /* ecma_init_gc_info */
/**
* Increase reference counter of an object
@ -60,19 +63,18 @@ ecma_gc_queue( ecma_object_t *object_p) /**< object */
void
ecma_ref_object(ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT(object_p->GCInfo.is_object_valid);
JERRY_ASSERT(object_p != NULL);
object_p->gc_info.refs++;
object_p->GCInfo.u.refs++;
/**
* Check that value was not overflowed
*/
JERRY_ASSERT(object_p->gc_info.refs > 0);
/**
* Check that value was not overflowed
*/
JERRY_ASSERT(object_p->GCInfo.u.refs > 0);
if ( unlikely( object_p->GCInfo.u.refs == 0 ) )
{
JERRY_UNREACHABLE();
}
if ( unlikely( object_p->gc_info.refs == 0 ) )
{
JERRY_UNREACHABLE();
}
} /* ecma_ref_object */
/**
@ -81,68 +83,349 @@ ecma_ref_object(ecma_object_t *object_p) /**< object */
void
ecma_deref_object(ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT(object_p != NULL);
JERRY_ASSERT(object_p->GCInfo.is_object_valid);
JERRY_ASSERT(object_p->GCInfo.u.refs > 0);
JERRY_ASSERT(object_p != NULL);
JERRY_ASSERT(object_p->gc_info.refs > 0);
object_p->GCInfo.u.refs--;
if ( object_p->GCInfo.u.refs == 0 )
{
ecma_gc_queue( object_p);
}
object_p->gc_info.refs--;
} /* ecma_deref_object */
/**
* Set may_ref_younger_objects of specified object to true,
* if value is object-value and it's object's generation
* is less than generation of object specified by obj_p.
*/
void
ecma_gc_update_may_ref_younger_object_flag_by_value( ecma_object_t *obj_p, /**< object */
ecma_value_t value) /**< value */
{
if ( value.value_type != ECMA_TYPE_OBJECT )
{
return;
}
ecma_object_t *ref_obj_p = ecma_get_pointer( value.value);
JERRY_ASSERT( ref_obj_p != NULL );
ecma_gc_update_may_ref_younger_object_flag_by_object( obj_p, ref_obj_p);
} /* ecma_gc_update_may_ref_younger_object_flag_by_value */
void
ecma_gc_update_may_ref_younger_object_flag_by_object( ecma_object_t *obj_p, /**< object */
ecma_object_t *ref_obj_p) /**< referenced object
or NULL */
{
if ( ref_obj_p == NULL )
{
return;
}
if ( ref_obj_p->gc_info.generation < obj_p->gc_info.generation )
{
obj_p->gc_info.may_ref_younger_objects = true;
}
} /* ecma_gc_update_may_ref_younger_object_flag_by_object */
/**
* Initialize garbage collector
*/
void
ecma_gc_init( void)
{
ecma_gc_objs_to_free_queue = NULL;
__memset( ecma_gc_objects_lists, 0, sizeof( ecma_gc_objects_lists));
} /* ecma_gc_init */
/**
* Mark objects as visited starting from specified object as root
* if referenced object's generation is less or equal to maximum_gen_to_traverse.
*/
void
ecma_gc_mark( ecma_object_t *object_p, /**< start object */
ecma_gc_gen_t maximum_gen_to_traverse) /**< start recursive traverse
if referenced object generation
is less or equal to maximum_gen_to_traverse */
{
JERRY_ASSERT( object_p != NULL );
object_p->gc_info.visited = true;
bool does_reference_object_to_traverse = false;
for ( ecma_property_t *property_p = ecma_get_pointer( object_p->properties_p), *next_property_p;
property_p != NULL;
property_p = next_property_p )
{
next_property_p = ecma_get_pointer( property_p->next_property_p);
switch ( (ecma_property_type_t) property_p->type )
{
case ECMA_PROPERTY_NAMEDDATA:
{
ecma_value_t value = property_p->u.named_data_property.value;
if ( value.value_type == ECMA_TYPE_OBJECT )
{
ecma_object_t *value_obj_p = ecma_get_pointer( value.value);
if ( value_obj_p->gc_info.generation <= maximum_gen_to_traverse )
{
if ( !value_obj_p->gc_info.visited )
{
ecma_gc_mark( value_obj_p, ECMA_GC_GEN_COUNT);
}
does_reference_object_to_traverse = true;
}
}
break;
}
case ECMA_PROPERTY_NAMEDACCESSOR:
{
ecma_object_t *getter_obj_p = ecma_get_pointer( property_p->u.named_accessor_property.get_p);
ecma_object_t *setter_obj_p = ecma_get_pointer( property_p->u.named_accessor_property.set_p);
if ( getter_obj_p != NULL )
{
if ( getter_obj_p->gc_info.generation <= maximum_gen_to_traverse )
{
if ( !getter_obj_p->gc_info.visited )
{
ecma_gc_mark( getter_obj_p, ECMA_GC_GEN_COUNT);
}
does_reference_object_to_traverse = true;
}
}
if ( setter_obj_p != NULL )
{
if ( setter_obj_p->gc_info.generation <= maximum_gen_to_traverse )
{
if ( !setter_obj_p->gc_info.visited )
{
ecma_gc_mark( setter_obj_p, ECMA_GC_GEN_COUNT);
}
does_reference_object_to_traverse = true;
}
}
break;
}
case ECMA_PROPERTY_INTERNAL:
{
ecma_internal_property_id_t property_id = property_p->u.internal_property.type;
uint32_t property_value = property_p->u.internal_property.value;
switch ( property_id )
{
case ECMA_INTERNAL_PROPERTY_NUMBER_INDEXED_ARRAY_VALUES: /* an array */
case ECMA_INTERNAL_PROPERTY_STRING_INDEXED_ARRAY_VALUES: /* an array */
case ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS: /* an array */
case ECMA_INTERNAL_PROPERTY_PROTOTYPE: /* the property's value is located in ecma_object_t */
case ECMA_INTERNAL_PROPERTY_EXTENSIBLE: /* the property's value is located in ecma_object_t */
case ECMA_INTERNAL_PROPERTY_PROVIDE_THIS: /* a boolean */
case ECMA_INTERNAL_PROPERTY_CLASS: /* an enum */
case ECMA_INTERNAL_PROPERTY_CODE: /* an integer */
{
break;
}
case ECMA_INTERNAL_PROPERTY_SCOPE: /* a lexical environment */
case ECMA_INTERNAL_PROPERTY_BINDING_OBJECT: /* an object */
{
ecma_object_t *obj_p = ecma_get_pointer( property_value);
if ( obj_p->gc_info.generation <= maximum_gen_to_traverse )
{
if ( !obj_p->gc_info.visited )
{
ecma_gc_mark( obj_p, ECMA_GC_GEN_COUNT);
}
does_reference_object_to_traverse = true;
}
break;
}
}
break;
}
}
}
if ( !does_reference_object_to_traverse )
{
object_p->gc_info.may_ref_younger_objects = false;
}
} /* ecma_gc_mark */
/**
* Free specified object
*/
void
ecma_gc_sweep( ecma_object_t *object_p) /**< object to free */
{
JERRY_ASSERT( object_p != NULL
&& !object_p->gc_info.visited
&& object_p->gc_info.refs == 0 );
for ( ecma_property_t *property = ecma_get_pointer( object_p->properties_p),
*next_property_p;
property != NULL;
property = next_property_p )
{
next_property_p = ecma_get_pointer( property->next_property_p);
ecma_free_property( property);
}
ecma_dealloc_object( object_p);
} /* ecma_gc_sweep */
/**
* Run garbage collecting
*/
void
ecma_gc_run( void)
ecma_gc_run( ecma_gc_gen_t max_gen_to_collect) /**< maximum generation to run collection on */
{
while ( ecma_gc_objs_to_free_queue != NULL )
JERRY_ASSERT( max_gen_to_collect < ECMA_GC_GEN_COUNT );
bool was_something_sweeped = false;
/* clearing visited flags for all objects of generations to be processed */
for ( ecma_gc_gen_t gen_id = 0; gen_id <= max_gen_to_collect; gen_id++ )
{
ecma_object_t *object_p = ecma_gc_objs_to_free_queue;
ecma_gc_objs_to_free_queue = ecma_get_pointer( object_p->GCInfo.u.next_queued_for_gc);
JERRY_ASSERT( !object_p->GCInfo.is_object_valid );
for ( ecma_property_t *property = ecma_get_pointer( object_p->properties_p), *next_property_p;
property != NULL;
property = next_property_p )
for ( ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ gen_id ];
obj_iter_p != NULL;
obj_iter_p = ecma_get_pointer( obj_iter_p->gc_info.next) )
{
next_property_p = ecma_get_pointer( property->next_property_p);
ecma_free_property( property);
obj_iter_p->gc_info.visited = false;
}
}
if ( object_p->is_lexical_environment )
/* if some object is referenced from stack or globals (i.e. it is root),
* start recursive marking traverse from the object */
for ( ecma_gc_gen_t gen_id = 0; gen_id <= max_gen_to_collect; gen_id++ )
{
for ( ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ gen_id ];
obj_iter_p != NULL;
obj_iter_p = ecma_get_pointer( obj_iter_p->gc_info.next) )
{
ecma_object_t *outer_lexical_environment_p = ecma_get_pointer( object_p->u.lexical_environment.outer_reference_p);
if ( outer_lexical_environment_p != NULL )
if ( obj_iter_p->gc_info.refs > 0
&& !obj_iter_p->gc_info.visited )
{
ecma_deref_object( outer_lexical_environment_p);
}
} else
{
ecma_object_t *prototype_object_p = ecma_get_pointer( object_p->u.object.prototype_object_p);
if ( prototype_object_p != NULL )
{
ecma_deref_object( prototype_object_p);
ecma_gc_mark( obj_iter_p, ECMA_GC_GEN_COUNT);
}
}
ecma_dealloc_object( object_p);
}
/* if some object from generations that are not processed during current session may reference
* younger generations, start recursive marking traverse from the object, but one the first level
* consider only references to object of at most max_gen_to_collect generation */
for ( ecma_gc_gen_t gen_id = max_gen_to_collect + 1; gen_id < ECMA_GC_GEN_COUNT; gen_id++ )
{
for ( ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ gen_id ];
obj_iter_p != NULL;
obj_iter_p = ecma_get_pointer( obj_iter_p->gc_info.next) )
{
if ( obj_iter_p->gc_info.may_ref_younger_objects > 0 )
{
ecma_gc_mark( obj_iter_p, max_gen_to_collect);
}
}
}
ecma_object_t *gen_last_obj_p[ max_gen_to_collect + 1 ];
#ifndef JERRY_NDEBUG
__memset( gen_last_obj_p, 0, sizeof(gen_last_obj_p) );
#endif /* !JERRY_NDEBUG */
for ( ecma_gc_gen_t gen_id = 0; gen_id <= max_gen_to_collect; gen_id++ )
{
ecma_object_t *obj_prev_p = NULL;
for ( ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ gen_id ],
*obj_next_p;
obj_iter_p != NULL;
obj_iter_p = obj_next_p )
{
obj_next_p = ecma_get_pointer( obj_iter_p->gc_info.next);
if ( !obj_iter_p->gc_info.visited )
{
was_something_sweeped = true;
ecma_gc_sweep( obj_iter_p);
if ( likely( obj_prev_p != NULL ) )
{
ecma_set_pointer( obj_prev_p->gc_info.next, obj_next_p);
}
else
{
ecma_gc_objects_lists[ gen_id ] = obj_next_p;
}
}
else
{
obj_prev_p = obj_iter_p;
if ( obj_iter_p->gc_info.generation != ECMA_GC_GEN_COUNT - 1 )
{
/* the object will be promoted to next generation */
obj_iter_p->gc_info.generation++;
}
}
}
gen_last_obj_p[ gen_id ] = obj_prev_p;
}
ecma_gc_gen_t gen_to_promote = max_gen_to_collect;
if ( unlikely( gen_to_promote == ECMA_GC_GEN_COUNT - 1 ) )
{
/* not promoting last generation */
gen_to_promote--;
}
/* promoting to next generation */
if ( gen_last_obj_p[ gen_to_promote ] != NULL )
{
ecma_set_pointer( gen_last_obj_p[ gen_to_promote ]->gc_info.next, ecma_gc_objects_lists[ gen_to_promote + 1 ]);
ecma_gc_objects_lists[ gen_to_promote + 1 ] = ecma_gc_objects_lists[ gen_to_promote ];
ecma_gc_objects_lists[ gen_to_promote ] = NULL;
}
for ( int32_t gen_id = (int32_t)gen_to_promote - 1;
gen_id >= 0;
gen_id-- )
{
ecma_gc_objects_lists[ gen_id + 1 ] = ecma_gc_objects_lists[ gen_id ];
ecma_gc_objects_lists[ gen_id ] = NULL;
}
#ifndef JERRY_NDEBUG
for ( ecma_gc_gen_t gen_id = ECMA_GC_GEN_0;
gen_id < ECMA_GC_GEN_COUNT;
gen_id++ )
{
for ( ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ gen_id ];
obj_iter_p != NULL;
obj_iter_p = ecma_get_pointer( obj_iter_p->gc_info.next) )
{
JERRY_ASSERT( obj_iter_p->gc_info.generation == gen_id );
}
}
#endif /* !JERRY_NDEBUG */
if ( !was_something_sweeped
&& max_gen_to_collect != ECMA_GC_GEN_COUNT - 1 )
{
ecma_gc_run( max_gen_to_collect + 1);
}
} /* ecma_gc_run */

View File

@ -23,16 +23,26 @@
#ifndef ECMA_GC_H
#define ECMA_GC_H
/**
* Garbage collector interface
*/
#include "ecma-globals.h"
/**
* GC generation identifier
*/
typedef enum
{
ECMA_GC_GEN_0, /**< generation 0 */
ECMA_GC_GEN_1, /**< generation 1 */
ECMA_GC_GEN_2, /**< generation 2 */
ECMA_GC_GEN_COUNT /**< generations' number */
} ecma_gc_gen_t;
extern void ecma_gc_init( void);
extern void ecma_init_gc_info(ecma_object_t *object_p);
extern void ecma_ref_object(ecma_object_t *object_p);
extern void ecma_deref_object(ecma_object_t *object_p);
extern void ecma_gc_run( void);
extern void ecma_gc_update_may_ref_younger_object_flag_by_value( ecma_object_t *obj_p, ecma_value_t value);
extern void ecma_gc_update_may_ref_younger_object_flag_by_object( ecma_object_t *obj_p, ecma_object_t *ref_obj_p);
extern void ecma_gc_run( ecma_gc_gen_t max_gen_to_collect);
#endif /* !ECMA_GC_H */

View File

@ -254,29 +254,32 @@ typedef struct ecma_property_t {
* Description of GC's information layout
*/
typedef struct {
FIXME( /* Handle cyclic dependencies */ )
/**
* Reference counter of the object.
*
* Number of references to the object from stack variables.
*/
unsigned int refs : CONFIG_ECMA_REFERENCE_COUNTER_WIDTH;
/**
* Flag that indicates if the object is valid for normal usage.
* If the flag is zero, then the object is not valid and is queued for GC.
* Identifier of GC generation.
*/
unsigned int is_object_valid : 1;
unsigned int generation : 2;
/** Details (depending on is_object_valid) */
union {
/**
* Number of refs to the object (if is_object_valid).
*
* Note: It is not a pointer. Maximum value of reference counter
* willn't be bigger than overall count of variables/objects/properties.
* The width of the field will be sufficient in most cases. However, it is not theoretically
* guaranteed. Overflow is handled in ecma_ref_object by stopping the engine.
*/
unsigned int refs : ECMA_POINTER_FIELD_WIDTH;
/**
* Compressed pointer to next object in the global list of objects with same generation.
*/
unsigned int next : ECMA_POINTER_FIELD_WIDTH;
/** Compressed pointer to next object in the list of objects, queued for GC (if !is_object_valid) */
unsigned int next_queued_for_gc : ECMA_POINTER_FIELD_WIDTH;
} __packed u;
/**
* Marker that is set if the object was visited during graph traverse.
*/
unsigned int visited : 1;
/**
* Flag indicating that the object may reference objects of younger generations in its properties.
*/
unsigned int may_ref_younger_objects : 1;
} __packed ecma_gc_info_t;
/**
@ -367,8 +370,10 @@ typedef struct ecma_object_t {
} __packed u;
/** GC's information */
ecma_gc_info_t GCInfo;
} __packed ecma_object_t;
ecma_gc_info_t gc_info;
FIXME( Remove aligned attribute after packing the struct )
} __packed __attribute__((aligned(16))) ecma_object_t;
/**
* Description of ECMA property descriptor

View File

@ -162,7 +162,7 @@ ecma_make_object_value( ecma_object_t* object_p) /**< object to reference in val
* and return new ecma-value
* pointing to copy of the number/string;
* case object;
* increase reference counter of the object
* increase reference counter of the object if do_ref_if_object is true
* and return the value as it was passed.
*
* TODO:
@ -171,7 +171,9 @@ ecma_make_object_value( ecma_object_t* object_p) /**< object to reference in val
* @return See note.
*/
ecma_value_t
ecma_copy_value( const ecma_value_t value) /**< ecma-value */
ecma_copy_value( const ecma_value_t value, /**< ecma-value */
bool do_ref_if_object) /**< if the value is object value,
increment reference counter of object */
{
ecma_value_t value_copy;
@ -213,7 +215,10 @@ ecma_copy_value( const ecma_value_t value) /**< ecma-value */
ecma_object_t *obj_p = ecma_get_pointer( value.value);
JERRY_ASSERT( obj_p != NULL );
ecma_ref_object( obj_p);
if ( do_ref_if_object )
{
ecma_ref_object( obj_p);
}
value_copy = value;
@ -232,7 +237,9 @@ ecma_copy_value( const ecma_value_t value) /**< ecma-value */
* Free the ecma-value
*/
void
ecma_free_value( ecma_value_t value) /**< value description */
ecma_free_value( ecma_value_t value, /**< value description */
bool do_deref_if_object) /**< if the value is object value,
decrement reference counter of object */
{
switch ( (ecma_type_t) value.value_type )
{
@ -258,7 +265,10 @@ ecma_free_value( ecma_value_t value) /**< value description */
case ECMA_TYPE_OBJECT:
{
ecma_deref_object( ecma_get_pointer( value.value));
if ( do_deref_if_object )
{
ecma_deref_object( ecma_get_pointer( value.value));
}
break;
}
@ -339,7 +349,7 @@ ecma_completion_value_t
ecma_copy_completion_value( ecma_completion_value_t value) /**< completion value */
{
return ecma_make_completion_value( value.type,
ecma_copy_value( value.value),
ecma_copy_value( value.value, true),
value.target);
} /* ecma_copy_completion_value */
@ -354,7 +364,7 @@ ecma_free_completion_value( ecma_completion_value_t completion_value) /**< compl
case ECMA_COMPLETION_TYPE_NORMAL:
case ECMA_COMPLETION_TYPE_THROW:
case ECMA_COMPLETION_TYPE_RETURN:
ecma_free_value( completion_value.value);
ecma_free_value( completion_value.value, true);
break;
case ECMA_COMPLETION_TYPE_CONTINUE:
case ECMA_COMPLETION_TYPE_BREAK:

View File

@ -83,12 +83,10 @@ ecma_create_object( ecma_object_t *prototype_object_p, /**< pointer to prototybe
ecma_object_type_t type) /**< object type */
{
ecma_object_t *object_p = ecma_alloc_object();
ecma_init_gc_info( object_p);
object_p->properties_p = ECMA_NULL_POINTER;
object_p->is_lexical_environment = false;
object_p->GCInfo.is_object_valid = true;
object_p->GCInfo.u.refs = 1;
object_p->u.object.extensible = is_extensible;
ecma_set_pointer( object_p->u.object.prototype_object_p, prototype_object_p);
@ -111,20 +109,13 @@ ecma_object_t*
ecma_create_decl_lex_env(ecma_object_t *outer_lexical_environment_p) /**< outer lexical environment */
{
ecma_object_t *new_lexical_environment_p = ecma_alloc_object();
ecma_init_gc_info( new_lexical_environment_p);
new_lexical_environment_p->is_lexical_environment = true;
new_lexical_environment_p->u.lexical_environment.type = ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE;
new_lexical_environment_p->properties_p = ECMA_NULL_POINTER;
new_lexical_environment_p->GCInfo.is_object_valid = true;
new_lexical_environment_p->GCInfo.u.refs = 1;
if ( outer_lexical_environment_p != NULL )
{
ecma_ref_object( outer_lexical_environment_p);
}
ecma_set_pointer( new_lexical_environment_p->u.lexical_environment.outer_reference_p, outer_lexical_environment_p);
return new_lexical_environment_p;
@ -148,29 +139,23 @@ ecma_create_object_lex_env(ecma_object_t *outer_lexical_environment_p, /**< oute
JERRY_ASSERT( binding_obj_p != NULL );
ecma_object_t *new_lexical_environment_p = ecma_alloc_object();
ecma_init_gc_info( new_lexical_environment_p);
new_lexical_environment_p->is_lexical_environment = true;
new_lexical_environment_p->u.lexical_environment.type = ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND;
new_lexical_environment_p->properties_p = ECMA_NULL_POINTER;
new_lexical_environment_p->GCInfo.is_object_valid = true;
new_lexical_environment_p->GCInfo.u.refs = 1;
if ( outer_lexical_environment_p != NULL )
{
ecma_ref_object( outer_lexical_environment_p);
}
ecma_set_pointer( new_lexical_environment_p->u.lexical_environment.outer_reference_p, outer_lexical_environment_p);
ecma_property_t *provide_this_prop_p = ecma_create_internal_property( new_lexical_environment_p, ECMA_INTERNAL_PROPERTY_PROVIDE_THIS);
provide_this_prop_p->u.internal_property.value = provide_this;
ecma_property_t *binding_object_prop_p = ecma_create_internal_property( new_lexical_environment_p, ECMA_INTERNAL_PROPERTY_BINDING_OBJECT);
ecma_ref_object( binding_obj_p);
ecma_set_pointer( binding_object_prop_p->u.internal_property.value, binding_obj_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( new_lexical_environment_p, binding_obj_p);
return new_lexical_environment_p;
} /* ecma_create_object_lex_env */
@ -302,7 +287,10 @@ ecma_create_named_accessor_property(ecma_object_t *obj_p, /**< object */
ecma_set_pointer( prop_p->u.named_accessor_property.name_p, ecma_new_ecma_string( name_p));
ecma_set_pointer( prop_p->u.named_accessor_property.get_p, get_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( obj_p, get_p);
ecma_set_pointer( prop_p->u.named_accessor_property.set_p, set_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( obj_p, set_p);
prop_p->u.named_accessor_property.enumerable = enumerable;
prop_p->u.named_accessor_property.configurable = configurable;
@ -409,7 +397,7 @@ ecma_free_named_data_property( ecma_property_t *property_p) /**< the property */
JERRY_ASSERT( property_p->type == ECMA_PROPERTY_NAMEDDATA );
ecma_free_array( ecma_get_pointer( property_p->u.named_data_property.name_p));
ecma_free_value( property_p->u.named_data_property.value);
ecma_free_value( property_p->u.named_data_property.value, false);
ecma_dealloc_property( property_p);
} /* ecma_free_named_data_property */
@ -424,19 +412,6 @@ ecma_free_named_accessor_property( ecma_property_t *property_p) /**< the propert
ecma_free_array( ecma_get_pointer( property_p->u.named_accessor_property.name_p));
ecma_object_t *get_p = ecma_get_pointer(property_p->u.named_accessor_property.get_p);
ecma_object_t *set_p = ecma_get_pointer(property_p->u.named_accessor_property.set_p);
if ( get_p != NULL )
{
ecma_deref_object( get_p);
}
if ( set_p != NULL )
{
ecma_deref_object( set_p);
}
ecma_dealloc_property( property_p);
} /* ecma_free_named_accessor_property */
@ -463,11 +438,6 @@ ecma_free_internal_property( ecma_property_t *property_p) /**< the property */
case ECMA_INTERNAL_PROPERTY_SCOPE: /* a lexical environment */
case ECMA_INTERNAL_PROPERTY_BINDING_OBJECT: /* an object */
{
ecma_deref_object( ecma_get_pointer( property_value));
break;
}
case ECMA_INTERNAL_PROPERTY_PROTOTYPE: /* the property's value is located in ecma_object_t */
case ECMA_INTERNAL_PROPERTY_EXTENSIBLE: /* the property's value is located in ecma_object_t */
case ECMA_INTERNAL_PROPERTY_PROVIDE_THIS: /* a boolean */

View File

@ -52,8 +52,8 @@ extern ecma_value_t ecma_make_simple_value( ecma_simple_value_t value);
extern ecma_value_t ecma_make_number_value( ecma_number_t* num_p);
extern ecma_value_t ecma_make_string_value( ecma_array_first_chunk_t* ecma_string_p);
extern ecma_value_t ecma_make_object_value( ecma_object_t* object_p);
extern ecma_value_t ecma_copy_value( const ecma_value_t value);
extern void ecma_free_value( const ecma_value_t value);
extern ecma_value_t ecma_copy_value( const ecma_value_t value, bool do_ref_if_object);
extern void ecma_free_value( const ecma_value_t value, bool do_deref_if_object);
extern ecma_completion_value_t ecma_make_completion_value( ecma_completion_type_t type, ecma_value_t value, uint8_t target);
extern ecma_completion_value_t ecma_make_simple_completion_value( ecma_simple_value_t simple_value);

View File

@ -167,7 +167,7 @@ ecma_op_to_primitive( ecma_value_t value, /**< ecma-value */
case ECMA_TYPE_STRING:
{
return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL,
ecma_copy_value( value),
ecma_copy_value( value, true),
ECMA_TARGET_ID_RESERVED);
}
case ECMA_TYPE_OBJECT:
@ -266,7 +266,7 @@ ecma_op_to_number( ecma_value_t value) /**< ecma-value */
case ECMA_TYPE_NUMBER:
{
return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL,
ecma_copy_value( value),
ecma_copy_value( value, true),
ECMA_TARGET_ID_RESERVED);
}
case ECMA_TYPE_SIMPLE:

View File

@ -130,11 +130,11 @@ ecma_op_create_function_object( const ecma_char_t* formal_parameter_list_p[], /*
class_prop_p->u.internal_property.value = ECMA_OBJECT_CLASS_FUNCTION;
// 9.
ecma_ref_object( scope_p);
ecma_property_t *scope_prop_p = ecma_create_internal_property( f, ECMA_INTERNAL_PROPERTY_SCOPE);
ecma_set_pointer( scope_prop_p->u.internal_property.value, scope_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( f, scope_p);
// 10., 11., 14., 15.
if ( formal_parameters_number != 0 )
{
@ -255,7 +255,7 @@ ecma_op_function_call( ecma_object_t *func_obj_p, /**< Function object */
// 1.
if ( is_strict )
{
this_binding = ecma_copy_value( this_arg_value);
this_binding = ecma_copy_value( this_arg_value, true);
}
else if ( ecma_is_value_undefined( this_arg_value)
|| ecma_is_value_null( this_arg_value) )
@ -301,7 +301,7 @@ ecma_op_function_call( ecma_object_t *func_obj_p, /**< Function object */
}
ecma_deref_object( local_env_p);
ecma_free_value( this_binding);
ecma_free_value( this_binding, true);
return ret_value;
}

View File

@ -193,8 +193,10 @@ ecma_op_set_mutable_binding(ecma_object_t *lex_env_p, /**< lexical environment *
if ( property_p->u.named_data_property.writable == ECMA_PROPERTY_WRITABLE )
{
ecma_free_value( property_p->u.named_data_property.value);
property_p->u.named_data_property.value = ecma_copy_value( value);
ecma_free_value( property_p->u.named_data_property.value, false);
property_p->u.named_data_property.value = ecma_copy_value( value, false);
ecma_gc_update_may_ref_younger_object_flag_by_value( lex_env_p, value);
}
else if ( is_strict )
{
@ -256,7 +258,7 @@ ecma_op_get_binding_value(ecma_object_t *lex_env_p, /**< lexical environment */
if ( property_p->u.named_data_property.writable == ECMA_PROPERTY_WRITABLE )
{
return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL,
ecma_copy_value( prop_value),
ecma_copy_value( prop_value, true),
ECMA_TARGET_ID_RESERVED);
} else if ( ecma_is_value_empty( prop_value) )
{
@ -463,7 +465,9 @@ ecma_op_initialize_immutable_binding(ecma_object_t *lex_env_p, /**< lexical envi
JERRY_ASSERT( prop_p->u.named_data_property.writable == ECMA_PROPERTY_NOT_WRITABLE
&& ecma_is_value_empty( prop_p->u.named_data_property.value) );
prop_p->u.named_data_property.value = ecma_copy_value( value);
prop_p->u.named_data_property.value = ecma_copy_value( value, false);
ecma_gc_update_may_ref_younger_object_flag_by_value( lex_env_p, value);
}
case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND:
{

View File

@ -80,7 +80,7 @@ ecma_op_object_get( ecma_object_t *obj_p, /**< the object */
if ( prop_p->type == ECMA_PROPERTY_NAMEDDATA )
{
return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL,
ecma_copy_value( prop_p->u.named_data_property.value),
ecma_copy_value( prop_p->u.named_data_property.value, true),
ECMA_TARGET_ID_RESERVED);
}
else
@ -547,23 +547,15 @@ ecma_op_object_define_own_property( ecma_object_t *obj_p, /**< the object */
property_desc.enumerable,
property_desc.configurable);
new_prop_p->u.named_data_property.value = ecma_copy_value( property_desc.value);
new_prop_p->u.named_data_property.value = ecma_copy_value( property_desc.value, false);
ecma_gc_update_may_ref_younger_object_flag_by_value( obj_p, property_desc.value);
}
else
{
// b.
JERRY_ASSERT( is_property_desc_accessor_descriptor );
if ( property_desc.get_p != NULL )
{
ecma_ref_object( property_desc.get_p);
}
if ( property_desc.set_p != NULL )
{
ecma_ref_object( property_desc.set_p);
}
ecma_create_named_accessor_property( obj_p,
property_name_p,
property_desc.get_p,
@ -757,8 +749,10 @@ ecma_op_object_define_own_property( ecma_object_t *obj_p, /**< the object */
{
JERRY_ASSERT( is_current_data_descriptor );
ecma_free_value( current_p->u.named_data_property.value);
current_p->u.named_data_property.value = ecma_copy_value( property_desc.value);
ecma_free_value( current_p->u.named_data_property.value, false);
current_p->u.named_data_property.value = ecma_copy_value( property_desc.value, false);
ecma_gc_update_may_ref_younger_object_flag_by_value( obj_p, property_desc.value);
}
if ( property_desc.is_writable_defined )
@ -772,16 +766,18 @@ ecma_op_object_define_own_property( ecma_object_t *obj_p, /**< the object */
{
JERRY_ASSERT( is_current_accessor_descriptor );
ecma_ref_object( property_desc.get_p);
ecma_set_pointer( current_p->u.named_accessor_property.get_p, property_desc.get_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( obj_p, property_desc.get_p);
}
if ( property_desc.is_set_defined )
{
JERRY_ASSERT( is_current_accessor_descriptor );
ecma_ref_object( property_desc.set_p);
ecma_set_pointer( current_p->u.named_accessor_property.set_p, property_desc.set_p);
ecma_gc_update_may_ref_younger_object_flag_by_object( obj_p, property_desc.set_p);
}
if ( property_desc.is_enumerable_defined )

View File

@ -86,7 +86,7 @@ ecma_make_reference(ecma_value_t base, /**< base value */
const ecma_char_t *name_p, /**< referenced name */
bool is_strict) /**< strict reference flag */
{
ecma_reference_t ref = (ecma_reference_t) { .base = ecma_copy_value( base),
ecma_reference_t ref = (ecma_reference_t) { .base = ecma_copy_value( base, true),
.referenced_name_p = name_p,
.is_strict = is_strict };
@ -102,7 +102,7 @@ ecma_make_reference(ecma_value_t base, /**< base value */
void
ecma_free_reference( const ecma_reference_t ref) /**< reference */
{
ecma_free_value( ref.base);
ecma_free_value( ref.base, true);
} /* ecma_free_reference */
/**