EM-ODP  3.7.0
Event Machine on ODP
objpool.c
1 /*
2  * Copyright (c) 2015, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
32 #include <objpool.h>
33 #include <list.h>
34 
35 static inline objpool_elem_t *
36 objpool_node2elem(list_node_t *const list_node);
37 
38 int objpool_init(objpool_t *const objpool, uint32_t nbr_subpools)
39 {
40  if (nbr_subpools > OBJSUBPOOLS_MAX)
41  nbr_subpools = OBJSUBPOOLS_MAX;
42 
43  objpool->nbr_subpools = nbr_subpools;
44 
45  for (uint32_t i = 0; i < nbr_subpools; i++) {
46  objsubpool_t *const subpool = &objpool->subpool[i];
47 
48  env_spinlock_init(&subpool->lock);
49  list_init(&subpool->list_head);
50  }
51 
52  return 0;
53 }
54 
55 void objpool_add(objpool_t *const objpool, uint32_t subpool_idx,
56  objpool_elem_t *const elem)
57 {
58  const uint32_t idx = subpool_idx % objpool->nbr_subpools;
59  objsubpool_t *const subpool = &objpool->subpool[idx];
60 
61  elem->subpool_idx = idx;
62 
63  env_spinlock_lock(&subpool->lock);
64  list_add(&subpool->list_head, &elem->list_node);
65  elem->in_pool = 1; /* true */
66  env_spinlock_unlock(&subpool->lock);
67 }
68 
70 objpool_rem(objpool_t *const objpool, uint32_t subpool_idx)
71 {
72  objpool_elem_t *elem = NULL;
73 
74  for (uint32_t i = 0; i < objpool->nbr_subpools; i++) {
75  const uint32_t idx = (subpool_idx + i) % objpool->nbr_subpools;
76  objsubpool_t *const subpool = &objpool->subpool[idx];
77 
78  env_spinlock_lock(&subpool->lock);
79 
80  list_node_t *const node = list_rem_first(&subpool->list_head);
81 
82  if (node != NULL) {
83  elem = objpool_node2elem(node);
84  elem->in_pool = 0; /* false */
85  }
86 
87  env_spinlock_unlock(&subpool->lock);
88 
89  if (node != NULL)
90  return elem;
91  }
92 
93  return NULL;
94 }
95 
96 int objpool_rem_elem(objpool_t *const objpool, objpool_elem_t *const elem)
97 {
98  const uint32_t idx = elem->subpool_idx;
99  objsubpool_t *const subpool = &objpool->subpool[idx];
100  int ret = -1;
101 
102  env_spinlock_lock(&subpool->lock);
103  if (elem->in_pool) {
104  list_rem(&subpool->list_head, &elem->list_node);
105  elem->in_pool = 0; /* false */
106  ret = 0;
107  }
108  env_spinlock_unlock(&subpool->lock);
109 
110  return ret;
111 }
112 
113 static inline objpool_elem_t *
114 objpool_node2elem(list_node_t *const list_node)
115 {
116  return (objpool_elem_t *)((uintptr_t)list_node - offsetof(objpool_elem_t, list_node));
117 }
objpool.h
list_node_t
Definition: list.h:42
objpool_t
Definition: objpool.h:64
objpool_elem_t
Definition: objpool.h:48
environment.h
objsubpool_t
Definition: objpool.h:54