xref: /freebsd/sys/contrib/ck/src/ck_array.c (revision 4f9d94bf6491250b649f5bc931b6d93e68373005)
1*1fb62fb0SOlivier Houchard /*
2*1fb62fb0SOlivier Houchard  * Copyright 2013-2015 Samy Al Bahra
3*1fb62fb0SOlivier Houchard  * Copyright 2013-2014 AppNexus, Inc.
4*1fb62fb0SOlivier Houchard  * All rights reserved.
5*1fb62fb0SOlivier Houchard  *
6*1fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
7*1fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
8*1fb62fb0SOlivier Houchard  * are met:
9*1fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
10*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
11*1fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
12*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
13*1fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
14*1fb62fb0SOlivier Houchard  *
15*1fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*1fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*1fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*1fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*1fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*1fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*1fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*1fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*1fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*1fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*1fb62fb0SOlivier Houchard  * SUCH DAMAGE.
26*1fb62fb0SOlivier Houchard  */
27*1fb62fb0SOlivier Houchard 
28*1fb62fb0SOlivier Houchard #include <ck_array.h>
29*1fb62fb0SOlivier Houchard #include <ck_cc.h>
30*1fb62fb0SOlivier Houchard #include <ck_pr.h>
31*1fb62fb0SOlivier Houchard #include <ck_stdbool.h>
32*1fb62fb0SOlivier Houchard #include <ck_string.h>
33*1fb62fb0SOlivier Houchard 
34*1fb62fb0SOlivier Houchard static struct _ck_array *
ck_array_create(struct ck_malloc * allocator,unsigned int length)35*1fb62fb0SOlivier Houchard ck_array_create(struct ck_malloc *allocator, unsigned int length)
36*1fb62fb0SOlivier Houchard {
37*1fb62fb0SOlivier Houchard 	struct _ck_array *active;
38*1fb62fb0SOlivier Houchard 
39*1fb62fb0SOlivier Houchard 	active = allocator->malloc(sizeof(struct _ck_array) + sizeof(void *) * length);
40*1fb62fb0SOlivier Houchard 	if (active == NULL)
41*1fb62fb0SOlivier Houchard 		return NULL;
42*1fb62fb0SOlivier Houchard 
43*1fb62fb0SOlivier Houchard 	active->n_committed = 0;
44*1fb62fb0SOlivier Houchard 	active->length = length;
45*1fb62fb0SOlivier Houchard 
46*1fb62fb0SOlivier Houchard 	return active;
47*1fb62fb0SOlivier Houchard }
48*1fb62fb0SOlivier Houchard 
49*1fb62fb0SOlivier Houchard bool
ck_array_init(struct ck_array * array,unsigned int mode,struct ck_malloc * allocator,unsigned int length)50*1fb62fb0SOlivier Houchard ck_array_init(struct ck_array *array, unsigned int mode, struct ck_malloc *allocator, unsigned int length)
51*1fb62fb0SOlivier Houchard {
52*1fb62fb0SOlivier Houchard 	struct _ck_array *active;
53*1fb62fb0SOlivier Houchard 
54*1fb62fb0SOlivier Houchard 	(void)mode;
55*1fb62fb0SOlivier Houchard 
56*1fb62fb0SOlivier Houchard 	if (allocator->realloc == NULL ||
57*1fb62fb0SOlivier Houchard 	    allocator->malloc == NULL ||
58*1fb62fb0SOlivier Houchard 	    allocator->free == NULL ||
59*1fb62fb0SOlivier Houchard 	    length == 0)
60*1fb62fb0SOlivier Houchard 		return false;
61*1fb62fb0SOlivier Houchard 
62*1fb62fb0SOlivier Houchard 	active = ck_array_create(allocator, length);
63*1fb62fb0SOlivier Houchard 	if (active == NULL)
64*1fb62fb0SOlivier Houchard 		return false;
65*1fb62fb0SOlivier Houchard 
66*1fb62fb0SOlivier Houchard 	array->n_entries = 0;
67*1fb62fb0SOlivier Houchard 	array->allocator = allocator;
68*1fb62fb0SOlivier Houchard 	array->active = active;
69*1fb62fb0SOlivier Houchard 	array->transaction = NULL;
70*1fb62fb0SOlivier Houchard 	return true;
71*1fb62fb0SOlivier Houchard }
72*1fb62fb0SOlivier Houchard 
73*1fb62fb0SOlivier Houchard bool
ck_array_put(struct ck_array * array,void * value)74*1fb62fb0SOlivier Houchard ck_array_put(struct ck_array *array, void *value)
75*1fb62fb0SOlivier Houchard {
76*1fb62fb0SOlivier Houchard 	struct _ck_array *target;
77*1fb62fb0SOlivier Houchard 	unsigned int size;
78*1fb62fb0SOlivier Houchard 
79*1fb62fb0SOlivier Houchard 	/*
80*1fb62fb0SOlivier Houchard 	 * If no transaction copy has been necessary, attempt to do in-place
81*1fb62fb0SOlivier Houchard 	 * modification of the array.
82*1fb62fb0SOlivier Houchard 	 */
83*1fb62fb0SOlivier Houchard 	if (array->transaction == NULL) {
84*1fb62fb0SOlivier Houchard 		target = array->active;
85*1fb62fb0SOlivier Houchard 
86*1fb62fb0SOlivier Houchard 		if (array->n_entries == target->length) {
87*1fb62fb0SOlivier Houchard 			size = target->length << 1;
88*1fb62fb0SOlivier Houchard 
89*1fb62fb0SOlivier Houchard 			target = array->allocator->realloc(target,
90*1fb62fb0SOlivier Houchard 			    sizeof(struct _ck_array) + sizeof(void *) * array->n_entries,
91*1fb62fb0SOlivier Houchard 			    sizeof(struct _ck_array) + sizeof(void *) * size,
92*1fb62fb0SOlivier Houchard 			    true);
93*1fb62fb0SOlivier Houchard 
94*1fb62fb0SOlivier Houchard 			if (target == NULL)
95*1fb62fb0SOlivier Houchard 				return false;
96*1fb62fb0SOlivier Houchard 
97*1fb62fb0SOlivier Houchard 			ck_pr_store_uint(&target->length, size);
98*1fb62fb0SOlivier Houchard 
99*1fb62fb0SOlivier Houchard 			/* Serialize with respect to contents. */
100*1fb62fb0SOlivier Houchard 			ck_pr_fence_store();
101*1fb62fb0SOlivier Houchard 			ck_pr_store_ptr(&array->active, target);
102*1fb62fb0SOlivier Houchard 		}
103*1fb62fb0SOlivier Houchard 
104*1fb62fb0SOlivier Houchard 		target->values[array->n_entries++] = value;
105*1fb62fb0SOlivier Houchard 		return true;
106*1fb62fb0SOlivier Houchard 	}
107*1fb62fb0SOlivier Houchard 
108*1fb62fb0SOlivier Houchard 	target = array->transaction;
109*1fb62fb0SOlivier Houchard 	if (array->n_entries == target->length) {
110*1fb62fb0SOlivier Houchard 		size = target->length << 1;
111*1fb62fb0SOlivier Houchard 
112*1fb62fb0SOlivier Houchard 		target = array->allocator->realloc(target,
113*1fb62fb0SOlivier Houchard 		    sizeof(struct _ck_array) + sizeof(void *) * array->n_entries,
114*1fb62fb0SOlivier Houchard 		    sizeof(struct _ck_array) + sizeof(void *) * size,
115*1fb62fb0SOlivier Houchard 		    true);
116*1fb62fb0SOlivier Houchard 
117*1fb62fb0SOlivier Houchard 		if (target == NULL)
118*1fb62fb0SOlivier Houchard 			return false;
119*1fb62fb0SOlivier Houchard 
120*1fb62fb0SOlivier Houchard 		target->length = size;
121*1fb62fb0SOlivier Houchard 		array->transaction = target;
122*1fb62fb0SOlivier Houchard 	}
123*1fb62fb0SOlivier Houchard 
124*1fb62fb0SOlivier Houchard 	target->values[array->n_entries++] = value;
125*1fb62fb0SOlivier Houchard 	return false;
126*1fb62fb0SOlivier Houchard }
127*1fb62fb0SOlivier Houchard 
128*1fb62fb0SOlivier Houchard int
ck_array_put_unique(struct ck_array * array,void * value)129*1fb62fb0SOlivier Houchard ck_array_put_unique(struct ck_array *array, void *value)
130*1fb62fb0SOlivier Houchard {
131*1fb62fb0SOlivier Houchard 	unsigned int i, limit;
132*1fb62fb0SOlivier Houchard 	void **v;
133*1fb62fb0SOlivier Houchard 
134*1fb62fb0SOlivier Houchard 	limit = array->n_entries;
135*1fb62fb0SOlivier Houchard 	if (array->transaction != NULL) {
136*1fb62fb0SOlivier Houchard 		v = array->transaction->values;
137*1fb62fb0SOlivier Houchard 	} else {
138*1fb62fb0SOlivier Houchard 		v = array->active->values;
139*1fb62fb0SOlivier Houchard 	}
140*1fb62fb0SOlivier Houchard 
141*1fb62fb0SOlivier Houchard 	for (i = 0; i < limit; i++) {
142*1fb62fb0SOlivier Houchard 		if (v[i] == value)
143*1fb62fb0SOlivier Houchard 			return 1;
144*1fb62fb0SOlivier Houchard 	}
145*1fb62fb0SOlivier Houchard 
146*1fb62fb0SOlivier Houchard 	return -!ck_array_put(array, value);
147*1fb62fb0SOlivier Houchard }
148*1fb62fb0SOlivier Houchard 
149*1fb62fb0SOlivier Houchard bool
ck_array_remove(struct ck_array * array,void * value)150*1fb62fb0SOlivier Houchard ck_array_remove(struct ck_array *array, void *value)
151*1fb62fb0SOlivier Houchard {
152*1fb62fb0SOlivier Houchard 	struct _ck_array *target;
153*1fb62fb0SOlivier Houchard 	unsigned int i;
154*1fb62fb0SOlivier Houchard 
155*1fb62fb0SOlivier Houchard 	if (array->transaction != NULL) {
156*1fb62fb0SOlivier Houchard 		target = array->transaction;
157*1fb62fb0SOlivier Houchard 
158*1fb62fb0SOlivier Houchard 		for (i = 0; i < array->n_entries; i++) {
159*1fb62fb0SOlivier Houchard 			if (target->values[i] == value) {
160*1fb62fb0SOlivier Houchard 				target->values[i] = target->values[--array->n_entries];
161*1fb62fb0SOlivier Houchard 				return true;
162*1fb62fb0SOlivier Houchard 			}
163*1fb62fb0SOlivier Houchard 		}
164*1fb62fb0SOlivier Houchard 
165*1fb62fb0SOlivier Houchard 		return false;
166*1fb62fb0SOlivier Houchard 	}
167*1fb62fb0SOlivier Houchard 
168*1fb62fb0SOlivier Houchard 	target = array->active;
169*1fb62fb0SOlivier Houchard 
170*1fb62fb0SOlivier Houchard 	for (i = 0; i < array->n_entries; i++) {
171*1fb62fb0SOlivier Houchard 		if (target->values[i] == value)
172*1fb62fb0SOlivier Houchard 			break;
173*1fb62fb0SOlivier Houchard 	}
174*1fb62fb0SOlivier Houchard 
175*1fb62fb0SOlivier Houchard 	if (i == array->n_entries)
176*1fb62fb0SOlivier Houchard 		return false;
177*1fb62fb0SOlivier Houchard 
178*1fb62fb0SOlivier Houchard 	/* If there are pending additions, immediately eliminate the operation. */
179*1fb62fb0SOlivier Houchard 	if (target->n_committed != array->n_entries) {
180*1fb62fb0SOlivier Houchard 		ck_pr_store_ptr(&target->values[i], target->values[--array->n_entries]);
181*1fb62fb0SOlivier Houchard 		return true;
182*1fb62fb0SOlivier Houchard 	}
183*1fb62fb0SOlivier Houchard 
184*1fb62fb0SOlivier Houchard 	/*
185*1fb62fb0SOlivier Houchard 	 * The assumption is that these allocations are small to begin with.
186*1fb62fb0SOlivier Houchard 	 * If there is no immediate opportunity for transaction, allocate a
187*1fb62fb0SOlivier Houchard 	 * transactional array which will be applied upon commit time.
188*1fb62fb0SOlivier Houchard 	 */
189*1fb62fb0SOlivier Houchard 	target = ck_array_create(array->allocator, array->n_entries);
190*1fb62fb0SOlivier Houchard 	if (target == NULL)
191*1fb62fb0SOlivier Houchard 		return false;
192*1fb62fb0SOlivier Houchard 
193*1fb62fb0SOlivier Houchard 	memcpy(target->values, array->active->values, sizeof(void *) * array->n_entries);
194*1fb62fb0SOlivier Houchard 	target->length = array->n_entries;
195*1fb62fb0SOlivier Houchard 	target->n_committed = array->n_entries;
196*1fb62fb0SOlivier Houchard 	target->values[i] = target->values[--array->n_entries];
197*1fb62fb0SOlivier Houchard 
198*1fb62fb0SOlivier Houchard 	array->transaction = target;
199*1fb62fb0SOlivier Houchard 	return true;
200*1fb62fb0SOlivier Houchard }
201*1fb62fb0SOlivier Houchard 
202*1fb62fb0SOlivier Houchard bool
ck_array_commit(ck_array_t * array)203*1fb62fb0SOlivier Houchard ck_array_commit(ck_array_t *array)
204*1fb62fb0SOlivier Houchard {
205*1fb62fb0SOlivier Houchard 	struct _ck_array *m = array->transaction;
206*1fb62fb0SOlivier Houchard 
207*1fb62fb0SOlivier Houchard 	if (m != NULL) {
208*1fb62fb0SOlivier Houchard 		struct _ck_array *p;
209*1fb62fb0SOlivier Houchard 
210*1fb62fb0SOlivier Houchard 		m->n_committed = array->n_entries;
211*1fb62fb0SOlivier Houchard 		ck_pr_fence_store();
212*1fb62fb0SOlivier Houchard 		p = array->active;
213*1fb62fb0SOlivier Houchard 		ck_pr_store_ptr(&array->active, m);
214*1fb62fb0SOlivier Houchard 		array->allocator->free(p, sizeof(struct _ck_array) +
215*1fb62fb0SOlivier Houchard 		    p->length * sizeof(void *), true);
216*1fb62fb0SOlivier Houchard 		array->transaction = NULL;
217*1fb62fb0SOlivier Houchard 
218*1fb62fb0SOlivier Houchard 		return true;
219*1fb62fb0SOlivier Houchard 	}
220*1fb62fb0SOlivier Houchard 
221*1fb62fb0SOlivier Houchard 	ck_pr_fence_store();
222*1fb62fb0SOlivier Houchard 	ck_pr_store_uint(&array->active->n_committed, array->n_entries);
223*1fb62fb0SOlivier Houchard 	return true;
224*1fb62fb0SOlivier Houchard }
225*1fb62fb0SOlivier Houchard 
226*1fb62fb0SOlivier Houchard void
ck_array_deinit(struct ck_array * array,bool defer)227*1fb62fb0SOlivier Houchard ck_array_deinit(struct ck_array *array, bool defer)
228*1fb62fb0SOlivier Houchard {
229*1fb62fb0SOlivier Houchard 
230*1fb62fb0SOlivier Houchard 	array->allocator->free(array->active,
231*1fb62fb0SOlivier Houchard 	    sizeof(struct _ck_array) + sizeof(void *) * array->active->length, defer);
232*1fb62fb0SOlivier Houchard 
233*1fb62fb0SOlivier Houchard 	if (array->transaction != NULL) {
234*1fb62fb0SOlivier Houchard 		array->allocator->free(array->transaction,
235*1fb62fb0SOlivier Houchard 		    sizeof(struct _ck_array) + sizeof(void *) * array->transaction->length, defer);
236*1fb62fb0SOlivier Houchard 	}
237*1fb62fb0SOlivier Houchard 
238*1fb62fb0SOlivier Houchard 	array->transaction = array->active = NULL;
239*1fb62fb0SOlivier Houchard 	return;
240*1fb62fb0SOlivier Houchard }
241