Bug 1792920, part 3 - Make Rust use a new MozExternalRefCountType instead of XPIDL's nsrefcnt. r=nika

The remaining uses of XPIDL's definition of nsrefcnt in Rust are all
now related to the return type of nsISupport's AddRef and Release
methods. The C++ name of the return types is MozExternalRefCountType,
not nsrefcnt (which is something else entirely), so use a more accurate
name.

I can't use the C++ definition, because it is in C++, and I eventually
want to remove the XPIDL definition, because it isn't accurate for C++,
so in this patch I'll make a specific one for Rust, so the code is a
little easier to read.

Differential Revision: https://phabricator.services.mozilla.com/D159322
This commit is contained in:
Andrew McCreight 2022-10-17 16:09:21 +00:00
Родитель f5c69690d6
Коммит 3d1c6d4ceb
2 изменённых файлов: 14 добавлений и 10 удалений

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use crate::interfaces::{nsISupports, nsrefcnt};
use crate::interfaces::{nsISupports};
use libc;
use nserror::{nsresult, NS_OK};
use std::cell::Cell;
@ -15,6 +15,10 @@ use std::ptr::{self, NonNull};
use std::sync::atomic::{self, AtomicUsize, Ordering};
use threadbound::ThreadBound;
// This should match the definition in mfbt/RefCountType.h, modulo the delicate
// effort at maintaining binary compatibility with Microsoft COM on Windows.
pub type MozExternalRefCountType = u32;
/// A trait representing a type which can be reference counted invasively.
/// The object is responsible for freeing its backing memory when its
/// reference count reaches 0.
@ -262,7 +266,7 @@ impl Refcnt {
/// Increment the reference count. Returns the new reference count. This is
/// unsafe as modifying this value can cause a use-after-free.
pub unsafe fn inc(&self) -> nsrefcnt {
pub unsafe fn inc(&self) -> MozExternalRefCountType {
// XXX: Checked add?
let new = self.0.get() + 1;
self.0.set(new);
@ -271,7 +275,7 @@ impl Refcnt {
/// Decrement the reference count. Returns the new reference count. This is
/// unsafe as modifying this value can cause a use-after-free.
pub unsafe fn dec(&self) -> nsrefcnt {
pub unsafe fn dec(&self) -> MozExternalRefCountType {
// XXX: Checked sub?
let new = self.0.get() - 1;
self.0.set(new);
@ -301,14 +305,14 @@ impl AtomicRefcnt {
/// Increment the reference count. Returns the new reference count. This is
/// unsafe as modifying this value can cause a use-after-free.
pub unsafe fn inc(&self) -> nsrefcnt {
pub unsafe fn inc(&self) -> MozExternalRefCountType {
let result = self.0.fetch_add(1, Ordering::Relaxed) + 1;
result.try_into().unwrap()
}
/// Decrement the reference count. Returns the new reference count. This is
/// unsafe as modifying this value can cause a use-after-free.
pub unsafe fn dec(&self) -> nsrefcnt {
pub unsafe fn dec(&self) -> MozExternalRefCountType {
let result = self.0.fetch_sub(1, Ordering::Release) - 1;
if result == 0 {
// We're going to destroy the object on this thread, so we need

Просмотреть файл

@ -68,8 +68,8 @@
//!
//! ```ignore
//! // Automatic nsISupports implementation
//! unsafe fn AddRef(&self) -> nsrefcnt;
//! unsafe fn Release(&self) -> nsrefcnt;
//! unsafe fn AddRef(&self) -> MozExternalRefCountType;
//! unsafe fn Release(&self) -> MozExternalRefCountType;
//! unsafe fn QueryInterface(&self, uuid: &nsIID, result: *mut *mut libc::c_void) -> nsresult;
//!
//! // Allocates and initializes a new instance of this type. The values will
@ -442,7 +442,7 @@ fn gen_casts(
let qi = quote! {
#base_qi
if *uuid == #base_name::IID {
// Implement QueryInterface in terms of coersions.
// Implement QueryInterface in terms of coercions.
self.addref();
*result = self.coerce::<#base_name>()
as *const #base_name
@ -710,7 +710,7 @@ fn xpcom_impl(args: AttributeArgs, template: ItemStruct) -> Result<TokenStream,
}
/// Automatically generated implementation of AddRef for nsISupports.
#vis unsafe fn AddRef(&self) -> ::xpcom::interfaces::nsrefcnt {
#vis unsafe fn AddRef(&self) -> ::xpcom::MozExternalRefCountType {
let new = self.__refcnt.inc();
::xpcom::trace_refcnt::NS_LogAddRef(
self as *const _ as *mut ::xpcom::reexports::libc::c_void,
@ -722,7 +722,7 @@ fn xpcom_impl(args: AttributeArgs, template: ItemStruct) -> Result<TokenStream,
}
/// Automatically generated implementation of Release for nsISupports.
#vis unsafe fn Release(&self) -> ::xpcom::interfaces::nsrefcnt {
#vis unsafe fn Release(&self) -> ::xpcom::MozExternalRefCountType {
let new = self.__refcnt.dec();
::xpcom::trace_refcnt::NS_LogRelease(
self as *const _ as *mut ::xpcom::reexports::libc::c_void,