Skip to content

Commit 11949c8

Browse files
author
Martijn The
committed
Add finalize_cb to jerry_context_data_manager_t
This patch adds a new finalize_cb callback to jerry_context_data_manager_t. The callback is run as the very last thing in jerry_cleanup, after the VM has been torn down entirely. There was already the deinit_cb, which is run while the VM is still in the process of being torn down. The reason the deinit_cb is not always sufficient is that there may still be objects alive (because they still being referenced) that have native pointers associated with the context manager that is being deinit'ed. As a result, the free_cb's for those objects can get called *after* the associated context manager's deinit_cb is run. This makes cleanup of manager state that is depended on by the live objects impossible to do in the deinit_cb. That type of cleanup can only be done when all values have been torn down completely. JerryScript-DCO-1.0-Signed-off-by: Martijn The martijn.the@intel.com
1 parent 634d9d3 commit 11949c8

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

jerry-core/api/jerry.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,31 @@ jerry_cleanup (void)
210210
}
211211
#endif /* JERRY_DEBUGGER */
212212

213-
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p), *next_p = NULL;
213+
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p);
214214
this_p != NULL;
215-
this_p = next_p)
215+
this_p = this_p->next_p)
216216
{
217-
next_p = this_p->next_p;
218217
if (this_p->manager_p->deinit_cb)
219218
{
220219
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
221220
}
222-
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
223221
}
224222

225223
ecma_finalize ();
226224
jmem_finalize ();
227225
jerry_make_api_unavailable ();
226+
227+
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p), *next_p = NULL;
228+
this_p != NULL;
229+
this_p = next_p)
230+
{
231+
next_p = this_p->next_p;
232+
if (this_p->manager_p->finalize_cb)
233+
{
234+
this_p->manager_p->finalize_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
235+
}
236+
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
237+
}
228238
} /* jerry_cleanup */
229239

230240
/**

jerry-core/include/jerryscript-core.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,44 @@ typedef bool (*jerry_objects_foreach_by_native_info_t) (const jerry_value_t obje
228228
*/
229229
typedef struct
230230
{
231-
void (*init_cb) (void *); /**< callback responsible for initializing a context item, or NULL to zero out the memory */
232-
void (*deinit_cb) (void *); /**< callback responsible for deinitializing a context item, or NULL */
233-
size_t bytes_needed; /**< number of bytes to allocate for this manager */
231+
/**
232+
* Callback responsible for initializing a context item, or NULL to zero out the memory. This is called lazily, the
233+
* first time jerry_get_context_data () is called with this manager.
234+
*
235+
* @param [in] data The buffer that JerryScript allocated for the manager. The buffer is zeroed out. The size is
236+
* determined by the bytes_needed field. The buffer is kept alive until jerry_cleanup () is called.
237+
*/
238+
void (*init_cb) (void *data);
239+
240+
/**
241+
* Callback responsible for deinitializing a context item, or NULL. This is called as part of jerry_cleanup (),
242+
* right *before* the VM has been cleaned up. This is a good place to release strong references to jerry_value_t's
243+
* that the manager may be holding.
244+
* Note: because the VM has not been fully cleaned up yet, jerry_object_native_info_t free_cb's can still get called
245+
* *after* all deinit_cb's have been run. See finalize_cb for a callback that is guaranteed to run *after* all
246+
* free_cb's have been run.
247+
*
248+
* @param [in] data The buffer that JerryScript allocated for the manager.
249+
*/
250+
void (*deinit_cb) (void *data);
251+
252+
/**
253+
* Callback responsible for finalizing a context item, or NULL. This is called as part of jerry_cleanup (),
254+
* right *after* the VM has been cleaned up and destroyed and jerry_... APIs cannot be called any more. At this point,
255+
* all values in the VM have been cleaned up. This is a good place to clean up native state that can only be cleaned
256+
* up at the very end when there are no more VM values around that may need to access that state.
257+
*
258+
* @param [in] data The buffer that JerryScript allocated for the manager. After returning from this callback,
259+
* the data pointer may no longer be used.
260+
*/
261+
void (*finalize_cb) (void *data);
262+
263+
/**
264+
* Number of bytes to allocate for this manager. This is the size of the buffer that JerryScript will allocate on
265+
* behalf of the manager. The pointer to this buffer is passed into init_cb, deinit_cb and finalize_cb. It is also
266+
* returned from the jerry_get_context_data () API.
267+
*/
268+
size_t bytes_needed;
234269
} jerry_context_data_manager_t;
235270

236271
/**

0 commit comments

Comments
 (0)