|
| 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 JCONTEXT_H |
| 21 | +#define JCONTEXT_H |
| 22 | + |
| 23 | +#include "jrt.h" |
| 24 | +#include "jmem-allocator.h" |
| 25 | +#include "jmem-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 JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT) |
| 38 | + |
| 39 | +/** |
| 40 | + * Heap structure |
| 41 | + * |
| 42 | + * Memory blocks returned by the allocator must not start from the |
| 43 | + * beginning of the heap area because offset 0 is reserved for |
| 44 | + * JMEM_CP_NULL. This special constant is used in several places, |
| 45 | + * e.g. it marks the end of the property chain list, so it cannot |
| 46 | + * be eliminated from the project. Although the allocator cannot |
| 47 | + * use the first 8 bytes of the heap, nothing prevents to use it |
| 48 | + * for other purposes. Currently the free region start is stored |
| 49 | + * there. |
| 50 | + */ |
| 51 | +typedef struct |
| 52 | +{ |
| 53 | + jmem_heap_free_t first; /**< first node in free region list */ |
| 54 | + uint8_t area[JMEM_HEAP_AREA_SIZE]; /**< heap area */ |
| 55 | +} jmem_heap_t; |
| 56 | + |
| 57 | +/** |
| 58 | + * JerryScript context |
| 59 | + * |
| 60 | + * The purpose of this header is storing |
| 61 | + * all global variables for Jerry |
| 62 | + */ |
| 63 | +typedef struct |
| 64 | +{ |
| 65 | + /** |
| 66 | + * Memory manager part. |
| 67 | + */ |
| 68 | + size_t jmem_heap_allocated_size; /**< size of allocated regions */ |
| 69 | + size_t jmem_heap_limit; /**< current limit of heap usage, that is upon being reached, |
| 70 | + * causes call of "try give memory back" callbacks */ |
| 71 | + jmem_heap_free_t *jmem_heap_list_skip_p; /**< This is used to speed up deallocation. */ |
| 72 | + jmem_pools_chunk_t *jmem_free_chunk_p; /**< list of free pool chunks */ |
| 73 | + jmem_free_unused_memory_callback_t jmem_free_unused_memory_callback; /**< Callback for freeing up memory. */ |
| 74 | + |
| 75 | +#ifdef JMEM_STATS |
| 76 | + jmem_heap_stats_t jmem_heap_stats; /**< heap's memory usage statistics */ |
| 77 | + jmem_pools_stats_t jmem_pools_stats; /**< pools' memory usage statistics */ |
| 78 | +#endif /* MEM_STATS */ |
| 79 | + |
| 80 | +#ifdef JERRY_VALGRIND_FREYA |
| 81 | + bool valgrind_freya_mempool_request; /**< Tells whether a pool manager |
| 82 | + * allocator request is in progress */ |
| 83 | +#endif /* JERRY_VALGRIND_FREYA */ |
| 84 | +} jerry_context_t; |
| 85 | + |
| 86 | +/** |
| 87 | + * Jerry global context. |
| 88 | + */ |
| 89 | +extern jerry_context_t jerry_global_context; |
| 90 | + |
| 91 | +/** |
| 92 | + * Jerry global heap. |
| 93 | + */ |
| 94 | +extern jmem_heap_t jerry_global_heap; |
| 95 | + |
| 96 | +/** |
| 97 | + * Provides a reference to a field in the current context. |
| 98 | + */ |
| 99 | +#define JERRY_CONTEXT(field) (jerry_global_context.field) |
| 100 | + |
| 101 | +/** |
| 102 | + * Provides a reference to the area field of the heap. |
| 103 | + */ |
| 104 | +#define JERRY_HEAP_CONTEXT(field) (jerry_global_heap.field) |
| 105 | + |
| 106 | +/** |
| 107 | + * @} |
| 108 | + * @} |
| 109 | + */ |
| 110 | + |
| 111 | +#endif /* !JCONTEXT_H */ |
0 commit comments