impl: add {pre,post}_assert statements
[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 #include "php_psi_stdinc.h"
27
28 #include "plist.h"
29
30 struct psi_plist {
31 size_t size;
32 size_t count;
33 void (*dtor)(void *);
34 void *list[0];
35 };
36
37 #define PLIST_ELE(l, i) (((char *)(l)->list) + (l)->size * (i))
38 #define PLIST_CPY(list, dest, src) do { \
39 if (list->size == sizeof(void *)) { \
40 *(void **)dest = *(void **)src; \
41 } else { \
42 memcpy(dest, src, list->size); \
43 } \
44 } while (0)
45 #define PLIST_MOV(l, i) memmove(PLIST_ELE(l, i), PLIST_ELE(l, i + 1), (l)->size * ((l)->count - i))
46
47 struct psi_plist *psi_plist_init(void (*dtor)(void *)) {
48 return psi_plist_init_ex(0, dtor);
49 }
50 struct psi_plist *psi_plist_init_ex(size_t size, void (*dtor)(void *)) {
51 struct psi_plist *list;
52
53 if (!size) {
54 size = sizeof(void*);
55 }
56
57 list = calloc(1, sizeof(*list) + size);
58 list->size = size;
59 list->dtor = dtor;
60
61 return list;
62 }
63
64 void psi_plist_free(struct psi_plist *list) {
65 size_t i;
66
67 if (list->dtor) for (i = 0; i < list->count; ++i) {
68 list->dtor(PLIST_ELE(list, i));
69 }
70 free(list);
71 }
72
73 size_t psi_plist_count(struct psi_plist *list) {
74 return list ? list->count : 0;
75 }
76
77 struct psi_plist *psi_plist_add(struct psi_plist *list, void *ptr) {
78 if (list && list->count) {
79 list = realloc(list, sizeof(*list) + list->size + list->count * list->size);
80 }
81 if (list) {
82 PLIST_CPY(list, PLIST_ELE(list, list->count++), ptr);
83 }
84 return list;
85 }
86
87 bool psi_plist_get(struct psi_plist *list, size_t index, void *ptr) {
88 if (list && list->count > index) {
89 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
90 return true;
91 }
92 return false;
93 }
94
95 bool psi_plist_del(struct psi_plist *list, size_t index, void *ptr) {
96 if (list && list->count > index) {
97 if (ptr) {
98 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
99 }
100 if (--list->count > index) {
101 PLIST_MOV(list, index);
102 }
103 return true;
104 }
105 return false;
106 }
107
108 bool psi_plist_shift(struct psi_plist *list, void *ptr) {
109 if (list && list->count) {
110 if (ptr) {
111 PLIST_CPY(list, ptr, PLIST_ELE(list, 0));
112 }
113 if (--list->count) {
114 PLIST_MOV(list, 0);
115 }
116 return true;
117 }
118 return false;
119 }
120
121 bool psi_plist_pop(struct psi_plist *list, void *ptr) {
122 if (list && list->count) {
123 --list->count;
124 if (ptr) {
125 PLIST_CPY(list, ptr, PLIST_ELE(list, list->count));
126 }
127 return true;
128 }
129 return false;
130 }
131
132 bool psi_plist_top(struct psi_plist *list, void *ptr) {
133 if (list && list->count) {
134 PLIST_CPY(list, ptr, PLIST_ELE(list, list->count - 1));
135 return true;
136 }
137 return false;
138 }
139
140
141 static void swp_ptr(void *a, void *b) {
142 void **_a = a, **_b = b, *_c;
143
144 _c = *_b;
145 *_b = *_a;
146 *_a = _c;
147 }
148
149 void psi_plist_sort(struct psi_plist *list, compare_func_t cmp, swap_func_t swp) {
150 if (!swp && list->size == sizeof(void *)) {
151 swp = swp_ptr;
152 }
153 assert(swp);
154 zend_insert_sort((void *) list->list, list->count, list->size, cmp, swp);
155 }
156