/* +----------------------------------------------------------------------+ | HipHop for PHP | +----------------------------------------------------------------------+ | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) | | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) | +----------------------------------------------------------------------+ | This source file is subject to version 2.00 of the Zend license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.zend.com/license/2_00.txt. | | If you did not receive a copy of the Zend license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@zend.com so we can mail you a copy immediately. | +----------------------------------------------------------------------+ */ #ifndef incl_HPHP_CPP_BASE_SHARED_SHARED_STRING_H_ #define incl_HPHP_CPP_BASE_SHARED_SHARED_STRING_H_ #include "hphp/util/base.h" #include "hphp/runtime/base/smart_ptr.h" #include "tbb/concurrent_hash_map.h" #include "tbb/atomic.h" #include "hphp/util/atomic.h" #include "hphp/util/hash.h" namespace HPHP { /////////////////////////////////////////////////////////////////////////////// /** * Interned immutable strings sharable across threads. */ class SharedStringData { public: explicit SharedStringData(const std::string &data); void incRefCount() const { m_count.fetch_and_increment(); } int decRefCount() const; void release(); const std::string &getString() const; typedef tbb::concurrent_hash_map InternMap; static void Create(InternMap::accessor &acc, const std::string &data); protected: mutable tbb::atomic m_count; const std::string m_data; static InternMap s_intern; }; class SharedString : public SmartPtr { public: SharedString() {} /* implicit */ SharedString(SharedStringData *px) : SmartPtr(px) {} /* implicit */ SharedString(const SharedString &src) : SmartPtr(src) {} /* implicit */ SharedString(const std::string &data) { operator=(data); } /* implicit */ SharedString(const char *data) { operator=(data); } SharedString &operator=(const std::string &data); SharedString &operator=(const char *data) { return operator=(std::string(data)); } }; struct shared_string_eq { bool operator()(const SharedString &s1, const SharedString &s2) const { // Because pointers and strings are 1-1, can just compare the ptr. return s1.get() == s2.get(); } }; struct shared_string_hash { size_t operator()(const SharedString &s) const { // Because pointers and strings are 1-1, can just hash the ptr. return hash_int64((int64_t)s.get()); } }; template class hphp_shared_string_map : public hphp_hash_map { }; /////////////////////////////////////////////////////////////////////////////// } #endif // incl_HPHP_CPP_BASE_SHARED_SHARED_STRING_H_