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>
32 #include <wtf/PassOwnPtr.h>
36 StructureIDTable::StructureIDTable()
37 : m_firstFreeOffset(0)
38 , m_table(adoptPtr(new StructureOrOffset
[s_initialSize
]))
40 , m_capacity(s_initialSize
)
42 // We pre-allocate the first offset so that the null Structure
43 // can still be represented as the StructureID '0'.
47 void StructureIDTable::resize(size_t newCapacity
)
49 // Create the new table.
50 OwnPtr
<StructureOrOffset
> newTable
= adoptPtr(new StructureOrOffset
[newCapacity
]);
52 // Copy the contents of the old table to the new table.
53 memcpy(newTable
.get(), table(), m_capacity
* sizeof(StructureOrOffset
));
55 // Store fence to make sure we've copied everything before doing the swap.
56 WTF::storeStoreFence();
58 // Swap the old and new tables.
59 swap(m_table
, newTable
);
61 // Put the old table (now labeled as new) into the list of old tables.
62 m_oldTables
.append(newTable
.release());
64 // Update the capacity.
65 m_capacity
= newCapacity
;
68 void StructureIDTable::flushOldTables()
73 StructureID
StructureIDTable::allocateID(Structure
* structure
)
76 if (!m_firstFreeOffset
) {
77 RELEASE_ASSERT(m_capacity
<= UINT_MAX
);
78 if (m_size
== m_capacity
)
79 resize(m_capacity
* 2);
80 ASSERT(m_size
< m_capacity
);
82 StructureOrOffset newEntry
;
83 newEntry
.structure
= structure
;
85 if (m_size
== s_unusedID
) {
87 return allocateID(structure
);
90 StructureID result
= m_size
;
91 table()[result
] = newEntry
;
96 ASSERT(m_firstFreeOffset
!= s_unusedID
);
98 StructureID result
= m_firstFreeOffset
;
99 m_firstFreeOffset
= table()[m_firstFreeOffset
].offset
;
100 table()[result
].structure
= structure
;
107 void StructureIDTable::deallocateID(Structure
* structure
, StructureID structureID
)
110 ASSERT(structureID
!= s_unusedID
);
111 RELEASE_ASSERT(table()[structureID
].structure
== structure
);
112 table()[structureID
].offset
= m_firstFreeOffset
;
113 m_firstFreeOffset
= structureID
;
115 UNUSED_PARAM(structure
);
116 UNUSED_PARAM(structureID
);