Azure IoT / azure_c_shared_utility

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers refcount.h Source File

refcount.h

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 
00005 /*this header contains macros for ref_counting a variable.
00006 
00007 There are no upper bound checks related to uint32_t overflow because we expect that bigger issues are in
00008 the system when more than 4 billion references exist to the same variable. In the case when such an overflow
00009 occurs, the object's ref count will reach zero (while still having 0xFFFFFFFF references) and likely the
00010 controlling code will take the decision to free the object's resources. Then, any of the 0xFFFFFFFF references
00011 will interact with deallocated memory / resources resulting in an undefined behavior.
00012 */
00013 
00014 #ifndef REFCOUNT_H
00015 #define REFCOUNT_H
00016 
00017 #include "azure_c_shared_utility/gballoc.h"
00018 #include "azure_c_shared_utility/macro_utils.h"
00019 
00020 #ifdef __cplusplus
00021 #include <cstdlib>
00022 #include <cstdint>
00023 extern "C"
00024 {
00025 #else
00026 #include <stdlib.h>
00027 #include <stdint.h>
00028 #endif
00029 
00030 // Include the platform-specific file that defines atomic functionality
00031 #include "refcount_os.h"
00032 
00033 #define REFCOUNT_TYPE(type) \
00034 struct C2(C2(REFCOUNT_, type), _TAG)
00035 
00036 #define REFCOUNT_SHORT_TYPE(type) \
00037 C2(REFCOUNT_, type)
00038 
00039 #define REFCOUNT_TYPE_DECLARE_CREATE(type) C2(REFCOUNT_SHORT_TYPE(type), _Create)
00040 #define REFCOUNT_TYPE_CREATE(type) C2(REFCOUNT_SHORT_TYPE(type), _Create)()
00041 
00042 /*this introduces a new refcount'd type based on another type */
00043 /*and an initializer for that new type that also sets the ref count to 1. The type must not have a flexible array*/
00044 /*the newly allocated memory shall be free'd by free()*/
00045 /*and the ref counting is handled internally by the type in the _Create/ _Clone /_Destroy functions */
00046 
00047 #define DEFINE_REFCOUNT_TYPE(type)                                                                   \
00048 REFCOUNT_TYPE(type)                                                                                  \
00049 {                                                                                                    \
00050     type counted;                                                                                    \
00051     COUNT_TYPE count;                                                                                \
00052 };                                                                                                   \
00053 static type* REFCOUNT_TYPE_DECLARE_CREATE(type) (void)                                               \
00054 {                                                                                                    \
00055     REFCOUNT_TYPE(type)* result = (REFCOUNT_TYPE(type)*)malloc(sizeof(REFCOUNT_TYPE(type)));         \
00056     if (result != NULL)                                                                              \
00057     {                                                                                                \
00058         INIT_REF(type, result);                                                                      \
00059     }                                                                                                \
00060     return (type*)result;                                                                            \
00061 }                                                                                                    \
00062 
00063 #ifndef DEC_RETURN_ZERO
00064 #error refcount_os.h does not define DEC_RETURN_ZERO
00065 #endif // !DEC_RETURN_ZERO
00066 #ifndef INC_REF
00067 #error refcount_os.h does not define INC_REF
00068 #endif // !INC_REF
00069 #ifndef DEC_REF
00070 #error refcount_os.h does not define DEC_REF
00071 #endif // !DEC_REF
00072 #ifndef INIT_REF
00073 #error refcount_os.h does not define INIT_REF
00074 #endif // !INIT_REF
00075 
00076 #ifdef __cplusplus
00077 }
00078 #endif
00079 
00080 #endif /*REFCOUNT_H*/
00081 
00082