]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libshared/src/libshared_memory/libshared_memory_alloc.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / test_and_benchmark / libshared / src / libshared_memory / libshared_memory_alloc.c
1 /***** includes *****/
2 #include "libshared_memory_internal.h"
3
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 );
6
7
8
9
10
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 )
13 {
14   struct lfds710_list_asu_element
15     *lasue = NULL;
16
17   struct libshared_memory_element
18     *me;
19
20   void
21     *allocation = NULL;
22
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
26
27   while( allocation == NULL and LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
28   {
29     me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
30
31     if( me->known_numa_node_flag == LOWERED )
32       allocation = alloc_from_memory_element( me, size_in_bytes, alignment_in_bytes );
33   }
34
35   return allocation;
36 }
37
38
39
40
41
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 )
44 {
45   lfds710_pal_uint_t
46     alignment_bump;
47
48   struct lfds710_list_asu_element
49     *lasue = NULL;
50
51   struct libshared_memory_element
52     *me = NULL,
53     *temp_me;
54
55   void
56     *allocation;
57
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 );
62
63   /* TRD : find the largest unknown-node memory element
64            then alloc from that
65   */
66
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) )
69   {
70     temp_me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
71
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 )
74         me = temp_me;
75   }
76
77   alignment_bump = (lfds710_pal_uint_t) me->current_pointer % alignment_in_bytes;
78
79   if( alignment_bump != 0 )
80     alignment_bump = alignment_in_bytes - alignment_bump;
81
82   *number_elements = (me->current_memory_size_in_bytes - alignment_bump) / element_size_in_bytes;
83
84   allocation = alloc_from_memory_element( me, *number_elements * element_size_in_bytes, alignment_in_bytes );
85
86   return allocation;
87 }
88
89
90
91
92
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 )
95 {
96   struct lfds710_list_asu_element
97     *lasue = NULL;
98
99   struct libshared_memory_element
100     *me;
101
102   void
103     *allocation = NULL;
104
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
109
110   while( allocation == NULL and LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(ms->list_of_allocations,lasue) )
111   {
112     me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
113
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 );
116   }
117
118   return allocation;
119 }
120
121
122
123
124
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 )
127 {
128   struct lfds710_list_asu_element
129     *lasue = NULL;
130
131   struct libshared_memory_element
132     *me = NULL,
133     *temp_me;
134
135   void
136     *allocation;
137
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
141
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) )
144   {
145     temp_me = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
146
147     if( me == NULL or temp_me->current_memory_size_in_bytes > me->current_memory_size_in_bytes )
148       me = temp_me;
149   }
150
151   allocation = alloc_from_memory_element( me, size_in_bytes, alignment_in_bytes );
152
153   return allocation;
154 }
155
156
157
158
159
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 )
162 {
163   lfds710_pal_uint_t
164     alignment_bump,
165     total_size_in_bytes;
166
167   void
168     *allocation;
169
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
173
174   alignment_bump = (lfds710_pal_uint_t) me->current_pointer % alignment_in_bytes;
175
176   if( alignment_bump != 0 )
177     alignment_bump = alignment_in_bytes - alignment_bump;
178
179   total_size_in_bytes = size_in_bytes + alignment_bump;
180
181   if( total_size_in_bytes > me->current_memory_size_in_bytes )
182     return NULL;
183
184   me->current_pointer += alignment_bump;
185
186   allocation = me->current_pointer;
187
188   me->current_pointer += size_in_bytes;
189
190   me->current_memory_size_in_bytes -= total_size_in_bytes;
191
192   return allocation;
193 }
194