]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/GCActivityCallback.cpp
JavaScriptCore-7600.1.4.13.1.tar.gz
[apple/javascriptcore.git] / heap / GCActivityCallback.cpp
CommitLineData
14957cd0
A
1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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.
81345200 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14957cd0
A
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "GCActivityCallback.h"
31
93a37866 32#include "Heap.h"
93a37866
A
33#include "JSLock.h"
34#include "JSObject.h"
81345200 35#include "VM.h"
93a37866
A
36
37#include <wtf/RetainPtr.h>
38#include <wtf/WTFThreadData.h>
39
40#if PLATFORM(EFL)
41#include <wtf/MainThread.h>
42#endif
43
14957cd0
A
44namespace JSC {
45
81345200 46bool GCActivityCallback::s_shouldCreateGCTimer = true;
14957cd0 47
81345200 48#if USE(CF) || PLATFORM(EFL)
93a37866 49
93a37866 50const double timerSlop = 2.0; // Fudge factor to avoid performance cost of resetting timer.
93a37866
A
51
52#if USE(CF)
81345200 53GCActivityCallback::GCActivityCallback(Heap* heap)
93a37866 54 : GCActivityCallback(heap->vm(), CFRunLoopGetCurrent())
14957cd0
A
55{
56}
57
81345200 58GCActivityCallback::GCActivityCallback(Heap* heap, CFRunLoopRef runLoop)
93a37866 59 : GCActivityCallback(heap->vm(), runLoop)
93a37866
A
60{
61}
62#elif PLATFORM(EFL)
81345200 63GCActivityCallback::GCActivityCallback(Heap* heap)
93a37866 64 : GCActivityCallback(heap->vm(), WTF::isMainThread())
14957cd0
A
65{
66}
93a37866 67#endif
14957cd0 68
81345200 69void GCActivityCallback::doWork()
93a37866
A
70{
71 Heap* heap = &m_vm->heap;
72 if (!isEnabled())
73 return;
74
81345200
A
75 JSLockHolder locker(m_vm);
76 if (heap->isDeferred()) {
77 scheduleTimer(0);
93a37866
A
78 return;
79 }
81345200
A
80
81 doCollection();
93a37866 82}
81345200 83
93a37866 84#if USE(CF)
81345200 85void GCActivityCallback::scheduleTimer(double newDelay)
6fe7ccc8 86{
93a37866
A
87 if (newDelay * timerSlop > m_delay)
88 return;
89 double delta = m_delay - newDelay;
90 m_delay = newDelay;
91 CFRunLoopTimerSetNextFireDate(m_timer.get(), CFRunLoopTimerGetNextFireDate(m_timer.get()) - delta);
6fe7ccc8
A
92}
93
81345200 94void GCActivityCallback::cancelTimer()
93a37866
A
95{
96 m_delay = s_decade;
97 CFRunLoopTimerSetNextFireDate(m_timer.get(), CFAbsoluteTimeGetCurrent() + s_decade);
98}
93a37866 99#elif PLATFORM(EFL)
81345200 100void GCActivityCallback::scheduleTimer(double newDelay)
93a37866
A
101{
102 if (newDelay * timerSlop > m_delay)
103 return;
104
105 stop();
106 m_delay = newDelay;
107
108 ASSERT(!m_timer);
109 m_timer = add(newDelay, this);
110}
111
81345200 112void GCActivityCallback::cancelTimer()
93a37866 113{
81345200 114 m_delay = s_hour;
93a37866
A
115 stop();
116}
117#endif
118
81345200 119void GCActivityCallback::didAllocate(size_t bytes)
93a37866
A
120{
121#if PLATFORM(EFL)
122 if (!isEnabled())
123 return;
124
125 ASSERT(WTF::isMainThread());
126#endif
127
128 // The first byte allocated in an allocation cycle will report 0 bytes to didAllocate.
129 // We pretend it's one byte so that we don't ignore this allocation entirely.
130 if (!bytes)
131 bytes = 1;
81345200
A
132 double bytesExpectedToReclaim = static_cast<double>(bytes) * deathRate();
133 double newDelay = lastGCLength() / gcTimeSlice(bytesExpectedToReclaim);
93a37866
A
134 scheduleTimer(newDelay);
135}
136
81345200 137void GCActivityCallback::willCollect()
93a37866
A
138{
139 cancelTimer();
14957cd0
A
140}
141
81345200 142void GCActivityCallback::cancel()
93a37866
A
143{
144 cancelTimer();
145}
146
147#else
148
81345200 149GCActivityCallback::GCActivityCallback(Heap* heap)
93a37866
A
150 : GCActivityCallback(heap->vm())
151{
152}
153
81345200 154void GCActivityCallback::doWork()
6fe7ccc8
A
155{
156}
157
81345200 158void GCActivityCallback::didAllocate(size_t)
6fe7ccc8
A
159{
160}
161
81345200 162void GCActivityCallback::willCollect()
6fe7ccc8
A
163{
164}
165
81345200 166void GCActivityCallback::cancel()
93a37866
A
167{
168}
169
170#endif
171
14957cd0
A
172}
173