rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_ref.c (1301B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "ref_count.h"
     17 
     18 struct test {
     19   ref_T ref;
     20   int val;
     21 };
     22 
     23 static void
     24 release(ref_T* ref)
     25 {
     26   ASSERT(NULL != ref);
     27   CHK(CONTAINER_OF(ref, struct test, ref)->val == (int)0xDEADBEEF);
     28 }
     29 
     30 int
     31 main(int argc, char** argv)
     32 {
     33   struct test test;
     34   (void)argc, (void)argv;
     35 
     36   ref_init(&test.ref);
     37   test.val = (int)0xDEADBEEF;
     38 
     39   ref_get(&test.ref);
     40   ref_get(&test.ref);
     41   ref_get(&test.ref);
     42 
     43   CHK(ref_put(&test.ref, release) == 0);
     44   CHK(ref_put(&test.ref, release) == 0);
     45   CHK(ref_put(&test.ref, release) == 0);
     46   CHK(ref_put(&test.ref, release) == 1);
     47   return 0;
     48 }