Skip to content

Add context support for memory allocator #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions jerry-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ project (JerryCore C ASM)
# Include directories
set(INCLUDE_CORE
${CMAKE_SOURCE_DIR}/jerry-core
${CMAKE_SOURCE_DIR}/jerry-core/lit
${CMAKE_SOURCE_DIR}/jerry-core/rcs
${CMAKE_SOURCE_DIR}/jerry-core/jmem
${CMAKE_SOURCE_DIR}/jerry-core/vm
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
${CMAKE_SOURCE_DIR}/jerry-core/ecma/base
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
${CMAKE_SOURCE_DIR}/jerry-core/ecma/operations
${CMAKE_SOURCE_DIR}/jerry-core/jcontext
${CMAKE_SOURCE_DIR}/jerry-core/jmem
${CMAKE_SOURCE_DIR}/jerry-core/jrt
${CMAKE_SOURCE_DIR}/jerry-core/lit
${CMAKE_SOURCE_DIR}/jerry-core/parser/js
${CMAKE_SOURCE_DIR}/jerry-core/parser/regexp
${CMAKE_SOURCE_DIR}/jerry-core/jrt)
${CMAKE_SOURCE_DIR}/jerry-core/vm)

# Third-party
# Valgrind
Expand All @@ -123,29 +123,29 @@ project (JerryCore C ASM)
# Sources
# Jerry core
file(GLOB SOURCE_CORE_API *.c)
file(GLOB SOURCE_CORE_LIT lit/*.c)
file(GLOB SOURCE_CORE_RCS rcs/*.c)
file(GLOB SOURCE_CORE_MEM jmem/*.c)
file(GLOB SOURCE_CORE_VM vm/*.c)
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
file(GLOB SOURCE_CORE_ECMA_BASE ecma/base/*.c)
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
file(GLOB SOURCE_CORE_ECMA_OPERATIONS ecma/operations/*.c)
file(GLOB SOURCE_CORE_JCONTEXT jcontext/*.c)
file(GLOB SOURCE_CORE_JMEM jmem/*.c)
file(GLOB SOURCE_CORE_JRT jrt/*.c)
file(GLOB SOURCE_CORE_LIT lit/*.c)
file(GLOB SOURCE_CORE_PARSER_JS parser/js/*.c)
file(GLOB SOURCE_CORE_PARSER_REGEXP parser/regexp/*.c)
file(GLOB SOURCE_CORE_JRT jrt/*.c)
file(GLOB SOURCE_CORE_VM vm/*.c)

set(SOURCE_CORE_FILES
${SOURCE_CORE_API}
${SOURCE_CORE_LIT}
${SOURCE_CORE_RCS}
${SOURCE_CORE_MEM}
${SOURCE_CORE_VM}
${SOURCE_CORE_ECMA_BUILTINS}
${SOURCE_CORE_ECMA_BASE}
${SOURCE_CORE_ECMA_BUILTINS}
${SOURCE_CORE_ECMA_OPERATIONS}
${SOURCE_CORE_JCONTEXT}
${SOURCE_CORE_JMEM}
${SOURCE_CORE_JRT}
${SOURCE_CORE_LIT}
${SOURCE_CORE_PARSER_JS}
${SOURCE_CORE_PARSER_REGEXP}
${SOURCE_CORE_JRT})
${SOURCE_CORE_VM})

# Jerry port
if(NOT ("${PORT_DIR}" STREQUAL ""))
Expand Down
48 changes: 48 additions & 0 deletions jerry-core/jcontext/jcontext.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Copyright 2016 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* 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 "jcontext.h"

/** \addtogroup context Jerry context
* @{
*
* \addtogroup context Context
* @{
*/

/**
* Global context.
*/
jerry_context_t jerry_global_context;
Copy link
Contributor

@jiangzidong jiangzidong Jul 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1)Is the "context" here similar with the v8 context https://developers.google.com/v8/embed#contexts
2)Will the name "global_context" be misleading? People may think it's global environment/scope
What about "native_context"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand. It is a global environment. This is an initial patch, which collects global variables into a structure, and later this structure can be used to support different global management. E.g. multiple Jerries in one process, optional memory allocation for Jerry, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry that I didn't put it clearly. Here "context" I think it is similar with V8 context or Node.js's vm.runInXXContext, right? If so, each context will have its own global env, like ecma_global_lex_env_p in jerry-core\ecma\operations\ecma-lex-env.c
The followings are my understanding, please correct me if I am wrong.
jerry_global_context is the default context when we run jerry. in the future, we will support creating new context (let's name it "context2") and leting some js codes run in the context2. In the above case, both jerry_global_context and context2 will have respective ecma_global_lex_env_p. But the name of "jerry_global_context" may let people feel it is the global env.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan here is a bit different. Jerry will support more "context models" in the future and these context models will be selected by an appropriate defines. E.g. the current context model (which will likely be the default context model) supports only a single global context. Other context models (e.g. JERRY_CONTEXT_MODEL_USER_CONTEXT_PTR) will defines other variables and structures. The point is, all context models will reuse the structures above for convenience. Hence creating new context models will be easy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry that I didn't fully understand. In a case which has 2 contexts, do the 2 context have 2 different jerry_global_context? or there is only one jerry_global_context in one jerry process? (I think only one jerry_global_context in one jerry process)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan is to have more contexts. But the other modes has not implemented yet, so we are quite free to do whatever we want. Those modes which support multiple contexts jerry_global_context will obviously not a good name, so they will define something like jerry_current_context, and this will be a pointer type.

I think a pseudo code will makes this easier to understand:

#ifdef JERRY_CONTEXT_MODE_SOMETHING1
// Whatever we want, e.g. a pointer to the current context
#elif defined JERRY_CONTEXT_MODE_SOMETHING2
// Whatever we want, e.g. a callback is defined to get the current context
#else
// Default context, simply one global context. This is the only mode we have now.
jerry_context_t global_context;
#endif

This is not implemented yet, just the general plan. The code will evolve into this direction. So we can define any kind of context modes depending on user requests. Hence we will not be limited to one context model like v8, but we can adapt several different ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, Thanks very much

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I admit the plan was not clear from the patch. Different context modes has different advantages / disadvantages, and we don't want to limit choices here. Instead everybody should choose at compile time the best context model for them.


/**
* Jerry global heap section attribute.
*/
#ifndef JERRY_HEAP_SECTION_ATTR
#define JERRY_GLOBAL_HEAP_SECTION
#else /* JERRY_HEAP_SECTION_ATTR */
#define JERRY_GLOBAL_HEAP_SECTION __attribute__ ((section (JERRY_HEAP_SECTION_ATTR)))
#endif /* !JERRY_HEAP_SECTION_ATTR */

/**
* Global heap.
*/
jmem_heap_t jerry_global_heap __attribute__ ((aligned (JMEM_ALIGNMENT))) JERRY_GLOBAL_HEAP_SECTION;

/**
* @}
* @}
*/
111 changes: 111 additions & 0 deletions jerry-core/jcontext/jcontext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright 2016 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* 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.
*/

/**
* Memory context for JerryScript
*/
#ifndef JCONTEXT_H
#define JCONTEXT_H

#include "jrt.h"
#include "jmem-allocator.h"
#include "jmem-config.h"

/** \addtogroup context Jerry context
* @{
*
* \addtogroup context Context
* @{
*/

/**
* Calculate heap area size, leaving space for a pointer to the free list
*/
#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)

/**
* Heap structure
*
* Memory blocks returned by the allocator must not start from the
* beginning of the heap area because offset 0 is reserved for
* JMEM_CP_NULL. This special constant is used in several places,
* e.g. it marks the end of the property chain list, so it cannot
* be eliminated from the project. Although the allocator cannot
* use the first 8 bytes of the heap, nothing prevents to use it
* for other purposes. Currently the free region start is stored
* there.
*/
typedef struct
{
jmem_heap_free_t first; /**< first node in free region list */
uint8_t area[JMEM_HEAP_AREA_SIZE]; /**< heap area */
} jmem_heap_t;

/**
* JerryScript context
*
* The purpose of this header is storing
* all global variables for Jerry
*/
typedef struct
{
/**
* Memory manager part.
*/
size_t jmem_heap_allocated_size; /**< size of allocated regions */
size_t jmem_heap_limit; /**< current limit of heap usage, that is upon being reached,
* causes call of "try give memory back" callbacks */
jmem_heap_free_t *jmem_heap_list_skip_p; /**< This is used to speed up deallocation. */
jmem_pools_chunk_t *jmem_free_chunk_p; /**< list of free pool chunks */
jmem_free_unused_memory_callback_t jmem_free_unused_memory_callback; /**< Callback for freeing up memory. */

#ifdef JMEM_STATS
jmem_heap_stats_t jmem_heap_stats; /**< heap's memory usage statistics */
jmem_pools_stats_t jmem_pools_stats; /**< pools' memory usage statistics */
#endif /* MEM_STATS */

#ifdef JERRY_VALGRIND_FREYA
bool valgrind_freya_mempool_request; /**< Tells whether a pool manager
* allocator request is in progress */
#endif /* JERRY_VALGRIND_FREYA */
} jerry_context_t;

/**
* Jerry global context.
*/
extern jerry_context_t jerry_global_context;

/**
* Jerry global heap.
*/
extern jmem_heap_t jerry_global_heap;

/**
* Provides a reference to a field in the current context.
*/
#define JERRY_CONTEXT(field) (jerry_global_context.field)

/**
* Provides a reference to the area field of the heap.
*/
#define JERRY_HEAP_CONTEXT(field) (jerry_global_heap.field)

/**
* @}
* @}
*/

#endif /* !JCONTEXT_H */
21 changes: 8 additions & 13 deletions jerry-core/jmem/jmem-allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
* Allocator implementation
*/

#include "jrt.h"
#include "jrt-libc-includes.h"
#include "jcontext.h"
#include "jmem-allocator.h"
#include "jmem-heap.h"
#include "jmem-poolman.h"
#include "jrt-libc-includes.h"

#define JMEM_ALLOCATOR_INTERNAL
#include "jmem-allocator-internal.h"

/**
* The 'try to give memory back' callback
*/
static jmem_free_unused_memory_callback_t jmem_free_unused_memory_callback = NULL;

/**
* Initialize memory allocators.
*/
Expand Down Expand Up @@ -94,9 +89,9 @@ void
jmem_register_free_unused_memory_callback (jmem_free_unused_memory_callback_t callback) /**< callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (jmem_free_unused_memory_callback == NULL);
JERRY_ASSERT (JERRY_CONTEXT (jmem_free_unused_memory_callback) == NULL);

jmem_free_unused_memory_callback = callback;
JERRY_CONTEXT (jmem_free_unused_memory_callback) = callback;
} /* jmem_register_free_unused_memory_callback */

/**
Expand All @@ -106,9 +101,9 @@ void
jmem_unregister_free_unused_memory_callback (jmem_free_unused_memory_callback_t callback) /**< callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (jmem_free_unused_memory_callback == callback);
JERRY_ASSERT (JERRY_CONTEXT (jmem_free_unused_memory_callback) == callback);

jmem_free_unused_memory_callback = NULL;
JERRY_CONTEXT (jmem_free_unused_memory_callback) = NULL;
} /* jmem_unregister_free_unused_memory_callback */

/**
Expand All @@ -117,9 +112,9 @@ jmem_unregister_free_unused_memory_callback (jmem_free_unused_memory_callback_t
void
jmem_run_free_unused_memory_callbacks (jmem_free_unused_memory_severity_t severity) /**< severity of the request */
{
if (jmem_free_unused_memory_callback != NULL)
if (JERRY_CONTEXT (jmem_free_unused_memory_callback) != NULL)
{
jmem_free_unused_memory_callback (severity);
JERRY_CONTEXT (jmem_free_unused_memory_callback) (severity);
}

jmem_pools_collect_empty ();
Expand Down
19 changes: 18 additions & 1 deletion jerry-core/jmem/jmem-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,24 @@ typedef enum
} jmem_free_unused_memory_severity_t;

/**
* A 'try give memory back' callback routine type.
* Free region node
*/
typedef struct
{
uint32_t next_offset; /* Offset of next region in list */
uint32_t size; /* Size of region */
} jmem_heap_free_t;

/**
* Node for free chunk list
*/
typedef struct mem_pools_chunk
{
struct mem_pools_chunk *next_p; /* pointer to next pool chunk */
} jmem_pools_chunk_t;

/**
* A free memory callback routine type.
*/
typedef void (*jmem_free_unused_memory_callback_t) (jmem_free_unused_memory_severity_t);

Expand Down
Loading