blob: 6fbbc5d53720758a247fcaf8d4c2077c46d9ccd9 [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
mark.lam@apple.com7d1b3b92017-02-21 01:51:05 +00002 * Copyright (C) 2003-2017 Apple Inc. All rights reserved.
kocienda66a6d362001-08-24 14:24:45 +00003 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
mjscdff33b2006-01-23 21:41:36 +000016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
ggaren07d4ce62005-07-14 18:27:04 +000017 * Boston, MA 02110-1301, USA.
mjs6f821c82002-03-22 00:31:57 +000018 *
kocienda66a6d362001-08-24 14:24:45 +000019 */
20
mjsb64c50a2005-10-03 21:13:12 +000021#include "config.h"
weinig@apple.com49b32502008-07-06 00:10:04 +000022#include "ArgList.h"
mjs6f821c82002-03-22 00:31:57 +000023
mhahnenberg@apple.comc1bc9d32013-01-24 21:39:55 +000024#include "JSCJSValue.h"
oliver@apple.com9d4f0ec2011-03-14 18:16:36 +000025#include "JSObject.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000026#include "JSCInlines.h"
ap@webkit.org960c28e2008-06-19 17:29:29 +000027
mrowe@apple.comcf617a52007-11-05 23:05:03 +000028using std::min;
29
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000030namespace JSC {
darinff399e02002-11-23 07:49:05 +000031
ggaren@apple.com96884092016-08-17 21:22:46 +000032void MarkedArgumentBuffer::addMarkSet(JSValue v)
33{
34 if (m_markSet)
35 return;
36
37 Heap* heap = Heap::heap(v);
38 if (!heap)
39 return;
40
41 m_markSet = &heap->markListSet();
42 m_markSet->add(this);
43}
44
darin@apple.com80d38f92008-06-16 05:28:46 +000045void ArgList::getSlice(int startIndex, ArgList& result) const
darinff399e02002-11-23 07:49:05 +000046{
ggaren@apple.com0af14682011-12-12 00:35:51 +000047 if (startIndex <= 0 || startIndex >= m_argCount) {
ggaren@apple.com2fd96da2011-11-16 23:37:15 +000048 result = ArgList();
oliver@apple.comf32186e2009-04-30 01:21:52 +000049 return;
50 }
ggaren@apple.com2fd96da2011-11-16 23:37:15 +000051
msaboff@apple.comb70e41b2013-09-13 18:03:55 +000052 result.m_args = m_args + startIndex;
ggaren@apple.com2fd96da2011-11-16 23:37:15 +000053 result.m_argCount = m_argCount - startIndex;
mjs0e6b4f02002-11-26 23:52:00 +000054}
55
fpizlo@apple.comf7240e02016-12-16 02:16:19 +000056void MarkedArgumentBuffer::markLists(SlotVisitor& visitor, ListSet& markSet)
ggaren879ab752007-11-05 21:27:15 +000057{
ap@webkit.orga1669f32008-06-12 14:34:38 +000058 ListSet::iterator end = markSet.end();
59 for (ListSet::iterator it = markSet.begin(); it != end; ++it) {
oliver@apple.comf32186e2009-04-30 01:21:52 +000060 MarkedArgumentBuffer* list = *it;
ggaren@apple.com58adda02011-12-14 02:55:02 +000061 for (int i = 0; i < list->m_size; ++i)
fpizlo@apple.comf7240e02016-12-16 02:16:19 +000062 visitor.appendUnbarriered(JSValue::decode(list->slotFor(i)));
ggaren879ab752007-11-05 21:27:15 +000063 }
64}
65
mark.lam@apple.com7d1b3b92017-02-21 01:51:05 +000066void MarkedArgumentBuffer::slowEnsureCapacity(size_t requestedCapacity)
67{
68 int newCapacity = Checked<int>(requestedCapacity).unsafeGet();
69 expandCapacity(newCapacity);
70}
71
ggaren@apple.com96884092016-08-17 21:22:46 +000072void MarkedArgumentBuffer::expandCapacity()
ggaren879ab752007-11-05 21:27:15 +000073{
fpizlo@apple.comd21b45e2016-02-07 19:03:29 +000074 int newCapacity = (Checked<int>(m_capacity) * 2).unsafeGet();
mark.lam@apple.com7d1b3b92017-02-21 01:51:05 +000075 expandCapacity(newCapacity);
76}
77
78void MarkedArgumentBuffer::expandCapacity(int newCapacity)
79{
80 ASSERT(m_capacity < newCapacity);
fpizlo@apple.comd21b45e2016-02-07 19:03:29 +000081 size_t size = (Checked<size_t>(newCapacity) * sizeof(EncodedJSValue)).unsafeGet();
82 EncodedJSValue* newBuffer = static_cast<EncodedJSValue*>(fastMalloc(size));
mark.lam@apple.com7d1b3b92017-02-21 01:51:05 +000083 for (int i = 0; i < m_size; ++i) {
msaboff@apple.comb70e41b2013-09-13 18:03:55 +000084 newBuffer[i] = m_buffer[i];
ggaren@apple.com96884092016-08-17 21:22:46 +000085 addMarkSet(JSValue::decode(m_buffer[i]));
86 }
ggaren@apple.com0af14682011-12-12 00:35:51 +000087
ggaren@apple.com58adda02011-12-14 02:55:02 +000088 if (EncodedJSValue* base = mallocBase())
fpizlo@apple.comd21b45e2016-02-07 19:03:29 +000089 fastFree(base);
ggaren@apple.com0af14682011-12-12 00:35:51 +000090
91 m_buffer = newBuffer;
92 m_capacity = newCapacity;
ggaren@apple.com96884092016-08-17 21:22:46 +000093}
94
95void MarkedArgumentBuffer::slowAppend(JSValue v)
96{
mark.lam@apple.com7d1b3b92017-02-21 01:51:05 +000097 ASSERT(m_size <= m_capacity);
98 if (m_size == m_capacity)
ggaren@apple.com96884092016-08-17 21:22:46 +000099 expandCapacity();
ggaren@apple.com0af14682011-12-12 00:35:51 +0000100
ggaren@apple.com58adda02011-12-14 02:55:02 +0000101 slotFor(m_size) = JSValue::encode(v);
ggaren@apple.com0af14682011-12-12 00:35:51 +0000102 ++m_size;
ggaren@apple.com96884092016-08-17 21:22:46 +0000103 addMarkSet(v);
darinf7fc08d2002-11-20 08:53:04 +0000104}
darin20330f72002-11-20 22:59:04 +0000105
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000106} // namespace JSC