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
);
32 InitFunc init
; // new Object
33 CopyFunc copy
; // clone $obj
34 DestroyFunc destroy
; // unset($obj)
35 SweepFunc sweep
; // sweep $obj
38 NativeDataInfo
* getNativeDataInfo(const StringData
* name
);
39 size_t getNativeDataSize(const Class
* cls
);
42 T
* data(ObjectData
*obj
) {
43 auto node
= reinterpret_cast<SweepNode
*>(obj
) - 1;
44 return reinterpret_cast<T
*>(node
) - 1;
47 void registerNativeDataInfo(const StringData
* name
,
49 NativeDataInfo::InitFunc init
,
50 NativeDataInfo::CopyFunc copy
,
51 NativeDataInfo::DestroyFunc destroy
,
52 NativeDataInfo::SweepFunc sweep
);
55 void nativeDataInfoInit(ObjectData
* obj
) {
60 typename
std::enable_if
<std::is_assignable
<T
,T
>::value
,
61 void>::type
nativeDataInfoCopy(ObjectData
* dest
, ObjectData
* src
) {
62 *data
<T
>(dest
) = *data
<T
>(src
);
65 // Dummy copy method for classes where the assignment has been deleted
67 typename
std::enable_if
<!std::is_assignable
<T
,T
>::value
,
68 void>::type
nativeDataInfoCopy(ObjectData
* dest
, ObjectData
* src
) {}
71 void nativeDataInfoDestroy(ObjectData
* obj
) {
75 // If the NDI class has a void sweep() method,
76 // call it during sweep, otherwise call ~T()
77 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(hasSweep
, sweep
);
80 typename
std::enable_if
<hasSweep
<T
,void ()>::value
,
81 void>::type
nativeDataInfoSweep(ObjectData
* obj
) {
82 data
<T
>(obj
)->sweep();
86 typename
std::enable_if
<!hasSweep
<T
,void ()>::value
,
87 void>::type
nativeDataInfoSweep(ObjectData
* obj
) {
93 // Skipping the ctor/dtor is generally a bad idea
94 // since memory props won't get setup/torn-down
100 void registerNativeDataInfo(const StringData
* name
, int64_t flags
= 0) {
101 registerNativeDataInfo(name
, sizeof(T
),
102 &nativeDataInfoInit
<T
>,
103 (flags
& NDIFlags::NO_COPY
)
104 ? nullptr : &nativeDataInfoCopy
<T
>,
105 &nativeDataInfoDestroy
<T
>,
106 (flags
& NDIFlags::NO_SWEEP
)
107 ? nullptr : &nativeDataInfoSweep
<T
>);
110 ObjectData
* nativeDataInstanceCtor(Class
* cls
);
111 void nativeDataInstanceCopy(ObjectData
* dest
, ObjectData
*src
);
112 void nativeDataInstanceDtor(ObjectData
* obj
, const Class
* cls
);
114 void sweepNativeData();
116 //////////////////////////////////////////////////////////////////////////////
117 }} // namespace HPHP::Native
119 #endif // _incl_HPHP_RUNTIME_VM_NATIVE_DATA_H