-
Notifications
You must be signed in to change notification settings - Fork 683
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
/** | ||
* 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; | ||
|
||
/** | ||
* @} | ||
* @} | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.