1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ARK_WEB_BASE_REF_COUNTED_CAPI_H_
17 #define ARK_WEB_BASE_REF_COUNTED_CAPI_H_
18 #pragma once
19 
20 #include <mutex>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <unistd.h>
24 
25 #include "base/include/ark_web_macros.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 ///
32 // All ref-counted framework structures must include this structure first.
33 ///
34 typedef struct _ark_web_base_ref_counted_t {
35     /**
36      * @brief Size of the data structure.
37      */
38     size_t size;
39 
40     /**
41      * @brief The reference count increment 1. Should be called for every new copy
42      *        of a pointer to a given object.
43      */
44     void(ARK_WEB_CALLBACK* incre_ref)(struct _ark_web_base_ref_counted_t* self);
45 
46     /**
47      * @brief The reference count decrement 1 and delete the object when the
48      *        reference count is 0
49      */
50     void(ARK_WEB_CALLBACK* decre_ref)(struct _ark_web_base_ref_counted_t* self);
51 } ark_web_base_ref_counted_t;
52 
53 /**
54  * @brief Check that the structure |s|, which is defined with a size_t member at
55  *        the top, is large enough to contain the specified member |m|.
56  */
57 #define ARK_WEB_MEMBER_EXISTS(s, m) \
58     ((intptr_t) & ((s)->m) - (intptr_t)(s) + sizeof((s)->m) <= *reinterpret_cast<size_t*>(s))
59 
60 #define ARK_WEB_FUNC_MEMBER_MISSING(s, f) (!ARK_WEB_MEMBER_EXISTS(s, f) || !((s)->f))
61 
62 #ifdef __cplusplus
63 }
64 #endif
65 
66 #endif // ARK_WEB_BASE_REF_COUNTED_CAPI_H_
67