diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-01-08 18:27:02 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-08 18:31:22 +0100 |
commit | 6dee642bad3c7b9c126a92d4a62eaae4fe3d713b (patch) | |
tree | 479caba6f75162a5c7f3e667dbc16645333fa4fb /ffi/src | |
parent | 0ac9db8b1e039b498169d63fcb01cbd11ee204ec (diff) |
ffi: Add and use ffi_free!.
Diffstat (limited to 'ffi/src')
-rw-r--r-- | ffi/src/core.rs | 15 | ||||
-rw-r--r-- | ffi/src/error.rs | 4 | ||||
-rw-r--r-- | ffi/src/lib.rs | 15 | ||||
-rw-r--r-- | ffi/src/net.rs | 7 | ||||
-rw-r--r-- | ffi/src/openpgp/fingerprint.rs | 5 | ||||
-rw-r--r-- | ffi/src/openpgp/keyid.rs | 5 | ||||
-rw-r--r-- | ffi/src/openpgp/mod.rs | 37 | ||||
-rw-r--r-- | ffi/src/openpgp/packet_pile.rs | 7 | ||||
-rw-r--r-- | ffi/src/openpgp/tpk.rs | 24 | ||||
-rw-r--r-- | ffi/src/store.rs | 40 |
10 files changed, 42 insertions, 117 deletions
diff --git a/ffi/src/core.rs b/ffi/src/core.rs index faa007c4..e31513a5 100644 --- a/ffi/src/core.rs +++ b/ffi/src/core.rs @@ -108,10 +108,7 @@ pub extern "system" fn sq_context_new(domain: *const c_char, /// Frees a context. #[no_mangle] pub extern "system" fn sq_context_free(context: *mut Context) { - if context.is_null() { return } - unsafe { - drop(Box::from_raw(context)); - } + ffi_free!(context) } /// Creates a Context that can be configured. @@ -295,10 +292,7 @@ pub extern "system" fn sq_reader_from_bytes(buf: *const uint8_t, /// Frees a reader. #[no_mangle] pub extern "system" fn sq_reader_free(reader: *mut Box<Read>) { - if reader.is_null() { return } - unsafe { - drop(Box::from_raw(reader)); - } + ffi_free!(reader) } /// Reads up to `len` bytes into `buf`. @@ -412,10 +406,7 @@ impl Write for WriterAlloc { /// Frees a writer. #[no_mangle] pub extern "system" fn sq_writer_free(writer: *mut Box<Write>) { - if writer.is_null() { return } - unsafe { - drop(Box::from_raw(writer)); - } + ffi_free!(writer) } /// Writes up to `len` bytes of `buf` into `writer`. diff --git a/ffi/src/error.rs b/ffi/src/error.rs index 96a64c96..05a1998a 100644 --- a/ffi/src/error.rs +++ b/ffi/src/error.rs @@ -11,9 +11,7 @@ use sequoia_core as core; /// Frees an error. #[no_mangle] pub extern "system" fn sq_error_free(error: *mut failure::Error) { - if ! error.is_null() { - unsafe { drop(Box::from_raw(error)) } - } + ffi_free!(error) } /// Returns the error message. diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index ebb74a6a..c9410816 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -126,6 +126,21 @@ extern crate sequoia_store; use std::collections::hash_map::{DefaultHasher, RandomState}; use std::hash::BuildHasher; +/* Canonical free(). */ + +/// Transfers ownership from C to Rust, then frees the object. +/// +/// NOP if called with NULL. +macro_rules! ffi_free { + ($name:ident) => {{ + if ! $name.is_null() { + unsafe { + drop(Box::from_raw($name)) + } + } + }}; +} + /// Like try! for ffi glue. /// /// Evaluates the given expression. On success, evaluate to diff --git a/ffi/src/net.rs b/ffi/src/net.rs index 416cedcf..bd98bca1 100644 --- a/ffi/src/net.rs +++ b/ffi/src/net.rs @@ -108,12 +108,7 @@ pub extern "system" fn sq_keyserver_sks_pool(ctx: Option<&mut Context>) /// Frees a keyserver object. #[no_mangle] pub extern "system" fn sq_keyserver_free(ks: *mut KeyServer) { - if ks.is_null() { - return - } - unsafe { - drop(Box::from_raw(ks)); - } + ffi_free!(ks) } /// Retrieves the key with the given `keyid`. diff --git a/ffi/src/openpgp/fingerprint.rs b/ffi/src/openpgp/fingerprint.rs index 256156b6..3c2a38ad 100644 --- a/ffi/src/openpgp/fingerprint.rs +++ b/ffi/src/openpgp/fingerprint.rs @@ -41,10 +41,7 @@ pub extern "system" fn sq_fingerprint_from_hex(hex: *const c_char) /// Frees a sq_fingerprint_t. #[no_mangle] pub extern "system" fn sq_fingerprint_free(fp: *mut Fingerprint) { - if fp.is_null() { return } - unsafe { - drop(Box::from_raw(fp)); - } + ffi_free!(fp) } /// Clones the Fingerprint. diff --git a/ffi/src/openpgp/keyid.rs b/ffi/src/openpgp/keyid.rs index ed4bb11e..5d739609 100644 --- a/ffi/src/openpgp/keyid.rs +++ b/ffi/src/openpgp/keyid.rs @@ -36,10 +36,7 @@ pub extern "system" fn sq_keyid_from_hex(id: *const c_char) -> *mut KeyID { /// Frees an `KeyID` object. #[no_mangle] pub extern "system" fn sq_keyid_free(keyid: *mut KeyID) { - if keyid.is_null() { return } - unsafe { - drop(Box::from_raw(keyid)); - } + ffi_free!(keyid) } /// Clones the KeyID. diff --git a/ffi/src/openpgp/mod.rs b/ffi/src/openpgp/mod.rs index 05afe9c0..9dc9548c 100644 --- a/ffi/src/openpgp/mod.rs +++ b/ffi/src/openpgp/mod.rs @@ -122,10 +122,7 @@ pub extern "system" fn sq_revocation_status_variant( pub extern "system" fn sq_revocation_status_free( rs: *mut RevocationStatus) { - if rs.is_null() { return }; - unsafe { - drop(Box::from_raw(rs)) - }; + ffi_free!(rs) } /* TSK */ @@ -158,12 +155,7 @@ pub extern "system" fn sq_tsk_new(ctx: Option<&mut Context>, /// Frees the TSK. #[no_mangle] pub extern "system" fn sq_tsk_free(tsk: *mut TSK) { - if tsk.is_null() { - return - } - unsafe { - drop(Box::from_raw(tsk)); - } + ffi_free!(tsk) } /// Returns a reference to the corresponding TPK. @@ -202,10 +194,7 @@ pub extern "system" fn sq_tsk_serialize(ctx: Option<&mut Context>, /// Frees the Packet. #[no_mangle] pub extern "system" fn sq_packet_free(p: *mut Packet) { - if p.is_null() { return } - unsafe { - drop(Box::from_raw(p)); - } + ffi_free!(p) } /// Returns the `Packet's` corresponding OpenPGP tag. @@ -242,10 +231,7 @@ pub extern "system" fn sq_packet_kind(p: Option<&Packet>) /// Frees the Signature. #[no_mangle] pub extern "system" fn sq_signature_free(s: *mut Signature) { - if s.is_null() { return } - unsafe { - drop(Box::from_raw(s)); - } + ffi_free!(s) } /// Converts the signature to a packet. @@ -721,19 +707,13 @@ pub extern "system" fn sq_packet_parser_from_bytes pub extern "system" fn sq_packet_parser_result_free( ppr: *mut PacketParserResult) { - if ppr.is_null() { return } - unsafe { - drop(Box::from_raw(ppr)); - } + ffi_free!(ppr) } /// Frees the packet parser. #[no_mangle] pub extern "system" fn sq_packet_parser_free(pp: *mut PacketParser) { - if pp.is_null() { return } - unsafe { - drop(Box::from_raw(pp)); - } + ffi_free!(pp) } /// Frees the packet parser EOF object. @@ -749,10 +729,7 @@ pub extern "system" fn sq_packet_parser_eof_is_message( /// Frees the packet parser EOF object. #[no_mangle] pub extern "system" fn sq_packet_parser_eof_free(eof: *mut PacketParserEOF) { - if eof.is_null() { return } - unsafe { - drop(Box::from_raw(eof)); - } + ffi_free!(eof) } /// Returns a reference to the packet that is being parsed. diff --git a/ffi/src/openpgp/packet_pile.rs b/ffi/src/openpgp/packet_pile.rs index fd87130c..552152db 100644 --- a/ffi/src/openpgp/packet_pile.rs +++ b/ffi/src/openpgp/packet_pile.rs @@ -74,12 +74,7 @@ pub extern "system" fn sq_packet_pile_from_bytes(ctx: Option<&mut Context>, /// Frees the packet_pile. #[no_mangle] pub extern "system" fn sq_packet_pile_free(packet_pile: *mut PacketPile) { - if packet_pile.is_null() { - return - } - unsafe { - drop(Box::from_raw(packet_pile)); - } + ffi_free!(packet_pile) } /// Clones the PacketPile. diff --git a/ffi/src/openpgp/tpk.rs b/ffi/src/openpgp/tpk.rs index eaa8ef40..49d6513b 100644 --- a/ffi/src/openpgp/tpk.rs +++ b/ffi/src/openpgp/tpk.rs @@ -107,12 +107,7 @@ pub extern "system" fn sq_tpk_from_packet_parser(ctx: Option<&mut Context>, /// Frees the TPK. #[no_mangle] pub extern "system" fn sq_tpk_free(tpk: *mut TPK) { - if tpk.is_null() { - return - } - unsafe { - drop(Box::from_raw(tpk)); - } + ffi_free!(tpk) } /// Clones the TPK. @@ -499,10 +494,7 @@ pub extern "system" fn sq_tpk_user_id_binding_iter(tpk: Option<&TPK>) pub extern "system" fn sq_user_id_binding_iter_free( iter: *mut UserIDBindingIter) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } /// Returns the next `UserIDBinding`. @@ -542,10 +534,7 @@ pub extern "system" fn sq_tpk_key_iter(tpk: Option<&TPK>) pub extern "system" fn sq_tpk_key_iter_free( iter: *mut KeyIterWrapper) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } /// Returns the next key. Returns NULL if there are no more elements. @@ -626,12 +615,7 @@ pub extern "system" fn sq_tpk_builder_autocrypt() -> *mut TPKBuilder { #[no_mangle] pub extern "system" fn sq_tpk_builder_free(tpkb: *mut TPKBuilder) { - if tpkb.is_null() { - return - } - unsafe { - drop(Box::from_raw(tpkb)); - } + ffi_free!(tpkb) } /// Sets the encryption and signature algorithms for primary and all diff --git a/ffi/src/store.rs b/ffi/src/store.rs index a18239f7..4a248a35 100644 --- a/ffi/src/store.rs +++ b/ffi/src/store.rs @@ -97,10 +97,7 @@ pub extern "system" fn sq_store_iter_next(iter: Option<&mut StoreIter>, /// Frees a sq_store_iter_t. #[no_mangle] pub extern "system" fn sq_store_iter_free(iter: *mut StoreIter) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } /// Lists all keys in the common key pool. @@ -145,10 +142,7 @@ pub extern "system" fn sq_key_iter_next(iter: Option<&mut KeyIter>, /// Frees a sq_key_iter_t. #[no_mangle] pub extern "system" fn sq_key_iter_free(iter: *mut KeyIter) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } @@ -190,10 +184,7 @@ pub extern "system" fn sq_log_iter_next(iter: Option<&mut LogIter>) /// Frees a sq_log_iter_t. #[no_mangle] pub extern "system" fn sq_log_iter_free(iter: *mut LogIter) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } /// Opens a store. @@ -224,10 +215,7 @@ pub extern "system" fn sq_store_open(ctx: Option<&mut Context>, /// Frees a sq_store_t. #[no_mangle] pub extern "system" fn sq_store_free(store: *mut Store) { - if store.is_null() { return }; - unsafe { - drop(Box::from_raw(store)) - }; + ffi_free!(store) } /// Adds a key identified by fingerprint to the store. @@ -365,10 +353,7 @@ pub extern "system" fn sq_binding_iter_next(iter: Option<&mut BindingIter>, /// Frees a sq_binding_iter_t. #[no_mangle] pub extern "system" fn sq_binding_iter_free(iter: *mut BindingIter) { - if iter.is_null() { return }; - unsafe { - drop(Box::from_raw(iter)) - }; + ffi_free!(iter) } /// Lists all log entries related to this store. @@ -385,19 +370,13 @@ pub extern "system" fn sq_store_log(ctx: Option<&mut Context>, /// Frees a sq_binding_t. #[no_mangle] pub extern "system" fn sq_binding_free(binding: *mut Binding) { - if binding.is_null() { return }; - unsafe { - drop(Box::from_raw(binding)) - }; + ffi_free!(binding) } /// Frees a sq_key_t. #[no_mangle] pub extern "system" fn sq_key_free(key: *mut Key) { - if key.is_null() { return }; - unsafe { - drop(Box::from_raw(key)) - }; + ffi_free!(key) } /// Frees a sq_log_t. @@ -587,10 +566,7 @@ pub extern "system" fn sq_key_log(ctx: Option<&mut Context>, /// Frees a sq_stats_t. #[no_mangle] pub extern "system" fn sq_stats_free(stats: *mut Stats) { - if stats.is_null() { return }; - unsafe { - drop(Box::from_raw(stats)) - }; + ffi_free!(stats) } /// Counter and timestamps. |