1 | //////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : malloc.c |
---|
3 | // Date : 05/03/2013 |
---|
4 | // Author : Jean-Baptiste Bréjon / alain greiner |
---|
5 | // Copyright (c) UPMC-LIP6 |
---|
6 | //////////////////////////////////////////////////////////////////////////////// |
---|
7 | |
---|
8 | #include "malloc.h" |
---|
9 | #include "stdio.h" |
---|
10 | |
---|
11 | // Global variables defining the heap descriptors array (one heap per cluster) |
---|
12 | giet_heap_t heap[X_SIZE][Y_SIZE]; |
---|
13 | |
---|
14 | // Macro returning the smallest power of 2 larger or equal to size value |
---|
15 | #define GET_SIZE_INDEX(size) (size <= 0x00000001) ? 0 :\ |
---|
16 | (size <= 0x00000002) ? 1 :\ |
---|
17 | (size <= 0x00000004) ? 2 :\ |
---|
18 | (size <= 0x00000008) ? 3 :\ |
---|
19 | (size <= 0x00000010) ? 4 :\ |
---|
20 | (size <= 0x00000020) ? 5 :\ |
---|
21 | (size <= 0x00000040) ? 6 :\ |
---|
22 | (size <= 0x00000080) ? 7 :\ |
---|
23 | (size <= 0x00000100) ? 8 :\ |
---|
24 | (size <= 0x00000200) ? 9 :\ |
---|
25 | (size <= 0x00000400) ? 10 :\ |
---|
26 | (size <= 0x00000800) ? 11 :\ |
---|
27 | (size <= 0x00001000) ? 12 :\ |
---|
28 | (size <= 0x00002000) ? 13 :\ |
---|
29 | (size <= 0x00004000) ? 14 :\ |
---|
30 | (size <= 0x00008000) ? 15 :\ |
---|
31 | (size <= 0x00010000) ? 16 :\ |
---|
32 | (size <= 0x00020000) ? 17 :\ |
---|
33 | (size <= 0x00040000) ? 18 :\ |
---|
34 | (size <= 0x00080000) ? 19 :\ |
---|
35 | (size <= 0x00100000) ? 20 :\ |
---|
36 | (size <= 0x00200000) ? 21 :\ |
---|
37 | (size <= 0x00400000) ? 22 :\ |
---|
38 | (size <= 0x00800000) ? 23 :\ |
---|
39 | (size <= 0x01000000) ? 24 :\ |
---|
40 | (size <= 0x02000000) ? 25 :\ |
---|
41 | (size <= 0x04000000) ? 26 :\ |
---|
42 | (size <= 0x08000000) ? 27 :\ |
---|
43 | (size <= 0x10000000) ? 28 :\ |
---|
44 | (size <= 0x20000000) ? 29 :\ |
---|
45 | (size <= 0x40000000) ? 30 :\ |
---|
46 | (size <= 0x80000000) ? 31 :\ |
---|
47 | 32 |
---|
48 | //////////////////////////////////////// |
---|
49 | void display_free_array( unsigned int x, |
---|
50 | unsigned int y ) |
---|
51 | { |
---|
52 | giet_shr_printf( |
---|
53 | " - coordinates = [%d][%d]\n" |
---|
54 | " - heap_base = %x\n" |
---|
55 | " - heap_size = %x\n" |
---|
56 | " - alloc_base = %x\n" |
---|
57 | " - alloc_size = %x\n" |
---|
58 | " - free[0] = %x\n" |
---|
59 | " - free[1] = %x\n" |
---|
60 | " - free[2] = %x\n" |
---|
61 | " - free[3] = %x\n" |
---|
62 | " - free[4] = %x\n" |
---|
63 | " - free[5] = %x\n" |
---|
64 | " - free[6] = %x\n" |
---|
65 | " - free[7] = %x\n" |
---|
66 | " - free[8] = %x\n" |
---|
67 | " - free[9] = %x\n" |
---|
68 | " - free[10] = %x\n" |
---|
69 | " - free[11] = %x\n" |
---|
70 | " - free[12] = %x\n" |
---|
71 | " - free[13] = %x\n" |
---|
72 | " - free[14] = %x\n" |
---|
73 | " - free[15] = %x\n" |
---|
74 | " - free[16] = %x\n" |
---|
75 | " - free[17] = %x\n" |
---|
76 | " - free[18] = %x\n" |
---|
77 | " - free[19] = %x\n" |
---|
78 | " - free[20] = %x\n" |
---|
79 | " - free[21] = %x\n" |
---|
80 | " - free[22] = %x\n" |
---|
81 | " - free[23] = %x\n", |
---|
82 | heap[x][y].x, heap[x][y].y, |
---|
83 | heap[x][y].heap_base, heap[x][y].heap_size, |
---|
84 | heap[x][y].alloc_base, heap[x][y].alloc_size, |
---|
85 | heap[x][y].free[0], heap[x][y].free[1], |
---|
86 | heap[x][y].free[2], heap[x][y].free[3], |
---|
87 | heap[x][y].free[4], heap[x][y].free[5], |
---|
88 | heap[x][y].free[6], heap[x][y].free[7], |
---|
89 | heap[x][y].free[8], heap[x][y].free[9], |
---|
90 | heap[x][y].free[10], heap[x][y].free[11], |
---|
91 | heap[x][y].free[12], heap[x][y].free[13], |
---|
92 | heap[x][y].free[14], heap[x][y].free[15], |
---|
93 | heap[x][y].free[16], heap[x][y].free[17], |
---|
94 | heap[x][y].free[18], heap[x][y].free[19], |
---|
95 | heap[x][y].free[20], heap[x][y].free[21], |
---|
96 | heap[x][y].free[22], heap[x][y].free[23] ); |
---|
97 | } // end display_free array() |
---|
98 | |
---|
99 | //////////////////////////////// |
---|
100 | void heap_init( unsigned int x, |
---|
101 | unsigned int y ) |
---|
102 | { |
---|
103 | unsigned int heap_base; // heap segment base |
---|
104 | unsigned int heap_size; // heap segment size |
---|
105 | unsigned int heap_index; // size_index in free[array] |
---|
106 | |
---|
107 | unsigned int alloc_base; // alloc[] array base |
---|
108 | unsigned int alloc_size; // alloc[] array size |
---|
109 | unsigned int alloc_index; // size_index in free[array] |
---|
110 | |
---|
111 | unsigned int index; // iterator |
---|
112 | |
---|
113 | // get heap_base, heap size, and heap index |
---|
114 | giet_heap_info( &heap_base, &heap_size, x, y ); |
---|
115 | heap_index = GET_SIZE_INDEX( heap_size ); |
---|
116 | |
---|
117 | // checking heap segment constraints |
---|
118 | if ( heap_size == 0 ) // heap segment exist |
---|
119 | { |
---|
120 | giet_exit("ERROR in malloc() : heap not found \n"); |
---|
121 | } |
---|
122 | if ( heap_size != (1<<heap_index) ) // heap size power of 2 |
---|
123 | { |
---|
124 | giet_exit("ERROR in malloc() : heap size must be power of 2\n"); |
---|
125 | } |
---|
126 | if ( heap_base % heap_size ) // heap segment aligned |
---|
127 | { |
---|
128 | giet_exit("ERROR in malloc() : heap segment must be aligned\n"); |
---|
129 | } |
---|
130 | |
---|
131 | // compute size of block containin alloc[] array |
---|
132 | alloc_size = heap_size / MIN_BLOCK_SIZE; |
---|
133 | if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE; |
---|
134 | |
---|
135 | // get index for the corresponding block |
---|
136 | alloc_index = GET_SIZE_INDEX( alloc_size ); |
---|
137 | |
---|
138 | // compute alloc[] array base address |
---|
139 | alloc_base = heap_base + heap_size - alloc_size; |
---|
140 | |
---|
141 | // reset the free[] array |
---|
142 | for ( index = 0 ; index < 32 ; index++ ) |
---|
143 | { |
---|
144 | heap[x][y].free[index] = 0; |
---|
145 | } |
---|
146 | |
---|
147 | // split the heap into various sizes blocks, |
---|
148 | // initializes the free[] array and NEXT pointers |
---|
149 | // base is the block base address |
---|
150 | unsigned int base = heap_base; |
---|
151 | unsigned int* ptr; |
---|
152 | for ( index = heap_index-1 ; index >= alloc_index ; index-- ) |
---|
153 | { |
---|
154 | heap[x][y].free[index] = base; |
---|
155 | ptr = (unsigned int*)base; |
---|
156 | *ptr = 0; |
---|
157 | base = base + (1<<index); |
---|
158 | } |
---|
159 | |
---|
160 | heap[x][y].init = HEAP_INITIALIZED; |
---|
161 | heap[x][y].x = x; |
---|
162 | heap[x][y].y = y; |
---|
163 | heap[x][y].heap_base = heap_base; |
---|
164 | heap[x][y].heap_size = heap_size; |
---|
165 | heap[x][y].alloc_size = alloc_size; |
---|
166 | heap[x][y].alloc_base = alloc_base; |
---|
167 | |
---|
168 | lock_release( &heap[x][y].lock ); |
---|
169 | |
---|
170 | #if GIET_DEBUG_MALLOC |
---|
171 | giet_shr_printf("\n[MALLOC DEBUG] Completing Heap[%d][%d] initialisation\n", x, y ); |
---|
172 | display_free_array(x,y); |
---|
173 | #endif |
---|
174 | |
---|
175 | } // end heap_init() |
---|
176 | |
---|
177 | //////////////////////////////////////////// |
---|
178 | unsigned int split_block( giet_heap_t* heap, |
---|
179 | unsigned int vaddr, |
---|
180 | unsigned int searched_index, |
---|
181 | unsigned int requested_index ) |
---|
182 | { |
---|
183 | // push the upper half block into free[searched_index-1] |
---|
184 | unsigned int* new = (unsigned int*)(vaddr + (1<<(searched_index-1))); |
---|
185 | *new = heap->free[searched_index-1]; |
---|
186 | heap->free[searched_index-1] = (unsigned int)new; |
---|
187 | |
---|
188 | if ( searched_index == requested_index + 1 ) // terminal case: return lower half block |
---|
189 | { |
---|
190 | return vaddr; |
---|
191 | } |
---|
192 | else // non terminal case : lower half block must be split again |
---|
193 | { |
---|
194 | return split_block( heap, vaddr, searched_index-1, requested_index ); |
---|
195 | } |
---|
196 | } // end split_block() |
---|
197 | |
---|
198 | ////////////////////////////////////////// |
---|
199 | unsigned int get_block( giet_heap_t* heap, |
---|
200 | unsigned int searched_index, |
---|
201 | unsigned int requested_index ) |
---|
202 | { |
---|
203 | // test terminal case |
---|
204 | if ( (1<<searched_index) > heap->heap_size ) // failure : return a NULL value |
---|
205 | { |
---|
206 | return 0; |
---|
207 | } |
---|
208 | else // search a block in free[searched_index] |
---|
209 | { |
---|
210 | unsigned int vaddr = heap->free[searched_index]; |
---|
211 | if ( vaddr == 0 ) // block not found : search in free[searched_index+1] |
---|
212 | { |
---|
213 | return get_block( heap, searched_index+1, requested_index ); |
---|
214 | } |
---|
215 | else // block found : pop it from free[searched_index] |
---|
216 | { |
---|
217 | // pop the block from free[searched_index] |
---|
218 | unsigned int next = *((unsigned int*)vaddr); |
---|
219 | heap->free[searched_index] = next; |
---|
220 | |
---|
221 | // test if the block must be split |
---|
222 | if ( searched_index == requested_index ) // no split required |
---|
223 | { |
---|
224 | return vaddr; |
---|
225 | } |
---|
226 | else // split is required |
---|
227 | { |
---|
228 | return split_block( heap, vaddr, searched_index, requested_index ); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | } // end get_block() |
---|
233 | |
---|
234 | //////////////////////////////////////// |
---|
235 | void * remote_malloc( unsigned int size, |
---|
236 | unsigned int x, |
---|
237 | unsigned int y ) |
---|
238 | { |
---|
239 | // checking arguments |
---|
240 | if (size == 0) |
---|
241 | { |
---|
242 | giet_exit("ERROR in malloc() : requested size = 0 \n"); |
---|
243 | } |
---|
244 | if ( x >= X_SIZE ) |
---|
245 | { |
---|
246 | giet_exit("ERROR in malloc() : x coordinate too large\n"); |
---|
247 | } |
---|
248 | if ( y >= Y_SIZE ) |
---|
249 | { |
---|
250 | giet_exit("ERROR in malloc() : y coordinate too large\n"); |
---|
251 | } |
---|
252 | |
---|
253 | // initializes the heap if first access |
---|
254 | if ( heap[x][y].init != HEAP_INITIALIZED ) |
---|
255 | { |
---|
256 | heap_init( x, y ); |
---|
257 | } |
---|
258 | |
---|
259 | // normalize size |
---|
260 | if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE; |
---|
261 | |
---|
262 | // compute requested_index for the free[] array |
---|
263 | unsigned int requested_index = GET_SIZE_INDEX( size ); |
---|
264 | |
---|
265 | // take the lock protecting access to heap[x][y] |
---|
266 | lock_acquire( &heap[x][y].lock ); |
---|
267 | |
---|
268 | // call the recursive function get_block |
---|
269 | unsigned int base = get_block( &heap[x][y], |
---|
270 | requested_index, |
---|
271 | requested_index ); |
---|
272 | |
---|
273 | // update the alloc[] array if block found |
---|
274 | if ( base != 0 ) |
---|
275 | { |
---|
276 | unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE; |
---|
277 | unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset); |
---|
278 | *ptr = requested_index; |
---|
279 | } |
---|
280 | |
---|
281 | // release the lock |
---|
282 | lock_release( &heap[x][y].lock ); |
---|
283 | |
---|
284 | #if GIET_DEBUG_MALLOC |
---|
285 | giet_shr_printf("\n[MALLOC DEBUG] Malloc for Heap[%d][%d] / size = %x / base = %x\n", |
---|
286 | x, y, size, base ); |
---|
287 | display_free_array(x,y); |
---|
288 | #endif |
---|
289 | |
---|
290 | return (void*)base; |
---|
291 | |
---|
292 | } // end remote_malloc() |
---|
293 | |
---|
294 | |
---|
295 | ////////////////////////////////// |
---|
296 | void * malloc( unsigned int size ) |
---|
297 | { |
---|
298 | unsigned int proc_id = giet_procid(); |
---|
299 | unsigned int cluster_xy = proc_id / NB_PROCS_MAX; |
---|
300 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
301 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
302 | |
---|
303 | return remote_malloc( size, x, y ); |
---|
304 | } |
---|
305 | |
---|
306 | /////////////////////////////////////////// |
---|
307 | void update_free_array( giet_heap_t* heap, |
---|
308 | unsigned int base, |
---|
309 | unsigned int size_index ) |
---|
310 | { |
---|
311 | // This recursive function try to merge the released block |
---|
312 | // with the companion block if this companion block is free. |
---|
313 | // This companion has the same size, and almost the same address |
---|
314 | // (only one address bit is different) |
---|
315 | // - If the companion is not in free[size_index], |
---|
316 | // the released block is pushed in free[size_index]. |
---|
317 | // - If the companion is found, it is evicted from free[size_index] |
---|
318 | // and the merged bloc is pushed in the free[size_index+1]. |
---|
319 | |
---|
320 | |
---|
321 | // compute released block size |
---|
322 | unsigned int size = 1<<size_index; |
---|
323 | |
---|
324 | // compute companion_base and merged_base |
---|
325 | unsigned int companion_base; // companion block base address |
---|
326 | unsigned int merged_base; // merged block base address |
---|
327 | if ( base % (size<<1) ) |
---|
328 | { |
---|
329 | companion_base = base + size; |
---|
330 | merged_base = base; |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | companion_base = base - size; |
---|
335 | merged_base = base - size; |
---|
336 | } |
---|
337 | |
---|
338 | // scan all blocks in free[size_index] |
---|
339 | // the iter & prev variables are actually addresses |
---|
340 | unsigned int found = 0; |
---|
341 | unsigned int iter = heap->free[size_index]; |
---|
342 | unsigned int prev = (unsigned int)&heap->free[size_index]; |
---|
343 | while ( iter != 0 ) |
---|
344 | { |
---|
345 | if ( iter == companion_base ) |
---|
346 | { |
---|
347 | found = 1; |
---|
348 | break; |
---|
349 | } |
---|
350 | iter = *(unsigned int*)iter; |
---|
351 | prev = iter; |
---|
352 | } |
---|
353 | |
---|
354 | if ( found == 0 ) // Companion not found |
---|
355 | { |
---|
356 | // push the block in free[size_index] |
---|
357 | *(unsigned int*)base = heap->free[size_index]; |
---|
358 | heap->free[size_index] = base; |
---|
359 | } |
---|
360 | else // Companion found : merge |
---|
361 | { |
---|
362 | // evict the searched block from free[size_index] |
---|
363 | *(unsigned int*)prev = *(unsigned int*)iter; |
---|
364 | |
---|
365 | // call the update_free() function for free[size_index+1] |
---|
366 | update_free_array( heap, merged_base , size_index+1 ); |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | ////////////////////// |
---|
371 | void free( void* ptr ) |
---|
372 | { |
---|
373 | // get the cluster coordinate from ptr value |
---|
374 | unsigned int x; |
---|
375 | unsigned int y; |
---|
376 | giet_get_xy( ptr, &x, &y ); |
---|
377 | |
---|
378 | // get the lock protecting heap[x][y] |
---|
379 | lock_acquire( &heap[x][y].lock ); |
---|
380 | |
---|
381 | // check ptr value |
---|
382 | unsigned int base = (unsigned int)ptr; |
---|
383 | if ( (base < heap[x][y].heap_base) || |
---|
384 | (base >= (heap[x][y].heap_base + heap[x][y].heap_size)) ) |
---|
385 | { |
---|
386 | giet_exit("ERROR in free() : illegal pointer for released block"); |
---|
387 | } |
---|
388 | |
---|
389 | // compute released block index in alloc[] array |
---|
390 | unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE; |
---|
391 | |
---|
392 | // get the released block size_index |
---|
393 | unsigned char* pchar = (unsigned char*)(heap[x][y].alloc_base + index); |
---|
394 | unsigned int size_index = (unsigned int)*pchar; |
---|
395 | |
---|
396 | // check released block alignment |
---|
397 | if ( base % (1 << size_index) ) |
---|
398 | { |
---|
399 | giet_exit("ERROR in free() : released block not aligned"); |
---|
400 | } |
---|
401 | |
---|
402 | // call the recursive function update_free_array() |
---|
403 | update_free_array( &heap[x][y], base, size_index ); |
---|
404 | |
---|
405 | // release the lock |
---|
406 | lock_release( &heap[x][y].lock ); |
---|
407 | |
---|
408 | #if GIET_DEBUG_MALLOC |
---|
409 | giet_shr_printf("\n[MALLOC DEBUG] Free for Heap[%d][%d] / base = %x / size = %x\n", |
---|
410 | x, y, base, 1<<size_index ); |
---|
411 | display_free_array(x,y); |
---|
412 | #endif |
---|
413 | |
---|
414 | } // end free() |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | // Local Variables: |
---|
420 | // tab-width: 4 |
---|
421 | // c-basic-offset: 4 |
---|
422 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
423 | // indent-tabs-mode: nil |
---|
424 | // End: |
---|
425 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
426 | |
---|
427 | |
---|
428 | |
---|