| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
|
| 5 |
/*this header contains macros for ref_counting a variable. |
| 6 |
|
| 7 |
There are no upper bound checks related to uint32_t overflow because we expect that bigger issues are in |
| 8 |
the system when more than 4 billion references exist to the same variable. In the case when such an overflow |
| 9 |
occurs, the object's ref count will reach zero (while still having 0xFFFFFFFF references) and likely the |
| 10 |
controlling code will take the decision to free the object's resources. Then, any of the 0xFFFFFFFF references |
| 11 |
will interact with deallocated memory / resources resulting in an undefined behavior. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef REFCOUNT_H |
| 15 |
#define REFCOUNT_H |
| 16 |
|
| 17 |
#ifdef __cplusplus |
| 18 |
#include <cstdlib> |
| 19 |
#include <cstdint> |
| 20 |
#else |
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdint.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "azure_c_shared_utility/gballoc.h" |
| 26 |
#include "azure_macro_utils/macro_utils.h" |
| 27 |
|
| 28 |
// Include the platform-specific file that defines atomic functionality |
| 29 |
#include "refcount_os.h" |
| 30 |
|
| 31 |
#ifdef __cplusplus |
| 32 |
extern "C" |
| 33 |
{ |
| 34 |
#endif |
| 35 |
|
| 36 |
#define REFCOUNT_TYPE(type) \ |
| 37 |
struct MU_C2(MU_C2(REFCOUNT_, type), _TAG) |
| 38 |
|
| 39 |
#define REFCOUNT_SHORT_TYPE(type) \ |
| 40 |
MU_C2(REFCOUNT_, type) |
| 41 |
|
| 42 |
#define REFCOUNT_TYPE_DECLARE_CREATE(type) MU_C2(REFCOUNT_SHORT_TYPE(type), _Create) |
| 43 |
#define REFCOUNT_TYPE_DECLARE_CREATE_WITH_EXTRA_SIZE(type) MU_C2(REFCOUNT_SHORT_TYPE(type), _Create_With_Extra_Size) |
| 44 |
#define REFCOUNT_TYPE_CREATE(type) MU_C2(REFCOUNT_SHORT_TYPE(type), _Create)() |
| 45 |
#define REFCOUNT_TYPE_CREATE_WITH_EXTRA_SIZE(type, size) MU_C2(REFCOUNT_SHORT_TYPE(type), _Create_With_Extra_Size)(size) |
| 46 |
#define REFCOUNT_TYPE_DECLARE_DESTROY(type) MU_C2(REFCOUNT_SHORT_TYPE(type), _Destroy) |
| 47 |
#define REFCOUNT_TYPE_DESTROY(type, var) MU_C2(REFCOUNT_SHORT_TYPE(type), _Destroy)(var) |
| 48 |
|
| 49 |
/*this introduces a new refcount'd type based on another type */ |
| 50 |
/*and an initializer for that new type that also sets the ref count to 1. The type must not have a flexible array*/ |
| 51 |
/*the newly allocated memory shall be free'd by free()*/ |
| 52 |
/*and the ref counting is handled internally by the type in the _Create/ _Create_With_Extra_Size /_Clone /_Destroy functions */ |
| 53 |
|
| 54 |
/* Codes_SRS_REFCOUNT_01_005: [ REFCOUNT_TYPE_CREATE_WITH_EXTRA_SIZE shall allocate memory for the type that is ref counted (type) plus extra memory enough to hold size bytes. ]*/ |
| 55 |
/* Codes_SRS_REFCOUNT_01_006: [ On success it shall return a non-NULL handle to the allocated ref counted type type. ]*/ |
| 56 |
/* Codes_SRS_REFCOUNT_01_007: [ If any error occurs, REFCOUNT_TYPE_CREATE_WITH_EXTRA_SIZE shall return NULL. ]*/ |
| 57 |
#define DEFINE_CREATE_WITH_EXTRA_SIZE(type) \ |
| 58 |
static type* REFCOUNT_TYPE_DECLARE_CREATE_WITH_EXTRA_SIZE(type)(size_t size) \ |
| 59 |
{ \ |
| 60 |
REFCOUNT_TYPE(type)* ref_counted; \ |
| 61 |
size_t malloc_size = sizeof(REFCOUNT_TYPE(type)) + size; \ |
| 62 |
if (malloc_size < size) \ |
| 63 |
{ \ |
| 64 |
ref_counted = NULL; \ |
| 65 |
} \ |
| 66 |
else \ |
| 67 |
{ \ |
| 68 |
ref_counted = (REFCOUNT_TYPE(type)*)malloc(malloc_size); \ |
| 69 |
} \ |
| 70 |
type* result; \ |
| 71 |
if (ref_counted == NULL) \ |
| 72 |
{ \ |
| 73 |
result = NULL; \ |
| 74 |
} \ |
| 75 |
else \ |
| 76 |
{ \ |
| 77 |
result = &ref_counted->counted; \ |
| 78 |
INIT_REF(type, result); \ |
| 79 |
} \ |
| 80 |
return result; \ |
| 81 |
} \ |
| 82 |
|
| 83 |
/* Codes_SRS_REFCOUNT_01_002: [ REFCOUNT_TYPE_CREATE shall allocate memory for the type that is ref counted. ]*/ |
| 84 |
/* Codes_SRS_REFCOUNT_01_003: [ On success it shall return a non-NULL handle to the allocated ref counted type type. ]*/ |
| 85 |
/* Codes_SRS_REFCOUNT_01_004: [ If any error occurs, REFCOUNT_TYPE_CREATE shall return NULL. ]*/ |
| 86 |
#define DEFINE_CREATE(type) \ |
| 87 |
static type* REFCOUNT_TYPE_DECLARE_CREATE(type) (void) \ |
| 88 |
{ \ |
| 89 |
return REFCOUNT_TYPE_DECLARE_CREATE_WITH_EXTRA_SIZE(type)(0); \ |
| 90 |
} \ |
| 91 |
|
| 92 |
/* Codes_SRS_REFCOUNT_01_008: [ REFCOUNT_TYPE_DESTROY shall free the memory allocated by REFCOUNT_TYPE_CREATE or REFCOUNT_TYPE_CREATE_WITH_EXTRA_SIZE. ]*/ |
| 93 |
/* Codes_SRS_REFCOUNT_01_009: [ If counted_type is NULL, REFCOUNT_TYPE_DESTROY shall return. ]*/ |
| 94 |
#define DEFINE_DESTROY(type) \ |
| 95 |
static void REFCOUNT_TYPE_DECLARE_DESTROY(type)(type* counted_type) \ |
| 96 |
{ \ |
| 97 |
void* ref_counted = (void*)((unsigned char*)counted_type - offsetof(REFCOUNT_TYPE(type), counted)); \ |
| 98 |
free(ref_counted); \ |
| 99 |
} |
| 100 |
|
| 101 |
#define DEFINE_REFCOUNT_TYPE(type) \ |
| 102 |
REFCOUNT_TYPE(type) \ |
| 103 |
{ \ |
| 104 |
COUNT_TYPE count; \ |
| 105 |
type counted; \ |
| 106 |
}; \ |
| 107 |
DEFINE_CREATE_WITH_EXTRA_SIZE(type) \ |
| 108 |
DEFINE_CREATE(type) \ |
| 109 |
DEFINE_DESTROY(type) \ |
| 110 |
|
| 111 |
#ifndef DEC_RETURN_ZERO |
| 112 |
#error refcount_os.h does not define DEC_RETURN_ZERO |
| 113 |
#endif // !DEC_RETURN_ZERO |
| 114 |
#ifndef INC_REF_VAR |
| 115 |
#error refcount_os.h does not define INC_REF_VAR |
| 116 |
#endif // !INC_REF |
| 117 |
#ifndef DEC_REF_VAR |
| 118 |
#error refcount_os.h does not define DEC_REF_VAR |
| 119 |
#endif // !DEC_REF |
| 120 |
#ifndef INIT_REF_VAR |
| 121 |
#error refcount_os.h does not define INIT_REF_VAR |
| 122 |
#endif // !INIT_REF |
| 123 |
|
| 124 |
#define INC_REF(type, var) INC_REF_VAR(((REFCOUNT_TYPE(type)*)((unsigned char*)var - offsetof(REFCOUNT_TYPE(type), counted)))->count) |
| 125 |
#define DEC_REF(type, var) DEC_REF_VAR(((REFCOUNT_TYPE(type)*)((unsigned char*)var - offsetof(REFCOUNT_TYPE(type), counted)))->count) |
| 126 |
#define INIT_REF(type, var) INIT_REF_VAR(((REFCOUNT_TYPE(type)*)((unsigned char*)var - offsetof(REFCOUNT_TYPE(type), counted)))->count) |
| 127 |
|
| 128 |
#ifdef __cplusplus |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
#endif /*REFCOUNT_H*/ |
| 133 |
|
| 134 |
|