1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "ecma-alloc.h" 17425bb815Sopenharmony_ci#include "ecma-gc.h" 18425bb815Sopenharmony_ci#include "ecma-globals.h" 19425bb815Sopenharmony_ci#include "ecma-helpers.h" 20425bb815Sopenharmony_ci#include "jrt.h" 21425bb815Sopenharmony_ci 22425bb815Sopenharmony_ci/** \addtogroup ecma ECMA 23425bb815Sopenharmony_ci * @{ 24425bb815Sopenharmony_ci * 25425bb815Sopenharmony_ci * \addtogroup ecmahelpers Helpers for operations with ECMA data types 26425bb815Sopenharmony_ci * @{ 27425bb815Sopenharmony_ci */ 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_ci/** 30425bb815Sopenharmony_ci * Allocate a collection of ecma values. 31425bb815Sopenharmony_ci * 32425bb815Sopenharmony_ci * @return pointer to the collection 33425bb815Sopenharmony_ci */ 34425bb815Sopenharmony_ciecma_collection_t * 35425bb815Sopenharmony_ciecma_new_collection (void) 36425bb815Sopenharmony_ci{ 37425bb815Sopenharmony_ci ecma_collection_t *collection_p; 38425bb815Sopenharmony_ci collection_p = (ecma_collection_t *) jmem_heap_alloc_block (sizeof (ecma_collection_t)); 39425bb815Sopenharmony_ci 40425bb815Sopenharmony_ci collection_p->item_count = 0; 41425bb815Sopenharmony_ci collection_p->capacity = ECMA_COLLECTION_INITIAL_CAPACITY; 42425bb815Sopenharmony_ci const uint32_t size = ECMA_COLLECTION_ALLOCATED_SIZE (ECMA_COLLECTION_INITIAL_CAPACITY); 43425bb815Sopenharmony_ci collection_p->buffer_p = (ecma_value_t *) jmem_heap_alloc_block (size); 44425bb815Sopenharmony_ci 45425bb815Sopenharmony_ci return collection_p; 46425bb815Sopenharmony_ci} /* ecma_new_collection */ 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_ci/** 49425bb815Sopenharmony_ci * Deallocate a collection of ecma values without freeing it's values 50425bb815Sopenharmony_ci */ 51425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE 52425bb815Sopenharmony_ciecma_collection_destroy (ecma_collection_t *collection_p) /**< value collection */ 53425bb815Sopenharmony_ci{ 54425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 55425bb815Sopenharmony_ci 56425bb815Sopenharmony_ci jmem_heap_free_block (collection_p->buffer_p, ECMA_COLLECTION_ALLOCATED_SIZE (collection_p->capacity)); 57425bb815Sopenharmony_ci jmem_heap_free_block (collection_p, sizeof (ecma_collection_t)); 58425bb815Sopenharmony_ci} /* ecma_collection_destroy */ 59425bb815Sopenharmony_ci 60425bb815Sopenharmony_ci/** 61425bb815Sopenharmony_ci * Free the object collection elements and deallocate the collection 62425bb815Sopenharmony_ci */ 63425bb815Sopenharmony_civoid 64425bb815Sopenharmony_ciecma_collection_free_objects (ecma_collection_t *collection_p) /**< value collection */ 65425bb815Sopenharmony_ci{ 66425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 67425bb815Sopenharmony_ci 68425bb815Sopenharmony_ci ecma_value_t *buffer_p = collection_p->buffer_p; 69425bb815Sopenharmony_ci 70425bb815Sopenharmony_ci for (uint32_t i = 0; i < collection_p->item_count; i++) 71425bb815Sopenharmony_ci { 72425bb815Sopenharmony_ci if (ecma_is_value_object (buffer_p[i])) 73425bb815Sopenharmony_ci { 74425bb815Sopenharmony_ci ecma_deref_object (ecma_get_object_from_value (buffer_p[i])); 75425bb815Sopenharmony_ci } 76425bb815Sopenharmony_ci } 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci ecma_collection_destroy (collection_p); 79425bb815Sopenharmony_ci} /* ecma_collection_free_objects */ 80425bb815Sopenharmony_ci 81425bb815Sopenharmony_ci/** 82425bb815Sopenharmony_ci * Free the non-object collection elements and deallocate the collection 83425bb815Sopenharmony_ci */ 84425bb815Sopenharmony_civoid 85425bb815Sopenharmony_ciecma_collection_free_if_not_object (ecma_collection_t *collection_p) /**< value collection */ 86425bb815Sopenharmony_ci{ 87425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 88425bb815Sopenharmony_ci 89425bb815Sopenharmony_ci ecma_value_t *buffer_p = collection_p->buffer_p; 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci for (uint32_t i = 0; i < collection_p->item_count; i++) 92425bb815Sopenharmony_ci { 93425bb815Sopenharmony_ci ecma_free_value_if_not_object (buffer_p[i]); 94425bb815Sopenharmony_ci } 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci ecma_collection_destroy (collection_p); 97425bb815Sopenharmony_ci} /* ecma_collection_free_if_not_object */ 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_ci/** 100425bb815Sopenharmony_ci * Free the collection elements and deallocate the collection 101425bb815Sopenharmony_ci */ 102425bb815Sopenharmony_civoid 103425bb815Sopenharmony_ciecma_collection_free (ecma_collection_t *collection_p) /**< value collection */ 104425bb815Sopenharmony_ci{ 105425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 106425bb815Sopenharmony_ci 107425bb815Sopenharmony_ci ecma_value_t *buffer_p = collection_p->buffer_p; 108425bb815Sopenharmony_ci 109425bb815Sopenharmony_ci for (uint32_t i = 0; i < collection_p->item_count; i++) 110425bb815Sopenharmony_ci { 111425bb815Sopenharmony_ci ecma_free_value (buffer_p[i]); 112425bb815Sopenharmony_ci } 113425bb815Sopenharmony_ci 114425bb815Sopenharmony_ci ecma_collection_destroy (collection_p); 115425bb815Sopenharmony_ci} /* ecma_collection_free */ 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci/** 118425bb815Sopenharmony_ci * Append new value to ecma values collection 119425bb815Sopenharmony_ci * 120425bb815Sopenharmony_ci * Note: The reference count of the values are not increased 121425bb815Sopenharmony_ci */ 122425bb815Sopenharmony_civoid 123425bb815Sopenharmony_ciecma_collection_push_back (ecma_collection_t *collection_p, /**< value collection */ 124425bb815Sopenharmony_ci ecma_value_t value) /**< ecma value to append */ 125425bb815Sopenharmony_ci{ 126425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 127425bb815Sopenharmony_ci 128425bb815Sopenharmony_ci ecma_value_t *buffer_p = collection_p->buffer_p; 129425bb815Sopenharmony_ci 130425bb815Sopenharmony_ci if (JERRY_LIKELY (collection_p->item_count < collection_p->capacity)) 131425bb815Sopenharmony_ci { 132425bb815Sopenharmony_ci buffer_p[collection_p->item_count++] = value; 133425bb815Sopenharmony_ci return; 134425bb815Sopenharmony_ci } 135425bb815Sopenharmony_ci 136425bb815Sopenharmony_ci const uint32_t new_capacity = collection_p->capacity + ECMA_COLLECTION_GROW_FACTOR; 137425bb815Sopenharmony_ci const uint32_t old_size = ECMA_COLLECTION_ALLOCATED_SIZE (collection_p->capacity); 138425bb815Sopenharmony_ci const uint32_t new_size = ECMA_COLLECTION_ALLOCATED_SIZE (new_capacity); 139425bb815Sopenharmony_ci 140425bb815Sopenharmony_ci buffer_p = jmem_heap_realloc_block (buffer_p, old_size, new_size); 141425bb815Sopenharmony_ci buffer_p[collection_p->item_count++] = value; 142425bb815Sopenharmony_ci collection_p->capacity = new_capacity; 143425bb815Sopenharmony_ci 144425bb815Sopenharmony_ci collection_p->buffer_p = buffer_p; 145425bb815Sopenharmony_ci} /* ecma_collection_push_back */ 146425bb815Sopenharmony_ci 147425bb815Sopenharmony_ci/** 148425bb815Sopenharmony_ci * Reserve space for the given amount of ecma_values in the collection 149425bb815Sopenharmony_ci */ 150425bb815Sopenharmony_civoid 151425bb815Sopenharmony_ciecma_collection_reserve (ecma_collection_t *collection_p, /**< value collection */ 152425bb815Sopenharmony_ci uint32_t count) /**< number of ecma values to reserve */ 153425bb815Sopenharmony_ci{ 154425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 155425bb815Sopenharmony_ci JERRY_ASSERT (UINT32_MAX - count > collection_p->capacity); 156425bb815Sopenharmony_ci 157425bb815Sopenharmony_ci const uint32_t new_capacity = collection_p->capacity + count; 158425bb815Sopenharmony_ci const uint32_t old_size = ECMA_COLLECTION_ALLOCATED_SIZE (collection_p->capacity); 159425bb815Sopenharmony_ci const uint32_t new_size = ECMA_COLLECTION_ALLOCATED_SIZE (new_capacity); 160425bb815Sopenharmony_ci 161425bb815Sopenharmony_ci ecma_value_t *buffer_p = collection_p->buffer_p; 162425bb815Sopenharmony_ci buffer_p = jmem_heap_realloc_block (buffer_p, old_size, new_size); 163425bb815Sopenharmony_ci 164425bb815Sopenharmony_ci collection_p->capacity = new_capacity; 165425bb815Sopenharmony_ci collection_p->buffer_p = buffer_p; 166425bb815Sopenharmony_ci} /* ecma_collection_reserve */ 167425bb815Sopenharmony_ci 168425bb815Sopenharmony_ci/** 169425bb815Sopenharmony_ci * Append a list of values to the end of the collection 170425bb815Sopenharmony_ci */ 171425bb815Sopenharmony_civoid 172425bb815Sopenharmony_ciecma_collection_append (ecma_collection_t *collection_p, /**< value collection */ 173425bb815Sopenharmony_ci const ecma_value_t *buffer_p, /**< values to append */ 174425bb815Sopenharmony_ci uint32_t count) /**< number of ecma values to append */ 175425bb815Sopenharmony_ci{ 176425bb815Sopenharmony_ci JERRY_ASSERT (collection_p != NULL); 177425bb815Sopenharmony_ci JERRY_ASSERT (collection_p->capacity >= collection_p->item_count); 178425bb815Sopenharmony_ci 179425bb815Sopenharmony_ci uint32_t free_count = collection_p->capacity - collection_p->item_count; 180425bb815Sopenharmony_ci 181425bb815Sopenharmony_ci if (free_count < count) 182425bb815Sopenharmony_ci { 183425bb815Sopenharmony_ci ecma_collection_reserve (collection_p, count - free_count); 184425bb815Sopenharmony_ci } 185425bb815Sopenharmony_ci 186425bb815Sopenharmony_ci memcpy (collection_p->buffer_p + collection_p->item_count, buffer_p, count * sizeof (ecma_value_t)); 187425bb815Sopenharmony_ci collection_p->item_count += count; 188425bb815Sopenharmony_ci} /* ecma_collection_append */ 189425bb815Sopenharmony_ci 190425bb815Sopenharmony_ci/** 191425bb815Sopenharmony_ci * @} 192425bb815Sopenharmony_ci * @} 193425bb815Sopenharmony_ci */ 194