Welcome to mirror list, hosted at ThFree Co, Russian Federation.

m-shared.h - github.com/P-p-H-d/mlib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f64bcf8aa855342b81caca303adf5b662e8168a0 (plain)
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
/*
 * M*LIB - SHARED Pointer Module
 *
 * Copyright (c) 2017-2024, Patrick Pelissier
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MSTARLIB_SHARED_PTR_H
#define MSTARLIB_SHARED_PTR_H

#include "m-core.h"
#include "m-atomic.h"
#include "m-genint.h"

M_BEGIN_PROTECTED_CODE

/* Define shared pointer and its function.
   USAGE: SHARED_PTR_DEF(name, type, [, oplist]) */
#define M_SHARED_PTR_DEF(name, ...)                                           \
  M_SHARED_PTR_DEF_AS(name, M_F(name,_t), __VA_ARGS__)


/* Define shared pointer and its function
   as the given name name_t
   USAGE: SHARED_PTR_DEF_AS(name, name_t, type, [, oplist]) */
#define M_SHARED_PTR_DEF_AS(name, name_t, ...)                                \
  M_BEGIN_PROTECTED_CODE                                                      \
  M_SHAR3D_PTR_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__)                             \
                     ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), M_SHAR3D_ATOMIC_OPLIST, name_t ), \
                      (name, __VA_ARGS__ ,                                       M_SHAR3D_ATOMIC_OPLIST, name_t ))) \
  M_END_PROTECTED_CODE


/* Define the oplist of a shared pointer.
   USAGE: SHARED_OPLIST(name [, oplist_of_the_type]) */
#define M_SHARED_PTR_OPLIST(...)                                              \
  M_SHAR3D_PTR_OPLIST_P1(M_IF_NARGS_EQ1(__VA_ARGS__)                          \
                        ((__VA_ARGS__, M_BASIC_OPLIST ),                      \
                         (__VA_ARGS__ )))


/* Define relaxed shared pointer and its function (thread unsafe).
   USAGE: SHARED_PTR_RELAXED_DEF(name, type, [, oplist]) */
#define M_SHARED_PTR_RELAXED_DEF(name, ...)                                   \
  M_SHARED_PTR_RELAXED_DEF_AS(name, M_F(name,_t), __VA_ARGS__)


/* Define relaxed shared pointer and its function (thread unsafe)
   as the given name name_t
   USAGE: SHARED_PTR_RELAXED_DEF(name, type, [, oplist]) */
#define M_SHARED_PTR_RELAXED_DEF_AS(name, name_t, ...)                        \
  M_BEGIN_PROTECTED_CODE                                                      \
  M_SHAR3D_PTR_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__)                             \
                     ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), M_SHAR3D_INTEGER_OPLIST, name_t ), \
                      (name, __VA_ARGS__,                                        M_SHAR3D_INTEGER_OPLIST, name_t ))) \
  M_END_PROTECTED_CODE


/* Define shared resource and its function.
   This is a bounded pool of resource shared by multiple owners.
   USAGE: SHARED_RESOURCE_DEF(name, type, [, oplist]) */
#define M_SHARED_RESOURCE_DEF(name, ...)                                      \
  M_SHARED_RESOURCE_DEF_AS(name, M_F(name,_t), M_F(name,_it_t), __VA_ARGS__)


/* Define shared resource and its function
   as the given name named_t and the iterator it_t
   This is a bounded pool of resource shared by multiple owners.
   USAGE: SHARED_RESOURCE_DEF_AS(name, name_t, it_t, type, [, oplist]) */
#define M_SHARED_RESOURCE_DEF_AS(name, name_t, it_t, ...)                     \
  M_BEGIN_PROTECTED_CODE                                                      \
  M_SHAR3D_RESOURCE_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__)                        \
                          ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t ), \
                           (name, __VA_ARGS__,                                        name_t, it_t ))) \
  M_END_PROTECTED_CODE



/*****************************************************************************/
/********************************** INTERNAL *********************************/
/*****************************************************************************/

// deferred evaluation
#define M_SHAR3D_PTR_OPLIST_P1(arg) M_SHAR3D_PTR_OPLIST_P2 arg

/* Validation of the given, shared_t oplist */
#define M_SHAR3D_PTR_OPLIST_P2(name, oplist)                                  \
  M_IF_OPLIST(oplist)(M_SHAR3D_PTR_OPLIST_P3, M_SHAR3D_PTR_OPLIST_FAILURE)(name, oplist)

/* Prepare a clean compilation failure */
#define M_SHAR3D_PTR_OPLIST_FAILURE(name, oplist)                             \
  ((M_LIB_ERROR(ARGUMENT_OF_SHARED_PTR_OPLIST_IS_NOT_AN_OPLIST, name, oplist)))

#define M_SHAR3D_PTR_OPLIST_P3(name, oplist) (                                \
  INIT(M_F(name, _init)),                                                     \
  CLEAR(M_F(name, _clear)),                                                   \
  INIT_SET(M_F(name, _init_set)),                                             \
  SET(M_F(name, _set))                                                        \
  INIT_MOVE(M_F(name, _init_move)),                                           \
  RESET(M_F(name, _reset)),                                                   \
  MOVE(M_F(name, _move)),                                                     \
  SWAP(M_F(name, _swap))                                                      \
  ,NAME(name)                                                                 \
  ,TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*)                        \
  )

// OPLIST to handle a counter of atomic type
#define M_SHAR3D_ATOMIC_OPLIST (TYPE(atomic_int),                             \
                               INIT_SET(atomic_init),                         \
                               ADD(atomic_fetch_add),                         \
                               SUB(atomic_fetch_sub),                         \
                               IT_CREF(atomic_load))

// OPLIST to handle a counter of non-atomic type
#define M_SHAR3D_INTEGER_OPLIST (TYPE(int),                                   \
                                INIT_SET(m_shar3d_integer_init_set),          \
                                ADD(m_shar3d_integer_add),                    \
                                SUB(m_shar3d_integer_sub),                    \
                                IT_CREF(m_shar3d_integer_cref))

/* Atomic like interface for basic integers */
M_INLINE void m_shar3d_integer_init_set(int *p, int val) { *p = val; }
M_INLINE int m_shar3d_integer_add(int *p, int val) { int r = *p;  *p += val; return r; }
M_INLINE int m_shar3d_integer_sub(int *p, int val) { int r = *p;  *p -= val; return r; }
M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }


/********************************** INTERNAL *********************************/

/* Contract of a shared pointer */
#define M_SHAR3D_CONTRACT(shared, cpt_oplist) do {                            \
    M_ASSERT(shared != NULL);                                                 \
    M_ASSERT(*shared == NULL || M_CALL_IT_CREF(cpt_oplist, &(*shared)->cpt) >= 1); \
  } while (0)

// deferred evaluation
#define M_SHAR3D_PTR_DEF_P1(arg) M_ID( M_SHAR3D_PTR_DEF_P2 arg )

/* Validate the oplist before going further */
#define M_SHAR3D_PTR_DEF_P2(name, type, oplist, cpt_oplist, shared_t)         \
  M_IF_OPLIST(oplist)(M_SHAR3D_PTR_DEF_P3, M_SHAR3D_PTR_DEF_FAILURE)(name, type, oplist, cpt_oplist, shared_t)

/* Stop processing with a compilation failure */
#define M_SHAR3D_PTR_DEF_FAILURE(name, type, oplist, cpt_oplist, shared_t)    \
  M_STATIC_FAILURE(M_LIB_NOT_AN_OPLIST, "(SHARED_PTR_DEF): the given argument is not a valid oplist: " #oplist)

/* Code generation */
#define M_SHAR3D_PTR_DEF_P3(name, type, oplist, cpt_oplist, shared_t)         \
  M_SHAR3D_PTR_DEF_TYPE(name, type, oplist, cpt_oplist, shared_t)             \
  M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist)                            \
  M_SHAR3D_PTR_DEF_CORE(name, type, oplist, cpt_oplist, shared_t)             \
  M_EMPLACE_QUEUE_DEF(name, cpt_oplist, M_F(name, _init_with), oplist, M_SHAR3D_PTR_DEF_EMPLACE)

/* Define the types */
#define M_SHAR3D_PTR_DEF_TYPE(name, type, oplist, cpt_oplist, shared_t)       \
                                                                              \
  typedef struct M_F(name, _s){                                               \
    type *data;                        /* Pointer to the data */              \
    M_GET_TYPE cpt_oplist  cpt; /* Counter of how many refs the data */       \
    bool  combineAlloc; /* Does the data and the ptr share the slot? */       \
  } *shared_t[1];                                                             \
  typedef struct M_F(name, _s) *M_F(name, _ptr);                              \
  typedef const struct M_F(name, _s) *M_F(name, _srcptr);                     \
                                                                              \
  /* Internal type for oplist */                                              \
  typedef shared_t M_F(name, _ct);                                            \
  typedef type     M_F(name, _subtype_ct);                                    \
                                                                              \
  typedef struct M_F(name, _combine_s) {                                      \
    struct M_F(name, _s) ptr;                                                 \
    type data;                                                                \
  } M_F(name, combine_ct)[1];                                                 \

/* Define the core functions */
#define M_SHAR3D_PTR_DEF_CORE(name, type, oplist, cpt_oplist, shared_t)       \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _init)(shared_t shared)                                           \
  {                                                                           \
    *shared = NULL;                                                           \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _init2)(shared_t shared, type *data)                              \
  {                                                                           \
    M_ASSERT (shared != NULL);                                                \
    /* The shared ptr get exclusive access to data */                         \
    struct M_F(name, _s) *ptr;                                                \
    if (M_UNLIKELY (data == NULL)) {                                          \
      *shared = NULL;                                                         \
      return;                                                                 \
    }                                                                         \
    ptr = M_CALL_NEW(oplist, struct M_F(name, _s));                           \
    if (M_UNLIKELY_NOMEM (ptr == NULL)) {                                     \
      M_MEMORY_FULL(sizeof(struct M_F(name, _s)));                            \
      return;                                                                 \
    }                                                                         \
    ptr->data = data;                                                         \
    M_CALL_INIT_SET(cpt_oplist, &ptr->cpt, 1);                                \
    ptr->combineAlloc = false;                                                \
    *shared = ptr;                                                            \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
  }                                                                           \
                                                                              \
  M_IF_METHOD(INIT, oplist)(                                                  \
  M_INLINE void                                                               \
  M_F(name, _init_new)(shared_t shared)                                       \
  {                                                                           \
    /* NOTE: Alloc 1 struct with both structures. */                          \
    struct M_F(name, _combine_s) *p =                                         \
      M_CALL_NEW(oplist, struct M_F(name, _combine_s));                       \
    if (M_UNLIKELY_NOMEM (p == NULL)) {                                       \
      M_MEMORY_FULL(sizeof(struct M_F(name, _combine_s)));                    \
      return;                                                                 \
    }                                                                         \
    struct M_F(name, _s) *ptr = &p->ptr;                                      \
    ptr->combineAlloc = true;                                                 \
    type *data = &p->data;                                                    \
    M_CALL_INIT( oplist, *data);                                              \
    ptr->data = data;                                                         \
    M_CALL_INIT_SET(cpt_oplist, &ptr->cpt, 1);                                \
    *shared = ptr;                                                            \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
  }                                                                           \
  , /* No INIT */ )                                                           \
                                                                              \
  M_INLINE bool                                                               \
  M_F(name, _NULL_p)(const shared_t shared)                                   \
  {                                                                           \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    return *shared == NULL;                                                   \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _init_set)(shared_t dest,                                         \
                       const shared_t shared)                                 \
  {                                                                           \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_ASSERT (dest != shared);                                                \
    *dest = *shared;                                                          \
    if (*dest != NULL) {                                                      \
      int n = M_CALL_ADD(cpt_oplist, &((*dest)->cpt), 1);                     \
      (void) n;        /* unused return value */                              \
    }                                                                         \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _clear)(shared_t dest)                                            \
  {                                                                           \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
    if (*dest != NULL)        {                                               \
      if (M_CALL_SUB(cpt_oplist, &((*dest)->cpt), 1) == 1)        {           \
        bool combineAlloc = (*dest)->combineAlloc;                            \
        /* Note: if combineAlloc is true, the address of the slot             \
           combining both data & ptr is the same as the address of the        \
           first element, aka data itself. Static analyzer tools don't        \
           seem to detect this and report error. */                           \
        M_CALL_CLEAR(oplist, *(*dest)->data);                                 \
        if (combineAlloc == false) {                                          \
          M_CALL_DEL(oplist, (*dest)->data);                                  \
        }                                                                     \
        M_CALL_DEL(oplist, *dest);                                            \
      }                                                                       \
      *dest = NULL;                                                           \
    }                                                                         \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _reset)(shared_t dest)                                            \
  {                                                                           \
    /* NOTE: Clear will also set dest to NULL */                              \
    M_F(name, _clear)(dest);                                                  \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _set)(shared_t dest,                                              \
                  const shared_t shared)                                      \
  {                                                                           \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_F(name, _clear)(dest);                                                  \
    M_F(name, _init_set)(dest, shared);                                       \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _init_move)(shared_t dest,                                        \
                        shared_t shared)                                      \
  {                                                                           \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_ASSERT (dest != NULL && dest != shared);                                \
    *dest = *shared;                                                          \
    *shared = NULL;                                                           \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _move)(shared_t dest,                                             \
                   shared_t shared)                                           \
  {                                                                           \
    M_SHAR3D_CONTRACT(dest, cpt_oplist);                                      \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_ASSERT (dest != shared);                                                \
    M_F(name, _clear)(dest);                                                  \
    M_F(name, _init_move)(dest, shared);                                      \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _swap)(shared_t p1,                                               \
                   shared_t p2)                                               \
  {                                                                           \
    M_SHAR3D_CONTRACT(p1, cpt_oplist);                                        \
    M_SHAR3D_CONTRACT(p2, cpt_oplist);                                        \
    /* NOTE: SWAP is not atomic */                                            \
    M_SWAP (struct M_F(name, _s)*, *p1, *p2);                                 \
    M_SHAR3D_CONTRACT(p1, cpt_oplist);                                        \
    M_SHAR3D_CONTRACT(p2, cpt_oplist);                                        \
  }                                                                           \
                                                                              \
  M_INLINE bool                                                               \
  M_F(name, _equal_p)(const shared_t p1,                                      \
                      const shared_t p2)                                      \
  {                                                                           \
    M_SHAR3D_CONTRACT(p1, cpt_oplist);                                        \
    M_SHAR3D_CONTRACT(p2, cpt_oplist);                                        \
    return *p1 == *p2;                                                        \
  }                                                                           \
                                                                              \
  M_INLINE type const *                                                       \
  M_F(name, _cref)(const shared_t shared)                                     \
  {                                                                           \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_ASSERT(*shared != NULL);                                                \
    type *data = (*shared)->data;                                             \
    M_ASSERT (data != NULL);                                                  \
    return M_CONST_CAST (type, data);                                         \
  }                                                                           \
                                                                              \
  M_INLINE type *                                                             \
  M_F(name, _ref)(shared_t shared)                                            \
  {                                                                           \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
    M_ASSERT(*shared != NULL);                                                \
    type *data = (*shared)->data;                                             \
    M_ASSERT (data != NULL);                                                  \
    return data;                                                              \
  }                                                                           \

/* Definition of the emplace_back function for arrays */
#define M_SHAR3D_PTR_DEF_EMPLACE(name, cpt_oplist, function_name, oplist, init_func, exp_emplace_type) \
  M_INLINE void                                                               \
  function_name(M_F(name, _ct) shared                                         \
                M_EMPLACE_LIST_TYPE_VAR(a, exp_emplace_type) )                \
  {                                                                           \
    /* NOTE: Alloc 1 struct with both structures. */                          \
    struct M_F(name, _combine_s) *p =                                         \
      M_CALL_NEW(oplist, struct M_F(name, _combine_s));                       \
    if (M_UNLIKELY_NOMEM (p == NULL)) {                                       \
      M_MEMORY_FULL(sizeof(struct M_F(name, _combine_s)));                    \
      return;                                                                 \
    }                                                                         \
    struct M_F(name, _s) *ptr = &p->ptr;                                      \
    ptr->combineAlloc = true;                                                 \
    M_F(name, _subtype_ct) *data = &p->data;                                  \
    M_EMPLACE_CALL_FUNC(a, init_func, oplist, *data, exp_emplace_type);       \
    ptr->data = data;                                                         \
    M_CALL_INIT_SET(cpt_oplist, &ptr->cpt, 1);                                \
    *shared = ptr;                                                            \
    M_SHAR3D_CONTRACT(shared, cpt_oplist);                                    \
  }                                                                           \



/********************************** INTERNAL *********************************/

#define M_SHAR3D_RESOURCE_CONTRACT(s) do {                                    \
    M_ASSERT (s != NULL);                                                     \
    M_ASSERT (s->buffer != NULL);                                             \
  } while (0)

// deferred
#define M_SHAR3D_RESOURCE_DEF_P1(arg) M_ID( M_SHAR3D_RESOURCE_DEF_P2 arg )

/* Validate the oplist before going further */
#define M_SHAR3D_RESOURCE_DEF_P2(name, type, oplist, shared_t, it_t)          \
  M_IF_OPLIST(oplist)(M_SHAR3D_RESOURCE_DEF_P3, M_SHAR3D_RESOURCE_DEF_FAILURE)(name, type, oplist, shared_t, it_t)

/* Stop processing with a compilation failure */
#define M_SHAR3D_RESOURCE_DEF_FAILURE(name, type, oplist, shared_t, it_t)     \
  M_STATIC_FAILURE(M_LIB_NOT_AN_OPLIST, "(SHARED_RESOURCE_DEF): the given argument is not a valid oplist: " #oplist)

#define M_SHAR3D_RESOURCE_DEF_P3(name, type, oplist, shared_t, it_t)          \
  M_SHAR3D_RESOURCE_DEF_TYPE(name, type, oplist, shared_t, it_t)              \
  M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist)                            \
  M_SHAR3D_RESOURCE_DEF_CORE(name, type, oplist, shared_t, it_t)              \

/* Define the types */
#define M_SHAR3D_RESOURCE_DEF_TYPE(name, type, oplist, shared_t, it_t)        \
                                                                              \
  /* Create an aligned type to avoid false sharing between threads */         \
  typedef struct M_F(name, _atype_s) {                                        \
    atomic_uint  cpt;                                                         \
    type         x;                                                           \
    M_CACHELINE_ALIGN(align, type, atomic_uint);                              \
  } M_F(name, _atype_ct);                                                     \
                                                                              \
  typedef struct M_F(name, _s) {                                              \
    m_genint_t             core;                                              \
    M_F(name, _atype_ct) *buffer;                                             \
  } shared_t[1];                                                              \
                                                                              \
  typedef struct M_F(name, _it_s) {                                           \
    unsigned int idx;                                                         \
    struct M_F(name, _s) *ref;                                                \
  } it_t[1];                                                                  \
                                                                              \
  /* Internal Types for oplist */                                             \
  typedef shared_t M_F(name, _ct);                                            \
  typedef type     M_F(name, _subtype_ct);                                    \

/* Define the core functions */
#define M_SHAR3D_RESOURCE_DEF_CORE(name, type, oplist, shared_t, it_t)        \
  M_INLINE void                                                               \
  M_F(name, _init)(shared_t s, size_t n)                                      \
  {                                                                           \
    M_ASSERT(s != NULL);                                                      \
    M_ASSERT (n > 0 && n < UINT_MAX);                                         \
    s->buffer = M_CALL_REALLOC(oplist, M_F(name, _atype_ct), NULL, n);        \
    if (M_UNLIKELY_NOMEM (s->buffer == NULL)) {                               \
      M_MEMORY_FULL(sizeof(M_F(name, _atype_ct)) * n);                        \
      return;                                                                 \
    }                                                                         \
    for(size_t i = 0; i < n; i++) {                                           \
      M_CALL_INIT(oplist, s->buffer[i].x);                                    \
      atomic_init (&s->buffer[i].cpt, 0U);                                    \
    }                                                                         \
    m_genint_init(s->core, (unsigned int) n);                                 \
    M_SHAR3D_RESOURCE_CONTRACT(s);                                            \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _clear)(shared_t s)                                               \
  {                                                                           \
    M_SHAR3D_RESOURCE_CONTRACT(s);                                            \
    size_t n = m_genint_size(s->core);                                        \
    for(size_t i = 0; i < n; i++) {                                           \
      M_CALL_CLEAR(oplist, s->buffer[i].x);                                   \
    }                                                                         \
    M_CALL_FREE(oplist, s->buffer);                                           \
    s->buffer = NULL;                                                         \
    m_genint_clear(s->core);                                                  \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _it)(it_t it, shared_t s)                                         \
  {                                                                           \
    M_SHAR3D_RESOURCE_CONTRACT(s);                                            \
    M_ASSERT (it != NULL);                                                    \
    unsigned int idx = m_genint_pop(s->core);                                 \
    it->idx = idx;                                                            \
    it->ref = s;                                                              \
    if (M_LIKELY (idx != M_GENINT_ERROR)) {                                   \
      M_ASSERT(atomic_load(&s->buffer[idx].cpt) == 0);                        \
      atomic_store(&s->buffer[idx].cpt, 1U);                                  \
    }                                                                         \
  }                                                                           \
                                                                              \
  M_INLINE bool                                                               \
  M_F(name, _end_p)(it_t it)                                                  \
  {                                                                           \
    M_ASSERT (it != NULL);                                                    \
    return it->idx == M_GENINT_ERROR;                                         \
  }                                                                           \
                                                                              \
  M_INLINE type *                                                             \
  M_F(name, _ref)(it_t it)                                                    \
  {                                                                           \
    M_ASSERT (it != NULL && it->ref != NULL && it->idx != M_GENINT_ERROR);    \
    M_SHAR3D_RESOURCE_CONTRACT(it->ref);                                      \
    return &it->ref->buffer[it->idx].x;                                       \
  }                                                                           \
                                                                              \
  M_INLINE type const *                                                       \
  M_F(name, _cref)(it_t it)                                                   \
  {                                                                           \
    M_ASSERT (it != NULL && it->ref != NULL && it->idx != M_GENINT_ERROR);    \
    M_SHAR3D_RESOURCE_CONTRACT(it->ref);                                      \
    return M_CONST_CAST (type, &it->ref->buffer[it->idx].x);                  \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _end)(it_t it, shared_t s)                                        \
  {                                                                           \
    M_SHAR3D_RESOURCE_CONTRACT(s);                                            \
    M_ASSERT (it != NULL);                                                    \
    M_ASSERT (it->ref == s);                                                  \
    unsigned int idx = it->idx;                                               \
    if (M_LIKELY (idx != M_GENINT_ERROR)) {                                   \
      unsigned int c = atomic_fetch_sub (&it->ref->buffer[idx].cpt, 1U);      \
      if (c == 1) {                                                           \
        m_genint_push(it->ref->core, idx);                                    \
      }                                                                       \
      it->idx = M_GENINT_ERROR;                                               \
    }                                                                         \
  }                                                                           \
                                                                              \
  M_INLINE void                                                               \
  M_F(name, _it_set)(it_t itd, it_t its)                                      \
  {                                                                           \
    M_ASSERT (itd != NULL && its != NULL);                                    \
    M_SHAR3D_RESOURCE_CONTRACT(its->ref);                                     \
    itd->ref = its->ref;                                                      \
    unsigned int idx = its->idx;                                              \
    itd->idx = idx;                                                           \
    if (M_LIKELY (idx != M_GENINT_ERROR)) {                                   \
      unsigned int c = atomic_fetch_add(&itd->ref->buffer[idx].cpt, 1U);      \
      M_ASSERT (c >= 1);                                                      \
    }                                                                         \
  }                                                                           \

M_END_PROTECTED_CODE

/********************************** INTERNAL *********************************/

#if M_USE_SMALL_NAME
#define SHARED_PTR_OPLIST M_SHARED_PTR_OPLIST
#define SHARED_PTR_DEF M_SHARED_PTR_DEF
#define SHARED_PTR_DEF_AS M_SHARED_PTR_DEF_AS
#define SHARED_PTR_RELAXED_DEF M_SHARED_PTR_RELAXED_DEF
#define SHARED_PTR_RELAXED_DEF_AS M_SHARED_PTR_RELAXED_DEF_AS
#define SHARED_RESOURCE_DEF M_SHARED_RESOURCE_DEF
#define SHARED_RESOURCE_DEF_AS M_SHARED_RESOURCE_DEF_AS
#endif

#endif