diff options
| author | 2018-12-10 02:20:24 +0100 | |
|---|---|---|
| committer | 2018-12-10 02:22:15 +0100 | |
| commit | 4de329327ce4aa175e8496d1bff8604bffb6c574 (patch) | |
| tree | 0c5fce7e138756a2479acab5a3098fa12ea6971b | |
| parent | 71dd25e14616bd261c3b6c80ff990cd1078266f6 (diff) | |
| download | libplist-4de329327ce4aa175e8496d1bff8604bffb6c574.tar.gz libplist-4de329327ce4aa175e8496d1bff8604bffb6c574.tar.bz2 | |
Remove node_iterator and operate on node list directly to improve memory usage
| -rw-r--r-- | libcnary/Makefile.am | 4 | ||||
| -rw-r--r-- | libcnary/include/iterator.h | 49 | ||||
| -rw-r--r-- | libcnary/include/node_iterator.h | 55 | ||||
| -rw-r--r-- | libcnary/iterator.c | 61 | ||||
| -rw-r--r-- | libcnary/node.c | 6 | ||||
| -rw-r--r-- | libcnary/node_iterator.c | 80 | ||||
| -rw-r--r-- | src/bplist.c | 5 | ||||
| -rw-r--r-- | src/plist.c | 11 | ||||
| -rw-r--r-- | src/xplist.c | 5 |
9 files changed, 7 insertions, 269 deletions
diff --git a/libcnary/Makefile.am b/libcnary/Makefile.am index bba9ada..5a26fb5 100644 --- a/libcnary/Makefile.am +++ b/libcnary/Makefile.am | |||
| @@ -8,11 +8,7 @@ libcnary_la_SOURCES = \ | |||
| 8 | node.c \ | 8 | node.c \ |
| 9 | list.c \ | 9 | list.c \ |
| 10 | node_list.c \ | 10 | node_list.c \ |
| 11 | iterator.c \ | ||
| 12 | node_iterator.c \ | ||
| 13 | include/node.h \ | 11 | include/node.h \ |
| 14 | include/list.h \ | 12 | include/list.h \ |
| 15 | include/node_list.h \ | 13 | include/node_list.h \ |
| 16 | include/iterator.h \ | ||
| 17 | include/node_iterator.h \ | ||
| 18 | include/object.h | 14 | include/object.h |
diff --git a/libcnary/include/iterator.h b/libcnary/include/iterator.h deleted file mode 100644 index a33c4ff..0000000 --- a/libcnary/include/iterator.h +++ /dev/null | |||
| @@ -1,49 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * iterator.h | ||
| 3 | * | ||
| 4 | * Created on: Mar 8, 2011 | ||
| 5 | * Author: posixninja | ||
| 6 | * | ||
| 7 | * Copyright (c) 2011 Joshua Hill. All Rights Reserved. | ||
| 8 | * | ||
| 9 | * This library is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This library is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with this library; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifndef ITERATOR_H_ | ||
| 25 | #define ITERATOR_H_ | ||
| 26 | |||
| 27 | struct list_t; | ||
| 28 | struct object_t; | ||
| 29 | |||
| 30 | typedef struct iterator_t { | ||
| 31 | struct object_t*(*next)(struct iterator_t* iterator); | ||
| 32 | int(*bind)(struct iterator_t* iterator, struct list_t* list); | ||
| 33 | |||
| 34 | unsigned int count; | ||
| 35 | unsigned int position; | ||
| 36 | |||
| 37 | struct list_t* list; | ||
| 38 | struct object_t* end; | ||
| 39 | struct object_t* begin; | ||
| 40 | struct object_t* value; | ||
| 41 | } iterator_t; | ||
| 42 | |||
| 43 | void iterator_destroy(struct iterator_t* iterator); | ||
| 44 | struct iterator_t* iterator_create(struct list_t* list); | ||
| 45 | |||
| 46 | struct object_t* iterator_next(struct iterator_t* iterator); | ||
| 47 | int iterator_bind(struct iterator_t* iterator, struct list_t* list); | ||
| 48 | |||
| 49 | #endif /* ITERATOR_H_ */ | ||
diff --git a/libcnary/include/node_iterator.h b/libcnary/include/node_iterator.h deleted file mode 100644 index d3bce3a..0000000 --- a/libcnary/include/node_iterator.h +++ /dev/null | |||
| @@ -1,55 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * node_iterator.h | ||
| 3 | * | ||
| 4 | * Created on: Mar 8, 2011 | ||
| 5 | * Author: posixninja | ||
| 6 | * | ||
| 7 | * Copyright (c) 2011 Joshua Hill. All Rights Reserved. | ||
| 8 | * | ||
| 9 | * This library is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This library is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with this library; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifndef NODE_ITERATOR_H_ | ||
| 25 | #define NODE_ITERATOR_H_ | ||
| 26 | |||
| 27 | #include "iterator.h" | ||
| 28 | #include "node_list.h" | ||
| 29 | |||
| 30 | // This class implements the abstract iterator class | ||
| 31 | typedef struct node_iterator_t { | ||
| 32 | // Super class | ||
| 33 | struct iterator_t super; | ||
| 34 | |||
| 35 | // Local members | ||
| 36 | struct node_t*(*next)(struct node_iterator_t* iterator); | ||
| 37 | int(*bind)(struct node_iterator_t* iterator, struct node_list_t* list); | ||
| 38 | |||
| 39 | unsigned int count; | ||
| 40 | unsigned int position; | ||
| 41 | |||
| 42 | struct node_list_t* list; | ||
| 43 | struct node_t* end; | ||
| 44 | struct node_t* begin; | ||
| 45 | struct node_t* value; | ||
| 46 | |||
| 47 | } node_iterator_t; | ||
| 48 | |||
| 49 | void node_iterator_destroy(node_iterator_t* iterator); | ||
| 50 | node_iterator_t* node_iterator_create(node_list_t* list); | ||
| 51 | |||
| 52 | struct node_t* node_iterator_next(struct node_iterator_t* iterator); | ||
| 53 | int node_iterator_bind(struct node_iterator_t* iterator, struct node_list_t* list); | ||
| 54 | |||
| 55 | #endif /* NODE_ITERATOR_H_ */ | ||
diff --git a/libcnary/iterator.c b/libcnary/iterator.c deleted file mode 100644 index 492a8ae..0000000 --- a/libcnary/iterator.c +++ /dev/null | |||
| @@ -1,61 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * iterator.c | ||
| 3 | * | ||
| 4 | * Created on: Mar 8, 2011 | ||
| 5 | * Author: posixninja | ||
| 6 | * | ||
| 7 | * Copyright (c) 2011 Joshua Hill. All Rights Reserved. | ||
| 8 | * | ||
| 9 | * This library is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This library is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with this library; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <stdio.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <string.h> | ||
| 27 | |||
| 28 | #include "list.h" | ||
| 29 | #include "object.h" | ||
| 30 | #include "iterator.h" | ||
| 31 | |||
| 32 | void iterator_destroy(iterator_t* iterator) { | ||
| 33 | if(iterator) { | ||
| 34 | free(iterator); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | iterator_t* iterator_create(list_t* list) { | ||
| 39 | iterator_t* iterator = (iterator_t*) malloc(sizeof(iterator_t)); | ||
| 40 | if(iterator == NULL) { | ||
| 41 | return NULL; | ||
| 42 | } | ||
| 43 | memset(iterator, '\0', sizeof(iterator_t)); | ||
| 44 | |||
| 45 | if(list != NULL) { | ||
| 46 | // Create and bind to list | ||
| 47 | |||
| 48 | } else { | ||
| 49 | // Empty Iterator | ||
| 50 | } | ||
| 51 | |||
| 52 | return iterator; | ||
| 53 | } | ||
| 54 | |||
| 55 | object_t* iterator_next(iterator_t* iterator) { | ||
| 56 | return NULL; | ||
| 57 | } | ||
| 58 | |||
| 59 | int iterator_bind(iterator_t* iterator, list_t* list) { | ||
| 60 | return -1; | ||
| 61 | } | ||
diff --git a/libcnary/node.c b/libcnary/node.c index 4b550dd..c24ca7a 100644 --- a/libcnary/node.c +++ b/libcnary/node.c | |||
| @@ -26,7 +26,6 @@ | |||
| 26 | 26 | ||
| 27 | #include "node.h" | 27 | #include "node.h" |
| 28 | #include "node_list.h" | 28 | #include "node_list.h" |
| 29 | #include "node_iterator.h" | ||
| 30 | 29 | ||
| 31 | void node_destroy(node_t* node) { | 30 | void node_destroy(node_t* node) { |
| 32 | if(!node) return; | 31 | if(!node) return; |
| @@ -114,7 +113,6 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child) | |||
| 114 | static void _node_debug(node_t* node, unsigned int depth) { | 113 | static void _node_debug(node_t* node, unsigned int depth) { |
| 115 | unsigned int i = 0; | 114 | unsigned int i = 0; |
| 116 | node_t* current = NULL; | 115 | node_t* current = NULL; |
| 117 | node_iterator_t* iter = NULL; | ||
| 118 | for(i = 0; i < depth; i++) { | 116 | for(i = 0; i < depth; i++) { |
| 119 | printf("\t"); | 117 | printf("\t"); |
| 120 | } | 118 | } |
| @@ -128,11 +126,9 @@ static void _node_debug(node_t* node, unsigned int depth) { | |||
| 128 | if(node->parent) { | 126 | if(node->parent) { |
| 129 | printf("NODE\n"); | 127 | printf("NODE\n"); |
| 130 | } | 128 | } |
| 131 | iter = node_iterator_create(node->children); | 129 | for (current = node_first_child(node); current; current = node_next_sibling(current)) { |
| 132 | while ((current = iter->next(iter))) { | ||
| 133 | _node_debug(current, depth+1); | 130 | _node_debug(current, depth+1); |
| 134 | } | 131 | } |
| 135 | node_iterator_destroy(iter); | ||
| 136 | } | 132 | } |
| 137 | 133 | ||
| 138 | } | 134 | } |
diff --git a/libcnary/node_iterator.c b/libcnary/node_iterator.c deleted file mode 100644 index e629b73..0000000 --- a/libcnary/node_iterator.c +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * node_iterator.c | ||
| 3 | * | ||
| 4 | * Created on: Mar 8, 2011 | ||
| 5 | * Author: posixninja | ||
| 6 | * | ||
| 7 | * Copyright (c) 2011 Joshua Hill. All Rights Reserved. | ||
| 8 | * | ||
| 9 | * This library is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This library is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with this library; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <stdio.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <string.h> | ||
| 27 | |||
| 28 | #include "node.h" | ||
| 29 | #include "node_list.h" | ||
| 30 | #include "node_iterator.h" | ||
| 31 | |||
| 32 | void node_iterator_destroy(node_iterator_t* iterator) { | ||
| 33 | if(iterator) { | ||
| 34 | free(iterator); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | node_iterator_t* node_iterator_create(node_list_t* list) { | ||
| 39 | node_iterator_t* iterator = (node_iterator_t*) malloc(sizeof(node_iterator_t)); | ||
| 40 | if(iterator == NULL) { | ||
| 41 | return NULL; | ||
| 42 | } | ||
| 43 | memset(iterator, '\0', sizeof(node_iterator_t)); | ||
| 44 | |||
| 45 | iterator->count = 0; | ||
| 46 | iterator->position = 0; | ||
| 47 | |||
| 48 | iterator->end = NULL; | ||
| 49 | iterator->begin = NULL; | ||
| 50 | iterator->value = NULL; | ||
| 51 | |||
| 52 | iterator->list = NULL; | ||
| 53 | iterator->next = node_iterator_next; | ||
| 54 | iterator->bind = node_iterator_bind; | ||
| 55 | |||
| 56 | |||
| 57 | if(list != NULL) { | ||
| 58 | iterator->bind(iterator, list); | ||
| 59 | } | ||
| 60 | |||
| 61 | return iterator; | ||
| 62 | } | ||
| 63 | |||
| 64 | node_t* node_iterator_next(node_iterator_t* iterator) { | ||
| 65 | node_t* node = iterator->value; | ||
| 66 | if (node) { | ||
| 67 | iterator->value = node->next; | ||
| 68 | } | ||
| 69 | iterator->position++; | ||
| 70 | return node; | ||
| 71 | } | ||
| 72 | |||
| 73 | int node_iterator_bind(node_iterator_t* iterator, node_list_t* list) { | ||
| 74 | iterator->position = 0; | ||
| 75 | iterator->end = list->end; | ||
| 76 | iterator->count = list->count; | ||
| 77 | iterator->begin = list->begin; | ||
| 78 | iterator->value = list->begin; | ||
| 79 | return 0; | ||
| 80 | } | ||
diff --git a/src/bplist.c b/src/bplist.c index 69f3dca..679a5e5 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -39,7 +39,6 @@ | |||
| 39 | #include "ptrarray.h" | 39 | #include "ptrarray.h" |
| 40 | 40 | ||
| 41 | #include <node.h> | 41 | #include <node.h> |
| 42 | #include <node_iterator.h> | ||
| 43 | 42 | ||
| 44 | /* Magic marker and size. */ | 43 | /* Magic marker and size. */ |
| 45 | #define BPLIST_MAGIC ((uint8_t*)"bplist") | 44 | #define BPLIST_MAGIC ((uint8_t*)"bplist") |
| @@ -938,12 +937,10 @@ static void serialize_plist(node_t* node, void* data) | |||
| 938 | ptr_array_add(ser->objects, node); | 937 | ptr_array_add(ser->objects, node); |
| 939 | 938 | ||
| 940 | //now recurse on children | 939 | //now recurse on children |
| 941 | node_iterator_t *ni = node_iterator_create(node->children); | ||
| 942 | node_t *ch; | 940 | node_t *ch; |
| 943 | while ((ch = node_iterator_next(ni))) { | 941 | for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { |
| 944 | serialize_plist(ch, data); | 942 | serialize_plist(ch, data); |
| 945 | } | 943 | } |
| 946 | node_iterator_destroy(ni); | ||
| 947 | 944 | ||
| 948 | return; | 945 | return; |
| 949 | } | 946 | } |
diff --git a/src/plist.c b/src/plist.c index f62e6be..cd22ca6 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -36,7 +36,6 @@ | |||
| 36 | #endif | 36 | #endif |
| 37 | 37 | ||
| 38 | #include <node.h> | 38 | #include <node.h> |
| 39 | #include <node_iterator.h> | ||
| 40 | #include <hashtable.h> | 39 | #include <hashtable.h> |
| 41 | 40 | ||
| 42 | extern void plist_xml_init(void); | 41 | extern void plist_xml_init(void); |
| @@ -209,12 +208,12 @@ static int plist_free_node(node_t* node) | |||
| 209 | plist_free_data(data); | 208 | plist_free_data(data); |
| 210 | node->data = NULL; | 209 | node->data = NULL; |
| 211 | 210 | ||
| 212 | node_iterator_t *ni = node_iterator_create(node->children); | ||
| 213 | node_t *ch; | 211 | node_t *ch; |
| 214 | while ((ch = node_iterator_next(ni))) { | 212 | for (ch = node_first_child(node); ch; ) { |
| 213 | node_t *next = node_next_sibling(ch); | ||
| 215 | plist_free_node(ch); | 214 | plist_free_node(ch); |
| 215 | ch = next; | ||
| 216 | } | 216 | } |
| 217 | node_iterator_destroy(ni); | ||
| 218 | 217 | ||
| 219 | node_destroy(node); | 218 | node_destroy(node); |
| 220 | 219 | ||
| @@ -366,12 +365,10 @@ static void plist_copy_node(node_t *node, void *parent_node_ptr) | |||
| 366 | *(plist_t*)parent_node_ptr = newnode; | 365 | *(plist_t*)parent_node_ptr = newnode; |
| 367 | } | 366 | } |
| 368 | 367 | ||
| 369 | node_iterator_t *ni = node_iterator_create(node->children); | ||
| 370 | node_t *ch; | 368 | node_t *ch; |
| 371 | while ((ch = node_iterator_next(ni))) { | 369 | for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { |
| 372 | plist_copy_node(ch, &newnode); | 370 | plist_copy_node(ch, &newnode); |
| 373 | } | 371 | } |
| 374 | node_iterator_destroy(ni); | ||
| 375 | } | 372 | } |
| 376 | 373 | ||
| 377 | PLIST_API plist_t plist_copy(plist_t node) | 374 | PLIST_API plist_t plist_copy(plist_t node) |
diff --git a/src/xplist.c b/src/xplist.c index 6e64427..29b1284 100644 --- a/src/xplist.c +++ b/src/xplist.c | |||
| @@ -40,7 +40,6 @@ | |||
| 40 | 40 | ||
| 41 | #include <node.h> | 41 | #include <node.h> |
| 42 | #include <node_list.h> | 42 | #include <node_list.h> |
| 43 | #include <node_iterator.h> | ||
| 44 | 43 | ||
| 45 | #include "plist.h" | 44 | #include "plist.h" |
| 46 | #include "base64.h" | 45 | #include "base64.h" |
| @@ -356,12 +355,10 @@ static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth) | |||
| 356 | if (node_data->type == PLIST_DICT) { | 355 | if (node_data->type == PLIST_DICT) { |
| 357 | assert((node->children->count % 2) == 0); | 356 | assert((node->children->count % 2) == 0); |
| 358 | } | 357 | } |
| 359 | node_iterator_t *ni = node_iterator_create(node->children); | ||
| 360 | node_t *ch; | 358 | node_t *ch; |
| 361 | while ((ch = node_iterator_next(ni))) { | 359 | for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { |
| 362 | node_to_xml(ch, outbuf, depth+1); | 360 | node_to_xml(ch, outbuf, depth+1); |
| 363 | } | 361 | } |
| 364 | node_iterator_destroy(ni); | ||
| 365 | } | 362 | } |
| 366 | 363 | ||
| 367 | /* fix indent for structured types */ | 364 | /* fix indent for structured types */ |
