src/ucx/array_list.c

Sun, 11 Jun 2023 15:53:55 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Jun 2023 15:53:55 +0200
changeset 502
11ac3761c0e3
parent 490
d218607f5a7e
child 504
c094afcdfb27
permissions
-rw-r--r--

fix non-blocking CGI handler and non-blocking SSL-IO

436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/array_list.h"
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <assert.h>
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
33 // LOW LEVEL ARRAY LIST FUNCTIONS
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 enum cx_array_copy_result cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 void **target,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 size_t *size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 size_t *capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 size_t index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 void const *src,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 size_t elem_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 size_t elem_count,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 struct cx_array_reallocator_s *reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 ) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
45 // assert pointers
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 assert(target != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 assert(size != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 assert(src != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
50 // determine capacity
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 size_t cap = capacity == NULL ? *size : *capacity;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
53 // check if resize is required
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
54 size_t minsize = index + elem_count;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
55 size_t newsize = *size < minsize ? minsize : *size;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 bool needrealloc = newsize > cap;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
58 // reallocate if possible
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if (needrealloc) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
60 // a reallocator and a capacity variable must be available
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if (reallocator == NULL || capacity == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
65 // check, if we need to repair the src pointer
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 uintptr_t targetaddr = (uintptr_t) *target;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 uintptr_t srcaddr = (uintptr_t) src;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 bool repairsrc = targetaddr <= srcaddr
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 && srcaddr < targetaddr + cap * elem_size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
71 // calculate new capacity (next number divisible by 16)
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
72 cap = newsize - (newsize % 16) + 16;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
73 assert(cap > newsize);
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
75 // perform reallocation
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 void *newmem = reallocator->realloc(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 *target, cap, elem_size, reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 if (newmem == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 return CX_ARRAY_COPY_REALLOC_FAILED;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
83 // repair src pointer, if necessary
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 if (repairsrc) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 src = ((char *) newmem) + (srcaddr - targetaddr);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
88 // store new pointer and capacity
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 *target = newmem;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 *capacity = cap;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
93 // determine target pointer
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 char *start = *target;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 start += index * elem_size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
97 // copy elements and set new size
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 memmove(start, src, elem_count * elem_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 *size = newsize;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
101 // return successfully
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return CX_ARRAY_COPY_SUCCESS;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
105 #ifndef CX_ARRAY_SWAP_SBO_SIZE
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
106 #define CX_ARRAY_SWAP_SBO_SIZE 512
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
107 #endif
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
108
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
109 void cx_array_swap(
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
110 void *arr,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
111 size_t elem_size,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
112 size_t idx1,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
113 size_t idx2
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
114 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
115 assert(arr != NULL);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
116
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
117 // short circuit
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
118 if (idx1 == idx2) return;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
119
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
120 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
121 void *tmp;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
122
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
123 // decide if we can use the local buffer
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
124 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
125 tmp = malloc(elem_size);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
126 // we don't want to enforce error handling
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
127 if (tmp == NULL) abort();
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
128 } else {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
129 tmp = sbo_mem;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
130 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
131
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
132 // calculate memory locations
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
133 char *left = arr, *right = arr;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
134 left += idx1 * elem_size;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
135 right += idx2 * elem_size;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
136
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
137 // three-way swap
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
138 memcpy(tmp, left, elem_size);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
139 memcpy(left, right, elem_size);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
140 memcpy(right, tmp, elem_size);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
141
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
142 // free dynamic memory, if it was needed
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
143 if (tmp != sbo_mem) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
144 free(tmp);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
145 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
146 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
147
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
148 // HIGH LEVEL ARRAY LIST FUNCTIONS
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 typedef struct {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 struct cx_list_s base;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 void *data;
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
153 size_t capacity;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 struct cx_array_reallocator_s reallocator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 } cx_array_list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 static void *cx_arl_realloc(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 void *array,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 size_t capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 size_t elem_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 struct cx_array_reallocator_s *alloc
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 ) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
163 // retrieve the pointer to the list allocator
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 CxAllocator const *al = alloc->ptr1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
166 // use the list allocator to reallocate the memory
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 return cxRealloc(al, array, capacity * elem_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 static void cx_arl_destructor(struct cx_list_s *list) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 cx_array_list *arl = (cx_array_list *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 cxFree(list->allocator, arl->data);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
175 static size_t cx_arl_insert_array(
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 struct cx_list_s *list,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
177 size_t index,
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
178 void const *array,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
179 size_t n
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
180 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
181 // out of bounds and special case check
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
182 if (index > list->size || n == 0) return 0;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
183
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
184 // get a correctly typed pointer to the list
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
185 cx_array_list *arl = (cx_array_list *) list;
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
186
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
187 // do we need to move some elements?
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
188 if (index < list->size) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
189 char const *first_to_move = (char const *) arl->data;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
190 first_to_move += index * list->item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
191 size_t elems_to_move = list->size - index;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
192 size_t start_of_moved = index + n;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
193
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
194 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
195 &arl->data,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
196 &list->size,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
197 &arl->capacity,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
198 start_of_moved,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
199 first_to_move,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
200 list->item_size,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
201 elems_to_move,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
202 &arl->reallocator
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
203 )) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
204 // if moving existing elems is unsuccessful, abort
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
205 return 0;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
206 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
207 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
208
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
209 // note that if we had to move the elements, the following operation
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
210 // is guaranteed to succeed, because we have the memory already allocated
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
211 // therefore, it is impossible to leave this function with an invalid array
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
212
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
213 // place the new elements
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
214 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
215 &arl->data,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
216 &list->size,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
217 &arl->capacity,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
218 index,
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
219 array,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
220 list->item_size,
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
221 n,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
222 &arl->reallocator
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
223 )) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
224 return n;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
225 } else {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
226 // array list implementation is "all or nothing"
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
227 return 0;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
228 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
229 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
230
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
231 static int cx_arl_insert_element(
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 size_t index,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
234 void const *element
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
236 return 1 != cx_arl_insert_array(list, index, element, 1);
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 static int cx_arl_insert_iter(
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
240 struct cx_mut_iterator_s *iter,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 void const *elem,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 int prepend
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 ) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
244 struct cx_list_s *list = iter->src_handle;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
245 if (iter->index < list->size) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
246 int result = cx_arl_insert_element(
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
247 list,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
248 iter->index + 1 - prepend,
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
249 elem
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
250 );
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
251 if (result == 0 && prepend != 0) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
252 iter->index++;
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
253 iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
254 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
255 return result;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
256 } else {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
257 int result = cx_arl_insert_element(list, list->size, elem);
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
258 iter->index = list->size;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
259 return result;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
260 }
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 static int cx_arl_remove(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 size_t index
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
267 cx_array_list *arl = (cx_array_list *) list;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
268
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
269 // out-of-bounds check
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 if (index >= list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 return 1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
274 // content destruction
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
275 cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
276
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
277 // short-circuit removal of last element
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
278 if (index == list->size - 1) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
279 list->size--;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
280 return 0;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
281 }
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
283 // just move the elements starting at index to the left
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 int result = cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 &arl->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 &list->size,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
287 &arl->capacity,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 index,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
289 ((char *) arl->data) + (index + 1) * list->item_size,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
290 list->item_size,
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
291 list->size - index - 1,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 &arl->reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 if (result == 0) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
295 // decrease the size
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 list->size--;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 return result;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
301 static void cx_arl_clear(struct cx_list_s *list) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
302 if (list->size == 0) return;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
303
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
304 cx_array_list *arl = (cx_array_list *) list;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
305 char *ptr = arl->data;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
306
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
307 if (list->simple_destructor) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
308 for (size_t i = 0; i < list->size; i++) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
309 cx_invoke_simple_destructor(list, ptr);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
310 ptr += list->item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
311 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
312 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
313 if (list->advanced_destructor) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
314 for (size_t i = 0; i < list->size; i++) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
315 cx_invoke_advanced_destructor(list, ptr);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
316 ptr += list->item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
317 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
318 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
319
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
320 memset(arl->data, 0, list->size * list->item_size);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
321 list->size = 0;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
322 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
323
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
324 static int cx_arl_swap(
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
325 struct cx_list_s *list,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
326 size_t i,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
327 size_t j
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
328 ) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
329 if (i >= list->size || j >= list->size) return 1;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
330 cx_array_list *arl = (cx_array_list *) list;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
331 cx_array_swap(arl->data, list->item_size, i, j);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
332 return 0;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
333 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
334
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 static void *cx_arl_at(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 size_t index
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 if (index < list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 cx_array_list const *arl = (cx_array_list const *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 char *space = arl->data;
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
342 return space + index * list->item_size;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 } else {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
348 static ssize_t cx_arl_find(
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 void const *elem
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
352 assert(list->cmpfunc != NULL);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
353 assert(list->size < SIZE_MAX / 2);
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 char *cur = ((cx_array_list const *) list)->data;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
356 for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 if (0 == list->cmpfunc(elem, cur)) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 return i;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 }
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
360 cur += list->item_size;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
363 return -1;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 static void cx_arl_sort(struct cx_list_s *list) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
367 assert(list->cmpfunc != NULL);
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 qsort(((cx_array_list *) list)->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 list->size,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
370 list->item_size,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 list->cmpfunc
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 static int cx_arl_compare(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 struct cx_list_s const *other
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
379 assert(list->cmpfunc != NULL);
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
380 if (list->size == other->size) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
381 char const *left = ((cx_array_list const *) list)->data;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
382 char const *right = ((cx_array_list const *) other)->data;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
383 for (size_t i = 0; i < list->size; i++) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
384 int d = list->cmpfunc(left, right);
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
385 if (d != 0) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
386 return d;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
387 }
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
388 left += list->item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
389 right += other->item_size;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
390 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
391 return 0;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
392 } else {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
393 return list->size < other->size ? -1 : 1;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
394 }
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 static void cx_arl_reverse(struct cx_list_s *list) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
398 if (list->size < 2) return;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
399 void *data = ((cx_array_list const *) list)->data;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
400 size_t half = list->size / 2;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
401 for (size_t i = 0; i < half; i++) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
402 cx_array_swap(data, list->item_size, i, list->size - 1 - i);
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
403 }
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
406 static bool cx_arl_iter_valid(void const *it) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
407 struct cx_iterator_s const *iter = it;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 struct cx_list_s const *list = iter->src_handle;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 return iter->index < list->size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
412 static void *cx_arl_iter_current(void const *it) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
413 struct cx_iterator_s const *iter = it;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 return iter->elem_handle;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
417 static void cx_arl_iter_next(void *it) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
418 struct cx_iterator_base_s *itbase = it;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
419 if (itbase->remove) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
420 struct cx_mut_iterator_s *iter = it;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
421 itbase->remove = false;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 cx_arl_remove(iter->src_handle, iter->index);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 } else {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
424 struct cx_iterator_s *iter = it;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 iter->index++;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
426 iter->elem_handle =
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
427 ((char *) iter->elem_handle)
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
428 + ((struct cx_list_s const *) iter->src_handle)->item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
429 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
430 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
431
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
432 static void cx_arl_iter_prev(void *it) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
433 struct cx_iterator_base_s *itbase = it;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
434 struct cx_mut_iterator_s *iter = it;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
435 cx_array_list *const list = iter->src_handle;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
436 if (itbase->remove) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
437 itbase->remove = false;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
438 cx_arl_remove(iter->src_handle, iter->index);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
439 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
440 iter->index--;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
441 if (iter->index < list->base.size) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
442 iter->elem_handle = ((char *) list->data)
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
443 + iter->index * list->base.item_size;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
444 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
445 }
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
446
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
447 static bool cx_arl_iter_flag_rm(void *it) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
448 struct cx_iterator_base_s *iter = it;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
449 if (iter->mutating) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
450 iter->remove = true;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
451 return true;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
452 } else {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
453 return false;
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 static struct cx_iterator_s cx_arl_iterator(
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
458 struct cx_list_s const *list,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
459 size_t index,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
460 bool backwards
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 struct cx_iterator_s iter;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 iter.index = index;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 iter.src_handle = list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 iter.elem_handle = cx_arl_at(list, index);
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
467 iter.base.valid = cx_arl_iter_valid;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
468 iter.base.current = cx_arl_iter_current;
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
469 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
470 iter.base.flag_removal = cx_arl_iter_flag_rm;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
471 iter.base.remove = false;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
472 iter.base.mutating = false;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
473
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
474 return iter;
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
475 }
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 static cx_list_class cx_array_list_class = {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 cx_arl_destructor,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
479 cx_arl_insert_element,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
480 cx_arl_insert_array,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 cx_arl_insert_iter,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 cx_arl_remove,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
483 cx_arl_clear,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
484 cx_arl_swap,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 cx_arl_at,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 cx_arl_find,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 cx_arl_sort,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 cx_arl_compare,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 cx_arl_reverse,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 cx_arl_iterator,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 };
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 CxList *cxArrayListCreate(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 CxAllocator const *allocator,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
495 cx_compare_func comparator,
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 size_t item_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 size_t initial_capacity
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 ) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
499 if (allocator == NULL) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
500 allocator = cxDefaultAllocator;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
501 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
502
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 if (list == NULL) return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
506 list->base.cl = &cx_array_list_class;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
507 list->base.allocator = allocator;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
508 list->base.cmpfunc = comparator;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
509 list->capacity = initial_capacity;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
510
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
511 if (item_size > 0) {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
512 list->base.item_size = item_size;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
513 } else {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
514 item_size = sizeof(void *);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
515 cxListStorePointers((CxList *) list);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
516 }
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
517
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
518 // allocate the array after the real item_size is known
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 list->data = cxCalloc(allocator, initial_capacity, item_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 if (list->data == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 cxFree(allocator, list);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 436
diff changeset
525 // configure the reallocator
436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 list->reallocator.realloc = cx_arl_realloc;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 list->reallocator.ptr1 = (void *) allocator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 return (CxList *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 }

mercurial