1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
|
/*
*
* (C) COPYRIGHT 2008-2012 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained from Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file mali_osk_lists.h
* Implementation of the OS abstraction layer for the kernel device driver.
* Note that the OSK list implementation is copied from the CUTILS
* doubly linked list (DLIST) implementation.
*/
#ifndef _OSK_LISTS_H_
#define _OSK_LISTS_H_
#ifndef _OSK_H_
#error "Include mali_osk.h directly"
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#include <osk/mali_osk_common.h>
/**
* @addtogroup base_api
* @{
*/
/**
* @addtogroup base_osk_api
* @{
*/
/**
* @addtogroup osk_dlist Doubly-linked list
* @{
*/
/**
* @addtogroup osk_dlist_public Public
* @{
*/
/**
* @brief Item of a list
*
* @note Can be integrated inside a wider structure.
*/
typedef struct osk_dlist_item
{
struct
{
struct osk_dlist_item *next; /**< @private */
struct osk_dlist_item *prev; /**< @private */
}oskp; /**< @private*/
}osk_dlist_item;
/**
* @brief Doubly-linked list
*/
typedef struct osk_dlist
{
struct
{
struct osk_dlist_item *front; /**< @private */
struct osk_dlist_item *back; /**< @private */
}oskp; /**< @private*/
}osk_dlist;
/**
* @brief Test if @c container_ptr is the back of the list
*
* @param [in] container_ptr Pointer to the front of the container to test.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @a container_ptr is NULL.
* @note If @c attribute is invalid then the behavior is undefined.
*
* @return Returns MALI_TRUE if @c container_ptr is the back of the list.
*/
#define OSK_DLIST_IS_BACK(container_ptr, attribute)\
(NULL == (OSK_CHECK_PTR(container_ptr))->attribute.oskp.next)
/**
* @brief Test if @c container_ptr is the front of the list
*
* @param [in] container_ptr Pointer to the front of the container to test.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @a container_ptr is NULL.
* @note If @c attribute is invalid then the behavior is undefined.
*
* @return Returns MALI_TRUE if @c container_ptr is the front of the list.
*/
#define OSK_DLIST_IS_FRONT(container_ptr, attribute)\
(NULL == (OSK_CHECK_PTR(container_ptr))->attribute.oskp.prev)
/**
* @brief Test if @c container_ptr is valid
*
* @param [in] container_ptr Pointer to the front of the container to test.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note If @c attribute is invalid then the behavior is undefined.
*
* @return Returns MALI_TRUE if @c container_ptr is valid or MALI_FALSE otherwise.
*/
#define OSK_DLIST_IS_VALID(container_ptr, attribute)\
( NULL != (container_ptr) )
/**
* @brief Return the next item in the list
*
* @param [in] container_ptr Pointer to an item of type @c type
* @param [in] type Type of the container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the next container item, or @c NULL.
* @note If this macro evaluates as null then the back of the list has been reached.
* @note An assert is triggered if @a container_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_NEXT(container_ptr, type, attribute)\
( OSK_DLIST_IS_BACK( container_ptr, attribute ) ?\
NULL :CONTAINER_OF( (container_ptr)->attribute.oskp.next, type, attribute ) )
/**
* @brief Return MALI_TRUE if the list is empty
*
* @param [in] osk_dlist_ptr Pointer to the @c osk_dlist to test.
*
* @note An assert is triggered if @a osk_dlist_ptr is NULL.
*
* @return Returns MALI_TRUE if @c osk_dlist_ptr is an empty list.
*/
#define OSK_DLIST_IS_EMPTY(osk_dlist_ptr)\
(NULL == OSK_CHECK_PTR(osk_dlist_ptr)->oskp.front)
/**
* @brief Return the previous item in the list
*
* @param [in] container_ptr Pointer to an item of type @c type
* @param [in] type Type of the container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the previous container item, or @c NULL.
* @note If this macro evaluates as null then the front of the list has been reached.
* @note An assert is triggered if @a container_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_PREV(container_ptr, type, attribute)\
( OSK_DLIST_IS_FRONT( container_ptr, attribute ) ?\
NULL : CONTAINER_OF( (container_ptr)->attribute.oskp.prev, type, attribute) )
/**
* @brief Return the front container of the list
*
* @param [in] osk_dlist_ptr Pointer to a list
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the front container item, or @c NULL.
* @note If this macro evaluates as null then the list is empty.
* @note An assert is triggered if @a osk_dlist_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_FRONT(osk_dlist_ptr, type, attribute)\
( OSK_CHECK_PTR( osk_dlist_ptr )->oskp.front == NULL ?\
NULL : CONTAINER_OF( (osk_dlist_ptr)->oskp.front, type, attribute ) )
/**
* @brief Check whether or not @c container_ptr is a member of @c osk_dlist_ptr.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_ptr Pointer to the item to check.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return MALI_TRUE if @c container_ptr is a member of @c osk_dlist_ptr, MALI_FALSE if not.
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_to_remove_ptr is NULL.
* @note If @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_MEMBER_OF(osk_dlist_ptr, container_ptr, attribute)\
oskp_dlist_member_of(osk_dlist_ptr, &(OSK_CHECK_PTR(container_ptr))->attribute)
/**
* @brief Return the back container of the list
*
* @param [in] osk_dlist_ptr Pointer to a list
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the back container item, or @c NULL.
*
* @note If this macro evaluates as null then the list is empty.
* @note An assert is triggered if @a osk_dlist_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_BACK(osk_dlist_ptr, type, attribute)\
( OSK_CHECK_PTR( osk_dlist_ptr )->oskp.back == NULL ?\
NULL : CONTAINER_OF( (osk_dlist_ptr)->oskp.back, type, attribute) )
/**
* @brief Initialize a list
*
* @param [out] osk_dlist_ptr Pointer to a osk_dlist
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
*/
#define OSK_DLIST_INIT(osk_dlist_ptr)\
do\
{\
OSK_CHECK_PTR(osk_dlist_ptr); \
(osk_dlist_ptr)->oskp.front = NULL; \
(osk_dlist_ptr)->oskp.back = NULL;\
}while(MALI_FALSE)
/**
* @brief Append @c container_to_insert_ptr at the back of @c osk_dlist_ptr
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_insert_ptr Pointer to an item to insert of type @c type.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_to_insert_ptr is NULL or if it already belongs to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_PUSH_BACK(osk_dlist_ptr, container_to_insert_ptr, type, attribute)\
OSK_DLIST_INSERT_BEFORE(osk_dlist_ptr, container_to_insert_ptr, NULL, type, attribute)
/**
* @brief Insert @c container_to_insert_ptr at the front of @c osk_dlist_ptr
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_insert_ptr Pointer to an item to insert of type @c type.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_to_insert_ptr is NULL or if it already belongs to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_PUSH_FRONT(osk_dlist_ptr, container_to_insert_ptr, type, attribute)\
OSK_DLIST_INSERT_AFTER(osk_dlist_ptr, container_to_insert_ptr, NULL, type, attribute)
/**
* @brief Remove the back of @c osk_dlist_ptr and return the element just removed
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to a container item.
*
* @note If @c OSK_DLIST_IS_VALID returns MALI_FALSE when testing the returned pointer then the list is empty
* @note An assert is triggered if @c osk_dlist_ptr is NULL or empty.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_POP_BACK(osk_dlist_ptr, type, attribute)\
CONTAINER_OF(\
oskp_dlist_remove(\
osk_dlist_ptr, \
&OSK_CHECK_PTR( OSK_DLIST_BACK(osk_dlist_ptr, type, attribute) )->attribute), \
type, \
attribute)
/**
* @brief Remove the front of @c osk_dlist_ptr and return the element just removed
*
* The front and the back of the list are automatically adjusted.
*
* @note The list must contain at least one item.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to a container item.
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL or empty.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_POP_FRONT(osk_dlist_ptr, type, attribute)\
CONTAINER_OF(\
oskp_dlist_remove(\
osk_dlist_ptr, \
&OSK_CHECK_PTR( OSK_DLIST_FRONT(osk_dlist_ptr, type, attribute) )->attribute), \
type, \
attribute)
/**
* @brief Append @c container_to_insert_ptr after @c container_pos_ptr in @c osk_dlist_ptr
*
* @note Insert the new element at the list front if @c container_pos_ptr is NULL.
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_insert_ptr Pointer to an item to insert of type @c type.
* @param [in, out] container_pos_ptr Pointer to the item of type @c type after which inserting the new item.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_pos_ptr is not NULL and not a member of the list.
* @note An assert is triggered if @c container_to_insert_ptr is NULL or if it already belongs to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_INSERT_AFTER(osk_dlist_ptr, container_to_insert_ptr, container_pos_ptr, type, attribute)\
oskp_dlist_insert_after(\
osk_dlist_ptr, \
&(OSK_CHECK_PTR(container_to_insert_ptr))->attribute, \
&((type*)container_pos_ptr)->attribute, \
NULL == container_pos_ptr)
/**
* @brief Append @c container_to_insert_ptr before @c container_pos_ptr in @c osk_dlist_ptr
*
* @note Insert the new element at the list back if @c container_pos_ptr is NULL.
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_insert_ptr Pointer to an item to insert of type @c type.
* @param [in, out] container_pos_ptr Pointer to the item of type @c type before which inserting the new item.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_pos_ptr is not NULL and not a member of the list.
* @note An assert is triggered if @c container_to_insert_ptr is NULL or if it already belongs to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_INSERT_BEFORE(osk_dlist_ptr, container_to_insert_ptr, container_pos_ptr, type, attribute)\
oskp_dlist_insert_before(\
osk_dlist_ptr, \
&(OSK_CHECK_PTR(container_to_insert_ptr))->attribute, \
&((type*)container_pos_ptr)->attribute, \
NULL == container_pos_ptr)
/**
* @brief Remove an item container from a doubly-linked list and return a pointer to the element
* which was next in the list.
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_remove_ptr Pointer to an item to remove of type @c type.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the item container that was immediately after the one
* removed from the list, or @c NULL.
*
* @note If this macro evaluates as null then the back of the list has been reached.
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_to_remove_ptr is NULL or if it doesn't belong to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
* @pre @p osk_dlist_ptr must have been initialized by @ref OSK_DLIST_INIT.
* @pre @p container_to_remove_ptr must be a member of list @p osk_dlist_ptr.
* @post @p container_to_remove_ptr is no longer a member of list @p osk_dlist_ptr.
*
*/
#define OSK_DLIST_REMOVE_AND_RETURN_NEXT(osk_dlist_ptr, container_to_remove_ptr, type, attribute)\
( OSK_DLIST_IS_BACK( container_to_remove_ptr, attribute ) ?\
( oskp_dlist_remove( osk_dlist_ptr, &( container_to_remove_ptr )->attribute ), NULL ) :\
CONTAINER_OF( oskp_dlist_remove_and_return_next( osk_dlist_ptr,\
&( container_to_remove_ptr )->attribute ),\
type,\
attribute ) )
/**
* @brief Remove an item container from a doubly-linked list.
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_remove_ptr Pointer to an item to remove of type @c type.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @note An assert error is triggered if @c osk_dlist_ptr is NULL.
* @note An assert error is triggered if @c container_to_remove_ptr is NULL or if it doesn't belong to the list.
* @note If @c attribute is invalid then the behavior is undefined.
*
* @pre @p osk_dlist_ptr must have been initialized by @ref OSK_DLIST_INIT.
* @pre @p container_to_remove_ptr must be a member of list @p osk_dlist_ptr.
* @post @p container_to_remove_ptr is no longer a member of list @p osk_dlist_ptr.
*/
#define OSK_DLIST_REMOVE(osk_dlist_ptr, container_to_remove_ptr, attribute)\
oskp_dlist_remove_item(osk_dlist_ptr, &((OSK_CHECK_PTR(container_to_remove_ptr))->attribute) )
/**
* @brief Remove an item container from a doubly-linked list and return a pointer to the element which was the
* previous one in the list.
*
* The front and the back of the list are automatically adjusted.
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in, out] container_to_remove_ptr Pointer to an item to remove of type @c type.
* @param [in] type Type of the list container
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
*
* @return A pointer to the item container that was immediately before the one
* removed from the list, or @c NULL.
*
* @note If this macro evaluates as null then the front of the list has been reached.
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c container_to_remove_ptr is NULL or if it doesn't belong to the list.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*
* @pre @p osk_dlist_ptr must have been initialized by @ref OSK_DLIST_INIT.
* @pre @p container_to_remove_ptr must be a member of list @p osk_dlist_ptr.
* @post @p container_to_remove_ptr is no longer a member of list @p osk_dlist_ptr.
*/
#define OSK_DLIST_REMOVE_AND_RETURN_PREV(osk_dlist_ptr, container_to_remove_ptr, type, attribute)\
( OSK_DLIST_IS_FRONT( container_to_remove_ptr, attribute ) ?\
( oskp_dlist_remove( osk_dlist_ptr, &( container_to_remove_ptr )->attribute ), NULL ) :\
CONTAINER_OF( oskp_dlist_remove_and_return_prev( osk_dlist_ptr,\
&( container_to_remove_ptr )->attribute ),\
type,\
attribute ) )
/**
* @brief Remove and call the destructor function for every item in the list, walking from start to end.
*
* @param [in, out] osk_dlist_ptr Pointer to the list to empty
* @param [in] type Type of the list container.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
* @param [in] destructor_func Destructor function called for every item present in the list.
*
* This function has to be of the form void func(type* item);
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c destructor_func is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_EMPTY_LIST(osk_dlist_ptr, type, attribute, destructor_func)\
do\
{\
type* oskp_it;\
OSK_ASSERT(NULL != osk_dlist_ptr); \
OSK_ASSERT(NULL != destructor_func); \
oskp_it = OSK_DLIST_FRONT(osk_dlist_ptr, type, attribute);\
while ( oskp_it != NULL )\
{\
type* to_delete = oskp_it;\
oskp_it = OSK_DLIST_REMOVE_AND_RETURN_NEXT(osk_dlist_ptr, oskp_it, type, attribute);\
destructor_func(to_delete);\
}\
}while(MALI_FALSE)
/**
* @brief Remove and call the destructor function for every item in the list, walking from the end and to the front.
*
* @param [in, out] osk_dlist_ptr Pointer to the list to empty
* @param [in] type Type of the list container.
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
* @param [in] destructor_func Destructor function called for every item present in the list.
*
* This function has to be of the form void func(type* item);
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note An assert is triggered if @c destructor_func is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_EMPTY_LIST_REVERSE(osk_dlist_ptr, type, attribute, destructor_func)\
do\
{\
type* oskp_it;\
OSK_ASSERT(NULL != osk_dlist_ptr); \
OSK_ASSERT(NULL != destructor_func); \
oskp_it = OSK_DLIST_BACK(osk_dlist_ptr, type, attribute);\
while ( oskp_it != NULL )\
{\
type* to_delete = oskp_it;\
oskp_it = OSK_DLIST_REMOVE_AND_RETURN_PREV(osk_dlist_ptr, oskp_it, type, attribute);\
destructor_func(to_delete);\
}\
}while(MALI_FALSE)
/**
* @brief Iterate forward through each container item of the given list
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in] type Container type of the list
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
* @param [out] container_iterator Iterator variable of type "type*" to use to iterate through the list.
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_FOREACH(osk_dlist_ptr, type, attribute, container_iterator)\
OSK_ASSERT(NULL != osk_dlist_ptr); \
for(\
container_iterator = OSK_DLIST_FRONT(osk_dlist_ptr, type, attribute);\
NULL != container_iterator; \
container_iterator = OSK_DLIST_NEXT(container_iterator, type, attribute))
/**
* @brief Reverse iterate through each container item of the given list
*
* @param [in, out] osk_dlist_ptr Pointer to a list
* @param [in] type Container type of the list
* @param [in] attribute Attribute of the container of type @c osk_dlist_item
* @param [out] container_iterator Iterator variable of type "type*" to use to iterate through the list.
*
* @note An assert is triggered if @c osk_dlist_ptr is NULL.
* @note If @c type or @c attribute is invalid then the behavior is undefined.
*/
#define OSK_DLIST_FOREACH_REVERSE(osk_dlist_ptr, type, attribute, container_iterator)\
OSK_ASSERT(NULL != osk_dlist_ptr); \
for(\
container_iterator = OSK_DLIST_BACK(osk_dlist_ptr, type, attribute);\
NULL != container_iterator; \
container_iterator = OSK_DLIST_PREV(container_iterator, type, attribute))
/**
* @}
*/
/* End osk_dlist_public */
/**
* @addtogroup osk_dlist_private Private
* @{
*/
/**
* @brief Insert a new item after an existing one.
*
* @param [in, out] list_ptr Pointer to the list the new item is going to be added to.
* @param [in, out] item_to_insert New item to insert in the list.
* @param [in, out] position Position after which to add the new item.
* @param [in] insert_at_front If this argument is equal to MALI_TRUE then @c position is ignored and the
* new item is added to the front.
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c insert_at_front is MALI_FALSE and @c position is NULL.
*/
OSK_STATIC_INLINE void oskp_dlist_insert_after(osk_dlist * const list_ptr, osk_dlist_item * const item_to_insert,
osk_dlist_item * const position, const mali_bool insert_at_front);
/**
* @brief Insert a new item before an existing one.
*
* @param [in, out] list_ptr Pointer to the list the new item is going to be added to.
* @param [in, out] item_to_insert New item to insert in the list.
* @param [in, out] position Position before which to add the new item.
* @param [in] insert_at_back If this argument is equal to MALI_TRUE then @c position is ignored and the new
* item is added to the back
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c insert_at_back is MALI_FALSE and @c position is NULL.
*/
OSK_STATIC_INLINE void oskp_dlist_insert_before(osk_dlist * const list_ptr, osk_dlist_item* const item_to_insert,
osk_dlist_item * const position, const mali_bool insert_at_back);
/**
* @brief Remove a given item from the list and return the item which was next in the list
*
* @param [in, out] list_ptr List from which the item needs to be removed
* @param [in, out] item_to_remove Item to remove from the list
*
* @return A pointer to the item which was next in the list. Return NULL if the back has just been removed.
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c item_to_remove is not a member of @c list_ptr
*/
OSK_STATIC_INLINE osk_dlist_item* oskp_dlist_remove_and_return_next(osk_dlist * const list_ptr,
osk_dlist_item * const item_to_remove) CHECK_RESULT;
/**
* @brief Remove a given item from the list and return the item which was before in the list
*
* @param [in, out] list_ptr List from which the item needs to be removed
* @param [in, out] item_to_remove Item to remove from the list
*
* @return A pointer to the item which was before in the list. Return NULL if the front has just been removed.
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c item_to_remove is not a member of @c list_ptr
*/
OSK_STATIC_INLINE osk_dlist_item* oskp_dlist_remove_and_return_prev(osk_dlist * const list_ptr,
osk_dlist_item * const item_to_remove) CHECK_RESULT;
/**
* @brief Remove a given item from the list and return it.
*
* @param [in, out] list_ptr List from which the item needs to be removed
* @param [in, out] item_to_remove Item to remove from the list
*
* @return A pointer to the item which has been removed from the list.
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c item_to_remove is not a member of @c list_ptr
*/
OSK_STATIC_INLINE osk_dlist_item* oskp_dlist_remove(osk_dlist * const list_ptr,
osk_dlist_item * const item_to_remove);
/**
* @brief Check that @c item is a member of the @c list
*
* @param [in] list Metadata of the list
* @param [in] item Item to check
*
* @note An assert error is triggered if @c list is NULL.
*
* @return MALI_TRUE if @c item is a member of @c list or MALI_FALSE otherwise.
*/
OSK_STATIC_INLINE mali_bool oskp_dlist_member_of(osk_dlist* const list, osk_dlist_item* const item) CHECK_RESULT;
/**
* @brief remove @c item_to_remove from @c front
*
* @param [in, out] front List from which the item needs to be removed
* @param [in, out] item_to_remove Item to remove from the list.
*
* @note An assert is triggered if @c list_ptr is NULL.
* @note An assert is triggered if @c item_to_remove is not a member of @c list_ptr
*/
OSK_STATIC_INLINE void oskp_dlist_remove_item(osk_dlist* const front, osk_dlist_item* const item_to_remove);
/**
* @}
*/
/* end osk_dlist_private */
/**
* @}
*/
/* end osk_dlist group */
/**
* @addtogroup osk_dlist Doubly-linked list
* @{
*/
/**
* @addtogroup osk_dlist_private Private
* @{
*/
CHECK_RESULT OSK_STATIC_INLINE mali_bool oskp_dlist_member_of(osk_dlist* const list, osk_dlist_item* const item)
{
mali_bool return_value = MALI_FALSE;
osk_dlist_item* it;
OSK_ASSERT(NULL != list);
it = list->oskp.front;
while(NULL != it)
{
if(item == it)
{
return_value = MALI_TRUE;
break;
}
it = it->oskp.next;
}
return return_value;
}
OSK_STATIC_INLINE void oskp_dlist_insert_before(osk_dlist * const front, osk_dlist_item * const item_to_insert,
osk_dlist_item * const position, const mali_bool insert_at_back)
{
OSK_ASSERT(NULL != front);
OSK_ASSERT(NULL != item_to_insert);
OSK_ASSERT((insert_at_back == MALI_TRUE) || (NULL != position));
OSK_ASSERT(MALI_FALSE == oskp_dlist_member_of(front, item_to_insert));
if(insert_at_back)
{
item_to_insert->oskp.prev = front->oskp.back;
/*if there are some other items in the list, update their links.*/
if(NULL != front->oskp.back)
{
front->oskp.back->oskp.next = item_to_insert;
}
item_to_insert->oskp.next = NULL;
front->oskp.back = item_to_insert;
}
else
{
/* insertion at a position which is not the back*/
OSK_ASSERT(MALI_FALSE != oskp_dlist_member_of(front, position));
item_to_insert->oskp.prev = position->oskp.prev;
item_to_insert->oskp.next = position;
position->oskp.prev = item_to_insert;
/*if there are some other items in the list, update their links.*/
if(NULL != item_to_insert->oskp.prev)
{
item_to_insert->oskp.prev->oskp.next = item_to_insert;
}
}
/* Did the element inserted became the new front */
if(front->oskp.front == item_to_insert->oskp.next)
{
front->oskp.front = item_to_insert;
}
}
OSK_STATIC_INLINE
void oskp_dlist_insert_after(osk_dlist * const front, osk_dlist_item * const item_to_insert,
osk_dlist_item * const position, mali_bool insert_at_front)
{
OSK_ASSERT(NULL != front);
OSK_ASSERT(NULL != item_to_insert);
OSK_ASSERT((insert_at_front == MALI_TRUE) || (NULL != position));
OSK_ASSERT(MALI_FALSE == oskp_dlist_member_of(front, item_to_insert));
if(insert_at_front)
{
item_to_insert->oskp.next = front->oskp.front;
/*if there are some other items in the list, update their links.*/
if(NULL != front->oskp.front)
{
front->oskp.front->oskp.prev = item_to_insert;
}
item_to_insert->oskp.prev = NULL;
front->oskp.front = item_to_insert;
}
else
{
/* insertion at a position which is not the front */
OSK_ASSERT(MALI_FALSE != oskp_dlist_member_of(front, position));
item_to_insert->oskp.next = position->oskp.next;
item_to_insert->oskp.prev = position;
position->oskp.next = item_to_insert;
/*if the item has not been inserted at the back, then update the links of the next item*/
if(NULL != item_to_insert->oskp.next)
{
item_to_insert->oskp.next->oskp.prev = item_to_insert;
}
}
/* Is the item inserted the new back ?*/
if(front->oskp.back == item_to_insert->oskp.prev)
{
front->oskp.back = item_to_insert;
}
}
OSK_STATIC_INLINE
void oskp_dlist_remove_item(osk_dlist* const front, osk_dlist_item* const item_to_remove)
{
OSK_ASSERT(NULL != front);
OSK_ASSERT(NULL != item_to_remove);
OSK_ASSERT(MALI_TRUE == oskp_dlist_member_of(front, item_to_remove));
/* if the item to remove is the current front*/
if( front->oskp.front == item_to_remove )
{
/* then make the front point to the next item*/
front->oskp.front = item_to_remove->oskp.next;
}
else
{
/* else just the previous item point to the next one*/
item_to_remove->oskp.prev->oskp.next = item_to_remove->oskp.next;
}
/* if the item to remove is the current back*/
if(front->oskp.back == item_to_remove)
{
/* then make the back point to the previous item*/
front->oskp.back = item_to_remove->oskp.prev;
}
else
{
/* else just the next item point to the previous one*/
item_to_remove->oskp.next->oskp.prev = item_to_remove->oskp.prev;
}
item_to_remove->oskp.next = NULL;
item_to_remove->oskp.prev = NULL;
}
OSK_STATIC_INLINE
osk_dlist_item* oskp_dlist_remove(osk_dlist * const front, osk_dlist_item * const item_to_remove)
{
oskp_dlist_remove_item(front, item_to_remove);
item_to_remove->oskp.next = NULL;
item_to_remove->oskp.prev = NULL;
return item_to_remove;
}
CHECK_RESULT OSK_STATIC_INLINE
osk_dlist_item* oskp_dlist_remove_and_return_next(osk_dlist * const front,
osk_dlist_item * const item_to_remove)
{
osk_dlist_item *next;
OSK_ASSERT(NULL != front);
OSK_ASSERT(NULL != item_to_remove);
next = item_to_remove->oskp.next;
oskp_dlist_remove_item(front, item_to_remove);
return next;
}
CHECK_RESULT OSK_STATIC_INLINE
osk_dlist_item* oskp_dlist_remove_and_return_prev(osk_dlist * const front,
osk_dlist_item * const item_to_remove)
{
osk_dlist_item *prev;
OSK_ASSERT(NULL != front);
OSK_ASSERT(NULL != item_to_remove);
prev = item_to_remove->oskp.prev;
oskp_dlist_remove_item(front, item_to_remove);
return prev;
}
/**
* @}
*/
/* end osk_dlist_private */
/**
* @}
*/
/* end osk_dlist group */
/** @} */ /* end group base_osk_api */
/** @} */ /* end group base_api */
#ifdef __cplusplus
}
#endif
#endif /* _OSK_LISTS_H_ */
|