1 #include "lfds600_freelist_internal.h"
7 /****************************************************************************/
8 void lfds600_freelist_query( struct lfds600_freelist_state *fs, enum lfds600_freelist_query_type query_type, void *query_input, void *query_output )
11 // TRD : query type can be any value in its range
12 // TRD : query_input can be NULL in some cases
13 assert( query_output != NULL );
17 case LFDS600_FREELIST_QUERY_ELEMENT_COUNT:
18 assert( query_input == NULL );
20 *(lfds600_atom_t *) query_output = fs->element_count;
23 case LFDS600_FREELIST_QUERY_VALIDATE:
24 // TRD : query_input can be NULL
26 lfds600_freelist_internal_validate( fs, (struct lfds600_validation_info *) query_input, (enum data_structure_validity *) query_output );
37 /****************************************************************************/
38 void lfds600_freelist_internal_validate( struct lfds600_freelist_state *fs, struct lfds600_validation_info *vi, enum data_structure_validity *lfds600_freelist_validity )
40 struct lfds600_freelist_element
49 // TRD : vi can be NULL
50 assert( lfds600_freelist_validity != NULL );
52 *lfds600_freelist_validity = VALIDITY_VALID;
54 fe_slow = fe_fast = (struct lfds600_freelist_element *) fs->top[LFDS600_FREELIST_POINTER];
56 /* TRD : first, check for a loop
58 both of which start at the top of the lfds600_freelist
61 we advance one pointer by one element
64 we exit the loop when both pointers are NULL
65 (have reached the end of the lfds600_freelist)
69 if we fast pointer 'sees' the slow pointer
70 which means we have a loop
76 fe_slow = fe_slow->next[LFDS600_FREELIST_POINTER];
79 fe_fast = fe_fast->next[LFDS600_FREELIST_POINTER];
82 fe_fast = fe_fast->next[LFDS600_FREELIST_POINTER];
84 while( fe_slow != NULL and fe_fast != fe_slow );
86 if( fe_fast != NULL and fe_slow != NULL and fe_fast == fe_slow )
87 *lfds600_freelist_validity = VALIDITY_INVALID_LOOP;
89 /* TRD : now check for expected number of elements
90 vi can be NULL, in which case we do not check
91 we know we don't have a loop from our earlier check
94 if( *lfds600_freelist_validity == VALIDITY_VALID and vi != NULL )
96 fe = (struct lfds600_freelist_element *) fs->top[LFDS600_FREELIST_POINTER];
101 fe = (struct lfds600_freelist_element *) fe->next[LFDS600_FREELIST_POINTER];
104 if( element_count < vi->min_elements )
105 *lfds600_freelist_validity = VALIDITY_INVALID_MISSING_ELEMENTS;
107 if( element_count > vi->max_elements )
108 *lfds600_freelist_validity = VALIDITY_INVALID_ADDITIONAL_ELEMENTS;