gnunet-android

GNUnet for Android
Log | Files | Refs | README

base.h (19421B)


      1 // Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
      2 //
      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 //     https://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 #ifndef OPENSSL_HEADER_BASE_H
     16 #define OPENSSL_HEADER_BASE_H
     17 
     18 
     19 // This file should be the first included by all BoringSSL headers. All
     20 // BoringSSL headers can be assumed to import this file, and this header can be
     21 // assumed to include stddef.h (size_t) and stdint.h (uint*_t, etc).
     22 
     23 #include <stddef.h>  // IWYU pragma: export
     24 #include <stdint.h>  // IWYU pragma: export
     25 #include <stdlib.h>
     26 #include <sys/types.h>
     27 
     28 #if defined(__MINGW32__)
     29 // stdio.h is needed on MinGW for __MINGW_PRINTF_FORMAT.
     30 #include <stdio.h>
     31 #endif
     32 
     33 #if defined(__APPLE__)
     34 #include <TargetConditionals.h>
     35 #endif
     36 
     37 // Include a BoringSSL-only header so consumers including this header without
     38 // setting up include paths do not accidentally pick up the system
     39 // opensslconf.h.
     40 #include <openssl/is_boringssl.h>
     41 #include <openssl/opensslconf.h>
     42 #include <openssl/target.h>  // IWYU pragma: export
     43 
     44 #if defined(BORINGSSL_PREFIX)
     45 #include <boringssl_prefix_symbols.h>
     46 #endif
     47 
     48 #if defined(__cplusplus)
     49 extern "C" {
     50 #endif
     51 
     52 
     53 #if defined(__APPLE__)
     54 // Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX|
     55 // targets macOS specifically.
     56 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
     57 #define OPENSSL_MACOS
     58 #endif
     59 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
     60 #define OPENSSL_IOS
     61 #endif
     62 #endif
     63 
     64 #define OPENSSL_IS_BORINGSSL
     65 #define OPENSSL_VERSION_NUMBER 0x1010107f
     66 #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER
     67 
     68 // BORINGSSL_API_VERSION is a positive integer that increments as BoringSSL
     69 // changes over time. The value itself is not meaningful. It will be incremented
     70 // whenever is convenient to coordinate an API change with consumers. This will
     71 // not denote any special point in development.
     72 //
     73 // A consumer may use this symbol in the preprocessor to temporarily build
     74 // against multiple revisions of BoringSSL at the same time. It is not
     75 // recommended to do so for longer than is necessary.
     76 #define BORINGSSL_API_VERSION 36
     77 
     78 #if defined(BORINGSSL_SHARED_LIBRARY)
     79 
     80 #if defined(OPENSSL_WINDOWS)
     81 
     82 #if defined(BORINGSSL_IMPLEMENTATION)
     83 #define OPENSSL_EXPORT __declspec(dllexport)
     84 #else
     85 #define OPENSSL_EXPORT __declspec(dllimport)
     86 #endif
     87 
     88 #else  // defined(OPENSSL_WINDOWS)
     89 
     90 #if defined(BORINGSSL_IMPLEMENTATION)
     91 #define OPENSSL_EXPORT __attribute__((visibility("default")))
     92 #else
     93 #define OPENSSL_EXPORT
     94 #endif
     95 
     96 #endif  // defined(OPENSSL_WINDOWS)
     97 
     98 #else  // defined(BORINGSSL_SHARED_LIBRARY)
     99 
    100 #define OPENSSL_EXPORT
    101 
    102 #endif  // defined(BORINGSSL_SHARED_LIBRARY)
    103 
    104 #if defined(_MSC_VER)
    105 
    106 // OPENSSL_DEPRECATED is used to mark a function as deprecated. Use
    107 // of any functions so marked in caller code will produce a warning.
    108 // OPENSSL_BEGIN_ALLOW_DEPRECATED and OPENSSL_END_ALLOW_DEPRECATED
    109 // can be used to suppress the warning in regions of caller code.
    110 #define OPENSSL_DEPRECATED __declspec(deprecated)
    111 #define OPENSSL_BEGIN_ALLOW_DEPRECATED \
    112   __pragma(warning(push)) __pragma(warning(disable : 4996))
    113 #define OPENSSL_END_ALLOW_DEPRECATED __pragma(warning(pop))
    114 
    115 #elif defined(__GNUC__) || defined(__clang__)
    116 
    117 #define OPENSSL_DEPRECATED __attribute__((__deprecated__))
    118 #define OPENSSL_BEGIN_ALLOW_DEPRECATED \
    119   _Pragma("GCC diagnostic push")       \
    120       _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
    121 #define OPENSSL_END_ALLOW_DEPRECATED _Pragma("GCC diagnostic pop")
    122 
    123 #else
    124 
    125 #define OPENSSL_DEPRECATED
    126 #define OPENSSL_BEGIN_ALLOW_DEPRECATED
    127 #define OPENSSL_END_ALLOW_DEPRECATED
    128 
    129 #endif
    130 
    131 
    132 #if defined(__GNUC__) || defined(__clang__)
    133 // MinGW has two different printf implementations. Ensure the format macro
    134 // matches the selected implementation. See
    135 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
    136 #if defined(__MINGW_PRINTF_FORMAT)
    137 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \
    138   __attribute__((                                                \
    139       __format__(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
    140 #else
    141 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \
    142   __attribute__((__format__(__printf__, string_index, first_to_check)))
    143 #endif
    144 #else
    145 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check)
    146 #endif
    147 
    148 // OPENSSL_GNUC_CLANG_PRAGMA emits a pragma on GCC or clang and nothing on other
    149 // compilers.
    150 #if defined(__GNUC__) || defined(__clang__)
    151 #define OPENSSL_GNUC_CLANG_PRAGMA(arg) _Pragma(arg)
    152 #else
    153 #define OPENSSL_GNUC_CLANG_PRAGMA(arg)
    154 #endif
    155 
    156 // OPENSSL_CLANG_PRAGMA emits a pragma on clang and nothing on other compilers.
    157 #if defined(__clang__)
    158 #define OPENSSL_CLANG_PRAGMA(arg) _Pragma(arg)
    159 #else
    160 #define OPENSSL_CLANG_PRAGMA(arg)
    161 #endif
    162 
    163 // OPENSSL_MSVC_PRAGMA emits a pragma on MSVC and nothing on other compilers.
    164 #if defined(_MSC_VER)
    165 #define OPENSSL_MSVC_PRAGMA(arg) __pragma(arg)
    166 #else
    167 #define OPENSSL_MSVC_PRAGMA(arg)
    168 #endif
    169 
    170 #if defined(__GNUC__) || defined(__clang__)
    171 #define OPENSSL_UNUSED __attribute__((unused))
    172 #elif defined(_MSC_VER)
    173 // __pragma wants to be on a separate line. The following is what it takes to
    174 // stop clang-format from messing with that.
    175 // clang-format off
    176 #define OPENSSL_UNUSED __pragma(warning(suppress : 4505)) \
    177 /* */
    178 // clang-format on
    179 #else
    180 #define OPENSSL_UNUSED
    181 #endif
    182 
    183 // C and C++ handle inline functions differently. In C++, an inline function is
    184 // defined in just the header file, potentially emitted in multiple compilation
    185 // units (in cases the compiler did not inline), but each copy must be identical
    186 // to satsify ODR. In C, a non-static inline must be manually emitted in exactly
    187 // one compilation unit with a separate extern inline declaration.
    188 //
    189 // In both languages, exported inline functions referencing file-local symbols
    190 // are problematic. C forbids this altogether (though GCC and Clang seem not to
    191 // enforce it). It works in C++, but ODR requires the definitions be identical,
    192 // including all names in the definitions resolving to the "same entity". In
    193 // practice, this is unlikely to be a problem, but an inline function that
    194 // returns a pointer to a file-local symbol
    195 // could compile oddly.
    196 //
    197 // Historically, we used static inline in headers. However, to satisfy ODR, use
    198 // plain inline in C++, to allow inline consumer functions to call our header
    199 // functions. Plain inline would also work better with C99 inline, but that is
    200 // not used much in practice, extern inline is tedious, and there are conflicts
    201 // with the old gnu89 model:
    202 // https://stackoverflow.com/questions/216510/extern-inline
    203 #if defined(__cplusplus)
    204 #define OPENSSL_INLINE inline
    205 #else
    206 // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro
    207 // (e.g. a |STACK_OF(T)| implementation) in a source file without tripping
    208 // clang's -Wunused-function.
    209 #define OPENSSL_INLINE static inline OPENSSL_UNUSED
    210 #endif
    211 
    212 #if defined(__cplusplus)
    213 // enums can be predeclared, but only in C++ and only if given an explicit type.
    214 // C doesn't support setting an explicit type for enums thus a #define is used
    215 // to do this only for C++. However, the ABI type between C and C++ need to have
    216 // equal sizes, which is confirmed in a unittest.
    217 #define BORINGSSL_ENUM_INT : int
    218 enum ssl_early_data_reason_t BORINGSSL_ENUM_INT;
    219 enum ssl_encryption_level_t BORINGSSL_ENUM_INT;
    220 enum ssl_private_key_result_t BORINGSSL_ENUM_INT;
    221 enum ssl_renegotiate_mode_t BORINGSSL_ENUM_INT;
    222 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT;
    223 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT;
    224 enum ssl_ticket_aead_result_t BORINGSSL_ENUM_INT;
    225 enum ssl_verify_result_t BORINGSSL_ENUM_INT;
    226 #else
    227 #define BORINGSSL_ENUM_INT
    228 #endif
    229 
    230 // ossl_ssize_t is a signed type which is large enough to fit the size of any
    231 // valid memory allocation. We prefer using |size_t|, but sometimes we need a
    232 // signed type for OpenSSL API compatibility. This type can be used in such
    233 // cases to avoid overflow.
    234 //
    235 // Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that
    236 // are sizes of or indices into C objects, can be converted without overflow.
    237 typedef ptrdiff_t ossl_ssize_t;
    238 
    239 // CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that
    240 // header for details. This type is defined in base.h as a forward declaration.
    241 typedef uint32_t CBS_ASN1_TAG;
    242 
    243 // CRYPTO_THREADID is a dummy value.
    244 typedef int CRYPTO_THREADID;
    245 
    246 // An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as
    247 // an opaque, non-NULL |ASN1_NULL*| pointer.
    248 typedef struct asn1_null_st ASN1_NULL;
    249 
    250 // CRYPTO_MUST_BE_NULL is an opaque type that is never returned from BoringSSL.
    251 // It is used in function parameters that must be NULL.
    252 typedef struct crypto_must_be_null_st CRYPTO_MUST_BE_NULL;
    253 
    254 typedef int ASN1_BOOLEAN;
    255 typedef struct ASN1_ITEM_st ASN1_ITEM;
    256 typedef struct asn1_object_st ASN1_OBJECT;
    257 typedef struct asn1_pctx_st ASN1_PCTX;
    258 typedef struct asn1_string_st ASN1_BIT_STRING;
    259 typedef struct asn1_string_st ASN1_BMPSTRING;
    260 typedef struct asn1_string_st ASN1_ENUMERATED;
    261 typedef struct asn1_string_st ASN1_GENERALIZEDTIME;
    262 typedef struct asn1_string_st ASN1_GENERALSTRING;
    263 typedef struct asn1_string_st ASN1_IA5STRING;
    264 typedef struct asn1_string_st ASN1_INTEGER;
    265 typedef struct asn1_string_st ASN1_OCTET_STRING;
    266 typedef struct asn1_string_st ASN1_PRINTABLESTRING;
    267 typedef struct asn1_string_st ASN1_STRING;
    268 typedef struct asn1_string_st ASN1_T61STRING;
    269 typedef struct asn1_string_st ASN1_TIME;
    270 typedef struct asn1_string_st ASN1_UNIVERSALSTRING;
    271 typedef struct asn1_string_st ASN1_UTCTIME;
    272 typedef struct asn1_string_st ASN1_UTF8STRING;
    273 typedef struct asn1_string_st ASN1_VISIBLESTRING;
    274 typedef struct asn1_type_st ASN1_TYPE;
    275 typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID;
    276 typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS;
    277 typedef struct CMS_ContentInfo_st CMS_ContentInfo;
    278 typedef struct CMS_SignerInfo_st CMS_SignerInfo;
    279 typedef struct DIST_POINT_st DIST_POINT;
    280 typedef struct DSA_SIG_st DSA_SIG;
    281 typedef struct GENERAL_NAME_st GENERAL_NAME;
    282 typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT;
    283 typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS;
    284 typedef struct Netscape_spkac_st NETSCAPE_SPKAC;
    285 typedef struct Netscape_spki_st NETSCAPE_SPKI;
    286 typedef struct RIPEMD160state_st RIPEMD160_CTX;
    287 typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM;
    288 typedef struct X509_algor_st X509_ALGOR;
    289 typedef struct X509_crl_st X509_CRL;
    290 typedef struct X509_extension_st X509_EXTENSION;
    291 typedef struct X509_info_st X509_INFO;
    292 typedef struct X509_name_entry_st X509_NAME_ENTRY;
    293 typedef struct X509_name_st X509_NAME;
    294 typedef struct X509_pubkey_st X509_PUBKEY;
    295 typedef struct X509_req_st X509_REQ;
    296 typedef struct X509_sig_st X509_SIG;
    297 typedef struct bignum_ctx BN_CTX;
    298 typedef struct bignum_st BIGNUM;
    299 typedef struct bio_method_st BIO_METHOD;
    300 typedef struct bio_st BIO;
    301 typedef struct blake2b_state_st BLAKE2B_CTX;
    302 typedef struct bn_gencb_st BN_GENCB;
    303 typedef struct bn_mont_ctx_st BN_MONT_CTX;
    304 typedef struct buf_mem_st BUF_MEM;
    305 typedef struct cbb_st CBB;
    306 typedef struct cbs_st CBS;
    307 typedef struct cmac_ctx_st CMAC_CTX;
    308 typedef struct conf_st CONF;
    309 typedef struct conf_value_st CONF_VALUE;
    310 typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL;
    311 typedef struct crypto_buffer_st CRYPTO_BUFFER;
    312 typedef struct ctr_drbg_state_st CTR_DRBG_STATE;
    313 typedef struct dh_st DH;
    314 typedef struct dsa_st DSA;
    315 typedef struct ec_group_st EC_GROUP;
    316 typedef struct ec_key_st EC_KEY;
    317 typedef struct ec_point_st EC_POINT;
    318 typedef struct ecdsa_method_st ECDSA_METHOD;
    319 typedef struct ecdsa_sig_st ECDSA_SIG;
    320 typedef struct engine_st ENGINE;
    321 typedef struct env_md_ctx_st EVP_MD_CTX;
    322 typedef struct env_md_st EVP_MD;
    323 typedef struct evp_aead_st EVP_AEAD;
    324 typedef struct evp_aead_ctx_st EVP_AEAD_CTX;
    325 typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;
    326 typedef struct evp_cipher_st EVP_CIPHER;
    327 typedef struct evp_encode_ctx_st EVP_ENCODE_CTX;
    328 typedef struct evp_hpke_aead_st EVP_HPKE_AEAD;
    329 typedef struct evp_hpke_ctx_st EVP_HPKE_CTX;
    330 typedef struct evp_hpke_kdf_st EVP_HPKE_KDF;
    331 typedef struct evp_hpke_kem_st EVP_HPKE_KEM;
    332 typedef struct evp_hpke_key_st EVP_HPKE_KEY;
    333 typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;
    334 typedef struct evp_pkey_st EVP_PKEY;
    335 typedef struct hmac_ctx_st HMAC_CTX;
    336 typedef struct md4_state_st MD4_CTX;
    337 typedef struct md5_state_st MD5_CTX;
    338 typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS;
    339 typedef struct pkcs12_st PKCS12;
    340 typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO;
    341 typedef struct private_key_st X509_PKEY;
    342 typedef struct rand_meth_st RAND_METHOD;
    343 typedef struct rc4_key_st RC4_KEY;
    344 typedef struct rsa_meth_st RSA_METHOD;
    345 typedef struct rsa_pss_params_st RSA_PSS_PARAMS;
    346 typedef struct rsa_st RSA;
    347 typedef struct sha256_state_st SHA256_CTX;
    348 typedef struct sha512_state_st SHA512_CTX;
    349 typedef struct sha_state_st SHA_CTX;
    350 typedef struct spake2_ctx_st SPAKE2_CTX;
    351 typedef struct srtp_protection_profile_st SRTP_PROTECTION_PROFILE;
    352 typedef struct ssl_cipher_st SSL_CIPHER;
    353 typedef struct ssl_credential_st SSL_CREDENTIAL;
    354 typedef struct ssl_ctx_st SSL_CTX;
    355 typedef struct ssl_early_callback_ctx SSL_CLIENT_HELLO;
    356 typedef struct ssl_ech_keys_st SSL_ECH_KEYS;
    357 typedef struct ssl_method_st SSL_METHOD;
    358 typedef struct ssl_private_key_method_st SSL_PRIVATE_KEY_METHOD;
    359 typedef struct ssl_quic_method_st SSL_QUIC_METHOD;
    360 typedef struct ssl_session_st SSL_SESSION;
    361 typedef struct ssl_st SSL;
    362 typedef struct ssl_ticket_aead_method_st SSL_TICKET_AEAD_METHOD;
    363 typedef struct st_ERR_FNS ERR_FNS;
    364 typedef struct trust_token_st TRUST_TOKEN;
    365 typedef struct trust_token_client_st TRUST_TOKEN_CLIENT;
    366 typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER;
    367 typedef struct trust_token_method_st TRUST_TOKEN_METHOD;
    368 typedef struct v3_ext_ctx X509V3_CTX;
    369 typedef struct v3_ext_method X509V3_EXT_METHOD;
    370 typedef struct x509_attributes_st X509_ATTRIBUTE;
    371 typedef struct x509_lookup_st X509_LOOKUP;
    372 typedef struct x509_lookup_method_st X509_LOOKUP_METHOD;
    373 typedef struct x509_object_st X509_OBJECT;
    374 typedef struct x509_purpose_st X509_PURPOSE;
    375 typedef struct x509_revoked_st X509_REVOKED;
    376 typedef struct x509_st X509;
    377 typedef struct x509_store_ctx_st X509_STORE_CTX;
    378 typedef struct x509_store_st X509_STORE;
    379 
    380 typedef void *OPENSSL_BLOCK;
    381 
    382 // BSSL_CHECK aborts if |condition| is not true.
    383 #define BSSL_CHECK(condition) \
    384   do {                        \
    385     if (!(condition)) {       \
    386       abort();                \
    387     }                         \
    388   } while (0);
    389 
    390 #if defined(__cplusplus)
    391 }  // extern C
    392 #elif !defined(BORINGSSL_NO_CXX)
    393 #define BORINGSSL_NO_CXX
    394 #endif
    395 
    396 #if defined(BORINGSSL_PREFIX)
    397 #define BSSL_NAMESPACE_BEGIN \
    398   namespace bssl {           \
    399   inline namespace BORINGSSL_PREFIX {
    400 #define BSSL_NAMESPACE_END \
    401   }                        \
    402   }
    403 #else
    404 #define BSSL_NAMESPACE_BEGIN namespace bssl {
    405 #define BSSL_NAMESPACE_END }
    406 #endif
    407 
    408 // MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see
    409 // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l)
    410 // so MSVC is just assumed to support C++11.
    411 #if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER)
    412 #define BORINGSSL_NO_CXX
    413 #endif
    414 
    415 #if !defined(BORINGSSL_NO_CXX)
    416 
    417 extern "C++" {
    418 
    419 #include <memory>
    420 
    421 // STLPort, used by some Android consumers, not have std::unique_ptr.
    422 #if defined(_STLPORT_VERSION)
    423 #define BORINGSSL_NO_CXX
    424 #endif
    425 
    426 }  // extern C++
    427 #endif  // !BORINGSSL_NO_CXX
    428 
    429 #if defined(BORINGSSL_NO_CXX)
    430 
    431 #define BORINGSSL_MAKE_DELETER(type, deleter)
    432 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func)
    433 
    434 #else
    435 
    436 extern "C++" {
    437 
    438 BSSL_NAMESPACE_BEGIN
    439 
    440 namespace internal {
    441 
    442 // The Enable parameter is ignored and only exists so specializations can use
    443 // SFINAE.
    444 template <typename T, typename Enable = void>
    445 struct DeleterImpl {};
    446 
    447 struct Deleter {
    448   template <typename T>
    449   void operator()(T *ptr) {
    450     // Rather than specialize Deleter for each type, we specialize
    451     // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only
    452     // including base.h as long as the destructor is not emitted. This matches
    453     // std::unique_ptr's behavior on forward-declared types.
    454     //
    455     // DeleterImpl itself is specialized in the corresponding module's header
    456     // and must be included to release an object. If not included, the compiler
    457     // will error that DeleterImpl<T> does not have a method Free.
    458     DeleterImpl<T>::Free(ptr);
    459   }
    460 };
    461 
    462 template <typename T, typename CleanupRet, void (*init)(T *),
    463           CleanupRet (*cleanup)(T *)>
    464 class StackAllocated {
    465  public:
    466   StackAllocated() { init(&ctx_); }
    467   ~StackAllocated() { cleanup(&ctx_); }
    468 
    469   StackAllocated(const StackAllocated &) = delete;
    470   StackAllocated &operator=(const StackAllocated &) = delete;
    471 
    472   T *get() { return &ctx_; }
    473   const T *get() const { return &ctx_; }
    474 
    475   T *operator->() { return &ctx_; }
    476   const T *operator->() const { return &ctx_; }
    477 
    478   void Reset() {
    479     cleanup(&ctx_);
    480     init(&ctx_);
    481   }
    482 
    483  private:
    484   T ctx_;
    485 };
    486 
    487 template <typename T, typename CleanupRet, void (*init)(T *),
    488           CleanupRet (*cleanup)(T *), void (*move)(T *, T *)>
    489 class StackAllocatedMovable {
    490  public:
    491   StackAllocatedMovable() { init(&ctx_); }
    492   ~StackAllocatedMovable() { cleanup(&ctx_); }
    493 
    494   StackAllocatedMovable(StackAllocatedMovable &&other) {
    495     init(&ctx_);
    496     move(&ctx_, &other.ctx_);
    497   }
    498   StackAllocatedMovable &operator=(StackAllocatedMovable &&other) {
    499     move(&ctx_, &other.ctx_);
    500     return *this;
    501   }
    502 
    503   T *get() { return &ctx_; }
    504   const T *get() const { return &ctx_; }
    505 
    506   T *operator->() { return &ctx_; }
    507   const T *operator->() const { return &ctx_; }
    508 
    509   void Reset() {
    510     cleanup(&ctx_);
    511     init(&ctx_);
    512   }
    513 
    514  private:
    515   T ctx_;
    516 };
    517 
    518 }  // namespace internal
    519 
    520 #define BORINGSSL_MAKE_DELETER(type, deleter)     \
    521   namespace internal {                            \
    522   template <>                                     \
    523   struct DeleterImpl<type> {                      \
    524     static void Free(type *ptr) { deleter(ptr); } \
    525   };                                              \
    526   }
    527 
    528 // Holds ownership of heap-allocated BoringSSL structures. Sample usage:
    529 //   bssl::UniquePtr<RSA> rsa(RSA_new());
    530 //   bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
    531 template <typename T>
    532 using UniquePtr = std::unique_ptr<T, internal::Deleter>;
    533 
    534 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func)             \
    535   inline UniquePtr<type> UpRef(type *v) {                    \
    536     if (v != nullptr) {                                      \
    537       up_ref_func(v);                                        \
    538     }                                                        \
    539     return UniquePtr<type>(v);                               \
    540   }                                                          \
    541                                                              \
    542   inline UniquePtr<type> UpRef(const UniquePtr<type> &ptr) { \
    543     return UpRef(ptr.get());                                 \
    544   }
    545 
    546 BSSL_NAMESPACE_END
    547 
    548 }  // extern C++
    549 
    550 #endif  // !BORINGSSL_NO_CXX
    551 
    552 #endif  // OPENSSL_HEADER_BASE_H