Branch data Line data Source code
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 : 1592710 : object_collection_init(struct object_collection *coll)
24 : : {
25 : 1592710 : coll->objs = coll->stub;
26 : 1592710 : coll->n = 0;
27 : 1592710 : coll->capacity = ARRAY_SIZE(coll->stub);
28 : 1592710 : }
29 : :
30 : : void
31 : 78612 : object_collection_add(struct object_collection *coll, void *obj)
32 : : {
33 [ + + ]: 78612 : if (coll->n >= coll->capacity) {
34 : : size_t old_size, new_size;
35 : :
36 : 1351 : old_size = coll->capacity * sizeof *coll->objs;
37 : 1351 : coll->capacity *= 2;
38 : 1351 : new_size = coll->capacity * sizeof *coll->objs;
39 : :
40 [ + + ]: 1351 : if (coll->objs == coll->stub) {
41 : 696 : coll->objs = xmalloc(new_size);
42 : 696 : memcpy(coll->objs, coll->stub, old_size);
43 : : } else {
44 : 655 : coll->objs = xrealloc(coll->objs, new_size);
45 : : }
46 : : }
47 : :
48 : 78612 : coll->objs[coll->n++] = obj;
49 : 78612 : }
50 : :
51 : : void
52 : 23 : object_collection_remove(struct object_collection *coll, void *obj)
53 : : {
54 : : size_t i;
55 : :
56 [ + - ]: 23 : for (i = 0; i < coll->n; i++) {
57 [ + - ]: 23 : if (coll->objs[i] == obj) {
58 : 23 : break;
59 : : }
60 : : }
61 [ - + ]: 23 : if (i == coll->n) {
62 : 0 : return;
63 : : }
64 : :
65 : 23 : coll->n--;
66 : : /* Swap the last item in if needed. */
67 [ + + ]: 23 : if (i != coll->n) {
68 : 1 : coll->objs[i] = coll->objs[coll->n];
69 : : }
70 : :
71 : : /* Shrink? Watermark at '/ 4' to get hysteresis and leave spare
72 : : * capacity. */
73 [ - + ][ # # ]: 23 : if (coll->objs != coll->stub && coll->n <= coll->capacity / 4) {
74 : : size_t actual_size, new_size;
75 : :
76 : 0 : actual_size = coll->n * sizeof *coll->objs;
77 : 0 : coll->capacity /= 2;
78 : 0 : new_size = coll->capacity * sizeof *coll->objs;
79 : :
80 [ # # ]: 0 : if (new_size <= sizeof(coll->stub)) {
81 : 0 : memcpy(coll->stub, coll->objs, actual_size);
82 : 0 : free(coll->objs);
83 : 0 : coll->objs = coll->stub;
84 : : } else {
85 : 0 : coll->objs = xrealloc(coll->objs, new_size);
86 : : }
87 : : }
88 : : }
89 : :
90 : : void
91 : 31 : object_collection_move(struct object_collection *to,
92 : : struct object_collection *from)
93 : : {
94 [ - + ]: 31 : ovs_assert(to->n == 0);
95 : :
96 : 31 : *to = *from;
97 [ + - ]: 31 : if (from->objs == from->stub) {
98 : 31 : to->objs = to->stub;
99 : : }
100 : 31 : object_collection_init(from);
101 : 31 : }
102 : :
103 : : /* Returns a NULL-terminated array of object pointers,
104 : : * destroys 'rules'. */
105 : : void *
106 : 1252 : object_collection_detach(struct object_collection *coll)
107 : : {
108 : : void **array;
109 : :
110 : 1252 : object_collection_add(coll, NULL);
111 : :
112 [ + + ]: 1252 : if (coll->objs == coll->stub) {
113 : 628 : coll->objs = xmemdup(coll->objs, coll->n * sizeof *coll->objs);
114 : : }
115 : :
116 : 1252 : array = coll->objs;
117 : 1252 : object_collection_init(coll);
118 : :
119 : 1252 : return array;
120 : : }
121 : :
122 : : void
123 : 77462 : object_collection_destroy(struct object_collection *coll)
124 : : {
125 [ + + ]: 77462 : if (coll->objs != coll->stub) {
126 : 72 : free(coll->objs);
127 : : }
128 : :
129 : : /* Make repeated destruction harmless. */
130 : 77462 : object_collection_init(coll);
131 : 77462 : }
|