raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / plist.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include "php_psi_stdinc.h"
33
34 #include "plist.h"
35
36 struct psi_plist {
37 size_t size;
38 size_t count;
39 void (*dtor)(void *);
40 void *list[1];
41 };
42
43 #define PLIST_ELE(l, i) (((char *)(l)->list) + (l)->size * (i))
44 #define PLIST_CPY(list, dest, src) do { \
45 if (list->size == sizeof(void *)) { \
46 *(void **)dest = *(void **)src; \
47 } else { \
48 memcpy(dest, src, list->size); \
49 } \
50 } while (0)
51 #define PLIST_MOV(l, i) memmove(PLIST_ELE(l, i), PLIST_ELE(l, i + 1), (l)->size * ((l)->count - i))
52
53 struct psi_plist *psi_plist_init(void (*dtor)(void *)) {
54 return psi_plist_init_ex(0, dtor);
55 }
56 struct psi_plist *psi_plist_init_ex(size_t size, void (*dtor)(void *)) {
57 struct psi_plist *list = calloc(1, sizeof(*list));
58
59 list->size = size ?: sizeof(void*);
60 list->dtor = dtor;
61
62 return list;
63 }
64
65 void psi_plist_free(struct psi_plist *list) {
66 size_t i;
67
68 if (list->dtor) for (i = 0; i < list->count; ++i) {
69 list->dtor(PLIST_ELE(list, i));
70 }
71 free(list);
72 }
73
74 size_t psi_plist_count(struct psi_plist *list) {
75 return list ? list->count : 0;
76 }
77
78 struct psi_plist *psi_plist_add(struct psi_plist *list, void *ptr) {
79 if (list->count) {
80 list = realloc(list, sizeof(*list) + list->count * list->size);
81 }
82 if (list) {
83 PLIST_CPY(list, PLIST_ELE(list, list->count++), ptr);
84 }
85 return list;
86 }
87
88 bool psi_plist_get(struct psi_plist *list, size_t index, void *ptr) {
89 if (list && list->count > index) {
90 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
91 return true;
92 }
93 return false;
94 }
95
96 bool psi_plist_del(struct psi_plist *list, size_t index, void *ptr) {
97 if (list->count > index) {
98 if (ptr) {
99 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
100 }
101 if (--list->count > index) {
102 PLIST_MOV(list, index);
103 }
104 return true;
105 }
106 return false;
107 }
108
109 bool psi_plist_shift(struct psi_plist *list, void *ptr) {
110 if (list->count) {
111 if (ptr) {
112 PLIST_CPY(list, ptr, PLIST_ELE(list, 0));
113 }
114 if (--list->count) {
115 PLIST_MOV(list, 0);
116 }
117 return true;
118 }
119 return false;
120 }
121
122 bool psi_plist_pop(struct psi_plist *list, void *ptr) {
123 if (list->count) {
124 --list->count;
125 if (ptr) {
126 PLIST_CPY(list, ptr, PLIST_ELE(list, list->count));
127 }
128 return true;
129 }
130 return false;
131 }
132
133
134 static void swp_ptr(void *a, void *b) {
135 void **_a = a, **_b = b, *_c;
136
137 _c = *_b;
138 *_b = *_a;
139 *_a = _c;
140 }
141
142 void psi_plist_sort(struct psi_plist *list, compare_func_t cmp, swap_func_t swp) {
143 if (!swp && list->size == sizeof(void *)) {
144 swp = swp_ptr;
145 }
146 assert(swp);
147 zend_insert_sort((void *) list->list, list->count, list->size, cmp, swp);
148 }
149