Make NativeDataInfo handlers optional
[hiphop-php.git] / hphp / runtime / vm / native-data.h
blobed1e7a07189d7c2f9e6b1d183cd2629de33b8f48
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 //////////////////////////////////////////////////////////////////////////////
23 // Class NativeData
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);
31 size_t sz;
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);
41 template<class T>
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,
48 size_t sz,
49 NativeDataInfo::InitFunc init,
50 NativeDataInfo::CopyFunc copy,
51 NativeDataInfo::DestroyFunc destroy,
52 NativeDataInfo::SweepFunc sweep);
54 template<class T>
55 void nativeDataInfoInit(ObjectData* obj) {
56 new (data<T>(obj)) T;
59 template<class T>
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
66 template<class T>
67 typename std::enable_if<!std::is_assignable<T,T>::value,
68 void>::type nativeDataInfoCopy(ObjectData* dest, ObjectData* src) {}
70 template<class T>
71 void nativeDataInfoDestroy(ObjectData* obj) {
72 data<T>(obj)->~T();
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);
79 template<class T>
80 typename std::enable_if<hasSweep<T,void ()>::value,
81 void>::type nativeDataInfoSweep(ObjectData* obj) {
82 data<T>(obj)->sweep();
85 template<class T>
86 typename std::enable_if<!hasSweep<T,void ()>::value,
87 void>::type nativeDataInfoSweep(ObjectData* obj) {
88 data<T>(obj)->~T();
91 enum NDIFlags {
92 NONE = 0,
93 // Skipping the ctor/dtor is generally a bad idea
94 // since memory props won't get setup/torn-down
95 NO_COPY = (1<<0),
96 NO_SWEEP = (1<<1),
99 template<class T>
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