1 #include "lfds600_queue_internal.h"
7 /****************************************************************************/
8 int lfds600_queue_new( struct lfds600_queue_state **qs, lfds600_atom_t number_elements )
13 struct lfds600_queue_element
14 *qe[LFDS600_QUEUE_PAC_SIZE];
17 // TRD : number_elements can be any value in its range
19 *qs = (struct lfds600_queue_state *) lfds600_abstraction_aligned_malloc( sizeof(struct lfds600_queue_state), LFDS600_ALIGN_DOUBLE_POINTER );
23 // TRD : the size of the lfds600_freelist is the size of the lfds600_queue (+1 for the leading dummy element, which is hidden from the caller)
24 lfds600_freelist_new( &(*qs)->fs, number_elements+1, lfds600_queue_internal_freelist_init_function, NULL );
26 if( (*qs)->fs != NULL )
28 lfds600_queue_internal_new_element_from_freelist( *qs, qe, NULL );
29 (*qs)->enqueue[LFDS600_QUEUE_POINTER] = (*qs)->dequeue[LFDS600_QUEUE_POINTER] = qe[LFDS600_QUEUE_POINTER];
30 (*qs)->aba_counter = 0;
34 if( (*qs)->fs == NULL )
36 lfds600_abstraction_aligned_free( *qs );
48 /****************************************************************************/
49 #pragma warning( disable : 4100 )
51 int lfds600_queue_internal_freelist_init_function( void **user_data, void *user_state )
56 assert( user_data != NULL );
57 assert( user_state == NULL );
59 *user_data = lfds600_abstraction_aligned_malloc( sizeof(struct lfds600_queue_element), LFDS600_ALIGN_DOUBLE_POINTER );
61 if( *user_data != NULL )
67 #pragma warning( default : 4100 )
73 /****************************************************************************/
74 void lfds600_queue_internal_new_element_from_freelist( struct lfds600_queue_state *qs, struct lfds600_queue_element *qe[LFDS600_QUEUE_PAC_SIZE], void *user_data )
76 struct lfds600_freelist_element
81 // TRD : user_data can be any value in its range
83 qe[LFDS600_QUEUE_POINTER] = NULL;
85 lfds600_freelist_pop( qs->fs, &fe );
88 lfds600_queue_internal_init_element( qs, qe, fe, user_data );
97 /****************************************************************************/
98 void lfds600_queue_internal_guaranteed_new_element_from_freelist( struct lfds600_queue_state *qs, struct lfds600_queue_element *qe[LFDS600_QUEUE_PAC_SIZE], void *user_data )
100 struct lfds600_freelist_element
103 assert( qs != NULL );
104 assert( qe != NULL );
105 // TRD : user_data can be any value in its range
107 qe[LFDS600_QUEUE_POINTER] = NULL;
109 lfds600_freelist_guaranteed_pop( qs->fs, &fe );
112 lfds600_queue_internal_init_element( qs, qe, fe, user_data );
121 /****************************************************************************/
122 void lfds600_queue_internal_init_element( struct lfds600_queue_state *qs, struct lfds600_queue_element *qe[LFDS600_QUEUE_PAC_SIZE], struct lfds600_freelist_element *fe, void *user_data )
124 assert( qs != NULL );
125 assert( qe != NULL );
126 assert( fe != NULL );
127 // TRD : user_data can be any value in its range
129 lfds600_freelist_get_user_data_from_element( fe, (void **) &qe[LFDS600_QUEUE_POINTER] );
130 qe[LFDS600_QUEUE_COUNTER] = (struct lfds600_queue_element *) lfds600_abstraction_increment( (lfds600_atom_t *) &qs->aba_counter );
132 qe[LFDS600_QUEUE_POINTER]->next[LFDS600_QUEUE_POINTER] = NULL;
133 qe[LFDS600_QUEUE_POINTER]->next[LFDS600_QUEUE_COUNTER] = (struct lfds600_queue_element *) lfds600_abstraction_increment( (lfds600_atom_t *) &qs->aba_counter );
135 qe[LFDS600_QUEUE_POINTER]->fe = fe;
136 qe[LFDS600_QUEUE_POINTER]->user_data = user_data;