Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First step to return value on storage model.
[simgrid.git] / win32_test_app / src / TThreadDynarray.c
1
2 #include <TThreadDynarray.h>
3
4 /*
5  * Constructs a ThreadDynarray with the specified capacity.
6  */
7 ThreadDynarray_t ThreadDynarray_new(unsigned long capacity)
8 {
9   ThreadDynarray_t ptr = calloc(1, sizeof(s_ThreadDynarray_t));
10
11   ptr->count = 0;
12   ptr->capacity = capacity;
13
14   memset(&(ptr->cs), 0, sizeof(CRITICAL_SECTION));
15   InitializeCriticalSection(&(ptr->cs));
16   ptr->is_locked = false;
17
18   if (capacity)
19     ptr->threads =
20         (ThreadEntry_t) calloc(capacity, sizeof(s_ThreadEntry_t));
21   else
22     ptr->threads = NULL;
23
24   return ptr;
25 }
26
27 /* 
28  * Destroy the ThreadDynarray 
29  */
30 void ThreadDynarray_destroy(ThreadDynarray_t ptr)
31 {
32   ThreadDynarray_clear(ptr);
33   DeleteCriticalSection(&(ptr->cs));
34   free(ptr);
35   ptr = NULL;
36 }
37
38 /*
39  * Returns an const pointer to entry pointed to by index.
40  */
41 ThreadEntry_t const ThreadDynarray_at(ThreadDynarray_t ptr,
42                                       unsigned long index)
43 {
44   ThreadEntry_t __entry;
45   ThreadDynarray_lock(ptr);
46   __entry = &(ptr->threads)[index];
47   ThreadDynarray_unlock(ptr);
48   return __entry;
49 }
50
51 /*
52  * Fill the content of the entry addressed by __entry with the content
53  * of the entry pointed to by index.
54  */
55 void ThreadDynarray_get(ThreadDynarray_t ptr, unsigned long index,
56                         ThreadEntry_t const __entry)
57 {
58   ThreadDynarray_lock(ptr);
59   ::memcpy(__entry, ThreadDynarray_at(ptr, index),
60            sizeof(s_ThreadEntry_t));
61   ThreadDynarray_unlock(ptr);
62 }
63
64 /* 
65  * Fill the content of the entry pointed to by index with the content of
66  * the entry addressed by __entry.
67  */
68 void ThreadDynarray_set(ThreadDynarray_t ptr, unsigned long index,
69                         ThreadEntry_t const __entry)
70 {
71
72   ThreadDynarray_lock(ptr);
73   memcpy(ThreadDynarray_at(ptr, index), __entry, sizeof(s_ThreadEntry_t));
74   ThreadDynarray_unlock(ptr);
75 }
76
77 /*
78  * Returns a const pointer to the first entry.
79  */
80 ThreadEntry_t const ThreadDynarray_getFront(ThreadDynarray_t ptr)
81 {
82   ThreadEntry_t __entry;
83   ThreadDynarray_lock(ptr);
84   __entry = ThreadDynarray_at(ptr, 0);
85   ThreadDynarray_unlock(ptr);
86   return __entry;
87 }
88
89 /*
90  * Returns a const pointer to the last entry.
91  */
92 ThreadEntry_t const ThreadDynarray_getBack(ThreadDynarray_t ptr)
93 {
94   ThreadEntry_t __entry;
95   ThreadDynarray_lock(ptr);
96   __entry = ThreadDynarray_at(ptr, ptr->count - 1);;
97   ThreadDynarray_unlock(ptr);
98   return __entry;
99 }
100
101 /*
102  * Inserts a copy of __entry at the front
103  */
104 void ThreadDynarray_pushFront(ThreadDynarray_t ptr,
105                               ThreadEntry_t const __entry)
106 {
107   ThreadDynarray_lock(ptr);
108
109   if (!ThreadDynarray_getCapacityAvailable(ptr))
110     ThreadDynarray_resize(ptr);
111
112   ptr->count++;
113   ThreadDynarray_move(ptr, 1, ThreadDynarray_getLowerBound(ptr),
114                       ThreadDynarray_getUpperBound(ptr));
115   ThreadDynarray_set(ptr, ThreadDynarray_getLowerBound(ptr), __entry);
116
117   ThreadDynarray_unlock(ptr);
118 }
119
120 /*
121  * Appends a copy of __entry to the end.
122  */
123 void ThreadDynarray_pushBack(ThreadDynarray_t ptr,
124                              ThreadEntry_t const __entry)
125 {
126   ThreadDynarray_lock(ptr);
127
128   if (!ThreadDynarray_getCapacityAvailable(ptr))
129     ThreadDynarray_resize(ptr);
130
131   ptr->count++;
132   ThreadDynarray_set(ptr, ThreadDynarray_getUpperBound(ptr), __entry);
133
134   ThreadDynarray_unlock(ptr);
135 }
136
137
138 /* 
139  * Inserts __entry at the position pointed to by index.
140  */
141 void ThreadDynarray_insert(ThreadDynarray_t ptr, unsigned long index,
142                            ThreadEntry_t const __entry)
143 {
144   ThreadDynarray_lock(ptr);
145
146   if (!ThreadDynarray_getCapacityAvailable(ptr))
147     ThreadDynarray_resize(ptr);
148
149   ThreadDynarray_move(ptr, index + 1, index, ptr->count - index);
150   ptr->count++;
151   ThreadDynarray_set(ptr, index, __entry);
152
153   ThreadDynarray_unlock(ptr);
154 }
155
156 /*
157  * Deletes the entry pointed to by index. If __entry is not NULL the
158  * fuction saves the entry threads at this address before.
159  */
160 void ThreadDynarray_erase(ThreadDynarray_t ptr, unsigned long index,
161                           ThreadEntry_t const __entry)
162 {
163
164   ThreadDynarray_lock(ptr);
165
166   if (__entry)
167     ThreadDynarray_set(ptr, index, __entry);
168
169   if (index != ThreadDynarray_getUpperBound(ptr))
170     ThreadDynarray_move(ptr, index, index + 1, (ptr->count - (index + 1)));
171
172   ptr->count--;
173
174   ThreadDynarray_unlock(ptr);
175 }
176
177 /*
178  * Find the first entry with the same content of the entry addressed by
179  * __entry.The function returns the index of the founded entry, -1 if
180  * no entry is founded.
181  */
182 long ThreadDynarray_getIndex(ThreadDynarray_t ptr,
183                              ThreadEntry_t const __entry)
184 {
185
186   unsigned long i;
187   ThreadDynarray_lock(ptr);
188
189   for (i = 0; i < ptr->count; i++) {
190     if (ThreadDynarray_compare(ptr, i, __entry)) {
191       ThreadDynarray_unlock(ptr);
192       return i;
193     }
194   }
195
196   ThreadDynarray_unlock(ptr);
197   return -1;
198 }
199
200 /* 
201  * Returns true if the entry exist.
202  */
203 bool ThreadDynarray_exist(ThreadDynarray_t ptr,
204                           ThreadEntry_t const __entry)
205 {
206   bool exist;
207
208   ThreadDynarray_lock(ptr);
209   exist = (-1 != ThreadDynarray_getIndex(ptr, __entry));
210   ThreadDynarray_unlock(ptr);
211   return exist;
212 }
213
214 /* Deletes the first entry with the same content of the entry addressed
215  * by __entry.The function returns true if the entry is deleted, false
216  * if no entry is founded.
217  */
218 bool ThreadDynarray_remove(ThreadDynarray_t ptr,
219                            ThreadEntry_t const __entry)
220 {
221   /* assert(!empty(ptr)); */
222
223   long __index;
224   ThreadDynarray_lock(ptr);
225   __index = ThreadDynarray_getIndex(ptr, __entry);
226
227   if (__index == -1) {
228     ThreadDynarray_unlock(ptr);
229     return false;
230   }
231
232   ThreadDynarray_set(ptr, (unsigned long) __index, NULL);
233   ThreadDynarray_unlock(ptr);
234   return true;
235 }
236
237 /*
238  * Erase all elements of the self.
239  */
240 void ThreadDynarray_clear(ThreadDynarray_t ptr)
241 {
242   ThreadDynarray_lock(ptr);
243
244   free(ptr->threads);
245   ptr->threads = NULL;
246
247   ptr->count = 0;
248   ptr->capacity = 0;
249   ThreadDynarray_unlock(ptr);
250 }
251
252 /*
253  * Resets entry count to zero.
254  */
255 void ThreadDynarray_reset(ThreadDynarray_t ptr)
256 {
257   ThreadDynarray_lock(ptr);
258   ptr->count = 0;
259   ThreadDynarray_unlock(ptr);
260 }
261
262 /*
263  * Moves count elements from src index to dst index.
264  */
265 void ThreadDynarray_move(ThreadDynarray_t ptr, const unsigned long dst,
266                          const unsigned long src, unsigned long count)
267 {
268   ThreadDynarray_lock(ptr);
269
270   if (ptr->count)
271     memmove(ThreadDynarray_at(ptr, dst), ThreadDynarray_at(ptr, src),
272             count * sizeof(s_ThreadEntry_t));
273
274   ThreadDynarray_unlock(ptr);
275 }
276
277 /* Compare the content of the entry pointed to by index with the content of
278  * the entry addressed by __entry. The function returns true if the contents
279  * are same.
280  */
281 bool ThreadDynarray_compare(ThreadDynarray_t ptr,
282                             const unsigned long index,
283                             ThreadEntry_t const __entry)
284 {
285   bool are_equals;
286   ThreadDynarray_lock(ptr);
287   are_equals =
288       (!memcmp
289        (ThreadDynarray_at(ptr, index), __entry, sizeof(s_ThreadEntry_t)));
290   ThreadDynarray_unlock(ptr);
291   return are_equals;
292 }
293
294 /*
295  * Returns a reference to a new ThreadDynarray new set is a clone of the self.
296  */
297 ThreadDynarray_t ThreadDynarray_clone(ThreadDynarray_t ptr)
298 {
299   ThreadDynarray_t new_ptr;
300   ThreadDynarray_lock(ptr);
301   ptr = ThreadDynarray_new(ptr->capacity);
302
303   if (ptr->count) {
304     memcpy(new_ptr->threads, ptr->threads,
305            ptr->count * sizeof(s_ThreadEntry_t));
306     new_ptr->count = ThreadDynarray_getCount(ptr);
307   }
308   ThreadDynarray_unlock(ptr);
309   return new_ptr;
310 }
311
312 /*
313  * Extends the capacity when the container is full.
314  */
315 void ThreadDynarray_resize(ThreadDynarray_t ptr)
316 {
317   ThreadDynarray_lock(ptr);
318
319   ptr->capacity = (!ptr->capacity) ? 1 : (ptr->count << 1);
320   ptr->threads =
321       (ThreadEntry_t) realloc(ptr->threads,
322                               ptr->capacity * sizeof(s_ThreadEntry_t));
323
324   ThreadDynarray_unlock(ptr);
325 }
326
327
328 /*
329  * Returns the number of elements.
330  */
331 unsigned long ThreadDynarray_getCount(ThreadDynarray_t ptr)
332 {
333   unsigned count;
334   ThreadDynarray_lock(ptr);
335   count = ptr->count;
336   ThreadDynarray_unlock(ptr);
337   return count;
338 }
339
340 /*
341  * Returns the current storage capacity of the ThreadDynarray. This is guaranteed
342  * to be at least as large as count().
343  */
344 unsigned long ThreadDynarray_getCapacity(ThreadDynarray_t ptr)
345 {
346   unsigned capacity;
347   ThreadDynarray_lock(ptr);
348   capacity = ptr->capacity;
349   ThreadDynarray_unlock(ptr);
350   return capacity;
351 }
352
353
354 /*
355  * Returns upper bound of self (max index).
356  */
357 unsigned long ThreadDynarray_getUpperBound(ThreadDynarray_t ptr)
358 {
359   unsigned long upper_bound;
360   ThreadDynarray_lock(ptr);
361   upper_bound = (ptr->count - 1);
362   ThreadDynarray_unlock(ptr);
363   return upper_bound;
364 }
365
366 /*
367  * Returns lower bound of self (always zero).
368  */
369 unsigned long ThreadDynarray_getLowerBound(ThreadDynarray_t ptr)
370 {
371   return 0;
372 }
373
374 /*
375  * Returns the size of the elements.
376  */
377 unsigned long ThreadDynarray_getElementSize(ThreadDynarray_t ptr)
378 {
379   return sizeof(s_ThreadEntry_t);
380 }
381
382 /*
383  * Returns true if the size of self is zero.
384  */
385 bool ThreadDynarray_isEmpty(ThreadDynarray_t ptr)
386 {
387   bool is_empty;
388   ThreadDynarray_lock(ptr);
389   is_empty = (ptr->count == 0);
390   ThreadDynarray_unlock(ptr);
391   return is_empty;
392 }
393
394 /*
395  * Returns true if capacity available.
396  */
397 bool ThreadDynarray_getCapacityAvailable(ThreadDynarray_t ptr)
398 {
399   bool capacity_available;
400   ThreadDynarray_lock(ptr);
401   capacity_available = (ptr->capacity > ptr->count);
402   ThreadDynarray_unlock(ptr);
403   return capacity_available;
404 }
405
406 /*
407  * Returns true if the container is full.
408  */
409 bool ThreadDynarray_is_full(ThreadDynarray_t ptr)
410 {
411   bool is_full;
412   ThreadDynarray_lock(ptr);
413   is_full = (!ThreadDynarray_isEmpty(ptr)
414              && !ThreadDynarray_getCapacityAvailable(ptr));
415   ThreadDynarray_unlock(ptr);
416   return is_full;
417 }
418
419 /* 
420  * Assignement.
421  */
422 ThreadDynarray_t ThreadDynarray_assign(ThreadDynarray_t src,
423                                        ThreadDynarray_t dst)
424 {
425   ThreadDynarray_lock(src);
426   ThreadDynarray_lock(dst);
427
428   if (src != dst) {
429     ThreadDynarray_clear(dst);
430
431     if (src->count) {
432       dst->count = src->count;
433       dst->capacity = src->capacity;
434       dst->threads =
435           (ThreadEntry_t) malloc(src->capacity * sizeof(s_ThreadEntry_t));
436       memcpy(dst->threads, src->threads,
437              src->count * sizeof(s_ThreadEntry_t));
438     }
439   }
440   ThreadDynarray_unlock(src);
441   ThreadDynarray_unlock(dst);
442
443   return dst;
444 }
445
446 /* 
447  * Returns true if the dynamic arrays are equal.
448  */
449 bool ThreadDynarray_areEquals(ThreadDynarray_t ptr1, ThreadDynarray_t ptr2)
450 {
451   bool are_equals;
452
453   ThreadDynarray_lock(ptr1);
454   ThreadDynarray_lock(ptr2);
455
456   are_equals = (ptr1->count == ptr2->count &&
457                 ptr1->capacity == ptr2->capacity &&
458                 !memcmp(ptr2->threads, ptr1->threads, ptr1->capacity)
459       );
460
461   ThreadDynarray_unlock(ptr1);
462   ThreadDynarray_unlock(ptr2);
463
464   return are_equals;
465 }
466
467 /* 
468  * Returns true if the dynamic arrays are not equal.
469  */
470 ThreadDynarray_areNotEquals(ThreadDynarray_t ptr1, ThreadDynarray_t ptr2)
471 {
472   return !ThreadDynarray_areEquals(ptr1, ptr2);
473 }
474
475 void ThreadDynarray_lock(ThreadDynarray_t ptr)
476 {
477   if (!ptr->is_locked) {
478     EnterCriticalSection(&(ptr->cs));
479     ptr->is_locked = true;
480   }
481 }
482
483 void ThreadDynarray_unlock(ThreadDynarray_t ptr)
484 {
485   if (ptr->is_locked) {
486     LeaveCriticalSection(&(ptr->cs));
487     ptr->is_locked = false;
488   }
489 }