|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <stdlib.h> |
| 16 | +#include "handle-scope-internal.h" |
| 17 | + |
| 18 | +static jerryx_handle_scope_t kJerryXHandleScopeRoot = |
| 19 | +{ |
| 20 | + .handle_count = 0, |
| 21 | + .handle_ptr = NULL, |
| 22 | +}; |
| 23 | +static jerryx_handle_scope_t *kJerryXHandleScopeCurrent = &kJerryXHandleScopeRoot; |
| 24 | +static jerryx_handle_scope_pool_t kJerryXHandleScopePool = |
| 25 | +{ |
| 26 | + .count = 0, |
| 27 | + .start = NULL, |
| 28 | +}; |
| 29 | + |
| 30 | +#define kJerryXHandleScopePoolPrelistLast \ |
| 31 | + kJerryXHandleScopePool.prelist + JERRYX_SCOPE_PRELIST_SIZE - 1 |
| 32 | + |
| 33 | +#define JerryXHandleScopePrelistIdx(scope) (scope - kJerryXHandleScopePool.prelist) |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Get current handle scope top of stack. |
| 38 | + */ |
| 39 | +inline |
| 40 | +jerryx_handle_scope_t * |
| 41 | +jerryx_handle_scope_get_current (void) |
| 42 | +{ |
| 43 | + return kJerryXHandleScopeCurrent; |
| 44 | +} /* jerryx_handle_scope_get_current */ |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * Get root handle scope. |
| 49 | + */ |
| 50 | +inline |
| 51 | +jerryx_handle_scope_t * |
| 52 | +jerryx_handle_scope_get_root (void) |
| 53 | +{ |
| 54 | + return &kJerryXHandleScopeRoot; |
| 55 | +} /* jerryx_handle_scope_get_root */ |
| 56 | + |
| 57 | + |
| 58 | +/** |
| 59 | + * Determines if given handle scope is located in pre-allocated list. |
| 60 | + * |
| 61 | + * @param scope - the one to be determined. |
| 62 | + */ |
| 63 | +static |
| 64 | +inline |
| 65 | +bool |
| 66 | +jerryx_handle_scope_is_in_prelist (jerryx_handle_scope_t *scope) |
| 67 | +{ |
| 68 | + return (kJerryXHandleScopePool.prelist <= scope) |
| 69 | + && (scope <= (kJerryXHandleScopePool.prelist + JERRYX_SCOPE_PRELIST_SIZE - 1)); |
| 70 | +} /** jerryx_handle_scope_is_in_prelist */ |
| 71 | + |
| 72 | + |
| 73 | +/** |
| 74 | + * Get the parent of given handle scope. |
| 75 | + * If given handle scope is in prelist, the parent must be in prelist too; |
| 76 | + * if given is the first item of heap chain list, the parent must be the last one of prelist; |
| 77 | + * the parent must be in chain list otherwise. |
| 78 | + * |
| 79 | + * @param scope - the one to be permformed on. |
| 80 | + * @returns - the parent of the given scope. |
| 81 | + */ |
| 82 | +jerryx_handle_scope_t * |
| 83 | +jerryx_handle_scope_get_parent (jerryx_handle_scope_t *scope) |
| 84 | +{ |
| 85 | + if (scope == &kJerryXHandleScopeRoot) |
| 86 | + { |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + if (!jerryx_handle_scope_is_in_prelist (scope)) |
| 90 | + { |
| 91 | + jerryx_handle_scope_dynamic_t *dy_scope = (jerryx_handle_scope_dynamic_t *) scope; |
| 92 | + if (dy_scope == kJerryXHandleScopePool.start) |
| 93 | + { |
| 94 | + return kJerryXHandleScopePoolPrelistLast; |
| 95 | + } |
| 96 | + jerryx_handle_scope_dynamic_t *parent = dy_scope->parent; |
| 97 | + return (jerryx_handle_scope_t *) parent; |
| 98 | + } |
| 99 | + if (scope == kJerryXHandleScopePool.prelist) |
| 100 | + { |
| 101 | + return &kJerryXHandleScopeRoot; |
| 102 | + } |
| 103 | + return kJerryXHandleScopePool.prelist + JerryXHandleScopePrelistIdx (scope) - 1; |
| 104 | +} /** jerryx_handle_scope_get_parent */ |
| 105 | + |
| 106 | + |
| 107 | +/** |
| 108 | + * Get the child of given handle scope. |
| 109 | + * If the given handle scope is in heap chain list, its child must be in heap chain list too; |
| 110 | + * if the given handle scope is the last one of prelist, its child must be the first item of chain list; |
| 111 | + * the children are in prelist otherwise. |
| 112 | + * |
| 113 | + * @param scope - the one to be permformed on. |
| 114 | + * @returns the child of the given scope. |
| 115 | + */ |
| 116 | +jerryx_handle_scope_t * |
| 117 | +jerryx_handle_scope_get_child (jerryx_handle_scope_t *scope) |
| 118 | +{ |
| 119 | + if (scope == &kJerryXHandleScopeRoot) |
| 120 | + { |
| 121 | + if (kJerryXHandleScopePool.count > 0) |
| 122 | + { |
| 123 | + return kJerryXHandleScopePool.prelist; |
| 124 | + } |
| 125 | + return NULL; |
| 126 | + } |
| 127 | + if (!jerryx_handle_scope_is_in_prelist (scope)) |
| 128 | + { |
| 129 | + jerryx_handle_scope_dynamic_t *child = ((jerryx_handle_scope_dynamic_t *) scope)->child; |
| 130 | + return (jerryx_handle_scope_t *) child; |
| 131 | + } |
| 132 | + if (scope == kJerryXHandleScopePoolPrelistLast) |
| 133 | + { |
| 134 | + return (jerryx_handle_scope_t *) kJerryXHandleScopePool.start; |
| 135 | + } |
| 136 | + long idx = JerryXHandleScopePrelistIdx (scope); |
| 137 | + if (idx < 0) |
| 138 | + { |
| 139 | + return NULL; |
| 140 | + } |
| 141 | + if ((unsigned long) idx >= kJerryXHandleScopePool.count - 1) |
| 142 | + { |
| 143 | + return NULL; |
| 144 | + } |
| 145 | + return kJerryXHandleScopePool.prelist + idx + 1; |
| 146 | +} /** jerryx_handle_scope_get_child */ |
| 147 | + |
| 148 | + |
| 149 | +/** |
| 150 | + * Claims a handle scope either from prelist or allocating a new memory block, |
| 151 | + * and increment pool's scope count by 1, and set current scope to the newly claimed one. |
| 152 | + * If there are still available spaces in prelist, claims a block in prelist; |
| 153 | + * otherwise allocates a new memory block from heap and sets its fields to default values, |
| 154 | + * and link it to previously dynamically allocated scope, or link it to pool's start pointer. |
| 155 | + * |
| 156 | + * @returns the newly claimed handle scope pointer. |
| 157 | + */ |
| 158 | +jerryx_handle_scope_t * |
| 159 | +jerryx_handle_scope_alloc (void) |
| 160 | +{ |
| 161 | + jerryx_handle_scope_t *scope; |
| 162 | + if (kJerryXHandleScopePool.count < JERRYX_SCOPE_PRELIST_SIZE) |
| 163 | + { |
| 164 | + scope = kJerryXHandleScopePool.prelist + kJerryXHandleScopePool.count; |
| 165 | + goto deferred; |
| 166 | + } |
| 167 | + |
| 168 | + do |
| 169 | + { |
| 170 | + jerryx_handle_scope_dynamic_t *dy_scope = malloc (sizeof (jerryx_handle_scope_dynamic_t)); |
| 171 | + JERRYX_HANDLE_SCOPE_ASSERT (dy_scope != NULL); |
| 172 | + dy_scope->child = NULL; |
| 173 | + |
| 174 | + if (kJerryXHandleScopePool.count != JERRYX_SCOPE_PRELIST_SIZE) |
| 175 | + { |
| 176 | + jerryx_handle_scope_dynamic_t *dy_current = (jerryx_handle_scope_dynamic_t *) kJerryXHandleScopeCurrent; |
| 177 | + dy_scope->parent = dy_current; |
| 178 | + dy_current->child = dy_scope; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + kJerryXHandleScopePool.start = dy_scope; |
| 183 | + dy_scope->parent = NULL; |
| 184 | + } |
| 185 | + |
| 186 | + scope = (jerryx_handle_scope_t *) dy_scope; |
| 187 | + } |
| 188 | + while (0); |
| 189 | + |
| 190 | +deferred: |
| 191 | + scope->handle_count = 0; |
| 192 | + scope->escaped = false; |
| 193 | + scope->handle_ptr = NULL; |
| 194 | + |
| 195 | + kJerryXHandleScopeCurrent = scope; |
| 196 | + kJerryXHandleScopePool.count += 1; |
| 197 | + return (jerryx_handle_scope_t *) scope; |
| 198 | +} /** jerryx_handle_scope_alloc */ |
| 199 | + |
| 200 | + |
| 201 | +/** |
| 202 | + * Deannounce a previously claimed handle scope, return it to pool |
| 203 | + * or free the allocated memory block. |
| 204 | + * |
| 205 | + * @param scope - the one to be freed. |
| 206 | + */ |
| 207 | +void |
| 208 | +jerryx_handle_scope_free (jerryx_handle_scope_t *scope) |
| 209 | +{ |
| 210 | + if (scope == &kJerryXHandleScopeRoot) |
| 211 | + { |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + kJerryXHandleScopePool.count -= 1; |
| 216 | + if (scope == kJerryXHandleScopeCurrent) |
| 217 | + { |
| 218 | + kJerryXHandleScopeCurrent = jerryx_handle_scope_get_parent (scope); |
| 219 | + } |
| 220 | + |
| 221 | + if (!jerryx_handle_scope_is_in_prelist (scope)) |
| 222 | + { |
| 223 | + jerryx_handle_scope_dynamic_t *dy_scope = (jerryx_handle_scope_dynamic_t *) scope; |
| 224 | + if (dy_scope == kJerryXHandleScopePool.start) |
| 225 | + { |
| 226 | + kJerryXHandleScopePool.start = dy_scope->child; |
| 227 | + } |
| 228 | + else if (dy_scope->parent != NULL) |
| 229 | + { |
| 230 | + dy_scope->parent->child = dy_scope->child; |
| 231 | + } |
| 232 | + free (dy_scope); |
| 233 | + return; |
| 234 | + } |
| 235 | + /** |
| 236 | + * Nothing to do with scopes in prelist |
| 237 | + */ |
| 238 | +} /** jerryx_handle_scope_free */ |
0 commit comments