]>
Commit | Line | Data |
---|---|---|
fe59694b JR |
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 | #ifndef OBJECT_COLLECTION_H | |
18 | #define OBJECT_COLLECTION_H 1 | |
19 | ||
20 | #include <limits.h> | |
21 | #include <stdlib.h> | |
22 | ||
23 | /* A set of object pointers. */ | |
24 | struct object_collection { | |
25 | void **objs; /* Objects. */ | |
26 | size_t n; /* Number of objects collected. */ | |
27 | size_t capacity; /* Number of objects that fit in 'objs'. */ | |
28 | void *stub[5]; /* Preallocated array to avoid malloc(). */ | |
29 | }; | |
30 | ||
31 | void object_collection_init(struct object_collection *); | |
32 | void object_collection_add(struct object_collection *, void *); | |
33 | void object_collection_remove(struct object_collection *, void *); | |
34 | void object_collection_move(struct object_collection *to, | |
35 | struct object_collection *from); | |
36 | void *object_collection_detach(struct object_collection *); | |
37 | void object_collection_destroy(struct object_collection *); | |
38 | ||
39 | /* Macro for declaring type-safe pointer collections. 'TYPE' is the pointer | |
40 | * type which are collected, 'NAME' is the name for the type to be used in the | |
41 | * function names. */ | |
42 | ||
43 | #define DECL_OBJECT_COLLECTION(TYPE, NAME) \ | |
44 | struct NAME##_collection { \ | |
45 | struct object_collection collection; \ | |
46 | }; \ | |
47 | \ | |
48 | static inline void NAME##_collection_init(struct NAME##_collection *coll) \ | |
49 | { \ | |
50 | object_collection_init(&coll->collection); \ | |
51 | } \ | |
52 | \ | |
53 | static inline void NAME##_collection_add(struct NAME##_collection *coll, \ | |
54 | TYPE obj) \ | |
55 | { \ | |
56 | object_collection_add(&coll->collection, obj); \ | |
57 | } \ | |
58 | \ | |
59 | static inline void NAME##_collection_remove(struct NAME##_collection *coll, \ | |
60 | TYPE obj) \ | |
61 | { \ | |
62 | object_collection_remove(&coll->collection, obj); \ | |
63 | } \ | |
64 | \ | |
65 | static inline void NAME##_collection_move(struct NAME##_collection *to, \ | |
66 | struct NAME##_collection *from) \ | |
67 | { \ | |
68 | object_collection_move(&to->collection, &from->collection); \ | |
69 | } \ | |
70 | \ | |
71 | static inline void NAME##_collection_destroy(struct NAME##_collection *coll) \ | |
72 | { \ | |
73 | object_collection_destroy(&coll->collection); \ | |
74 | } \ | |
75 | \ | |
76 | static inline TYPE* NAME##_collection_##NAME##s(const struct NAME##_collection *coll) \ | |
77 | { \ | |
78 | return (TYPE*)coll->collection.objs; \ | |
79 | } \ | |
80 | \ | |
fe59694b JR |
81 | static inline size_t NAME##_collection_n(const struct NAME##_collection *coll) \ |
82 | { \ | |
83 | return coll->collection.n; \ | |
84 | } \ | |
85 | \ | |
86 | static inline TYPE* NAME##_collection_detach(struct NAME##_collection *coll) \ | |
87 | { \ | |
88 | return (TYPE*)object_collection_detach(&coll->collection); \ | |
89 | } | |
90 | ||
91 | #endif /* object-collection.h */ |