2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef _incl_HPHP_RUNTIME_VM_NATIVE_DATA_H
17 #define _incl_HPHP_RUNTIME_VM_NATIVE_DATA_H
19 #include "hphp/runtime/base/types.h"
21 namespace HPHP
{ namespace Native
{
22 //////////////////////////////////////////////////////////////////////////////
25 struct NativeDataInfo
{
26 typedef void (*InitFunc
)(ObjectData
*obj
);
27 typedef void (*CopyFunc
)(ObjectData
*dest
, ObjectData
*src
);
28 typedef void (*DestroyFunc
)(ObjectData
*obj
);
29 typedef void (*SweepFunc
)(ObjectData
*sweep
);
33 InitFunc init
; // new Object
34 CopyFunc copy
; // clone $obj
35 DestroyFunc destroy
; // unset($obj)
36 SweepFunc sweep
; // sweep $obj
38 void setObjectDataAttribute(uint16_t attr
) {
43 NativeDataInfo
* getNativeDataInfo(const StringData
* name
);
44 size_t getNativeDataSize(const Class
* cls
);
47 T
* data(ObjectData
*obj
) {
48 auto node
= reinterpret_cast<SweepNode
*>(obj
) - 1;
49 return reinterpret_cast<T
*>(node
) - 1;
52 void registerNativeDataInfo(const StringData
* name
,
54 NativeDataInfo::InitFunc init
,
55 NativeDataInfo::CopyFunc copy
,
56 NativeDataInfo::DestroyFunc destroy
,
57 NativeDataInfo::SweepFunc sweep
);
60 void nativeDataInfoInit(ObjectData
* obj
) {
65 typename
std::enable_if
<std::is_assignable
<T
,T
>::value
,
66 void>::type
nativeDataInfoCopy(ObjectData
* dest
, ObjectData
* src
) {
67 *data
<T
>(dest
) = *data
<T
>(src
);
70 // Dummy copy method for classes where the assignment has been deleted
72 typename
std::enable_if
<!std::is_assignable
<T
,T
>::value
,
73 void>::type
nativeDataInfoCopy(ObjectData
* dest
, ObjectData
* src
) {}
76 void nativeDataInfoDestroy(ObjectData
* obj
) {
80 // If the NDI class has a void sweep() method,
81 // call it during sweep, otherwise call ~T()
82 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(hasSweep
, sweep
);
85 typename
std::enable_if
<hasSweep
<T
,void ()>::value
,
86 void>::type
nativeDataInfoSweep(ObjectData
* obj
) {
87 data
<T
>(obj
)->sweep();
91 typename
std::enable_if
<!hasSweep
<T
,void ()>::value
,
92 void>::type
nativeDataInfoSweep(ObjectData
* obj
) {
98 // Skipping the ctor/dtor is generally a bad idea
99 // since memory props won't get setup/torn-down
105 void registerNativeDataInfo(const StringData
* name
,
107 registerNativeDataInfo(name
, sizeof(T
),
108 &nativeDataInfoInit
<T
>,
109 (flags
& NDIFlags::NO_COPY
)
110 ? nullptr : &nativeDataInfoCopy
<T
>,
111 &nativeDataInfoDestroy
<T
>,
112 (flags
& NDIFlags::NO_SWEEP
)
113 ? nullptr : &nativeDataInfoSweep
<T
>);
116 ObjectData
* nativeDataInstanceCtor(Class
* cls
);
117 void nativeDataInstanceCopy(ObjectData
* dest
, ObjectData
*src
);
118 void nativeDataInstanceDtor(ObjectData
* obj
, const Class
* cls
);
120 void sweepNativeData();
122 //////////////////////////////////////////////////////////////////////////////
123 }} // namespace HPHP::Native
125 #endif // _incl_HPHP_RUNTIME_VM_NATIVE_DATA_H