Add a HHIR-level peephole optimization to reorder CheckTypes
[hiphop-php.git] / hphp / util / string-holder.h
blob05f6421c716e7e2f0efe3fa06b34e7b8aac4072f
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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 #pragma once
19 #include <stdint.h>
20 #include <cstddef>
22 namespace HPHP {
23 ///////////////////////////////////////////////////////////////////////////////
25 /**
26 * For now, use this for StringHolder, instead of something like
27 * std::default_delete.
29 enum struct FreeType {
30 NoFree,
31 Free,
32 LocalFree,
36 * String holder class for storing a reference to a string that is not
37 * owned by us. The type indicates whether/how to deallocate the string upon
38 * destruction.
40 struct StringHolder {
41 /* implicit */
42 StringHolder(std::nullptr_t = nullptr)
43 : m_data(nullptr), m_len(0), m_type(FreeType::NoFree) {}
44 StringHolder(const char* data, uint32_t len, FreeType t)
45 : m_data(data), m_len(len), m_type(t) {}
46 StringHolder(StringHolder&& o) noexcept
47 : m_data(o.m_data), m_len(o.m_len), m_type(o.m_type) {
48 o.m_data = nullptr;
50 StringHolder(const StringHolder&) = delete;
52 ~StringHolder();
54 StringHolder& operator=(StringHolder&&) noexcept;
55 StringHolder& operator=(const StringHolder&) = delete;
57 uint32_t size() const { return m_len; }
59 // The length is not the size of the buffer, but the useful size of it. For
60 // example, we could allocate a big buffer, but only uses part of it. The
61 // capacity of the buffer isn't stored here.
62 void shrinkTo(uint32_t newLen);
64 const char* data() const { return m_data; }
66 operator bool() const { return data() != nullptr; }
68 private:
69 const char* m_data;
70 uint32_t m_len;
71 FreeType m_type;
74 ///////////////////////////////////////////////////////////////////////////////