From cb7e54fe2285887cb41507bd5674677110e63bde Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Sat, 11 Oct 2014 22:57:37 +0400 Subject: [PATCH] Preparser landing patch: refactor linked-list --- src/libintstructs/linked-list.c | 135 ++++++++++++++++++++++++++++++++ src/libintstructs/linked-list.h | 42 ++++------ src/libintstructs/stack.h | 105 ++++--------------------- src/libintstructs/tree.h | 32 ++++++++ 4 files changed, 198 insertions(+), 116 deletions(-) create mode 100644 src/libintstructs/linked-list.c create mode 100644 src/libintstructs/tree.h diff --git a/src/libintstructs/linked-list.c b/src/libintstructs/linked-list.c new file mode 100644 index 000000000..c83cc7bd9 --- /dev/null +++ b/src/libintstructs/linked-list.c @@ -0,0 +1,135 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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 "linked-list.h" +#include "jerry-libc.h" +#include "globals.h" +#include "mem-heap.h" +#include "lp-string.h" + +#define ASSERT_LIST(list) \ +do { \ + linked_list_header *header = (linked_list_header *) list; \ + JERRY_ASSERT (header->magic == LINKED_LIST_MAGIC); \ +} while (0); + +linked_list +linked_list_init (size_t element_size) +{ + size_t size = mem_heap_recommend_allocation_size (element_size); + linked_list list = mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM); + if (list == NULL) + { + __printf ("Out of memory"); + JERRY_UNREACHABLE (); + } + __memset (list, 0, size); + linked_list_header* header = (linked_list_header *) list; + header->magic = LINKED_LIST_MAGIC; + header->prev = header->next = NULL; + header->block_size = (uint8_t) (size - sizeof (linked_list_header)); + return list; +} + +void +linked_list_free (linked_list list) +{ + ASSERT_LIST (list); + linked_list_header *header = (linked_list_header *) list; + if (header->next) + { + linked_list_free ((linked_list) header->next); + } + mem_heap_free_block (list); +} + +void * +linked_list_element (linked_list list, size_t element_size, size_t element_num) +{ + ASSERT_LIST (list); + linked_list_header *header = (linked_list_header *) list; + linked_list raw = list + sizeof (linked_list_header); + if (header->block_size <= element_size * (element_num + 1)) + { + if (header->next) + { + return linked_list_element ((linked_list) header->next, element_size, + element_num - (header->block_size / element_size)); + } + else + { + return NULL; + } + } + raw += element_size * element_num; + return raw; +} + +void +linked_list_set_element (linked_list list, size_t element_size, size_t element_num, void *element) +{ + ASSERT_LIST (list); + linked_list_header *header = (linked_list_header *) list; + linked_list raw = list + sizeof (linked_list_header); + if (header->block_size <= element_size * (element_num + 1)) + { + if (header->next == NULL) + { + header->next = (linked_list_header *) linked_list_init (element_size); + header->next->prev = header; + } + linked_list_set_element ((linked_list) header->next, element_size, + element_num - (header->block_size / element_size), + element); + return; + } + if (element == NULL) + { + return; + } + switch (element_size) + { + case 1: + { + ((uint8_t *) raw)[element_num] = *((uint8_t *) element); + break; + } + case 2: + { + ((uint16_t *) raw)[element_num] = *((uint16_t *) element); + break; + } + case 4: + { + ((uint32_t *) raw)[element_num] = *((uint32_t *) element); + break; + } + case 8: + { + ((uint64_t *) raw)[element_num] = *((uint64_t *) element); + break; + } + case sizeof (lp_string): + { + ((lp_string *) raw)[element_num] = *((lp_string *) element); + break; + } + default: + { + __printf ("Element_size %d is not supported\n", element_size); + JERRY_UNIMPLEMENTED (); + } + } +} diff --git a/src/libintstructs/linked-list.h b/src/libintstructs/linked-list.h index 528e5bf88..13c3bfcfb 100644 --- a/src/libintstructs/linked-list.h +++ b/src/libintstructs/linked-list.h @@ -16,34 +16,26 @@ #ifndef LINKED_LIST_H #define LINKED_LIST_H -#define DEFINE_LINKED_LIST_TYPE(NAME, TYPE) \ -typedef struct NAME##_linked_list \ -{ \ - struct NAME##_linked_list *next; \ - struct NAME##_linked_list *prev; \ -} \ -__packed \ -NAME##_linked_list; +#include "globals.h" -#define DEFINE_LIST_FREE(NAME) \ -static void free_##NAME##_linked_list (uint8_t *) __unused;\ -static void free_##NAME##_linked_list (uint8_t *list) { \ - if (((NAME##_linked_list *)list)->next) { \ - free_##NAME##_linked_list ((uint8_t *) ((NAME##_linked_list *)list)->next); \ - } \ - mem_heap_free_block (list); \ -} +#define LINKED_LIST_MAGIC 0x42 -#define DEFINE_LIST_ELEMENT(NAME, TYPE) \ -static TYPE NAME##_list_element (uint8_t *, uint8_t) __unused; \ -static TYPE NAME##_list_element (uint8_t * raw, uint8_t i) { \ - return ((TYPE *) (raw + sizeof (NAME##_linked_list)))[i]; \ +typedef struct linked_list_header +{ + uint8_t magic; + uint8_t block_size; + uint8_t used_size; + struct linked_list_header *next; + struct linked_list_header *prev; } +__packed +linked_list_header; -#define DEFINE_SET_LIST_ELEMENT(NAME, TYPE) \ -static void set_##NAME##_list_element (uint8_t *, uint8_t, TYPE) __unused; \ -static void set_##NAME##_list_element (uint8_t * raw, uint8_t i, TYPE value) { \ - ((TYPE *) (raw + sizeof (NAME##_linked_list)))[i] = value; \ -} +typedef uint8_t* linked_list; + +linked_list linked_list_init (size_t); +void linked_list_free (linked_list); +void *linked_list_element (linked_list, size_t, size_t); +void linked_list_set_element (linked_list, size_t, size_t, void *); #endif /* LINKED_LIST_H */ diff --git a/src/libintstructs/stack.h b/src/libintstructs/stack.h index 32bdf8d24..22f12e0a0 100644 --- a/src/libintstructs/stack.h +++ b/src/libintstructs/stack.h @@ -69,10 +69,6 @@ #include "linked-list.h" #define DEFINE_STACK_TYPE(NAME, DATA_TYPE, TYPE) \ -DEFINE_LINKED_LIST_TYPE (NAME, TYPE) \ -DEFINE_LIST_FREE (NAME) \ -DEFINE_LIST_ELEMENT (NAME, TYPE) \ -DEFINE_SET_LIST_ELEMENT (NAME, TYPE) \ typedef TYPE NAME##_stack_value_type; \ typedef DATA_TYPE NAME##_stack_data_type; \ typedef struct \ @@ -80,81 +76,42 @@ typedef struct \ DATA_TYPE length; \ DATA_TYPE current; \ DATA_TYPE block_len; \ - uint8_t *blocks; \ - uint8_t *last; \ + linked_list blocks; \ } \ __packed \ NAME##_stack; #define STACK_INIT(TYPE, NAME) \ do { \ - size_t stack_size = mem_heap_recommend_allocation_size (sizeof (TYPE) * NAME##_global_size); \ - NAME.blocks = NAME.last = mem_heap_alloc_block (stack_size, MEM_HEAP_ALLOC_SHORT_TERM); \ - __memset (NAME.blocks, 0, stack_size); \ + NAME.blocks = linked_list_init (sizeof (TYPE)); \ NAME.current = NAME##_global_size; \ - NAME.length = NAME.block_len = (NAME##_stack_data_type) ((stack_size-sizeof (NAME##_linked_list)) / sizeof (TYPE)); \ + NAME.length = NAME.block_len = ((linked_list_header *) NAME.blocks)->block_size / sizeof (TYPE); \ } while (0) #define STACK_FREE(NAME) \ do { \ - free_##NAME##_linked_list (NAME.blocks); \ + linked_list_free (NAME.blocks); \ NAME.length = NAME.current = 0; \ + NAME.blocks = NULL; \ } while (0) -#ifdef JERRY_NDEBUG #define DEFINE_INCREASE_STACK_SIZE(NAME, TYPE) \ static void increase_##NAME##_stack_size (void) __unused; \ static void \ increase_##NAME##_stack_size (void) { \ - if (NAME.current == NAME.length && ((NAME##_linked_list *) NAME.last)->next == NULL) \ + linked_list_set_element (NAME.blocks, sizeof (TYPE), NAME.current, NULL); \ + if (NAME.current >= NAME.length) \ { \ - size_t temp_size = mem_heap_recommend_allocation_size ((size_t) (sizeof (NAME##_linked_list))); \ - JERRY_ASSERT ((temp_size-sizeof (NAME##_linked_list)) / sizeof (TYPE) == NAME.block_len); \ - NAME##_linked_list *temp = (NAME##_linked_list *) mem_heap_alloc_block ( \ - temp_size, MEM_HEAP_ALLOC_SHORT_TERM); \ - if (temp == NULL) \ - { \ - jerry_exit (ERR_MEMORY); \ - } \ - __memset (temp, 0, temp_size); \ - ((NAME##_linked_list *) NAME.last)->next = temp; \ - temp->prev = (NAME##_linked_list *) NAME.last; \ - NAME.last = (uint8_t *) temp; \ NAME.length = (NAME##_stack_data_type) (NAME.length + NAME.block_len); \ } \ NAME.current = (NAME##_stack_data_type) (NAME.current + 1); \ } -#else -#define DEFINE_INCREASE_STACK_SIZE(NAME, TYPE) \ -static void increase_##NAME##_stack_size (void) __unused; \ -static void \ -increase_##NAME##_stack_size (void) { \ - if (NAME.current == NAME.length && ((NAME##_linked_list *) NAME.last)->next == NULL) \ - { \ - size_t temp_size = mem_heap_recommend_allocation_size ((size_t) (sizeof (NAME##_linked_list))); \ - JERRY_ASSERT ((temp_size-sizeof (NAME##_linked_list)) / sizeof (TYPE) == NAME.block_len); \ - NAME##_linked_list *temp = (NAME##_linked_list *) mem_heap_alloc_block ( \ - temp_size, MEM_HEAP_ALLOC_SHORT_TERM); \ - if (temp == NULL) \ - { \ - mem_heap_print (true, false, true); \ - JERRY_UNREACHABLE (); \ - } \ - __memset (temp, 0, temp_size); \ - ((NAME##_linked_list *) NAME.last)->next = temp; \ - temp->prev = (NAME##_linked_list *) NAME.last; \ - NAME.last = (uint8_t *) temp; \ - NAME.length = (NAME##_stack_data_type) (NAME.length + NAME.block_len); \ - } \ - NAME.current = (NAME##_stack_data_type) (NAME.current + 1); \ -} -#endif #define DEFINE_DECREASE_STACE_SIZE(NAME, TYPE) \ static void decrease_##NAME##_stack_size (void) __unused; \ static void \ decrease_##NAME##_stack_size (void) { \ - JERRY_ASSERT (NAME.current - 1 >= NAME##_global_size); \ + JERRY_ASSERT (NAME.current > NAME##_global_size); \ NAME.current = (NAME##_stack_data_type) (NAME.current - 1); \ } @@ -162,62 +119,35 @@ decrease_##NAME##_stack_size (void) { \ static TYPE NAME##_stack_element (NAME##_stack_data_type) __unused; \ static TYPE NAME##_stack_element (NAME##_stack_data_type elem) { \ JERRY_ASSERT (elem < NAME.current); \ - NAME##_linked_list *block = (NAME##_linked_list *) NAME.blocks; \ - for (NAME##_stack_data_type i = 0; i < elem / NAME.block_len; i++) { \ - JERRY_ASSERT (block->next); \ - block = block->next; \ - } \ - return NAME##_list_element ((uint8_t *) block, (uint8_t) ((elem)%NAME.block_len)); \ + return *((TYPE *) linked_list_element (NAME.blocks, sizeof (TYPE), elem)); \ } #define DEFINE_SET_STACK_ELEMENT(NAME, TYPE) \ static void set_##NAME##_stack_element (NAME##_stack_data_type, TYPE) __unused; \ static void set_##NAME##_stack_element (NAME##_stack_data_type elem, TYPE value) { \ JERRY_ASSERT (elem < NAME.current); \ - NAME##_linked_list *block = (NAME##_linked_list *) NAME.blocks; \ - for (NAME##_stack_data_type i = 0; i < elem / NAME.block_len; i++) { \ - JERRY_ASSERT (block->next); \ - block = block->next; \ - } \ - set_##NAME##_list_element ((uint8_t *) block, (uint8_t) ((elem)%NAME.block_len), value); \ + linked_list_set_element (NAME.blocks, sizeof (TYPE), elem, &value); \ } #define DEFINE_STACK_HEAD(NAME, TYPE) \ static TYPE NAME##_stack_head (NAME##_stack_data_type) __unused; \ static TYPE NAME##_stack_head (NAME##_stack_data_type elem) { \ JERRY_ASSERT (elem <= NAME.current); \ - JERRY_ASSERT (elem <= NAME.block_len); \ - if ((NAME.current % NAME.block_len) < elem) { \ - return NAME##_list_element ((uint8_t *) ((NAME##_linked_list *) NAME.last)->prev, \ - (uint8_t) ((NAME.current % NAME.block_len) - elem + NAME.block_len)); \ - } else { \ - return NAME##_list_element (NAME.last, (uint8_t) ((NAME.current % NAME.block_len) - elem)); \ - } \ + return *((TYPE *) linked_list_element (NAME.blocks, sizeof (TYPE), (size_t) (NAME.current - elem))); \ } #define DEFINE_SET_STACK_HEAD(NAME, DATA_TYPE, TYPE) \ static void set_##NAME##_stack_head (DATA_TYPE, TYPE) __unused; \ static void set_##NAME##_stack_head (DATA_TYPE elem, TYPE value) { \ JERRY_ASSERT (elem <= NAME.current); \ - JERRY_ASSERT (elem <= NAME.block_len); \ - if ((NAME.current % NAME.block_len) < elem) { \ - set_##NAME##_list_element ((uint8_t *) ((NAME##_linked_list *) NAME.last)->prev, \ - (uint8_t) ((NAME.current % NAME.block_len) - elem + NAME.block_len), value); \ - } else { \ - set_##NAME##_list_element (NAME.last, (uint8_t) ((NAME.current % NAME.block_len) - elem), value); \ - } \ + linked_list_set_element (NAME.blocks, sizeof (TYPE), (size_t) (NAME.current - elem), &value); \ } #define DEFINE_STACK_PUSH(NAME, TYPE) \ static void NAME##_stack_push (TYPE) __unused; \ static void NAME##_stack_push (TYPE value) { \ - if (NAME.current % NAME.block_len == 0 && NAME.current != 0) { \ - increase_##NAME##_stack_size (); \ - set_##NAME##_list_element (NAME.last, (uint8_t) ((NAME.current - 1) % NAME.block_len), value); \ - } else { \ - set_##NAME##_list_element (NAME.last, (uint8_t) ((NAME.current) % NAME.block_len), value); \ - increase_##NAME##_stack_size (); \ - } \ + linked_list_set_element (NAME.blocks, sizeof (TYPE), NAME.current, &value); \ + increase_##NAME##_stack_size (); \ } #define DEFINE_CONVERT_TO_RAW_DATA(NAME, TYPE) \ @@ -241,15 +171,8 @@ static TYPE *convert_##NAME##_to_raw_data (void) { \ #define STACK_PUSH(NAME, VALUE) \ do { NAME##_stack_push (VALUE); } while (0) -#define STACK_POP(NAME, VALUE) \ -do { \ - decrease_##NAME##_stack_size (); \ - VALUE = NAME##_list_element (NAME.last, (uint8_t) (NAME.current % NAME.block_len)); \ -} while (0) - #define STACK_DROP(NAME, I) \ do { \ - JERRY_ASSERT ((I) >= 0); \ for (size_t i = 0, till = (size_t) (I); i < till; i++) { \ decrease_##NAME##_stack_size (); } } while (0) diff --git a/src/libintstructs/tree.h b/src/libintstructs/tree.h new file mode 100644 index 000000000..7995d70ca --- /dev/null +++ b/src/libintstructs/tree.h @@ -0,0 +1,32 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef TREE_H +#define TREE_H + +#include "linked-list.h" + +#define TREE_MAGIC 0x43 + +typedef struct tree_header +{ + uint8_t magic; + struct tree_header *parent; + linked_list children; +} +__packed +tree_header; + +#endif /* TREE_H */