1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is the Python XPCOM language bindings.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * ActiveState Tool Corp.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2000
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Mark Hammond <MarkH@ActiveState.com> (original author)
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | // PyGWeakReference - implements weak references for gateways.
|
---|
39 | //
|
---|
40 | // This code is part of the XPCOM extensions for Python.
|
---|
41 | //
|
---|
42 | // Written November 2000 by Mark Hammond.
|
---|
43 | //
|
---|
44 | // Based heavily on the Python COM support, which is
|
---|
45 | // (c) Mark Hammond and Greg Stein.
|
---|
46 | //
|
---|
47 | // (c) 2000, ActiveState corp.
|
---|
48 |
|
---|
49 | #include "PyXPCOM_std.h"
|
---|
50 |
|
---|
51 | #include <iprt/asm.h>
|
---|
52 |
|
---|
53 | PyXPCOM_GatewayWeakReference::PyXPCOM_GatewayWeakReference( PyG_Base *base )
|
---|
54 | {
|
---|
55 | m_pBase = base;
|
---|
56 |
|
---|
57 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
58 | // bloat view uses 40 chars - stick "(WR)" at the end of this position.
|
---|
59 | strncpy(refcntLogRepr, m_pBase->refcntLogRepr, sizeof(refcntLogRepr));
|
---|
60 | refcntLogRepr[sizeof(refcntLogRepr)-1] = '\0';
|
---|
61 | char *dest = refcntLogRepr + ((strlen(refcntLogRepr) > 36) ? 36 : strlen(refcntLogRepr));
|
---|
62 | strcpy(dest, "(WR)");
|
---|
63 | #endif // NS_BUILD_REFCNT_LOGGING
|
---|
64 | }
|
---|
65 |
|
---|
66 | PyXPCOM_GatewayWeakReference::~PyXPCOM_GatewayWeakReference()
|
---|
67 | {
|
---|
68 | // Simply zap my reference to the gateway!
|
---|
69 | // No need to zap my gateway's reference to me, as
|
---|
70 | // it already holds a reference, so if we are destructing,
|
---|
71 | // then it can't possibly hold one.
|
---|
72 | m_pBase = NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | nsrefcnt
|
---|
76 | PyXPCOM_GatewayWeakReference::AddRef(void)
|
---|
77 | {
|
---|
78 | nsrefcnt cnt = (nsrefcnt) ASMAtomicIncS32((volatile int32_t *)&mRefCnt);
|
---|
79 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
80 | NS_LOG_ADDREF(this, cnt, refcntLogRepr, sizeof(*this));
|
---|
81 | #endif
|
---|
82 | return cnt;
|
---|
83 | }
|
---|
84 |
|
---|
85 | nsrefcnt
|
---|
86 | PyXPCOM_GatewayWeakReference::Release(void)
|
---|
87 | {
|
---|
88 | nsrefcnt cnt = (nsrefcnt) ASMAtomicDecS32((volatile int32_t *)&mRefCnt);
|
---|
89 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
90 | NS_LOG_RELEASE(this, cnt, refcntLogRepr);
|
---|
91 | #endif
|
---|
92 | if ( cnt == 0 )
|
---|
93 | delete this;
|
---|
94 | return cnt;
|
---|
95 | }
|
---|
96 |
|
---|
97 | NS_IMPL_THREADSAFE_QUERY_INTERFACE1(PyXPCOM_GatewayWeakReference, nsIWeakReference)
|
---|
98 |
|
---|
99 | NS_IMETHODIMP
|
---|
100 | PyXPCOM_GatewayWeakReference::QueryReferent(REFNSIID iid, void * *ret)
|
---|
101 | {
|
---|
102 | {
|
---|
103 | // Temp scope for lock. We can't hold the lock during
|
---|
104 | // a QI, as this may itself need the lock.
|
---|
105 | // Make sure our object isn't dieing right now on another thread.
|
---|
106 | CEnterLeaveXPCOMFramework _celf;
|
---|
107 | if (m_pBase == NULL)
|
---|
108 | return NS_ERROR_NULL_POINTER;
|
---|
109 | m_pBase->AddRef(); // Can't die while we have a ref.
|
---|
110 | } // end of lock scope - lock unlocked.
|
---|
111 | nsresult nr = m_pBase->QueryInterface(iid, ret);
|
---|
112 | m_pBase->Release();
|
---|
113 | return nr;
|
---|
114 | }
|
---|