Refactor back ends into implementations of BackEnd.
[hiphop-php.git] / hphp / runtime / vm / named-entity.h
blobc39c81c7d08e3614e0674ee2ed5dc10c0a395566
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_VM_NAMED_ENTITY_H_
18 #define incl_HPHP_VM_NAMED_ENTITY_H_
20 #include "hphp/runtime/base/rds.h"
21 #include "hphp/runtime/base/string-data.h"
23 #include "hphp/runtime/vm/type-alias.h"
25 #include "hphp/util/atomic.h"
27 #include "folly/AtomicHashMap.h"
29 namespace HPHP {
31 class Class;
32 class Func;
34 //////////////////////////////////////////////////////////////////////
37 * NamedEntity represents a user-defined name that may map to
38 * different objects in different requests. Classes and functions are
39 * in separate namespaces, so we have a targetcache offset for
40 * resolving each.
42 * Classes and typedefs are in the same namespace when we're naming
43 * types, but different namespaces at sites that allocate classes. If
44 * a typedef is defined for a given name, we'll cache it in each
45 * request at m_cachedTypedef. Classes are always cached at
46 * m_cachedClass.
48 struct NamedEntity {
49 explicit NamedEntity()
50 : m_cachedClass(RDS::kInvalidHandle)
51 , m_cachedFunc(RDS::kInvalidHandle)
52 , m_cachedTypeAlias(RDS::kInvalidHandle)
53 , m_clsList(nullptr)
56 NamedEntity(NamedEntity&& ne)
57 : m_cachedClass(ne.m_cachedClass)
58 , m_cachedFunc(ne.m_cachedFunc)
59 , m_cachedTypeAlias(ne.m_cachedTypeAlias)
60 , m_clsList(nullptr)
62 m_clsList.store(ne.m_clsList.load(std::memory_order_acquire),
63 std::memory_order_release);
66 mutable RDS::Link<Class*> m_cachedClass;
67 mutable RDS::Link<Func*> m_cachedFunc;
68 mutable RDS::Link<TypeAliasReq> m_cachedTypeAlias;
71 * Get the RDS::Handle that caches this Class*, creating a
72 * (non-persistent) one if it doesn't exist yet.
74 RDS::Handle getClassHandle() const {
75 m_cachedClass.bind();
76 return m_cachedClass.handle();
80 * Get the RDS::Handle that caches this Func*, creating a
81 * (non-persistent) one if it doesn't exist yet.
83 RDS::Handle getFuncHandle() const {
84 m_cachedFunc.bind();
85 return m_cachedFunc.handle();
88 void setCachedFunc(Func *f);
89 Func* getCachedFunc() const;
91 void setCachedClass(Class* c);
92 Class* getCachedClass() const;
94 const TypeAliasReq* getCachedTypeAlias() const;
95 void setCachedTypeAlias(const TypeAliasReq&);
97 Class* clsList() const {
98 return m_clsList.load(std::memory_order_acquire);
101 // Call while holding s_classesMutex. Add or remove classes from
102 // the list.
103 void pushClass(Class* cls);
104 void removeClass(Class* goner);
106 private:
107 std::atomic<Class*> m_clsList;
110 //////////////////////////////////////////////////////////////////////
113 * Lookup a TypeAliasReq for the supplied NamedEntity (which must be
114 * the NamedEntity for `name'), if necessary invoking autoload for
115 * types but not classes.
117 const TypeAliasReq* getTypeAliasWithAutoload(const NamedEntity* ne,
118 const StringData* name);
120 //////////////////////////////////////////////////////////////////////
122 struct ahm_string_data_isame {
123 bool operator()(const StringData *s1, const StringData *s2) const {
124 // ahm uses -1, -2, -3 as magic values
125 return int64_t(s1) > 0 && s1->isame(s2);
129 typedef folly::AtomicHashMap<
130 const StringData*,
131 NamedEntity,
132 string_data_hash,
133 ahm_string_data_isame
134 > NamedEntityMap;
135 typedef std::pair<const StringData*,const NamedEntity*> NamedEntityPair;
137 //////////////////////////////////////////////////////////////////////
141 #endif