11diff --git a/src/iotjs.c b/src/iotjs.c
2- index 5a93eb7..4a5a8bd 100644
2+ index 2df7e0f..bd7b62f 100644
33--- a/src/iotjs.c
44+++ b/src/iotjs.c
5- @@ -242,6 +242,9 @@ terminate:
5+ @@ -234,6 +234,8 @@ void iotjs_conf_console_out(int (*out)(int lv, const char* fmt, ...)) {
6+ iotjs_set_console_out(out);
7+ }
8+
9+ + void print_mem_stat();
10+ +
11+ int iotjs_entry(int argc, char** argv) {
12+ int ret_code = 0;
13+
14+ @@ -265,6 +267,9 @@ terminate:
615 iotjs_terminate(env);
716
817 exit:
@@ -13,130 +22,45 @@ index 5a93eb7..4a5a8bd 100644
1322 iotjs_environment_config(env)->debugger->context_reset) {
1423 iotjs_environment_release();
1524diff --git a/src/iotjs_util.c b/src/iotjs_util.c
16- index abd7a86..cc6b4e3 100644
25+ index abd7a86..5668015 100644
1726--- a/src/iotjs_util.c
1827+++ b/src/iotjs_util.c
19- @@ -75,12 +75,76 @@ iotjs_string_t iotjs_file_read(const char* path) {
20- }
28+ @@ -27,6 +27,11 @@
29+
30+ void force_terminate();
31+
32+ + void jstest_free(void*);
33+ + void* jstest_malloc(size_t);
34+ + void* jstest_calloc(size_t, size_t);
35+ + void* jstest_realloc(void*, size_t);
36+ +
37+ iotjs_string_t iotjs_file_read(const char* path) {
38+ FILE* file = fopen(path, "rb");
39+ if (file == NULL) {
40+ @@ -76,7 +81,7 @@ iotjs_string_t iotjs_file_read(const char* path) {
2141
2242
23- + /*
24- + * Memory statistic for system allocator.
25- + *
26- + *
27- + * When allocating a chunk of memory, the real size (with padding) is
28- + * located in a descriptor (mm_allocnode_s) before the allocated memory area:
29- + *
30- + * struct mm_freenode_s
31- + * {
32- + * mmsize_t size; // Size of the chunk
33- + * ...
34- + * };
35- + *
36- + * The SIZEOF_MM_ALLOCNODE defines the size of the mm_allocnode_s structure,
37- + * that helps to find the size variable.
38- + *
39- + * Note: on NuttX and TizenRT, the size variable contains the size of the
40- + * mm_freenode_s as well, but that is not calculated into the statistic.
41- + *
42- + * The SIZEOF_MM_ALLOCNODE is defined in:
43- + *
44- + * NuttX: include/nuttx/mm/mm.h
45- + * TizenRT: os/include/tinyara/mm/mm.h
46- + */
47- +
48- + #if defined(__NUTTX__) || defined(__TIZENRT__)
49- + #if !defined(NDEBUG) && defined(__TIZENRT__)
50- + #define SIZEOF_MM_ALLOCNODE 16
51- + #else
52- + #define SIZEOF_MM_ALLOCNODE 8
53- + #endif
54- + #else
55- + #error "Undefined memory allocation chunk size."
56- + #endif
57- +
58- + size_t allocated_bytes = 0;
59- + size_t peak_allocated_bytes = 0;
60- +
61- + void mem_stat_alloc(size_t size)
62- + {
63- + allocated_bytes += size;
64- +
65- + if (allocated_bytes > peak_allocated_bytes) {
66- + peak_allocated_bytes = allocated_bytes;
67- + }
68- + }
69- +
70- + void mem_stat_free(size_t size)
71- + {
72- + allocated_bytes -= size;
73- + }
74- +
75- + void print_mem_stat()
76- + {
77- + printf("Heap stats:\n");
78- + printf(" Malloc peak allocated: %u bytes\n", peak_allocated_bytes);
79- + }
80- +
8143 char* iotjs_buffer_allocate(size_t size) {
82- char* buffer = (char*)(calloc(size, sizeof(char)));
44+ - char* buffer = (char*)(calloc(size, sizeof(char)));
45+ + char* buffer = (char*)(jstest_calloc(size, sizeof(char)));
8346 if (buffer == NULL) {
8447 DLOG("Out of memory");
8548 force_terminate();
86- }
87- +
88- + // memstat
89- + size_t new_size;
90- + memcpy(&new_size, (buffer - SIZEOF_MM_ALLOCNODE), sizeof(size_t));
91- + mem_stat_alloc(new_size - SIZEOF_MM_ALLOCNODE);
92- +
93- return buffer;
94- }
95-
96- @@ -99,17 +163,31 @@ char* iotjs_buffer_allocate_from_number_array(size_t size,
49+ @@ -99,7 +104,7 @@ char* iotjs_buffer_allocate_from_number_array(size_t size,
9750
9851 char* iotjs_buffer_reallocate(char* buffer, size_t size) {
9952 IOTJS_ASSERT(buffer != NULL);
100- +
101- + size_t old_size;
102- + memcpy(&old_size, (buffer - SIZEOF_MM_ALLOCNODE), sizeof(size_t));
103- + mem_stat_free(old_size - SIZEOF_MM_ALLOCNODE);
104- +
105- char* newbuffer = (char*)(realloc(buffer, size));
53+ - char* newbuffer = (char*)(realloc(buffer, size));
54+ + char* newbuffer = (char*)(jstest_realloc(buffer, size));
10655 if (newbuffer == NULL) {
10756 DLOG("Out of memmory");
10857 force_terminate();
109- }
110- +
111- + size_t new_size;
112- + memcpy(&new_size, (newbuffer - SIZEOF_MM_ALLOCNODE), sizeof(size_t));
113- + mem_stat_alloc(new_size - SIZEOF_MM_ALLOCNODE);
114- +
115- return newbuffer;
116- }
117-
58+ @@ -110,7 +115,7 @@ char* iotjs_buffer_reallocate(char* buffer, size_t size) {
11859
11960 void iotjs_buffer_release(char* buffer) {
12061 if (buffer) {
121- + size_t size;
122- + memcpy(&size, (buffer - SIZEOF_MM_ALLOCNODE), sizeof(size_t));
123- + mem_stat_free(size - SIZEOF_MM_ALLOCNODE);
124- +
125- free(buffer);
62+ - free(buffer);
63+ + jstest_free(buffer);
12664 }
12765 }
128- diff --git a/src/iotjs_util.h b/src/iotjs_util.h
129- index a891604..786830e 100644
130- --- a/src/iotjs_util.h
131- +++ b/src/iotjs_util.h
132- @@ -23,6 +23,10 @@
133- // Return value should be released with iotjs_string_destroy()
134- iotjs_string_t iotjs_file_read(const char* path);
13566
136- + void mem_stat_alloc(size_t size);
137- + void mem_stat_free(size_t size);
138- + void print_mem_stat();
139- +
140- char* iotjs_buffer_allocate(size_t size);
141- char* iotjs_buffer_allocate_from_number_array(size_t size,
142- const jerry_value_t array);
0 commit comments