| 170 | } |
| 171 | |
| 172 | int |
| 173 | rte_ring_init(struct rte_ring *r, const char *name, unsigned int count, |
| 174 | unsigned int flags) |
| 175 | { |
| 176 | int ret; |
| 177 | |
| 178 | /* compilation-time checks */ |
| 179 | RTE_BUILD_BUG_ON((sizeof(struct rte_ring) & |
| 180 | RTE_CACHE_LINE_MASK) != 0); |
| 181 | RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) & |
| 182 | RTE_CACHE_LINE_MASK) != 0); |
| 183 | RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) & |
| 184 | RTE_CACHE_LINE_MASK) != 0); |
| 185 | |
| 186 | RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) != |
| 187 | offsetof(struct rte_ring_hts_headtail, sync_type)); |
| 188 | RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) != |
| 189 | offsetof(struct rte_ring_hts_headtail, ht.pos.tail)); |
| 190 | |
| 191 | RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) != |
| 192 | offsetof(struct rte_ring_rts_headtail, sync_type)); |
| 193 | RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) != |
| 194 | offsetof(struct rte_ring_rts_headtail, tail.val.pos)); |
| 195 | |
| 196 | /* future proof flags, only allow supported values */ |
| 197 | if (flags & ~RING_F_MASK) { |
| 198 | RTE_LOG(ERR, RING, |
| 199 | "Unsupported flags requested %#x\n", flags); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | /* init the ring structure */ |
| 204 | memset(r, 0, sizeof(*r)); |
| 205 | ret = strlcpy(r->name, name, sizeof(r->name)); |
| 206 | if (ret < 0 || ret >= (int)sizeof(r->name)) |
| 207 | return -ENAMETOOLONG; |
| 208 | r->flags = flags; |
| 209 | ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type); |
| 210 | if (ret != 0) |
| 211 | return ret; |
| 212 | |
| 213 | if (flags & RING_F_EXACT_SZ) { |
| 214 | r->size = rte_align32pow2(count + 1); |
| 215 | r->mask = r->size - 1; |
| 216 | r->capacity = count; |
| 217 | } else { |
| 218 | if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) { |
| 219 | RTE_LOG(ERR, RING, |
| 220 | "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n", |
| 221 | RTE_RING_SZ_MASK); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | r->size = count; |
| 225 | r->mask = count - 1; |
| 226 | r->capacity = r->mask; |
| 227 | } |
| 228 | |
| 229 | /* set default values for head-tail distance */ |