Fix refcounting in arReturn() and stop leaking static strings.
[hiphop-php.git] / hphp / runtime / vm / native-data.h
blobd9a94146b775648dd81d0febca7266bfe40e2c70
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 uint16_t odattrs;
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) {
39 odattrs |= attr;
43 NativeDataInfo* getNativeDataInfo(const StringData* name);
44 size_t getNativeDataSize(const Class* cls);
46 template<class T>
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,
53 size_t sz,
54 NativeDataInfo::InitFunc init,
55 NativeDataInfo::CopyFunc copy,
56 NativeDataInfo::DestroyFunc destroy,
57 NativeDataInfo::SweepFunc sweep);
59 template<class T>
60 void nativeDataInfoInit(ObjectData* obj) {
61 new (data<T>(obj)) T;
64 template<class T>
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
71 template<class T>
72 typename std::enable_if<!std::is_assignable<T,T>::value,
73 void>::type nativeDataInfoCopy(ObjectData* dest, ObjectData* src) {}
75 template<class T>
76 void nativeDataInfoDestroy(ObjectData* obj) {
77 data<T>(obj)->~T();
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);
84 template<class T>
85 typename std::enable_if<hasSweep<T,void ()>::value,
86 void>::type nativeDataInfoSweep(ObjectData* obj) {
87 data<T>(obj)->sweep();
90 template<class T>
91 typename std::enable_if<!hasSweep<T,void ()>::value,
92 void>::type nativeDataInfoSweep(ObjectData* obj) {
93 data<T>(obj)->~T();
96 enum NDIFlags {
97 NONE = 0,
98 // Skipping the ctor/dtor is generally a bad idea
99 // since memory props won't get setup/torn-down
100 NO_COPY = (1<<0),
101 NO_SWEEP = (1<<1),
104 template<class T>
105 void registerNativeDataInfo(const StringData* name,
106 int64_t flags = 0) {
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