Skip to content

Commit

Permalink
Replace iotjs_handlewrap_t struct
Browse files Browse the repository at this point in the history
Replaced the iotjs_handlewrap_t by using the
uv_handle_t structure directly. For this
a two new functions and two new macros were introduced:
* iotjs_uv_handle_create
* iotjs_uv_handle_close
* IOTJS_UV_HANDLE_DATA
* IOTJS_UV_HANDLE_EXTRA_DATA

For more information about the methods and macros
please see the iotjs_uv_handle.h file.

Additionally binary code size is reduced a bit.

IoT.js-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
  • Loading branch information
galpeter authored and yichoi committed Sep 11, 2018
1 parent 7639ce6 commit 2af4a2a
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 614 deletions.
13 changes: 3 additions & 10 deletions src/iotjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "iotjs_def.h"

#include "iotjs.h"
#include "iotjs_handlewrap.h"
#include "iotjs_js.h"
#include "iotjs_string_ext.h"

Expand All @@ -27,6 +26,8 @@
#include "jerryscript-port.h"
#include "jerryscript.h"

#include "iotjs_uv_handle.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -186,18 +187,10 @@ static int iotjs_start(iotjs_environment_t* env) {
}


static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) {
iotjs_handlewrap_t* handle_wrap = iotjs_handlewrap_from_handle(handle);
IOTJS_ASSERT(handle_wrap != NULL);

iotjs_handlewrap_close(handle_wrap, NULL);
}


void iotjs_end(iotjs_environment_t* env) {
uv_loop_t* loop = iotjs_environment_loop(env);
// Close uv loop.
uv_walk(loop, iotjs_uv_walk_to_close_callback, NULL);
uv_walk(loop, (uv_walk_cb)iotjs_uv_handle_close, NULL);
uv_run(loop, UV_RUN_DEFAULT);

int res = uv_loop_close(loop);
Expand Down
109 changes: 0 additions & 109 deletions src/iotjs_handlewrap.c

This file was deleted.

70 changes: 0 additions & 70 deletions src/iotjs_handlewrap.h

This file was deleted.

68 changes: 68 additions & 0 deletions src/iotjs_uv_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "iotjs_def.h"

#include "iotjs_uv_handle.h"


uv_handle_t* iotjs_uv_handle_create(size_t handle_size,
const jerry_value_t jobject,
JNativeInfoType* native_info,
size_t extra_data_size) {
IOTJS_ASSERT(jerry_value_is_object(jobject));

/* Make sure that the jerry_value_t is aligned */
size_t aligned_request_size = IOTJS_ALIGNUP(handle_size, 8u);

char* request_memory = iotjs_buffer_allocate(
aligned_request_size + sizeof(iotjs_uv_handle_data) + extra_data_size);
uv_handle_t* uv_handle = (uv_handle_t*)request_memory;
uv_handle->data = request_memory + aligned_request_size;

IOTJS_UV_HANDLE_DATA(uv_handle)->jobject = jobject;
IOTJS_UV_HANDLE_DATA(uv_handle)->on_close_cb = NULL;
jerry_acquire_value(jobject);

jerry_set_object_native_pointer(jobject, uv_handle, native_info);

return uv_handle;
}


static void iotjs_uv_handle_close_processor(uv_handle_t* handle) {
iotjs_uv_handle_data* handle_data = IOTJS_UV_HANDLE_DATA(handle);

if (handle_data->on_close_cb != NULL) {
handle_data->on_close_cb(handle);
}

// Decrease ref count of Javascript object. From now the object can be
// reclaimed.
jerry_release_value(handle_data->jobject);
IOTJS_RELEASE(handle);
}


void iotjs_uv_handle_close(uv_handle_t* handle, OnCloseHandler close_handler) {
if (handle == NULL || uv_is_closing(handle)) {
DDLOG("Attempt to close uninitialized or already closed handle");
return;
}

iotjs_uv_handle_data* handle_data = IOTJS_UV_HANDLE_DATA(handle);
handle_data->on_close_cb = close_handler;
uv_close(handle, iotjs_uv_handle_close_processor);
}
75 changes: 75 additions & 0 deletions src/iotjs_uv_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IOTJS_UV_HANDLE
#define IOTJS_UV_HANDLE

#include <uv.h>

#include "iotjs_binding.h"

typedef void (*OnCloseHandler)(uv_handle_t*);

typedef struct {
jerry_value_t jobject;
OnCloseHandler on_close_cb;
} iotjs_uv_handle_data;

#define IOTJS_ALIGNUP(value, alignment) \
(((value) + ((alignment)-1)) & ~((alignment)-1))

/**
* Allocate and initialize an uv_handle_t structure with a jerry callback and
* extra data.
*
* The allocated memory has the following layout:
*
* |-------------| <- start of uv_handle_t*
* | uv_handle_t |
* | |
* |-------------|
* | PADDING | <- alignment padding
* |-------------| <- start of the iotjs_uv_handle_data struct
* | handle_data |
* |-------------| <- start of the extra data if required
* | extra |
* |-------------|
*
*/
uv_handle_t* iotjs_uv_handle_create(size_t handle_size,
const jerry_value_t jobject,
JNativeInfoType* native_info,
size_t extra_data_size);
void iotjs_uv_handle_close(uv_handle_t* handle, OnCloseHandler close_handler);

/**
* Returns a pointer to the handle data struct referenced
* by the uv_handle_t->data member.
*/
#define IOTJS_UV_HANDLE_DATA(UV_HANDLE) \
((iotjs_uv_handle_data*)((UV_HANDLE)->data))

/**
* Returns a char* pointer for any extra data.
*
* IMPORTANT!
* Make sure that the extra data is correctly allocated by using the
* iotjs_uv_handle_create method call.
*/
#define IOTJS_UV_HANDLE_EXTRA_DATA(UV_HANDLE) \
((char*)((char*)((UV_HANDLE)->data) + sizeof(iotjs_uv_handle_data)))


#endif /* IOTJS_UV_HANDLE */
1 change: 0 additions & 1 deletion src/modules/iotjs_module_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

#include "iotjs_def.h"
#include "iotjs_module_crypto.h"
#include "iotjs_handlewrap.h"
#include "iotjs_module_buffer.h"

/* These enum values are the same as the ones in crypto.js as well as the
Expand Down
2 changes: 0 additions & 2 deletions src/modules/iotjs_module_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include "iotjs_module_mqtt.h"


#include "iotjs_handlewrap.h"

static void iotjs_mqttclient_destroy(iotjs_mqttclient_t *mqttclient) {
IOTJS_RELEASE(mqttclient->buffer);
IOTJS_RELEASE(mqttclient);
Expand Down
Loading

0 comments on commit 2af4a2a

Please sign in to comment.