Lazily create a linked list of context items (#1833)

This approach has the benefit that it does not require any *a priori*
initialization, and that each context pointer is identified by the way
in which it was created. Additionally, retrieving the context pointer
now requires that the entity responsible for creating/destroying it
(the manager) be given. Since managers are stored in global static
const structures, they should not normally be visible across source
files, and thus there should be no danger that a context item will be
retrieved by the wrong manager and thus cast into the wrong data type.

Since the items are stored in a linked list, their number will be
limited to exactly as many as are needed for a given context, with the
caveat that storing too many on a context will cause slow retrieval.

Thanks @mhdawson for the idea!

Fixes https://github.com/jerryscript-project/jerryscript/issues/1845

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof gabriel.schulhof@intel.com
This commit is contained in:
Gabriel "_|Nix|_" Schulhof
2017-05-24 15:10:18 +03:00
committed by Zoltan Herczeg
parent 29f57ec58f
commit de53adbf88
6 changed files with 233 additions and 185 deletions
+14 -2
View File
@@ -26,6 +26,7 @@
#include "jmem.h"
#include "re-bytecode.h"
#include "vm-defines.h"
#include "jerryscript.h"
/** \addtogroup context Context
* @{
@@ -36,6 +37,18 @@
*/
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects
/**
* User context item
*/
typedef struct jerry_context_data_header
{
struct jerry_context_data_header *next_p; /**< pointer to next context item */
const jerry_context_data_manager_t *manager_p; /**< manager responsible for deleting this item */
} jerry_context_data_header_t;
#define JERRY_CONTEXT_DATA_HEADER_USER_DATA(item_p) \
((uint8_t *) (item_p + 1))
/**
* JerryScript context
*
@@ -63,8 +76,7 @@ typedef struct
ecma_lit_storage_item_t *number_list_first_p; /**< first item of the literal number list */
ecma_object_t *ecma_global_lex_env_p; /**< global lexical environment */
vm_frame_ctx_t *vm_top_context_p; /**< top (current) interpreter context */
void *user_context_p; /**< user-provided context-specific pointer */
ecma_user_context_deinit_t user_context_deinit_cb; /**< user-provided deleter for context-specific pointer */
jerry_context_data_header_t *context_data_p; /**< linked list of user-provided context-specific pointers */
size_t ecma_gc_objects_number; /**< number of currently allocated objects */
size_t ecma_gc_new_objects; /**< number of newly allocated objects since last GC session */
size_t jmem_heap_allocated_size; /**< size of allocated regions */