]> git.proxmox.com Git - mirror_ovs.git/blob - lib/object-collection.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / object-collection.c
1 /*
2 * Copyright (c) 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "object-collection.h"
20 #include "util.h"
21
22 void
23 object_collection_init(struct object_collection *coll)
24 {
25 coll->objs = coll->stub;
26 coll->n = 0;
27 coll->capacity = ARRAY_SIZE(coll->stub);
28 }
29
30 void
31 object_collection_add(struct object_collection *coll, void *obj)
32 {
33 if (coll->n >= coll->capacity) {
34 size_t old_size, new_size;
35
36 old_size = coll->capacity * sizeof *coll->objs;
37 coll->capacity *= 2;
38 new_size = coll->capacity * sizeof *coll->objs;
39
40 if (coll->objs == coll->stub) {
41 coll->objs = xmalloc(new_size);
42 memcpy(coll->objs, coll->stub, old_size);
43 } else {
44 coll->objs = xrealloc(coll->objs, new_size);
45 }
46 }
47
48 coll->objs[coll->n++] = obj;
49 }
50
51 void
52 object_collection_remove(struct object_collection *coll, void *obj)
53 {
54 size_t i;
55
56 for (i = 0; i < coll->n; i++) {
57 if (coll->objs[i] == obj) {
58 break;
59 }
60 }
61 if (i == coll->n) {
62 return;
63 }
64
65 coll->n--;
66 /* Swap the last item in if needed. */
67 if (i != coll->n) {
68 coll->objs[i] = coll->objs[coll->n];
69 }
70
71 /* Shrink? Watermark at '/ 4' to get hysteresis and leave spare
72 * capacity. */
73 if (coll->objs != coll->stub && coll->n <= coll->capacity / 4) {
74 size_t actual_size, new_size;
75
76 actual_size = coll->n * sizeof *coll->objs;
77 coll->capacity /= 2;
78 new_size = coll->capacity * sizeof *coll->objs;
79
80 if (new_size <= sizeof(coll->stub)) {
81 memcpy(coll->stub, coll->objs, actual_size);
82 free(coll->objs);
83 coll->objs = coll->stub;
84 } else {
85 coll->objs = xrealloc(coll->objs, new_size);
86 }
87 }
88 }
89
90 void
91 object_collection_move(struct object_collection *to,
92 struct object_collection *from)
93 {
94 ovs_assert(to->n == 0);
95
96 *to = *from;
97 if (from->objs == from->stub) {
98 to->objs = to->stub;
99 }
100 object_collection_init(from);
101 }
102
103 /* Returns a NULL-terminated array of object pointers,
104 * destroys 'rules'. */
105 void *
106 object_collection_detach(struct object_collection *coll)
107 {
108 void **array;
109
110 object_collection_add(coll, NULL);
111
112 if (coll->objs == coll->stub) {
113 coll->objs = xmemdup(coll->objs, coll->n * sizeof *coll->objs);
114 }
115
116 array = coll->objs;
117 object_collection_init(coll);
118
119 return array;
120 }
121
122 void
123 object_collection_destroy(struct object_collection *coll)
124 {
125 if (coll->objs != coll->stub) {
126 free(coll->objs);
127 }
128
129 /* Make repeated destruction harmless. */
130 object_collection_init(coll);
131 }