2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "StructureIDTable.h"
30 #include <wtf/Atomics.h>
31 #include <wtf/DataLog.h>
35 StructureIDTable::StructureIDTable()
36 : m_firstFreeOffset(0)
37 , m_table(std::make_unique
<StructureOrOffset
[]>(s_initialSize
))
39 , m_capacity(s_initialSize
)
41 // We pre-allocate the first offset so that the null Structure
42 // can still be represented as the StructureID '0'.
46 void StructureIDTable::resize(size_t newCapacity
)
48 // Create the new table.
49 auto newTable
= std::make_unique
<StructureOrOffset
[]>(newCapacity
);
51 // Copy the contents of the old table to the new table.
52 memcpy(newTable
.get(), table(), m_capacity
* sizeof(StructureOrOffset
));
54 // Store fence to make sure we've copied everything before doing the swap.
55 WTF::storeStoreFence();
57 // Swap the old and new tables.
58 swap(m_table
, newTable
);
60 // Put the old table (now labeled as new) into the list of old tables.
61 m_oldTables
.append(WTF::move(newTable
));
63 // Update the capacity.
64 m_capacity
= newCapacity
;
67 void StructureIDTable::flushOldTables()
72 StructureID
StructureIDTable::allocateID(Structure
* structure
)
75 if (!m_firstFreeOffset
) {
76 RELEASE_ASSERT(m_capacity
<= UINT_MAX
);
77 if (m_size
== m_capacity
)
78 resize(m_capacity
* 2);
79 ASSERT(m_size
< m_capacity
);
81 StructureOrOffset newEntry
;
82 newEntry
.structure
= structure
;
84 if (m_size
== s_unusedID
) {
86 return allocateID(structure
);
89 StructureID result
= m_size
;
90 table()[result
] = newEntry
;
95 ASSERT(m_firstFreeOffset
!= s_unusedID
);
97 StructureID result
= m_firstFreeOffset
;
98 m_firstFreeOffset
= table()[m_firstFreeOffset
].offset
;
99 table()[result
].structure
= structure
;
106 void StructureIDTable::deallocateID(Structure
* structure
, StructureID structureID
)
109 ASSERT(structureID
!= s_unusedID
);
110 RELEASE_ASSERT(table()[structureID
].structure
== structure
);
111 table()[structureID
].offset
= m_firstFreeOffset
;
112 m_firstFreeOffset
= structureID
;
114 UNUSED_PARAM(structure
);
115 UNUSED_PARAM(structureID
);