Skip to content

Commit 2be9d63

Browse files
committed
Add context support for memory allocator.
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 33a1203 commit 2be9d63

File tree

5 files changed

+328
-212
lines changed

5 files changed

+328
-212
lines changed

jerry-core/mem/mem-allocator.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@
2121
#include "jrt.h"
2222
#include "jrt-libc-includes.h"
2323
#include "mem-allocator.h"
24+
#include "mem-context.h"
2425
#include "mem-heap.h"
2526
#include "mem-poolman.h"
2627

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-context.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 "mem-context.h"
18+
19+
/** \addtogroup mem Memory allocation
20+
* @{
21+
*
22+
* \addtogroup heap Context
23+
* @{
24+
*/
25+
26+
/**
27+
* Global context.
28+
*/
29+
jerry_context_t jerry_global_context;
30+
31+
#ifndef JERRY_HEAP_SECTION_ATTR
32+
33+
/**
34+
* Global heap.
35+
*/
36+
mem_heap_t jerry_global_heap __attribute__ ((aligned (MEM_ALIGNMENT)));
37+
38+
#else /* JERRY_HEAP_SECTION_ATTR */
39+
40+
/**
41+
* Jerry global heap section attribute.
42+
*/
43+
#define JERRY_GLOBAL_HEAP_SECTION __attribute__ ((section (JERRY_HEAP_SECTION_ATTR)))
44+
45+
/**
46+
* Global heap.
47+
*/
48+
mem_heap_t jerry_global_heap __attribute__ ((aligned (MEM_ALIGNMENT))) JERRY_GLOBAL_HEAP_SECTION;
49+
50+
#endif /* !JERRY_HEAP_SECTION_ATTR */
51+
52+
/**
53+
* @}
54+
* @}
55+
*/

jerry-core/mem/mem-context.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 MEM_CONTEXT_H
21+
#define MEM_CONTEXT_H
22+
23+
#include "jrt.h"
24+
#include "mem-allocator.h"
25+
#include "mem-config.h"
26+
27+
/** \addtogroup mem Memory allocation
28+
* @{
29+
*
30+
* \addtogroup heap Context
31+
* @{
32+
*/
33+
34+
/**
35+
* Free region node
36+
*/
37+
typedef struct
38+
{
39+
uint32_t next_offset; /* Offset of next region in list */
40+
uint32_t size; /* Size of region */
41+
} mem_heap_free_t;
42+
43+
/**
44+
* Node for free chunk list
45+
*/
46+
typedef struct mem_pools_chunk
47+
{
48+
struct mem_pools_chunk *next_p; /* pointer to next pool chunk */
49+
} mem_pools_chunk_t;
50+
51+
/**
52+
* Calculate heap area size, leaving space for a pointer to the free list
53+
*/
54+
#define MEM_HEAP_AREA_SIZE (MEM_HEAP_SIZE - MEM_ALIGNMENT)
55+
56+
/**
57+
* Heap structure
58+
*/
59+
typedef struct
60+
{
61+
mem_heap_free_t first; /**< first node in free region list */
62+
uint8_t area[MEM_HEAP_AREA_SIZE]; /**< heap area */
63+
} mem_heap_t;
64+
65+
/**
66+
* JerryScript context
67+
*
68+
* The purpose of this header is storing
69+
* all global variables for Jerry
70+
*/
71+
typedef struct
72+
{
73+
/**
74+
* Memory manager part.
75+
*/
76+
size_t mem_heap_allocated_size; /**< size of allocated regions */
77+
size_t mem_heap_limit; /**< current limit of heap usage, that is upon being reached,
78+
* causes call of "try give memory back" callbacks */
79+
mem_heap_free_t *mem_heap_list_skip_p; /**< This is used to speed up deallocation. */
80+
mem_pools_chunk_t *mem_free_chunk_p; /**< list of free pool chunks */
81+
mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback; /**< The 'try to give memory back' callback */
82+
83+
#ifdef MEM_STATS
84+
mem_heap_stats_t mem_heap_stats; /**< heap's memory usage statistics */
85+
mem_pools_stats_t mem_pools_stats; /**< pools' memory usage statistics */
86+
#endif /* MEM_STATS */
87+
88+
#ifdef JERRY_VALGRIND_FREYA
89+
bool valgrind_freya_mempool_request; /**< Tells whether a pool manager
90+
* allocator request is in progress */
91+
#endif /* JERRY_VALGRIND_FREYA */
92+
} jerry_context_t;
93+
94+
/**
95+
* Jerry global context.
96+
*/
97+
extern jerry_context_t jerry_global_context;
98+
99+
/**
100+
* Jerry global heap.
101+
*/
102+
extern mem_heap_t jerry_global_heap;
103+
104+
/**
105+
* Provides a reference to a field in the current context.
106+
*/
107+
#define JERRY_CONTEXT(field) (jerry_global_context.field)
108+
109+
/**
110+
* Provides a reference to the area field of the heap.
111+
*/
112+
#define MEM_HEAP_CONTEXT(field) (jerry_global_heap.field)
113+
114+
/**
115+
* @}
116+
* @}
117+
*/
118+
119+
#endif /* !MEM_CONTEXT_H */

0 commit comments

Comments
 (0)