3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ==================================================================================
21 Mnemonic: ring_static.c
22 Abstract: Implements a ring of information (probably to act as a
24 Author: E. Scott Daniels
28 #ifndef _ring_static_c
29 #define _ring_static_c
39 #define RING_FAST 1 // when set we skip nil pointer checks on the ring pointer
44 static void* uta_mk_ring( int size ) {
48 if( size <= 0 || (r = (ring_t *) malloc( sizeof( *r ) )) == NULL ) {
52 r->head = r->tail = 0;
59 r->nelements = size; // because we always have an empty element when full
60 if( (r->data = (void **) malloc( sizeof( void** ) * (r->nelements + 1) )) == NULL ) {
65 memset( r->data, 0, sizeof( void** ) * r->nelements );
70 Ditch the ring. The caller is responsible for extracting any remaining
71 pointers and freeing them as needed.
73 static void uta_ring_free( void* vr ) {
76 if( (r = (ring_t*) vr) == NULL ) {
85 Pull the next data pointer from the ring; null if there isn't
86 anything to be pulled.
88 static inline void* uta_ring_extract( void* vr ) {
90 uint16_t ti; // real index in data
92 if( !RING_FAST ) { // compiler should drop the conditional when always false
93 if( (r = (ring_t*) vr) == NULL ) {
100 if( r->tail == r->head ) { // empty ring
106 if( r->tail >= r->nelements ) {
114 Insert the pointer at the next open space in the ring.
115 Returns 1 if the inert was ok, and 0 if the ring is full.
117 static inline int uta_ring_insert( void* vr, void* new_data ) {
120 if( !RING_FAST ) { // compiler should drop the conditional when always false
121 if( (r = (ring_t*) vr) == NULL ) {
128 if( r->head+1 == r->tail || (r->head+1 >= r->nelements && !r->tail) ) { // ring is full
132 r->data[r->head] = new_data;
134 if( r->head >= r->nelements ) {