datapath-windows: set the nlBuf tail properly
[cascardo/ovs.git] / ofproto / ofproto-dpif-rid.c
1 /*
2  * Copyright (c) 2014 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 "id-pool.h"
20 #include "ovs-thread.h"
21 #include "ofproto-dpif-rid.h"
22
23 struct recirc_id_pool {
24     struct ovs_mutex lock;
25     struct id_pool *rids;
26 };
27
28 #define RECIRC_ID_BASE  300
29 #define RECIRC_ID_N_IDS  1024
30
31 struct recirc_id_pool *
32 recirc_id_pool_create(void)
33 {
34     struct recirc_id_pool *pool;
35
36     pool = xmalloc(sizeof *pool);
37     pool->rids = id_pool_create(RECIRC_ID_BASE, RECIRC_ID_N_IDS);
38     ovs_mutex_init(&pool->lock);
39
40     return pool;
41 }
42
43 void
44 recirc_id_pool_destroy(struct recirc_id_pool *pool)
45 {
46     id_pool_destroy(pool->rids);
47     ovs_mutex_destroy(&pool->lock);
48     free(pool);
49 }
50
51 uint32_t
52 recirc_id_alloc(struct recirc_id_pool *pool)
53 {
54     uint32_t id;
55     bool ret;
56
57     ovs_mutex_lock(&pool->lock);
58     ret = id_pool_alloc_id(pool->rids, &id);
59     ovs_mutex_unlock(&pool->lock);
60
61     if (!ret) {
62         return 0;
63     }
64
65     return id;
66 }
67
68 void
69 recirc_id_free(struct recirc_id_pool *pool, uint32_t id)
70 {
71     ovs_mutex_lock(&pool->lock);
72     id_pool_free_id(pool->rids, id);
73     ovs_mutex_unlock(&pool->lock);
74 }