/src/rtpproxy/src/rtpp_genuid.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org> |
3 | | * Copyright (c) 2006-2015 Sippy Software, Inc., http://www.sippysoft.com |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | * |
27 | | */ |
28 | | |
29 | | #include <stdatomic.h> |
30 | | #include <stddef.h> |
31 | | #include <stdint.h> |
32 | | #include <stdlib.h> |
33 | | |
34 | | #include "rtpp_types.h" |
35 | | #include "rtpp_refcnt.h" |
36 | | #include "rtpp_genuid.h" |
37 | | #include "rtpp_genuid_fin.h" |
38 | | #include "rtpp_mallocs.h" |
39 | | |
40 | | struct rtpp_genuid_priv { |
41 | | struct rtpp_genuid pub; |
42 | | _Atomic(uint64_t) lastuid; |
43 | | }; |
44 | | |
45 | | static uint64_t rtpp_genuid_gen(struct rtpp_genuid *); |
46 | | |
47 | | DEFINE_SMETHODS(rtpp_genuid, |
48 | | .gen = &rtpp_genuid_gen, |
49 | | ); |
50 | | |
51 | | struct rtpp_genuid * |
52 | | rtpp_genuid_ctor(void) |
53 | 2 | { |
54 | 2 | struct rtpp_genuid_priv *pvt; |
55 | | |
56 | 2 | pvt = rtpp_rzmalloc(sizeof(struct rtpp_genuid_priv), PVT_RCOFFS(pvt)); |
57 | 2 | if (pvt == NULL) { |
58 | 0 | goto e0; |
59 | 0 | } |
60 | 2 | atomic_init(&pvt->lastuid, 0); |
61 | | #if defined(RTPP_DEBUG) |
62 | | pvt->pub.smethods = GET_SMETHODS(&pvt->pub); |
63 | | RTPP_OBJ_DTOR_ATTACH(&pvt->pub, (rtpp_refcnt_dtor_t)&rtpp_genuid_fin, |
64 | | &(pvt->pub)); |
65 | | #endif |
66 | 2 | return (&pvt->pub); |
67 | | |
68 | 0 | e0: |
69 | 0 | return (NULL); |
70 | 2 | } |
71 | | |
72 | | static uint64_t |
73 | | rtpp_genuid_gen(struct rtpp_genuid *pub) |
74 | 164k | { |
75 | 164k | struct rtpp_genuid_priv *pvt; |
76 | | |
77 | 164k | PUB2PVT(pub, pvt); |
78 | | |
79 | | return atomic_fetch_add_explicit(&(pvt->lastuid), 1, |
80 | 164k | memory_order_relaxed); |
81 | 164k | } |