Chromium Code Reviews| Index: public/platform/WebPrivateHeapPtr.h |
| diff --git a/public/platform/WebPrivateHeapPtr.h b/public/platform/WebPrivateHeapPtr.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bfd246b3947579f6ef875275f5120e5e32dfa5d3 |
| --- /dev/null |
| +++ b/public/platform/WebPrivateHeapPtr.h |
| @@ -0,0 +1,182 @@ |
| +/* |
| + * Copyright (c) 2014, Opera Software ASA. All rights reserved. |
|
haraken
2014/02/17 09:04:57
Can you use the 4-line copyright?
|
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions |
| + * are met: |
| + * 1. Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * 2. Redistributions in binary form must reproduce the above copyright |
| + * notice, this list of conditions and the following disclaimer in the |
| + * documentation and/or other materials provided with the distribution. |
| + * 3. Neither the name of Opera Software ASA nor the names of its |
| + * contributors may be used to endorse or promote products derived |
| + * from this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#ifndef WebPrivateHeapPtr_h |
| +#define WebPrivateHeapPtr_h |
| + |
| +#include "WebCommon.h" |
| + |
| +#if INSIDE_BLINK |
|
haraken
2014/02/17 09:04:57
What's the difference between INSIDE_BLINK and BLI
sof
2014/02/17 10:22:26
Good question; I can't discern a pattern within th
tkent
2014/02/17 23:42:46
INSIDE_BLINK means that it's in either of webkit.d
sof
2014/02/18 10:14:17
Excellent clarification & info, many thanks :)
On
|
| +#include "heap/Handle.h" |
| +#include "wtf/PassRefPtr.h" |
| +#endif |
| + |
| +namespace blink { |
| + |
| +// A version of WebPrivatePtr that holds a heap-residing object, |
| +// but without exposing how that is done within Blink. If no |
| +// heap support (i.e., no Oilpan), WebPrivateHeapPtr is equal |
| +// to WebPrivatePtr and the object will be reference counted. |
|
haraken
2014/02/17 09:04:57
"heap" is ambiguous. We want to use "garbage-colle
|
| +// |
| +// See WebPrivatePtr.h comment for how to make use of WebPrivateHeapPtr; |
| +// same pattern applies, but for one detail. Instead of declaring & defining, |
| +// |
| +// #if INSIDE_BLINK |
| +// WebFoo(const WTF::PassRefPtr<WebCore::Foo>&); |
| +// #endif |
| +// |
| +// provide |
| +// |
| +// #if INSIDE_BLINK |
| +// WebFoo(const WTF::PassRefPtrWillBeRawPtr<WebCore::Foo>&); |
| +// #endif |
| +// |
| +template <typename T> |
| +class WebPrivateHeapPtr { |
| +public: |
| + WebPrivateHeapPtr() : m_ptr(0) { } |
| + ~WebPrivateHeapPtr() |
| + { |
| + // We don't destruct the object pointed by m_ptr here because we don't |
| + // want to expose destructors of core classes to embedders. We should |
| + // call reset() manually in destructors of classes with WebPrivateHeapPtr |
| + // members. |
| + BLINK_ASSERT(!m_ptr); |
| + } |
| + |
| + bool isNull() const { return !m_ptr; } |
| + |
| +#if INSIDE_BLINK |
| + WebPrivateHeapPtr(const PassRefPtrWillBeRawPtr<T>& p) |
| +# if !ENABLE(OILPAN) |
| + : m_ptr(p.leakRef()) |
| +# endif |
| + { |
| +# if ENABLE(OILPAN) |
| + assign(new WebCore::Persistent<T>(p)); |
|
haraken
2014/02/17 09:04:57
How about calling 'new Persistent' in assign? assi
sof
2014/02/17 09:23:21
Yes, let's do that.
|
| +# endif |
| + } |
| + |
| + void reset() |
| + { |
| +# if ENABLE(OILPAN) |
| + if (this->m_ptr) { |
| + delete reinterpret_cast<WebCore::Persistent<T>*>(this->m_ptr); |
| + this->m_ptr = 0; |
| + } |
| +# else |
| + assign(0); |
| +# endif |
| + } |
| + |
| + WebPrivateHeapPtr<T>& operator=(const WebPrivateHeapPtr<T>& other) |
| + { |
| +# if ENABLE(OILPAN) |
| + assign(new WebCore::Persistent<T>(other.get())); |
| +# else |
| + T* p = other.m_ptr; |
| + if (p) |
| + p->ref(); |
| + assign(p); |
| +# endif |
| + return *this; |
| + } |
| + |
| + WebPrivateHeapPtr<T>& operator=(WebPrivateHeapPtr<T>& other) |
| + { |
| + assign(new WebCore::Persistent<T>(other.get())); |
| + return *this; |
| + } |
| + |
| + WebPrivateHeapPtr<T>& operator=(const PassRefPtrWillBeRawPtr<T>& p) |
| + { |
| +# if ENABLE(OILPAN) |
| + assign(new WebCore::Persistent<T>(p)); |
| +# else |
| + assign(p.leakRef()); |
| +# endif |
| + return *this; |
| + } |
| + |
| + T* get() const |
| + { |
| +# if ENABLE(OILPAN) |
| + if (!m_ptr) |
| + return 0; |
| + return (reinterpret_cast<WebCore::Persistent<T> * >(m_ptr))->get(); |
| +# else |
| + return m_ptr; |
| +# endif |
| + } |
| + |
| + T* operator->() const |
| + { |
| + ASSERT(m_ptr); |
| +# if ENABLE(OILPAN) |
| + return (reinterpret_cast<WebCore::Persistent<T> * >(m_ptr))->get(); |
| +# else |
| + return m_ptr; |
| +# endif |
| + } |
| +#endif |
| + |
| +private: |
| +#if INSIDE_BLINK |
| +# if ENABLE(OILPAN) |
| + void assign(WebCore::Persistent<T>* p) |
| + { |
| + reset(); |
| + m_ptr = reinterpret_cast<T*>(p); |
| + } |
| +# else |
| + void assign(T* p) |
| + { |
| + // p is already ref'd for us by the caller |
| + if (m_ptr) |
| + m_ptr->deref(); |
| + m_ptr = p; |
| + } |
| +# endif |
| +#else |
| + // Disable the assignment operator; we define it above for when |
| + // INSIDE_BLINK is set, but we need to make sure that it is not |
| + // used outside there; the compiler-provided version won't handle reference |
| + // counting properly. |
| + WebPrivateHeapPtr<T>& operator=(const WebPrivateHeapPtr<T>& other); |
| +#endif |
| + // Disable the copy constructor; classes that contain a WebPrivateHeapPtr |
| + // should implement their copy constructor using assign(). |
| + WebPrivateHeapPtr(const WebPrivateHeapPtr<T>&); |
| + |
| + T* m_ptr; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif |