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:
committed by
Zoltan Herczeg
parent
29f57ec58f
commit
de53adbf88
+40
-26
@@ -166,22 +166,6 @@ jerry_init (jerry_init_flag_t flags) /**< combination of Jerry flags */
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
} /* jerry_init */
|
||||
|
||||
/**
|
||||
* Initialize Jerry engine with custom user context.
|
||||
*/
|
||||
void
|
||||
jerry_init_with_user_context (jerry_init_flag_t flags, /**< combination of Jerry flags */
|
||||
jerry_user_context_init_t init_cb, /**< callback to call to create the user context or
|
||||
* NULL, in which case no user context will be
|
||||
* created */
|
||||
jerry_user_context_deinit_t deinit_cb) /**< callback to call to free the user context or
|
||||
* NULL if it does not need to be freed */
|
||||
{
|
||||
jerry_init (flags);
|
||||
JERRY_CONTEXT (user_context_p) = (init_cb ? init_cb () : NULL);
|
||||
JERRY_CONTEXT (user_context_deinit_cb) = deinit_cb;
|
||||
} /* jerry_init_with_user_context */
|
||||
|
||||
/**
|
||||
* Terminate Jerry engine
|
||||
*/
|
||||
@@ -190,6 +174,15 @@ jerry_cleanup (void)
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p), *next_p = NULL;
|
||||
this_p != NULL;
|
||||
this_p = next_p)
|
||||
{
|
||||
next_p = this_p->next_p;
|
||||
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
|
||||
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
|
||||
}
|
||||
|
||||
ecma_finalize ();
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
@@ -201,23 +194,44 @@ jerry_cleanup (void)
|
||||
|
||||
jmem_finalize ();
|
||||
jerry_make_api_unavailable ();
|
||||
|
||||
if (JERRY_CONTEXT (user_context_deinit_cb))
|
||||
{
|
||||
JERRY_CONTEXT (user_context_deinit_cb) (JERRY_CONTEXT (user_context_p));
|
||||
}
|
||||
} /* jerry_cleanup */
|
||||
|
||||
/**
|
||||
* Retrieve user context.
|
||||
* Retrieve a context data item, or create a new one.
|
||||
*
|
||||
* @return the user-provided context-specific pointer
|
||||
* @param manager_p pointer to the manager whose context data item should be returned.
|
||||
*
|
||||
* @return a pointer to the user-provided context-specific data item for the given manager, creating such a pointer if
|
||||
* none was found.
|
||||
*/
|
||||
void *
|
||||
jerry_get_user_context (void)
|
||||
jerry_get_context_data (const jerry_context_data_manager_t *manager_p)
|
||||
{
|
||||
return JERRY_CONTEXT (user_context_p);
|
||||
} /* jerry_get_user_context */
|
||||
void *ret = NULL;
|
||||
jerry_context_data_header_t *item_p;
|
||||
|
||||
for (item_p = JERRY_CONTEXT (context_data_p); item_p != NULL; item_p = item_p->next_p)
|
||||
{
|
||||
if (item_p->manager_p == manager_p)
|
||||
{
|
||||
return JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
|
||||
}
|
||||
}
|
||||
|
||||
item_p = jmem_heap_alloc_block (sizeof (jerry_context_data_header_t) + manager_p->bytes_needed);
|
||||
item_p->manager_p = manager_p;
|
||||
item_p->next_p = JERRY_CONTEXT (context_data_p);
|
||||
JERRY_CONTEXT (context_data_p) = item_p;
|
||||
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
|
||||
|
||||
memset (ret, 0, manager_p->bytes_needed);
|
||||
if (manager_p->init_cb)
|
||||
{
|
||||
manager_p->init_cb (ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
} /* jerry_get_context_data */
|
||||
|
||||
/**
|
||||
* Register external magic string array
|
||||
|
||||
@@ -195,15 +195,16 @@ typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p);
|
||||
typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_name,
|
||||
const jerry_value_t property_value,
|
||||
void *user_data_p);
|
||||
/**
|
||||
* Function type for user context allocation
|
||||
*/
|
||||
typedef void *(*jerry_user_context_init_t) (void);
|
||||
|
||||
/**
|
||||
* Function type for user context deallocation
|
||||
* User context item manager
|
||||
*/
|
||||
typedef void (*jerry_user_context_deinit_t) (void *user_context_p);
|
||||
typedef struct
|
||||
{
|
||||
void (*init_cb) (void *); /**< callback responsible for initializing a context item, or NULL to zero out the memory */
|
||||
void (*deinit_cb) (void *); /**< callback responsible for deinitializing a context item */
|
||||
size_t bytes_needed; /**< number of bytes to allocate for this manager */
|
||||
} jerry_context_data_manager_t;
|
||||
|
||||
/**
|
||||
* Function type for allocating buffer for JerryScript instance.
|
||||
@@ -227,15 +228,12 @@ typedef struct jerry_instance_t jerry_instance_t;
|
||||
* General engine functions.
|
||||
*/
|
||||
void jerry_init (jerry_init_flag_t flags);
|
||||
void jerry_init_with_user_context (jerry_init_flag_t flags,
|
||||
jerry_user_context_init_t init_cb,
|
||||
jerry_user_context_deinit_t deinit_cb);
|
||||
void jerry_cleanup (void);
|
||||
void jerry_register_magic_strings (const jerry_char_ptr_t *ex_str_items_p, uint32_t count,
|
||||
const jerry_length_t *str_lengths_p);
|
||||
void jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p, size_t *out_stack_limit_p);
|
||||
void jerry_gc (void);
|
||||
void *jerry_get_user_context (void);
|
||||
void *jerry_get_context_data (const jerry_context_data_manager_t *manager_p);
|
||||
|
||||
/**
|
||||
* Parser and executor functions.
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user