1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "CSSSelectorList.h"
29
30 namespace WebCore {
31
~CSSSelectorList()32 CSSSelectorList::~CSSSelectorList()
33 {
34 deleteSelectors();
35 }
36
adopt(CSSSelectorList & list)37 void CSSSelectorList::adopt(CSSSelectorList& list)
38 {
39 deleteSelectors();
40 m_selectorArray = list.m_selectorArray;
41 list.m_selectorArray = 0;
42 }
43
adoptSelectorVector(Vector<CSSSelector * > & selectorVector)44 void CSSSelectorList::adoptSelectorVector(Vector<CSSSelector*>& selectorVector)
45 {
46 deleteSelectors();
47 const size_t size = selectorVector.size();
48 ASSERT(size);
49 if (size == 1) {
50 m_selectorArray = selectorVector[0];
51 m_selectorArray->setLastInSelectorList();
52 selectorVector.shrink(0);
53 return;
54 }
55 m_selectorArray = reinterpret_cast<CSSSelector*>(fastMalloc(sizeof(CSSSelector) * selectorVector.size()));
56 for (size_t i = 0; i < size; ++i) {
57 memcpy(&m_selectorArray[i], selectorVector[i], sizeof(CSSSelector));
58 // We want to free the memory (which was allocated with new), but we
59 // don't want the destructor to run since it will affect the copy
60 // we've just made. In theory this is undefined, but operator delete
61 // is only defined taking a void*, so in practice it should be ok.
62 delete reinterpret_cast<char*>(selectorVector[i]);
63 ASSERT(!m_selectorArray[i].isLastInSelectorList());
64 }
65 m_selectorArray[size - 1].setLastInSelectorList();
66 selectorVector.shrink(0);
67 }
68
deleteSelectors()69 void CSSSelectorList::deleteSelectors()
70 {
71 if (!m_selectorArray)
72 return;
73
74 // We had two cases in adoptSelectVector. The fast case of a 1 element
75 // vector took the CSSSelector directly, which was allocated with new.
76 // The second case we allocated a new fastMalloc buffer, which should be
77 // freed with fastFree, and the destructors called manually.
78 CSSSelector* s = m_selectorArray;
79 bool done = s->isLastInSelectorList();
80 if (done)
81 delete s;
82 else {
83 while (1) {
84 s->~CSSSelector();
85 if (done)
86 break;
87 ++s;
88 done = s->isLastInSelectorList();
89 }
90 fastFree(m_selectorArray);
91 }
92 }
93
94 }
95