Skip to content

Commit 9bb8320

Browse files
committed
Move GC, lcache, literal storage, lexical env global variables to jerry context.
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent e7ec053 commit 9bb8320

File tree

9 files changed

+218
-175
lines changed

9 files changed

+218
-175
lines changed

jerry-core/ecma/base/ecma-gc.c

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ecma-helpers.h"
2525
#include "ecma-lcache.h"
2626
#include "ecma-property-hashmap.h"
27+
#include "jcontext.h"
2728
#include "jrt.h"
2829
#include "jrt-libc-includes.h"
2930
#include "jrt-bit-fields.h"
@@ -44,44 +45,15 @@
4445
*/
4546

4647
/**
47-
* An object's GC color
48+
* Current state of an object's visited flag that
49+
* indicates whether the object is in visited state:
4850
*
49-
* Tri-color marking:
50-
* WHITE_GRAY, unvisited -> WHITE // not referenced by a live object or the reference not found yet
51-
* WHITE_GRAY, visited -> GRAY // referenced by some live object
52-
* BLACK -> BLACK // all referenced objects are gray or black
53-
*/
54-
typedef enum
55-
{
56-
ECMA_GC_COLOR_WHITE_GRAY, /**< white or gray */
57-
ECMA_GC_COLOR_BLACK, /**< black */
58-
ECMA_GC_COLOR__COUNT /**< number of colors */
59-
} ecma_gc_color_t;
60-
61-
/**
62-
* List of marked (visited during current GC session) and umarked objects
63-
*/
64-
static ecma_object_t *ecma_gc_objects_lists[ECMA_GC_COLOR__COUNT];
65-
66-
/**
67-
* Current state of an object's visited flag that indicates whether the object is in visited state:
6851
* visited_field | visited_flip_flag | real_value
6952
* false | false | false
7053
* false | true | true
7154
* true | false | true
7255
* true | true | false
7356
*/
74-
static bool ecma_gc_visited_flip_flag = false;
75-
76-
/**
77-
* Number of currently allocated objects
78-
*/
79-
static size_t ecma_gc_objects_number = 0;
80-
81-
/**
82-
* Number of newly allocated objects since last GC session
83-
*/
84-
static size_t ecma_gc_new_objects_since_last_gc = 0;
8557

8658
static void ecma_gc_mark (ecma_object_t *object_p);
8759
static void ecma_gc_sweep (ecma_object_t *object_p);
@@ -119,7 +91,7 @@ ecma_gc_is_object_visited (ecma_object_t *object_p) /**< object */
11991

12092
bool flag_value = (object_p->type_flags_refs & ECMA_OBJECT_FLAG_GC_VISITED) != 0;
12193

122-
return flag_value != ecma_gc_visited_flip_flag;
94+
return flag_value != JERRY_CONTEXT (ecma_gc_visited_flip_flag);
12395
} /* ecma_gc_is_object_visited */
12496

12597
/**
@@ -131,7 +103,7 @@ ecma_gc_set_object_visited (ecma_object_t *object_p, /**< object */
131103
{
132104
JERRY_ASSERT (object_p != NULL);
133105

134-
if (is_visited != ecma_gc_visited_flip_flag)
106+
if (is_visited != JERRY_CONTEXT (ecma_gc_visited_flip_flag))
135107
{
136108
object_p->type_flags_refs = (uint16_t) (object_p->type_flags_refs | ECMA_OBJECT_FLAG_GC_VISITED);
137109
}
@@ -147,16 +119,16 @@ ecma_gc_set_object_visited (ecma_object_t *object_p, /**< object */
147119
inline void
148120
ecma_init_gc_info (ecma_object_t *object_p) /**< object */
149121
{
150-
ecma_gc_objects_number++;
151-
ecma_gc_new_objects_since_last_gc++;
122+
JERRY_CONTEXT (ecma_gc_objects_number)++;
123+
JERRY_CONTEXT (ecma_gc_new_objects)++;
152124

153-
JERRY_ASSERT (ecma_gc_new_objects_since_last_gc <= ecma_gc_objects_number);
125+
JERRY_ASSERT (JERRY_CONTEXT (ecma_gc_new_objects) <= JERRY_CONTEXT (ecma_gc_objects_number));
154126

155127
JERRY_ASSERT (object_p->type_flags_refs < ECMA_OBJECT_REF_ONE);
156128
object_p->type_flags_refs = (uint16_t) (object_p->type_flags_refs | ECMA_OBJECT_REF_ONE);
157129

158-
ecma_gc_set_object_next (object_p, ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]);
159-
ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = object_p;
130+
ecma_gc_set_object_next (object_p, JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]));
131+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]) = object_p;
160132

161133
/* Should be set to false at the beginning of garbage collection */
162134
ecma_gc_set_object_visited (object_p, false);
@@ -194,11 +166,11 @@ ecma_deref_object (ecma_object_t *object_p) /**< object */
194166
void
195167
ecma_gc_init (void)
196168
{
197-
ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = NULL;
198-
ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] = NULL;
199-
ecma_gc_visited_flip_flag = false;
200-
ecma_gc_objects_number = 0;
201-
ecma_gc_new_objects_since_last_gc = 0;
169+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]) = NULL;
170+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]) = NULL;
171+
JERRY_CONTEXT (ecma_gc_visited_flip_flag) = false;
172+
JERRY_CONTEXT (ecma_gc_objects_number) = 0;
173+
JERRY_CONTEXT (ecma_gc_new_objects) = 0;
202174
} /* ecma_gc_init */
203175

204176
/**
@@ -474,8 +446,8 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
474446
}
475447
}
476448

477-
JERRY_ASSERT (ecma_gc_objects_number > 0);
478-
ecma_gc_objects_number--;
449+
JERRY_ASSERT (JERRY_CONTEXT (ecma_gc_objects_number) > 0);
450+
JERRY_CONTEXT (ecma_gc_objects_number)--;
479451

480452
if (!ecma_is_lexical_environment (object_p))
481453
{
@@ -508,12 +480,12 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
508480
void
509481
ecma_gc_run (jmem_free_unused_memory_severity_t severity) /**< gc severity */
510482
{
511-
ecma_gc_new_objects_since_last_gc = 0;
483+
JERRY_CONTEXT (ecma_gc_new_objects) = 0;
512484

513-
JERRY_ASSERT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] == NULL);
485+
JERRY_ASSERT (JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]) == NULL);
514486

515487
/* if some object is referenced from stack or globals (i.e. it is root), mark it */
516-
for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY];
488+
for (ecma_object_t *obj_iter_p = JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]);
517489
obj_iter_p != NULL;
518490
obj_iter_p = ecma_gc_get_object_next (obj_iter_p))
519491
{
@@ -531,17 +503,18 @@ ecma_gc_run (jmem_free_unused_memory_severity_t severity) /**< gc severity */
531503
{
532504
marked_anything_during_current_iteration = false;
533505

534-
for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY], *obj_prev_p = NULL, *obj_next_p;
535-
obj_iter_p != NULL;
536-
obj_iter_p = obj_next_p)
506+
ecma_object_t *obj_prev_p = NULL;
507+
ecma_object_t *obj_iter_p = JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]);
508+
509+
while (obj_iter_p != NULL)
537510
{
538-
obj_next_p = ecma_gc_get_object_next (obj_iter_p);
511+
ecma_object_t *obj_next_p = ecma_gc_get_object_next (obj_iter_p);
539512

540513
if (ecma_gc_is_object_visited (obj_iter_p))
541514
{
542515
/* Moving the object to list of marked objects */
543-
ecma_gc_set_object_next (obj_iter_p, ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]);
544-
ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] = obj_iter_p;
516+
ecma_gc_set_object_next (obj_iter_p, JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]));
517+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]) = obj_iter_p;
545518

546519
if (likely (obj_prev_p != NULL))
547520
{
@@ -551,7 +524,7 @@ ecma_gc_run (jmem_free_unused_memory_severity_t severity) /**< gc severity */
551524
}
552525
else
553526
{
554-
ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = obj_next_p;
527+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]) = obj_next_p;
555528
}
556529

557530
ecma_gc_mark (obj_iter_p);
@@ -561,20 +534,23 @@ ecma_gc_run (jmem_free_unused_memory_severity_t severity) /**< gc severity */
561534
{
562535
obj_prev_p = obj_iter_p;
563536
}
537+
538+
obj_iter_p = obj_next_p;
564539
}
565540
}
566541
while (marked_anything_during_current_iteration);
567542

568543
/* Sweeping objects that are currently unmarked */
569-
for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY], *obj_next_p;
570-
obj_iter_p != NULL;
571-
obj_iter_p = obj_next_p)
544+
ecma_object_t *obj_iter_p = JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]);
545+
546+
while (obj_iter_p != NULL)
572547
{
573-
obj_next_p = ecma_gc_get_object_next (obj_iter_p);
548+
ecma_object_t *obj_next_p = ecma_gc_get_object_next (obj_iter_p);
574549

575550
JERRY_ASSERT (!ecma_gc_is_object_visited (obj_iter_p));
576551

577552
ecma_gc_sweep (obj_iter_p);
553+
obj_iter_p = obj_next_p;
578554
}
579555

580556
if (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_HIGH)
@@ -598,10 +574,11 @@ ecma_gc_run (jmem_free_unused_memory_severity_t severity) /**< gc severity */
598574
}
599575

600576
/* Unmarking all objects */
601-
ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK];
602-
ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] = NULL;
577+
ecma_object_t *black_objects = JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]);
578+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]) = black_objects;
579+
JERRY_CONTEXT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]) = NULL;
603580

604-
ecma_gc_visited_flip_flag = !ecma_gc_visited_flip_flag;
581+
JERRY_CONTEXT (ecma_gc_visited_flip_flag) = !JERRY_CONTEXT (ecma_gc_visited_flip_flag);
605582

606583
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
607584
/* Free RegExp bytecodes stored in cache */
@@ -621,7 +598,9 @@ ecma_free_unused_memory (jmem_free_unused_memory_severity_t severity) /**< sever
621598
* If there is enough newly allocated objects since last GC, probably it is worthwhile to start GC now.
622599
* Otherwise, probability to free sufficient space is considered to be low.
623600
*/
624-
if (ecma_gc_new_objects_since_last_gc * CONFIG_ECMA_GC_NEW_OBJECTS_SHARE_TO_START_GC > ecma_gc_objects_number)
601+
size_t new_objects_share = CONFIG_ECMA_GC_NEW_OBJECTS_SHARE_TO_START_GC;
602+
603+
if (JERRY_CONTEXT (ecma_gc_new_objects) * new_objects_share > JERRY_CONTEXT (ecma_gc_objects_number))
625604
{
626605
ecma_gc_run (severity);
627606
}

jerry-core/ecma/base/ecma-globals.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,64 @@ typedef struct
911911
* If regexp, the other flags must be RE_FLAG... */
912912
} ecma_compiled_code_t;
913913

914+
/**
915+
* An object's GC color
916+
*
917+
* Tri-color marking:
918+
* WHITE_GRAY, unvisited -> WHITE // not referenced by a live object or the reference not found yet
919+
* WHITE_GRAY, visited -> GRAY // referenced by some live object
920+
* BLACK -> BLACK // all referenced objects are gray or black
921+
*/
922+
typedef enum
923+
{
924+
ECMA_GC_COLOR_WHITE_GRAY, /**< white or gray */
925+
ECMA_GC_COLOR_BLACK, /**< black */
926+
ECMA_GC_COLOR__COUNT /**< number of colors */
927+
} ecma_gc_color_t;
928+
929+
/**
930+
* Number of values in a literal storage item
931+
*/
932+
#define ECMA_LIT_STORAGE_VALUE_COUNT 3
933+
934+
/**
935+
* Literal storage item
936+
*/
937+
typedef struct
938+
{
939+
jmem_cpointer_t next_cp; /**< cpointer ot next item */
940+
jmem_cpointer_t values[ECMA_LIT_STORAGE_VALUE_COUNT]; /**< list of values */
941+
} ecma_lit_storage_item_t;
942+
943+
#ifndef CONFIG_ECMA_LCACHE_DISABLE
944+
945+
/**
946+
* Entry of LCache hash table
947+
*/
948+
typedef struct
949+
{
950+
/** Pointer to a property of the object */
951+
ecma_property_t *prop_p;
952+
953+
/** Compressed pointer to object (ECMA_NULL_POINTER marks record empty) */
954+
jmem_cpointer_t object_cp;
955+
956+
/** Compressed pointer to property's name */
957+
jmem_cpointer_t prop_name_cp;
958+
} ecma_lcache_hash_entry_t;
959+
960+
/**
961+
* Number of rows in LCache's hash table
962+
*/
963+
#define ECMA_LCACHE_HASH_ROWS_COUNT 128
964+
965+
/**
966+
* Number of entries in a row of LCache's hash table
967+
*/
968+
#define ECMA_LCACHE_HASH_ROW_LENGTH 2
969+
970+
#endif /* !CONFIG_ECMA_LCACHE_DISABLE */
971+
914972
/**
915973
* @}
916974
* @}

jerry-core/ecma/base/ecma-init-finalize.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ ecma_init (void)
3939
ecma_init_builtins ();
4040
ecma_lcache_init ();
4141
ecma_init_lit_storage ();
42-
ecma_init_environment ();
42+
ecma_init_global_lex_env ();
4343

4444
jmem_register_free_unused_memory_callback (ecma_free_unused_memory);
4545
} /* ecma_init */
@@ -52,7 +52,7 @@ ecma_finalize (void)
5252
{
5353
jmem_unregister_free_unused_memory_callback (ecma_free_unused_memory);
5454

55-
ecma_finalize_environment ();
55+
ecma_finalize_global_lex_env ();
5656
ecma_finalize_builtins ();
5757
ecma_gc_run (JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW);
5858
ecma_finalize_lit_storage ();

0 commit comments

Comments
 (0)