1 #include "lfds610_stack_internal.h"
7 /****************************************************************************/
8 int lfds610_stack_new( struct lfds610_stack_state **ss, lfds610_atom_t number_elements )
14 // TRD : number_elements can be any value in its range
16 *ss = (struct lfds610_stack_state *) lfds610_liblfds_aligned_malloc( sizeof(struct lfds610_stack_state), LFDS610_ALIGN_DOUBLE_POINTER );
20 // TRD : the size of the lfds610_freelist is the size of the lfds610_stack
21 lfds610_freelist_new( &(*ss)->fs, number_elements, lfds610_stack_internal_freelist_init_function, NULL );
23 if( (*ss)->fs == NULL )
25 lfds610_liblfds_aligned_free( *ss );
29 if( (*ss)->fs != NULL )
31 (*ss)->top[LFDS610_STACK_POINTER] = NULL;
32 (*ss)->top[LFDS610_STACK_COUNTER] = 0;
33 (*ss)->aba_counter = 0;
38 LFDS610_BARRIER_STORE;
47 /****************************************************************************/
48 #pragma warning( disable : 4100 )
50 void lfds610_stack_use( struct lfds610_stack_state *ss )
59 #pragma warning( default : 4100 )
65 /****************************************************************************/
66 #pragma warning( disable : 4100 )
68 int lfds610_stack_internal_freelist_init_function( void **user_data, void *user_state )
73 assert( user_data != NULL );
74 assert( user_state == NULL );
76 *user_data = lfds610_liblfds_aligned_malloc( sizeof(struct lfds610_stack_element), LFDS610_ALIGN_DOUBLE_POINTER );
78 if( *user_data != NULL )
84 #pragma warning( default : 4100 )
90 /****************************************************************************/
91 void lfds610_stack_internal_new_element_from_freelist( struct lfds610_stack_state *ss, struct lfds610_stack_element *se[LFDS610_STACK_PAC_SIZE], void *user_data )
93 struct lfds610_freelist_element
98 // TRD : user_data can be any value in its range
100 lfds610_freelist_pop( ss->fs, &fe );
103 se[LFDS610_STACK_POINTER] = NULL;
106 lfds610_stack_internal_init_element( ss, se, fe, user_data );
115 /****************************************************************************/
116 void lfds610_stack_internal_new_element( struct lfds610_stack_state *ss, struct lfds610_stack_element *se[LFDS610_STACK_PAC_SIZE], void *user_data )
118 struct lfds610_freelist_element
121 assert( ss != NULL );
122 assert( se != NULL );
123 // TRD : user_data can be any value in its range
125 lfds610_freelist_guaranteed_pop( ss->fs, &fe );
128 se[LFDS610_STACK_POINTER] = NULL;
131 lfds610_stack_internal_init_element( ss, se, fe, user_data );
140 /****************************************************************************/
141 void lfds610_stack_internal_init_element( struct lfds610_stack_state *ss, struct lfds610_stack_element *se[LFDS610_STACK_PAC_SIZE], struct lfds610_freelist_element *fe, void *user_data )
143 assert( ss != NULL );
144 assert( se != NULL );
145 assert( fe != NULL );
146 // TRD : user_data can be any value in its range
148 lfds610_freelist_get_user_data_from_element( fe, (void **) &se[LFDS610_STACK_POINTER] );
150 se[LFDS610_STACK_COUNTER] = (struct lfds610_stack_element *) lfds610_abstraction_increment( (lfds610_atom_t *) &ss->aba_counter );
152 se[LFDS610_STACK_POINTER]->next[LFDS610_STACK_POINTER] = NULL;
153 se[LFDS610_STACK_POINTER]->next[LFDS610_STACK_COUNTER] = 0;
154 se[LFDS610_STACK_POINTER]->fe = fe;
155 se[LFDS610_STACK_POINTER]->user_data = user_data;