Skip to content

Commit 10cdc5d

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 10cdc5d

File tree

4 files changed

+108
-13
lines changed

4 files changed

+108
-13
lines changed

docs/02.API-REFERENCE.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,53 @@ typedef uint32_t jerry_value_t;
123123

124124
Structure that defines how a context data item will be initialized and deinitialized. JerryScript zeroes out the memory
125125
for the item by default, and if the `init_cb` field is not NULL, it will be called with the pointer to the memory as
126-
an additional custom initializer. The `deinit_cb` (if non-`NULL`) is called during a call to `jerry_cleanup()` to run
127-
any custom deinitialization.
126+
an additional custom initializer. The `deinit_cb` (if non-`NULL`) is called during a call to `jerry_cleanup ()` to run
127+
any custom deinitialization *before* the VM has been fully cleaned up. The `finalize_cb` (if non-`NULL`) is also called
128+
during a call to `jerry_cleanup ()` to run any custom deinitialization *after* the VM has been fully cleaned up.
128129

129130
**Prototype**
130131

131132
```c
132133
typedef struct
133134
{
134-
void (*init_cb) (void *); /**< callback responsible for initializing a context item, or NULL */
135-
void (*deinit_cb) (void *); /**< callback responsible for deinitializing a context item, or NULL */
136-
size_t bytes_needed; /**< number of bytes to allocate for this manager */
135+
/**
136+
* Callback responsible for initializing a context item, or NULL to zero out the memory. This is called lazily, the
137+
* first time jerry_get_context_data () is called with this manager.
138+
*
139+
* @param [in] data The buffer that JerryScript allocated for the manager. The buffer is zeroed out. The size is
140+
* determined by the bytes_needed field. The buffer is kept alive until jerry_cleanup () is called.
141+
*/
142+
void (*init_cb) (void *data);
143+
144+
/**
145+
* Callback responsible for deinitializing a context item, or NULL. This is called as part of jerry_cleanup (),
146+
* right *before* the VM has been cleaned up. This is a good place to release strong references to jerry_value_t's
147+
* that the manager may be holding.
148+
* Note: because the VM has not been fully cleaned up yet, jerry_object_native_info_t free_cb's can still get called
149+
* *after* all deinit_cb's have been run. See finalize_cb for a callback that is guaranteed to run *after* all
150+
* free_cb's have been run.
151+
*
152+
* @param [in] data The buffer that JerryScript allocated for the manager.
153+
*/
154+
void (*deinit_cb) (void *data);
155+
156+
/**
157+
* Callback responsible for finalizing a context item, or NULL. This is called as part of jerry_cleanup (),
158+
* right *after* the VM has been cleaned up and destroyed and jerry_... APIs cannot be called any more. At this point,
159+
* all values in the VM have been cleaned up. This is a good place to clean up native state that can only be cleaned
160+
* up at the very end when there are no more VM values around that may need to access that state.
161+
*
162+
* @param [in] data The buffer that JerryScript allocated for the manager. After returning from this callback,
163+
* the data pointer may no longer be used.
164+
*/
165+
void (*finalize_cb) (void *data);
166+
167+
/**
168+
* Number of bytes to allocate for this manager. This is the size of the buffer that JerryScript will allocate on
169+
* behalf of the manager. The pointer to this buffer is passed into init_cb, deinit_cb and finalize_cb. It is also
170+
* returned from the jerry_get_context_data () API.
171+
*/
172+
size_t bytes_needed;
137173
} jerry_context_data_manager_t;
138174
```
139175

jerry-core/api/jerry.c

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

213+
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p);
214+
this_p != NULL;
215+
this_p = this_p->next_p)
216+
{
217+
if (this_p->manager_p->deinit_cb)
218+
{
219+
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
220+
}
221+
}
222+
223+
ecma_finalize ();
224+
jerry_make_api_unavailable ();
225+
213226
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p), *next_p = NULL;
214227
this_p != NULL;
215228
this_p = next_p)
216229
{
217230
next_p = this_p->next_p;
218-
if (this_p->manager_p->deinit_cb)
231+
if (this_p->manager_p->finalize_cb)
219232
{
220-
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
233+
this_p->manager_p->finalize_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
221234
}
222235
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
223236
}
224237

225-
ecma_finalize ();
226238
jmem_finalize ();
227-
jerry_make_api_unavailable ();
228239
} /* jerry_cleanup */
229240

230241
/**

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
/**

tests/unit-core/test-context-data.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
static bool test_context_data1_new_called = false;
2121
static bool test_context_data2_new_called = false;
22+
static bool test_context_data3_new_called = false;
2223
static bool test_context_data1_free_called = false;
2324
static bool test_context_data2_free_called = false;
24-
static bool test_context_data3_new_called = false;
25+
static bool test_context_data1_finalize_called = false;
2526

2627
/* Context item 1 */
2728
const char *string1 = "item1";
@@ -38,12 +39,23 @@ test_context_data1_free (void *user_data_p)
3839
{
3940
test_context_data1_free_called = true;
4041
TEST_ASSERT ((*(const char **) user_data_p) == string1);
42+
TEST_ASSERT (!test_context_data1_finalize_called);
4143
} /* test_context_data1_free */
4244

45+
static void
46+
test_context_data1_finalize (void *user_data_p)
47+
{
48+
TEST_ASSERT (test_context_data1_free_called);
49+
TEST_ASSERT (!test_context_data1_finalize_called);
50+
TEST_ASSERT ((*(const char **) user_data_p) == string1);
51+
test_context_data1_finalize_called = true;
52+
} /* test_context_data1_finalize */
53+
4354
static const jerry_context_data_manager_t manager1 =
4455
{
4556
.init_cb = test_context_data1_new,
4657
.deinit_cb = test_context_data1_free,
58+
.finalize_cb = test_context_data1_finalize,
4759
.bytes_needed = sizeof (const char *)
4860
};
4961

@@ -86,6 +98,7 @@ static const jerry_context_data_manager_t manager3 =
8698
.init_cb = test_context_data3_new,
8799
/* NULL is allowed: */
88100
.deinit_cb = NULL,
101+
.finalize_cb = NULL,
89102
.bytes_needed = 0,
90103
};
91104

0 commit comments

Comments
 (0)