2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
22 #include "hphp/util/assertions.h"
25 ///////////////////////////////////////////////////////////////////////////////
27 #define INITIAL_CAPACITY 4
29 struct PointerListData
;
32 * During its lifetime, an instance of PointerList can transition
35 * This is the initial state for a newly constructed PointerList.
36 * There are no elements and no malloced block of memory.
37 * State 1: (m_val & 1) != 0
38 * In this state there is exactly one element which resides directly
39 * in m_val. The element is retrieved by returning a copy of m_val
40 * with the low bit cleared.
41 * State 2: m_val != 0 && (m_val & 1) == 0
42 * In this state, m_data points to a malloced block of memory. The
43 * number of elements, the capacity of the block, and the values of
44 * all the elements reside in the malloced block of memory.
49 PointerListData
* m_data
;
50 uintptr_t m_val
; // m_val is provided for convenience so that we don't
51 // have to repeatedly cast m_data to a uintptr_t
55 T
* get(int index
) const;
56 void set(int index
, T
* val
);
66 PointerList(const PointerList
&);
67 PointerList
& operator=(const PointerList
&);
70 struct PointerListData
{
76 PointerList
<T
>::PointerList() {
81 PointerList
<T
>::~PointerList() {
86 T
* PointerList
<T
>::get(int index
) const {
87 if (m_val
& uintptr_t(1U)) {
88 // If the low bit is set, that means that this PointerList
89 // contains exactly one pointer which is stored in m_val
91 // Clear the low bit before returning the value
92 return (T
*)(m_val
& ~(uintptr_t(1U)));
94 // Index into the malloced block of memory
96 assert(index
>= 0 && index
< m_data
->m_len
);
97 return *((T
**)(m_data
+1) + index
);
100 template <typename T
>
101 void PointerList
<T
>::set(int index
, T
* val
) {
102 if (m_val
& uintptr_t(1U)) {
103 // If the low bit is set, that means that this PointerList
104 // contains exactly one pointer which is stored in m_val.
106 if (!(uintptr_t(val
) & uintptr_t(1U))) {
107 // If the new value's lowest bit is zero, we can store
108 // the new value directly into m_val, mark the low bit,
110 m_val
= uintptr_t(val
) | uintptr_t(1U);
113 // Otherwise the new value's lowest bit is not zero, so we can't
114 // use the trick where we store the value directly into m_val. To
115 // handle this case we call the grow method (which will allocate
116 // a chunk of memory) and then we fall through to the case below.
119 // If we reach this point, m_data should be non-NULL and it should
120 // not have its low bit set.
121 assert(!(m_val
& uintptr_t(1U)));
123 assert(index
>= 0 && index
< m_data
->m_len
);
124 // Index into the malloced block of memory
125 *((T
**)(m_data
+1) + index
) = val
;
128 template <typename T
>
129 bool PointerList
<T
>::empty() const {
130 if (!m_val
) return true;
134 template <typename T
>
135 int PointerList
<T
>::size() const {
137 if (m_val
& uintptr_t(1U)) {
138 // If the low bit is set, that means we have exactly one element
141 // Read the malloced block of memory to find out how many elements
143 return m_data
->m_len
;
145 // If m_data is NULL, that means we have no elements
150 template <typename T
>
151 int PointerList
<T
>::capacity() {
152 if (m_val
&& !(m_val
& uintptr_t(1U))) {
153 // If the m_data is non-NULL and the low bit is not set, read
154 // the malloced block of memory to find out how many elements
155 // can be stored before we need to grow.
156 return m_data
->m_capacity
;
158 // If m_data is NULL or if the low bit is set, that means that
159 // storing more elements may require us to grow, so we return
160 // a capacity equal to our current size.
164 template <typename T
>
165 void PointerList
<T
>::clear() {
167 if (!(m_val
& uintptr_t(1U))) {
168 // If the low bit is not set, we need to free the malloced block
172 // Set m_data to NULL
177 template <typename T
>
178 void PointerList
<T
>::grow() {
184 if (m_val
& uintptr_t(1U)) {
185 // If the low bit is set, that means we have exactly one element and
186 // that our new capacity should be INITIAL_CAPACITY. We also must clear
187 // the low bit so that we can memcpy this one element into the newly
188 // allocated block of memory.
190 newCapacity
= INITIAL_CAPACITY
;
191 m_val
= m_val
& ~(uintptr_t(1U));
192 elms
= (void*)(&m_val
);
196 newCapacity
= m_data
->m_capacity
* 2;
197 elms
= (void*)(m_data
+1);
200 PointerListData
* new_data
= (PointerListData
*)malloc(
201 sizeof(PointerListData
) + sizeof(T
*) * newCapacity
);
202 new_data
->m_len
= len
;
203 new_data
->m_capacity
= newCapacity
;
204 void* newElms
= (void*)(new_data
+1);
205 // Copy over all of the elements in the newly allocated block of memory
206 memcpy(newElms
, elms
, sizeof(T
*) * len
);
212 // If there are currently no elements, all we have to do is allocate a
213 // block of memory and initialize m_len and m_capacity.
214 m_data
= (PointerListData
*)malloc(
215 sizeof(PointerListData
) + sizeof(T
*) * INITIAL_CAPACITY
);
217 m_data
->m_capacity
= INITIAL_CAPACITY
;
219 // Sanity check that malloc always returns a chunk of memory
220 // that is aligned on a 2-byte boundary at the very least.
221 // This is important because we steal the low bit of m_data
222 // for our own nefarious purposes.
223 assert(!(uintptr_t(m_data
) & uintptr_t(1U)));
226 template <typename T
>
227 void PointerList
<T
>::push(T
* val
) {
228 if (!m_val
&& !(uintptr_t(val
) & uintptr_t(1U))) {
229 // If m_data is null and the low bit on the new value is zero,
230 // we can store the new value directly into m_data and mark
232 m_val
= (uintptr_t(val
) | uintptr_t(1U));
236 if (sz
>= capacity()) grow();
241 template <typename T
>
242 void PointerList
<T
>::pop() {
244 if (uintptr_t(m_val
) & uintptr_t(1U)) {
245 // If the value being popped was stored directly into m_data,
246 // we just need to null out m_data
248 } else if (m_data
->m_len
> 0) {
249 // Otherwise, we just decrement the length
255 #undef INITIAL_CAPACITY
257 ///////////////////////////////////////////////////////////////////////////////