]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/object-collection.h
2 * Copyright (c) 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef OBJECT_COLLECTION_H
18 #define OBJECT_COLLECTION_H 1
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(). */
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
*);
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
43 #define DECL_OBJECT_COLLECTION(TYPE, NAME) \
44 struct NAME##_collection { \
45 struct object_collection collection; \
48 static inline void NAME##_collection_init(struct NAME##_collection *coll) \
50 object_collection_init(&coll->collection); \
53 static inline void NAME##_collection_add(struct NAME##_collection *coll, \
56 object_collection_add(&coll->collection, obj); \
59 static inline void NAME##_collection_remove(struct NAME##_collection *coll, \
62 object_collection_remove(&coll->collection, obj); \
65 static inline void NAME##_collection_move(struct NAME##_collection *to, \
66 struct NAME##_collection *from) \
68 object_collection_move(&to->collection, &from->collection); \
71 static inline void NAME##_collection_destroy(struct NAME##_collection *coll) \
73 object_collection_destroy(&coll->collection); \
76 static inline TYPE* NAME##_collection_##NAME##s(const struct NAME##_collection *coll) \
78 return (TYPE*)coll->collection.objs; \
81 static inline size_t NAME##_collection_n(const struct NAME##_collection *coll) \
83 return coll->collection.n; \
86 static inline TYPE* NAME##_collection_detach(struct NAME##_collection *coll) \
88 return (TYPE*)object_collection_detach(&coll->collection); \
91 #endif /* object-collection.h */