Skip to content

Commit 6adf22e

Browse files
committed
Add context support for memory allocator.
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 7813801 commit 6adf22e

File tree

7 files changed

+339
-230
lines changed

7 files changed

+339
-230
lines changed

jerry-core/CMakeLists.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ project (JerryCore C ASM)
105105
# Include directories
106106
set(INCLUDE_CORE
107107
${CMAKE_SOURCE_DIR}/jerry-core
108-
${CMAKE_SOURCE_DIR}/jerry-core/lit
109-
${CMAKE_SOURCE_DIR}/jerry-core/rcs
110-
${CMAKE_SOURCE_DIR}/jerry-core/mem
111-
${CMAKE_SOURCE_DIR}/jerry-core/vm
112-
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
108+
${CMAKE_SOURCE_DIR}/jerry-core/context
113109
${CMAKE_SOURCE_DIR}/jerry-core/ecma/base
110+
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
114111
${CMAKE_SOURCE_DIR}/jerry-core/ecma/operations
112+
${CMAKE_SOURCE_DIR}/jerry-core/jrt
113+
${CMAKE_SOURCE_DIR}/jerry-core/lit
114+
${CMAKE_SOURCE_DIR}/jerry-core/mem
115115
${CMAKE_SOURCE_DIR}/jerry-core/parser/js
116116
${CMAKE_SOURCE_DIR}/jerry-core/parser/regexp
117-
${CMAKE_SOURCE_DIR}/jerry-core/jrt)
117+
${CMAKE_SOURCE_DIR}/jerry-core/vm)
118118

119119
# Third-party
120120
# Valgrind
@@ -123,29 +123,29 @@ project (JerryCore C ASM)
123123
# Sources
124124
# Jerry core
125125
file(GLOB SOURCE_CORE_API *.c)
126-
file(GLOB SOURCE_CORE_LIT lit/*.c)
127-
file(GLOB SOURCE_CORE_RCS rcs/*.c)
128-
file(GLOB SOURCE_CORE_MEM mem/*.c)
129-
file(GLOB SOURCE_CORE_VM vm/*.c)
130-
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
126+
file(GLOB SOURCE_CORE_CONTEXT context/*.c)
131127
file(GLOB SOURCE_CORE_ECMA_BASE ecma/base/*.c)
128+
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
132129
file(GLOB SOURCE_CORE_ECMA_OPERATIONS ecma/operations/*.c)
130+
file(GLOB SOURCE_CORE_JRT jrt/*.c)
131+
file(GLOB SOURCE_CORE_LIT lit/*.c)
132+
file(GLOB SOURCE_CORE_MEM mem/*.c)
133133
file(GLOB SOURCE_CORE_PARSER_JS parser/js/*.c)
134134
file(GLOB SOURCE_CORE_PARSER_REGEXP parser/regexp/*.c)
135-
file(GLOB SOURCE_CORE_JRT jrt/*.c)
135+
file(GLOB SOURCE_CORE_VM vm/*.c)
136136

137137
set(SOURCE_CORE_FILES
138138
${SOURCE_CORE_API}
139-
${SOURCE_CORE_LIT}
140-
${SOURCE_CORE_RCS}
141-
${SOURCE_CORE_MEM}
142-
${SOURCE_CORE_VM}
143-
${SOURCE_CORE_ECMA_BUILTINS}
139+
${SOURCE_CORE_CONTEXT}
144140
${SOURCE_CORE_ECMA_BASE}
141+
${SOURCE_CORE_ECMA_BUILTINS}
145142
${SOURCE_CORE_ECMA_OPERATIONS}
143+
${SOURCE_CORE_JRT}
144+
${SOURCE_CORE_LIT}
145+
${SOURCE_CORE_MEM}
146146
${SOURCE_CORE_PARSER_JS}
147147
${SOURCE_CORE_PARSER_REGEXP}
148-
${SOURCE_CORE_JRT})
148+
${SOURCE_CORE_VM})
149149

150150
# Jerry port
151151
file(GLOB SOURCE_PORT_FILES ${PORT_DIR}/*.c)

jerry-core/context/context.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "context.h"
18+
19+
/** \addtogroup context Jerry context
20+
* @{
21+
*
22+
* \addtogroup context Context
23+
* @{
24+
*/
25+
26+
/**
27+
* Global context.
28+
*/
29+
jerry_context_t jerry_global_context;
30+
31+
/**
32+
* Jerry global heap section attribute.
33+
*/
34+
#ifndef JERRY_HEAP_SECTION_ATTR
35+
#define JERRY_GLOBAL_HEAP_SECTION
36+
#else /* JERRY_HEAP_SECTION_ATTR */
37+
#define JERRY_GLOBAL_HEAP_SECTION __attribute__ ((section (JERRY_HEAP_SECTION_ATTR)))
38+
#endif /* !JERRY_HEAP_SECTION_ATTR */
39+
40+
/**
41+
* Global heap.
42+
*/
43+
mem_heap_t jerry_global_heap __attribute__ ((aligned (MEM_ALIGNMENT))) JERRY_GLOBAL_HEAP_SECTION;
44+
45+
/**
46+
* @}
47+
* @}
48+
*/

jerry-core/context/context.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Memory context for JerryScript
19+
*/
20+
#ifndef CONTEXT_H
21+
#define CONTEXT_H
22+
23+
#include "jrt.h"
24+
#include "mem-allocator.h"
25+
#include "mem-config.h"
26+
27+
/** \addtogroup context Jerry context
28+
* @{
29+
*
30+
* \addtogroup context Context
31+
* @{
32+
*/
33+
34+
/**
35+
* Calculate heap area size, leaving space for a pointer to the free list
36+
*/
37+
#define MEM_HEAP_AREA_SIZE (MEM_HEAP_SIZE - MEM_ALIGNMENT)
38+
39+
/**
40+
* Heap structure
41+
*/
42+
typedef struct
43+
{
44+
mem_heap_free_t first; /**< first node in free region list */
45+
uint8_t area[MEM_HEAP_AREA_SIZE]; /**< heap area */
46+
} mem_heap_t;
47+
48+
/**
49+
* JerryScript context
50+
*
51+
* The purpose of this header is storing
52+
* all global variables for Jerry
53+
*/
54+
typedef struct
55+
{
56+
/**
57+
* Memory manager part.
58+
*/
59+
size_t mem_heap_allocated_size; /**< size of allocated regions */
60+
size_t mem_heap_limit; /**< current limit of heap usage, that is upon being reached,
61+
* causes call of "try give memory back" callbacks */
62+
mem_heap_free_t *mem_heap_list_skip_p; /**< This is used to speed up deallocation. */
63+
mem_pools_chunk_t *mem_free_chunk_p; /**< list of free pool chunks */
64+
mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback; /**< The 'try to give memory back' callback */
65+
66+
#ifdef MEM_STATS
67+
mem_heap_stats_t mem_heap_stats; /**< heap's memory usage statistics */
68+
mem_pools_stats_t mem_pools_stats; /**< pools' memory usage statistics */
69+
#endif /* MEM_STATS */
70+
71+
#ifdef JERRY_VALGRIND_FREYA
72+
bool valgrind_freya_mempool_request; /**< Tells whether a pool manager
73+
* allocator request is in progress */
74+
#endif /* JERRY_VALGRIND_FREYA */
75+
} jerry_context_t;
76+
77+
/**
78+
* Jerry global context.
79+
*/
80+
extern jerry_context_t jerry_global_context;
81+
82+
/**
83+
* Jerry global heap.
84+
*/
85+
extern mem_heap_t jerry_global_heap;
86+
87+
/**
88+
* Provides a reference to a field in the current context.
89+
*/
90+
#define JERRY_CONTEXT(field) (jerry_global_context.field)
91+
92+
/**
93+
* Provides a reference to the area field of the heap.
94+
*/
95+
#define MEM_HEAP_CONTEXT(field) (jerry_global_heap.field)
96+
97+
/**
98+
* @}
99+
* @}
100+
*/
101+
102+
#endif /* !CONTEXT_H */

jerry-core/mem/mem-allocator.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Allocator implementation
1919
*/
2020

21+
#include "context.h"
2122
#include "jrt.h"
2223
#include "jrt-libc-includes.h"
2324
#include "mem-allocator.h"
@@ -27,11 +28,6 @@
2728
#define MEM_ALLOCATOR_INTERNAL
2829
#include "mem-allocator-internal.h"
2930

30-
/**
31-
* The 'try to give memory back' callback
32-
*/
33-
static mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback = NULL;
34-
3531
/**
3632
* Initialize memory allocators.
3733
*/
@@ -94,9 +90,9 @@ void
9490
mem_register_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
9591
{
9692
/* Currently only one callback is supported */
97-
JERRY_ASSERT (mem_try_give_memory_back_callback == NULL);
93+
JERRY_ASSERT (JERRY_CONTEXT (mem_try_give_memory_back_callback) == NULL);
9894

99-
mem_try_give_memory_back_callback = callback;
95+
JERRY_CONTEXT (mem_try_give_memory_back_callback) = callback;
10096
} /* mem_register_a_try_give_memory_back_callback */
10197

10298
/**
@@ -106,9 +102,9 @@ void
106102
mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
107103
{
108104
/* Currently only one callback is supported */
109-
JERRY_ASSERT (mem_try_give_memory_back_callback == callback);
105+
JERRY_ASSERT (JERRY_CONTEXT (mem_try_give_memory_back_callback) == callback);
110106

111-
mem_try_give_memory_back_callback = NULL;
107+
JERRY_CONTEXT (mem_try_give_memory_back_callback) = NULL;
112108
} /* mem_unregister_a_try_give_memory_back_callback */
113109

114110
/**
@@ -118,9 +114,9 @@ void
118114
mem_run_try_to_give_memory_back_callbacks (mem_try_give_memory_back_severity_t severity) /**< severity of
119115
the request */
120116
{
121-
if (mem_try_give_memory_back_callback != NULL)
117+
if (JERRY_CONTEXT (mem_try_give_memory_back_callback) != NULL)
122118
{
123-
mem_try_give_memory_back_callback (severity);
119+
JERRY_CONTEXT (mem_try_give_memory_back_callback) (severity);
124120
}
125121

126122
mem_pools_collect_empty ();

jerry-core/mem/mem-allocator.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ typedef enum
6868
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH, /* 'high' severity */
6969
} mem_try_give_memory_back_severity_t;
7070

71+
/**
72+
* Free region node
73+
*/
74+
typedef struct
75+
{
76+
uint32_t next_offset; /* Offset of next region in list */
77+
uint32_t size; /* Size of region */
78+
} mem_heap_free_t;
79+
80+
/**
81+
* Node for free chunk list
82+
*/
83+
typedef struct mem_pools_chunk
84+
{
85+
struct mem_pools_chunk *next_p; /* pointer to next pool chunk */
86+
} mem_pools_chunk_t;
87+
7188
/**
7289
* A 'try give memory back' callback routine type.
7390
*/

0 commit comments

Comments
 (0)