2 #include "libshared_memory_internal.h"
4 /***** private prototypes *****/
5 static void *alloc_from_memory_element( struct libshared_memory_element *me, lfds710_pal_uint_t size_in_bytes, lfds710_pal_uint_t alignment_in_bytes );
11 /****************************************************************************/
12 void *libshared_memory_alloc_from_unknown_node( struct libshared_memory_state *ms, lfds710_pal_uint_t size_in_bytes, lfds710_pal_uint_t alignment_in_bytes )
14 struct lfds710_list_asu_element
17 struct libshared_memory_element
23 LFDS710_PAL_ASSERT( ms != NULL );
24 // TRD : size_in_bytes can be any value in its range
25 // TRD : alignment_in_bytes can be any value in its range
27 while( allocation == NULL and LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
29 me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
31 if( me->known_numa_node_flag == LOWERED )
32 allocation = alloc_from_memory_element( me, size_in_bytes, alignment_in_bytes );
42 /****************************************************************************/
43 void *libshared_memory_alloc_largest_possible_array_from_unknown_node( struct libshared_memory_state *ms, lfds710_pal_uint_t element_size_in_bytes, lfds710_pal_uint_t alignment_in_bytes, lfds710_pal_uint_t *number_elements )
48 struct lfds710_list_asu_element
51 struct libshared_memory_element
58 LFDS710_PAL_ASSERT( ms != NULL );
59 // TRD : element_size_in_bytes can be any value in its range
60 // TRD : alignment_in_bytes can be any value in its range
61 LFDS710_PAL_ASSERT( number_elements != NULL );
63 /* TRD : find the largest unknown-node memory element
67 // TRD : find the correct memory element - in this case, the one with most free space
68 while( LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
70 temp_me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
72 if( temp_me->known_numa_node_flag == LOWERED )
73 if( me == NULL or temp_me->current_memory_size_in_bytes > me->current_memory_size_in_bytes )
77 alignment_bump = (lfds710_pal_uint_t) me->current_pointer % alignment_in_bytes;
79 if( alignment_bump != 0 )
80 alignment_bump = alignment_in_bytes - alignment_bump;
82 *number_elements = (me->current_memory_size_in_bytes - alignment_bump) / element_size_in_bytes;
84 allocation = alloc_from_memory_element( me, *number_elements * element_size_in_bytes, alignment_in_bytes );
93 /****************************************************************************/
94 void *libshared_memory_alloc_from_specific_node( struct libshared_memory_state *ms, lfds710_pal_uint_t numa_node_id, lfds710_pal_uint_t size_in_bytes, lfds710_pal_uint_t alignment_in_bytes )
96 struct lfds710_list_asu_element
99 struct libshared_memory_element
105 LFDS710_PAL_ASSERT( ms != NULL );
106 // TRD : numa_node_id can be any value in its range
107 // TRD : size_in_bytes can be any value in its range
108 // TRD : alignment_in_bytes can be any value in its range
110 while( allocation == NULL and LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
112 me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
114 if( me->known_numa_node_flag == RAISED and me->numa_node_id == numa_node_id )
115 allocation = alloc_from_memory_element( me, size_in_bytes, alignment_in_bytes );
125 /****************************************************************************/
126 void *libshared_memory_alloc_from_most_free_space_node( struct libshared_memory_state *ms, lfds710_pal_uint_t size_in_bytes, lfds710_pal_uint_t alignment_in_bytes )
128 struct lfds710_list_asu_element
131 struct libshared_memory_element
138 LFDS710_PAL_ASSERT( ms != NULL );
139 // TRD : size_in_bytes can be any value in its range
140 // TRD : alignment_in_bytes can be any value in its range
142 // TRD : find the correct memory element - in this case, the one with most free space
143 while( LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
145 temp_me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
147 if( me == NULL or temp_me->current_memory_size_in_bytes > me->current_memory_size_in_bytes )
151 allocation = alloc_from_memory_element( me, size_in_bytes, alignment_in_bytes );
160 /****************************************************************************/
161 static void *alloc_from_memory_element( struct libshared_memory_element *me, lfds710_pal_uint_t size_in_bytes, lfds710_pal_uint_t alignment_in_bytes )
170 LFDS710_PAL_ASSERT( me != NULL );
171 // TRD : size_in_bytes can be any value in its range
172 // TRD : alignment_in_bytes can be any value in its range
174 alignment_bump = (lfds710_pal_uint_t) me->current_pointer % alignment_in_bytes;
176 if( alignment_bump != 0 )
177 alignment_bump = alignment_in_bytes - alignment_bump;
179 total_size_in_bytes = size_in_bytes + alignment_bump;
181 if( total_size_in_bytes > me->current_memory_size_in_bytes )
184 me->current_pointer += alignment_bump;
186 allocation = me->current_pointer;
188 me->current_pointer += size_in_bytes;
190 me->current_memory_size_in_bytes -= total_size_in_bytes;