Skip to content

Commit cc44908

Browse files
committed
Add info to external pointer free callback.
Furthermore reduce memory consumption when only one external pointer is assigned to an object. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent 874a6a4 commit cc44908

28 files changed

+287
-118
lines changed

docs/02.API-REFERENCE.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -696,29 +696,51 @@ typedef jerry_value_t (*jerry_external_handler_t) (const jerry_call_info_t *call
696696

697697
- [jerry_create_external_function](#jerry_create_external_function)
698698

699+
## jerry_value_free_callback_t
700+
701+
**Summary**
702+
703+
Native free callback of generic value types.
704+
705+
*Note*:
706+
- Referred values by this method must have at least 1 reference. (Correct API usage satisfies this condition)
707+
708+
**Prototype**
709+
710+
```c
711+
typedef void (*jerry_value_free_callback_t) (void *native_p);
712+
```
713+
714+
*New in version [[NEXT_RELEASE]]*.
715+
716+
**See also**
717+
718+
- [jerry_create_external_string](#jerry_create_external_string)
719+
- [jerry_create_external_string_sz](#jerry_create_external_string_sz)
720+
- [jerry_create_arraybuffer_external](#jerry_create_arraybuffer_external)
721+
699722
## jerry_object_native_free_callback_t
700723

701724
**Summary**
702725

703-
Native free callback of an object. It is used in `jerry_object_native_info_t` and for external Array buffers.
726+
Native free callback of an object. It is part of the type information provided by `jerry_object_native_info_t`.
704727

705728
*Note*:
706729
- Referred values by this method must have at least 1 reference. (Correct API usage satisfies this condition)
707730

708731
**Prototype**
709732

710733
```c
711-
typedef void (*jerry_object_native_free_callback_t) (void *native_p);
734+
typedef void (*jerry_object_native_free_callback_t) (void *native_p, struct jerry_object_native_info_t *info_p);
712735
```
713736

714737
*New in version 2.0*: Renamed from `jerry_object_free_callback_t`.
715-
716738
*Changed in version 2.2*: API calls are once again allowed. (See note)
739+
*Changed in version [[NEXT_RELEASE]]*: `info_p` argument is added
717740

718741
**See also**
719742

720743
- [jerry_object_native_info_t](#jerry_object_native_info_t)
721-
- [jerry_create_arraybuffer_external](#jerry_create_arraybuffer_external)
722744

723745
## jerry_error_object_created_callback_t
724746

@@ -5184,7 +5206,7 @@ so the user can release the buffer which was provided.
51845206
jerry_value_t
51855207
jerry_create_arraybuffer_external (const jerry_length_t size
51865208
uint8_t *buffer_p,
5187-
jerry_object_native_free_callback_t free_cb);
5209+
jerry_value_free_callback_t free_cb);
51885210
```
51895211

51905212
- `size` - size of the buffer to use **in bytes** (should not be 0)
@@ -5195,6 +5217,7 @@ jerry_create_arraybuffer_external (const jerry_length_t size
51955217
- if the `size` is zero or `buffer_p` is a null pointer this will return an empty ArrayBuffer.
51965218

51975219
*New in version 2.0*.
5220+
*Changed in version [[NEXT_RELEASE]]*: type of `free_cb` has been changed.
51985221

51995222
**Example**
52005223

@@ -6002,14 +6025,15 @@ is no longer needed.
60026025
```c
60036026
jerry_value_t
60046027
jerry_create_external_string (const jerry_char_t *str_p,
6005-
jerry_object_native_free_callback_t free_cb)
6028+
jerry_value_free_callback_t free_cb)
60066029
```
60076030

60086031
- `str_p` - non-null pointer to string
60096032
- `free_cb` - optional callback which is called right before the string is freed
60106033
- return value - value of the created string
60116034

60126035
*New in version 2.4*.
6036+
*Changed in version [[NEXT_RELEASE]]*: type of `free_cb` has been changed.
60136037

60146038
**Example**
60156039

@@ -6048,7 +6072,7 @@ is no longer needed.
60486072
jerry_value_t
60496073
jerry_create_external_string_sz (const jerry_char_t *str_p,
60506074
jerry_size_t str_size,
6051-
jerry_object_native_free_callback_t free_cb)
6075+
jerry_value_free_callback_t free_cb)
60526076
```
60536077

60546078
- `str_p` - non-null pointer to string
@@ -6057,6 +6081,7 @@ jerry_create_external_string_sz (const jerry_char_t *str_p,
60576081
- return value - value of the created string
60586082

60596083
*New in version 2.4*.
6084+
*Changed in version [[NEXT_RELEASE]]*: type of `free_cb` has been changed.
60606085

60616086
**Example**
60626087

@@ -7945,8 +7970,11 @@ typedef struct
79457970
#define SECRET_INFO ((void *) 42)
79467971

79477972
static void
7948-
buffer_native_freecb (void *native_p)
7973+
buffer_native_freecb (void *native_p,
7974+
jerry_object_native_info_t *info_p)
79497975
{
7976+
(void) info_p;
7977+
79507978
char *data_p = ((buffer_native_object_t*)native_p)->data_p;
79517979

79527980
if (data_p != NULL)
@@ -7958,14 +7986,21 @@ buffer_native_freecb (void *native_p)
79587986
}
79597987

79607988
static void
7961-
shape_native_freecb (void *native_p)
7989+
shape_native_freecb (void *native_p,
7990+
jerry_object_native_info_t *info_p)
79627991
{
7992+
(void) info_p;
7993+
79637994
free (native_p);
79647995
}
79657996

79667997
static void
7967-
destructor_freecb (void *native_p)
7998+
destructor_freecb (void *native_p,
7999+
jerry_object_native_info_t *info_p)
79688000
{
8001+
(void) native_p;
8002+
(void) info_p;
8003+
79698004
printf("Note: the object has been freed\n");
79708005
}
79718006

@@ -8517,7 +8552,8 @@ typedef struct
85178552
int match_foo_value;
85188553
} find_object_data_t;
85198554

8520-
static void native_freecb (void *native_p)
8555+
static void native_freecb (void *native_p,
8556+
jerry_object_native_info_t *info_p)
85218557
{
85228558
/* `native_p` was allocated via malloc. */
85238559
free (native_p);

jerry-core/api/jerry.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ jerry_create_string_sz (const jerry_char_t *str_p, /**< pointer to string */
24032403
*/
24042404
jerry_value_t
24052405
jerry_create_external_string (const jerry_char_t *str_p, /**< pointer to string */
2406-
jerry_object_native_free_callback_t free_cb) /**< free callback */
2406+
jerry_value_free_callback_t free_cb) /**< free callback */
24072407
{
24082408
return jerry_create_external_string_sz (str_p, lit_zt_utf8_string_size ((lit_utf8_byte_t *) str_p), free_cb);
24092409
} /* jerry_create_external_string */
@@ -2419,7 +2419,7 @@ jerry_create_external_string (const jerry_char_t *str_p, /**< pointer to string
24192419
jerry_value_t
24202420
jerry_create_external_string_sz (const jerry_char_t *str_p, /**< pointer to string */
24212421
jerry_size_t str_size, /**< string size */
2422-
jerry_object_native_free_callback_t free_cb) /**< free callback */
2422+
jerry_value_free_callback_t free_cb) /**< free callback */
24232423
{
24242424
jerry_assert_api_available ();
24252425

@@ -3950,7 +3950,7 @@ jerry_objects_foreach_by_native_info (const jerry_object_native_info_t *native_i
39503950
{
39513951
native_pointer_p = ecma_get_native_pointer_value (iter_p, (void *) native_info_p);
39523952
if (native_pointer_p
3953-
&& !foreach_p (ecma_make_object_value (iter_p), native_pointer_p->data_p, user_data_p))
3953+
&& !foreach_p (ecma_make_object_value (iter_p), native_pointer_p->native_p, user_data_p))
39543954
{
39553955
return true;
39563956
}
@@ -3994,7 +3994,7 @@ jerry_get_object_native_pointer (const jerry_value_t obj_val, /**< object to get
39943994

39953995
if (out_native_pointer_p != NULL)
39963996
{
3997-
*out_native_pointer_p = native_pointer_p->data_p;
3997+
*out_native_pointer_p = native_pointer_p->native_p;
39983998
}
39993999

40004000
return true;
@@ -5139,7 +5139,7 @@ jerry_create_arraybuffer (const jerry_length_t size) /**< size of the ArrayBuffe
51395139
jerry_value_t
51405140
jerry_create_arraybuffer_external (const jerry_length_t size, /**< size of the buffer to used */
51415141
uint8_t *buffer_p, /**< buffer to use as the ArrayBuffer's backing */
5142-
jerry_object_native_free_callback_t free_cb) /**< buffer free callback */
5142+
jerry_value_free_callback_t free_cb) /**< buffer free callback */
51435143
{
51445144
jerry_assert_api_available ();
51455145

@@ -5152,9 +5152,7 @@ jerry_create_arraybuffer_external (const jerry_length_t size, /**< size of the b
51525152
}
51535153
else
51545154
{
5155-
arraybuffer = ecma_arraybuffer_new_object_external (size,
5156-
buffer_p,
5157-
(ecma_object_native_free_callback_t) free_cb);
5155+
arraybuffer = ecma_arraybuffer_new_object_external (size, buffer_p, free_cb);
51585156
}
51595157

51605158
return jerry_return (ecma_make_object_value (arraybuffer));

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,36 +1086,52 @@ ecma_gc_free_native_pointer (ecma_property_t *property_p) /**< property */
10861086

10871087
ecma_property_value_t *value_p = ECMA_PROPERTY_VALUE_PTR (property_p);
10881088

1089-
if (value_p->value == JMEM_CP_NULL)
1089+
if (JERRY_LIKELY (*property_p & ECMA_PROPERTY_FLAG_SINGLE_EXTERNAL))
10901090
{
1091+
JERRY_ASSERT (value_p->value != JMEM_CP_NULL);
1092+
1093+
ecma_native_pointer_t *native_pointer_p;
1094+
native_pointer_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_native_pointer_t,
1095+
value_p->value);
1096+
1097+
jerry_object_native_free_callback_t free_cb = native_pointer_p->info_p->free_cb;
1098+
1099+
if (free_cb != NULL)
1100+
{
1101+
free_cb (native_pointer_p->native_p, native_pointer_p->info_p);
1102+
}
1103+
1104+
jmem_heap_free_block (native_pointer_p, sizeof (ecma_native_pointer_t));
10911105
return;
10921106
}
10931107

1094-
ecma_native_pointer_t *native_pointer_p;
1108+
if (value_p->value == JMEM_CP_NULL)
1109+
{
1110+
return;
1111+
}
10951112

1096-
native_pointer_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_native_pointer_t,
1097-
value_p->value);
1098-
JERRY_ASSERT (native_pointer_p != NULL);
1113+
ecma_native_pointer_chain_t *item_p;
1114+
item_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_native_pointer_chain_t, value_p->value);
10991115

11001116
do
11011117
{
1102-
if (native_pointer_p->info_p != NULL)
1118+
if (item_p->data.info_p != NULL)
11031119
{
1104-
ecma_object_native_free_callback_t free_cb = native_pointer_p->info_p->free_cb;
1120+
jerry_object_native_free_callback_t free_cb = item_p->data.info_p->free_cb;
11051121

11061122
if (free_cb != NULL)
11071123
{
1108-
free_cb (native_pointer_p->data_p);
1124+
free_cb (item_p->data.native_p, item_p->data.info_p);
11091125
}
11101126
}
11111127

1112-
ecma_native_pointer_t *next_p = native_pointer_p->next_p;
1128+
ecma_native_pointer_chain_t *next_p = item_p->next_p;
11131129

1114-
jmem_heap_free_block (native_pointer_p, sizeof (ecma_native_pointer_t));
1130+
jmem_heap_free_block (item_p, sizeof (ecma_native_pointer_chain_t));
11151131

1116-
native_pointer_p = next_p;
1132+
item_p = next_p;
11171133
}
1118-
while (native_pointer_p != NULL);
1134+
while (item_p != NULL);
11191135
} /* ecma_gc_free_native_pointer */
11201136

11211137
/**

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,22 @@ typedef ecma_value_t (*ecma_native_handler_t) (const struct jerry_call_info_t *c
309309
const uint32_t args_count);
310310

311311
/**
312-
* Native free callback of an object.
313-
*/
314-
typedef void (*ecma_object_native_free_callback_t) (void *native_p);
315-
316-
/**
317-
* Type information of a native pointer.
312+
* Representation for native pointer data.
318313
*/
319314
typedef struct
320315
{
321-
ecma_object_native_free_callback_t free_cb; /**< the free callback of the native pointer */
322-
} ecma_object_native_info_t;
316+
void *native_p; /**< points to the data of the object */
317+
jerry_object_native_info_t *info_p; /**< native info */
318+
} ecma_native_pointer_t;
323319

324320
/**
325-
* Representation for native pointer data.
321+
* Representation for native pointer data chain.
326322
*/
327-
typedef struct ecma_native_pointer_t
323+
typedef struct ecma_native_pointer_chain_t
328324
{
329-
void *data_p; /**< points to the data of the object */
330-
ecma_object_native_info_t *info_p; /**< native info */
331-
struct ecma_native_pointer_t *next_p; /**< points to the next ecma_native_pointer_t element */
332-
} ecma_native_pointer_t;
325+
ecma_native_pointer_t data; /**< pointer data */
326+
struct ecma_native_pointer_chain_t *next_p; /**< next in the list */
327+
} ecma_native_pointer_chain_t;
333328

334329
/**
335330
* Option bits for ecma_parse_options_t.
@@ -425,7 +420,7 @@ typedef enum
425420
ECMA_PROPERTY_FLAG_CONFIGURABLE = (1u << 0), /**< property is configurable */
426421
ECMA_PROPERTY_FLAG_ENUMERABLE = (1u << 1), /**< property is enumerable */
427422
ECMA_PROPERTY_FLAG_WRITABLE = (1u << 2), /**< property is writable */
428-
423+
ECMA_PROPERTY_FLAG_SINGLE_EXTERNAL = (1u << 2), /**< only one external pointer is assigned to this object */
429424
ECMA_PROPERTY_FLAG_DELETED = (1u << 3), /**< property is deleted */
430425
ECMA_FAST_ARRAY_FLAG = (1u << 3), /**< array is fast array */
431426
ECMA_PROPERTY_FLAG_LCACHED = (1u << 4), /**< property is lcached */
@@ -1751,7 +1746,7 @@ typedef struct
17511746
typedef struct
17521747
{
17531748
ecma_long_string_t header;
1754-
ecma_object_native_free_callback_t free_cb; /**< free callback */
1749+
jerry_value_free_callback_t free_cb; /**< free callback */
17551750
} ecma_external_string_t;
17561751

17571752
/**
@@ -1995,7 +1990,7 @@ typedef struct
19951990
{
19961991
ecma_extended_object_t extended_object; /**< extended object part */
19971992
void *buffer_p; /**< external buffer pointer */
1998-
ecma_object_native_free_callback_t free_cb; /**< the free callback for the above buffer pointer */
1993+
jerry_value_free_callback_t free_cb; /**< the free callback for the above buffer pointer */
19991994
} ecma_arraybuffer_external_info;
20001995

20011996
/**

0 commit comments

Comments
 (0)