|
| 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