2 #include "lfds700_queue_internal.h"
4 /***** private prototypes *****/
5 static void lfds700_queue_internal_validate( struct lfds700_queue_state *qs, struct lfds700_misc_validation_info *vi, enum lfds700_misc_validity *lfds700_queue_validity );
11 /****************************************************************************/
12 void lfds700_queue_query( struct lfds700_queue_state *qs, enum lfds700_queue_query query_type, void *query_input, void *query_output )
14 struct lfds700_queue_element
17 LFDS700_MISC_BARRIER_LOAD;
19 LFDS700_PAL_ASSERT( qs != NULL );
20 // TRD : query_type can be any value in its range
24 case LFDS700_QUEUE_QUERY_SINGLETHREADED_GET_COUNT:
25 LFDS700_PAL_ASSERT( query_input == NULL );
26 LFDS700_PAL_ASSERT( query_output != NULL );
28 *(lfds700_pal_uint_t *) query_output = 0;
30 qe = (struct lfds700_queue_element *) qs->dequeue[POINTER];
34 ( *(lfds700_pal_uint_t *) query_output )++;
35 qe = (struct lfds700_queue_element *) qe->next[POINTER];
38 // TRD : remember there is a dummy element in the queue
39 ( *(lfds700_pal_uint_t *) query_output )--;
42 case LFDS700_QUEUE_QUERY_SINGLETHREADED_VALIDATE:
43 // TRD : query_input can be NULL
44 LFDS700_PAL_ASSERT( query_output != NULL );
46 lfds700_queue_internal_validate( qs, (struct lfds700_misc_validation_info *) query_input, (enum lfds700_misc_validity *) query_output );
57 /****************************************************************************/
58 static void lfds700_queue_internal_validate( struct lfds700_queue_state *qs, struct lfds700_misc_validation_info *vi, enum lfds700_misc_validity *lfds700_queue_validity )
63 struct lfds700_queue_element
67 LFDS700_PAL_ASSERT( qs != NULL );
68 // TRD : vi can be NULL
69 LFDS700_PAL_ASSERT( lfds700_queue_validity != NULL );
71 *lfds700_queue_validity = LFDS700_MISC_VALIDITY_VALID;
73 qe_slow = qe_fast = (struct lfds700_queue_element *) qs->dequeue[POINTER];
75 /* TRD : first, check for a loop
77 both of which start at the dequeue end of the queue
80 we advance one pointer by one element
83 we exit the loop when both pointers are NULL
84 (have reached the end of the queue)
88 if we fast pointer 'sees' the slow pointer
89 which means we have a loop
95 qe_slow = qe_slow->next[POINTER];
98 qe_fast = qe_fast->next[POINTER];
100 if( qe_fast != NULL )
101 qe_fast = qe_fast->next[POINTER];
103 while( qe_slow != NULL and qe_fast != qe_slow );
105 if( qe_fast != NULL and qe_slow != NULL and qe_fast == qe_slow )
106 *lfds700_queue_validity = LFDS700_MISC_VALIDITY_INVALID_LOOP;
108 /* TRD : now check for expected number of elements
109 vi can be NULL, in which case we do not check
110 we know we don't have a loop from our earlier check
113 if( *lfds700_queue_validity == LFDS700_MISC_VALIDITY_VALID and vi != NULL )
115 lfds700_queue_query( qs, LFDS700_QUEUE_QUERY_SINGLETHREADED_GET_COUNT, NULL, (void *) &number_elements );
117 if( number_elements < vi->min_elements )
118 *lfds700_queue_validity = LFDS700_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
120 if( number_elements > vi->max_elements )
121 *lfds700_queue_validity = LFDS700_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;