Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7997da210c966ba71e579b44e8126d6c8e33bdf4
[simgrid.git] / src / xbt / mmalloc / swag.h
1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* Warning, this module is done to be efficient and performs tons of cast and dirty things. So avoid using it unless
7  * you really know what you are doing. */
8
9 #ifndef XBT_SWAG_H
10 #define XBT_SWAG_H
11
12 /*
13  * XBT_swag: a O(1) set based on linked lists
14  *
15  *  Warning, this module is done to be efficient and performs tons of cast and dirty things. So make sure you know what
16  *  you are doing while using it.
17  *  It is basically a fifo but with restrictions so that it can be used as a set. Any operation (add, remove, belongs)
18  *  is O(1) and no call to malloc/free is done.
19  *
20  *  @deprecated If you are using C++, you might want to use `boost::intrusive::set` instead.
21  */
22
23 /* Swag types
24  *
25  *  Specific set.
26  *
27  *  These typedefs are public so that the compiler can do his job but believe me, you don't want to try to play with
28  *  those structs directly. Use them as an abstract datatype.
29  */
30
31 typedef struct xbt_swag_hookup {
32   void *next;
33   void *prev;
34 } s_xbt_swag_hookup_t;
35
36 /* This type should be added to a type that is to be used in a swag.
37  *
38  *  Whenever a new object with this struct is created, all fields have to be set to NULL
39  *
40  * Here is an example like that :
41
42 \code
43 typedef struct foo {
44   s_xbt_swag_hookup_t set1_hookup;
45   s_xbt_swag_hookup_t set2_hookup;
46
47   double value;
48 } s_foo_t, *foo_t;
49 ...
50 {
51   s_foo_t elem;
52   xbt_swag_t set1=NULL;
53   xbt_swag_t set2=NULL;
54
55   set1 = xbt_swag_new(xbt_swag_offset(elem, set1_hookup));
56   set2 = xbt_swag_new(xbt_swag_offset(elem, set2_hookup));
57
58 }
59 \endcode
60 */
61
62 struct xbt_swag {
63   void *head;
64   void *tail;
65   size_t offset;
66   int count;
67 };
68 typedef struct xbt_swag s_xbt_swag_t;
69
70 #endif /* XBT_SWAG_H */