Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: Source/platform/heap/Heap.h

Issue 228403002: Deque: Add HeapDeque and prevent buggy use of swap and operator= (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix Ians nit Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/platform/heap/Handle.h ('k') | Source/platform/heap/HeapTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 1395 matching lines...) Expand 10 before | Expand all | Expand 10 after
1406 } 1406 }
1407 1407
1408 template<typename U, size_t otherCapacity> 1408 template<typename U, size_t otherCapacity>
1409 void appendVector(const HeapVector<U, otherCapacity>& other) 1409 void appendVector(const HeapVector<U, otherCapacity>& other)
1410 { 1410 {
1411 const Vector<U, otherCapacity, HeapAllocator>& otherVector = other; 1411 const Vector<U, otherCapacity, HeapAllocator>& otherVector = other;
1412 Vector<T, inlineCapacity, HeapAllocator>::appendVector(otherVector); 1412 Vector<T, inlineCapacity, HeapAllocator>::appendVector(otherVector);
1413 } 1413 }
1414 }; 1414 };
1415 1415
1416 template<typename T, size_t inlineCapacity = 0>
1417 class HeapDeque : public Deque<T, inlineCapacity, HeapAllocator> {
1418 public:
1419 HeapDeque() { }
1420
1421 explicit HeapDeque(size_t size) : Deque<T, inlineCapacity, HeapAllocator>(si ze)
1422 {
1423 }
1424
1425 HeapDeque(size_t size, const T& val) : Deque<T, inlineCapacity, HeapAllocato r>(size, val)
1426 {
1427 }
1428
1429 // FIXME: Doesn't work if there is an inline buffer, due to crbug.com/360572
1430 HeapDeque<T, 0>& operator=(const HeapDeque& other)
1431 {
1432 HeapDeque<T> copy(other);
1433 swap(copy);
1434 return *this;
1435 }
1436
1437 // FIXME: Doesn't work if there is an inline buffer, due to crbug.com/360572
1438 inline void swap(HeapDeque& other)
1439 {
1440 Deque<T, inlineCapacity, HeapAllocator>::swap(other);
1441 }
1442
1443 template<size_t otherCapacity>
1444 HeapDeque(const HeapDeque<T, otherCapacity>& other)
1445 : Deque<T, inlineCapacity, HeapAllocator>(other)
1446 {
1447 }
1448
1449 template<typename U>
1450 void append(const U& other)
1451 {
1452 Deque<T, inlineCapacity, HeapAllocator>::append(other);
1453 }
1454 };
1455
1416 template<typename T> 1456 template<typename T>
1417 struct ThreadingTrait<Member<T> > { 1457 struct ThreadingTrait<Member<T> > {
1418 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; 1458 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
1419 }; 1459 };
1420 1460
1421 template<typename T> 1461 template<typename T>
1422 struct ThreadingTrait<WeakMember<T> > { 1462 struct ThreadingTrait<WeakMember<T> > {
1423 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; 1463 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
1424 }; 1464 };
1425 1465
(...skipping 20 matching lines...) Expand all
1446 template<typename T, size_t inlineCapacity> 1486 template<typename T, size_t inlineCapacity>
1447 struct ThreadingTrait<Vector<T, inlineCapacity, HeapAllocator> > { 1487 struct ThreadingTrait<Vector<T, inlineCapacity, HeapAllocator> > {
1448 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; 1488 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
1449 }; 1489 };
1450 1490
1451 template<typename T, typename Traits> 1491 template<typename T, typename Traits>
1452 struct ThreadingTrait<HeapVectorBacking<T, Traits> > { 1492 struct ThreadingTrait<HeapVectorBacking<T, Traits> > {
1453 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; 1493 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
1454 }; 1494 };
1455 1495
1496 template<typename T, size_t inlineCapacity>
1497 struct ThreadingTrait<Deque<T, inlineCapacity, HeapAllocator> > {
1498 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
1499 };
1500
1456 template<typename Table> 1501 template<typename Table>
1457 struct ThreadingTrait<HeapHashTableBacking<Table> > { 1502 struct ThreadingTrait<HeapHashTableBacking<Table> > {
1458 typedef typename Table::KeyType Key; 1503 typedef typename Table::KeyType Key;
1459 typedef typename Table::ValueType Value; 1504 typedef typename Table::ValueType Value;
1460 static const ThreadAffinity Affinity = 1505 static const ThreadAffinity Affinity =
1461 (ThreadingTrait<Key>::Affinity == MainThreadOnly) 1506 (ThreadingTrait<Key>::Affinity == MainThreadOnly)
1462 && (ThreadingTrait<Value>::Affinity == MainThreadOnly) ? MainThreadOnly : AnyThread; 1507 && (ThreadingTrait<Value>::Affinity == MainThreadOnly) ? MainThreadOnly : AnyThread;
1463 }; 1508 };
1464 1509
1465 template<typename T, typename U, typename V, typename W, typename X> 1510 template<typename T, typename U, typename V, typename W, typename X>
1466 struct ThreadingTrait<HeapHashMap<T, U, V, W, X> > : public ThreadingTrait<HashM ap<T, U, V, W, X, HeapAllocator> > { }; 1511 struct ThreadingTrait<HeapHashMap<T, U, V, W, X> > : public ThreadingTrait<HashM ap<T, U, V, W, X, HeapAllocator> > { };
1467 1512
1468 template<typename T, typename U, typename V> 1513 template<typename T, typename U, typename V>
1469 struct ThreadingTrait<HeapHashSet<T, U, V> > : public ThreadingTrait<HashSet<T, U, V, HeapAllocator> > { }; 1514 struct ThreadingTrait<HeapHashSet<T, U, V> > : public ThreadingTrait<HashSet<T, U, V, HeapAllocator> > { };
1470 1515
1471 template<typename T, size_t inlineCapacity> 1516 template<typename T, size_t inlineCapacity>
1472 struct ThreadingTrait<HeapVector<T, inlineCapacity> > : public ThreadingTrait<Ve ctor<T, inlineCapacity, HeapAllocator> > { }; 1517 struct ThreadingTrait<HeapVector<T, inlineCapacity> > : public ThreadingTrait<Ve ctor<T, inlineCapacity, HeapAllocator> > { };
1473 1518
1519 template<typename T, size_t inlineCapacity>
1520 struct ThreadingTrait<HeapDeque<T, inlineCapacity> > : public ThreadingTrait<Deq ue<T, inlineCapacity, HeapAllocator> > { };
1521
1474 // The standard implementation of GCInfoTrait<T>::get() just returns a static 1522 // The standard implementation of GCInfoTrait<T>::get() just returns a static
1475 // from the class T, but we can't do that for HashMap, HashSet and Vector 1523 // from the class T, but we can't do that for HashMap, HashSet, Deque and
1476 // because they are in WTF and know nothing of GCInfos. Instead we have a 1524 // Vector because they are in WTF and know nothing of GCInfos. Instead we have
1477 // specialization of GCInfoTrait for these three classes here. 1525 // a specialization of GCInfoTrait for these four classes here.
1478 1526
1479 template<typename Key, typename Value, typename T, typename U, typename V> 1527 template<typename Key, typename Value, typename T, typename U, typename V>
1480 struct GCInfoTrait<HashMap<Key, Value, T, U, V, HeapAllocator> > { 1528 struct GCInfoTrait<HashMap<Key, Value, T, U, V, HeapAllocator> > {
1481 static const GCInfo* get() { return &info; } 1529 static const GCInfo* get() { return &info; }
1482 static const GCInfo info; 1530 static const GCInfo info;
1483 }; 1531 };
1484 1532
1485 template<typename Key, typename Value, typename T, typename U, typename V> 1533 template<typename Key, typename Value, typename T, typename U, typename V>
1486 const GCInfo GCInfoTrait<HashMap<Key, Value, T, U, V, HeapAllocator> >::info = { 1534 const GCInfo GCInfoTrait<HashMap<Key, Value, T, U, V, HeapAllocator> >::info = {
1487 TraceTrait<HashMap<Key, Value, T, U, V, HeapAllocator> >::trace, 1535 TraceTrait<HashMap<Key, Value, T, U, V, HeapAllocator> >::trace,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 struct FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator> > : public Finali zerTraitImpl<Vector<T, inlineCapacity, HeapAllocator>, true> { }; 1573 struct FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator> > : public Finali zerTraitImpl<Vector<T, inlineCapacity, HeapAllocator>, true> { };
1526 1574
1527 template<typename T, size_t inlineCapacity> 1575 template<typename T, size_t inlineCapacity>
1528 const GCInfo GCInfoTrait<Vector<T, inlineCapacity, HeapAllocator> >::info = { 1576 const GCInfo GCInfoTrait<Vector<T, inlineCapacity, HeapAllocator> >::info = {
1529 TraceTrait<Vector<T, inlineCapacity, HeapAllocator> >::trace, 1577 TraceTrait<Vector<T, inlineCapacity, HeapAllocator> >::trace,
1530 FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator> >::finalize, 1578 FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator> >::finalize,
1531 // Finalizer is needed to destruct things stored in the inline capacity. 1579 // Finalizer is needed to destruct things stored in the inline capacity.
1532 inlineCapacity && VectorTraits<T>::needsDestruction, 1580 inlineCapacity && VectorTraits<T>::needsDestruction,
1533 }; 1581 };
1534 1582
1583 template<typename T>
1584 struct GCInfoTrait<Deque<T, 0, HeapAllocator> > {
1585 static const GCInfo* get() { return &info; }
1586 static const GCInfo info;
1587 };
1588
1589 template<typename T>
1590 const GCInfo GCInfoTrait<Deque<T, 0, HeapAllocator> >::info = {
1591 TraceTrait<Deque<T, 0, HeapAllocator> >::trace,
1592 0,
1593 false, // Deque needs no finalizer if it has no inline capacity.
1594 };
1595
1596 template<typename T, size_t inlineCapacity>
1597 struct GCInfoTrait<Deque<T, inlineCapacity, HeapAllocator> > {
1598 static const GCInfo* get() { return &info; }
1599 static const GCInfo info;
1600 };
1601
1602 template<typename T, size_t inlineCapacity>
1603 struct FinalizerTrait<Deque<T, inlineCapacity, HeapAllocator> > : public Finaliz erTraitImpl<Deque<T, inlineCapacity, HeapAllocator>, true> { };
1604
1605 template<typename T, size_t inlineCapacity>
1606 const GCInfo GCInfoTrait<Deque<T, inlineCapacity, HeapAllocator> >::info = {
1607 TraceTrait<Deque<T, inlineCapacity, HeapAllocator> >::trace,
1608 FinalizerTrait<Deque<T, inlineCapacity, HeapAllocator> >::finalize,
1609 // Finalizer is needed to destruct things stored in the inline capacity.
1610 inlineCapacity && VectorTraits<T>::needsDestruction,
1611 };
1612
1535 template<typename T, typename Traits> 1613 template<typename T, typename Traits>
1536 struct GCInfoTrait<HeapVectorBacking<T, Traits> > { 1614 struct GCInfoTrait<HeapVectorBacking<T, Traits> > {
1537 static const GCInfo* get() { return &info; } 1615 static const GCInfo* get() { return &info; }
1538 static const GCInfo info; 1616 static const GCInfo info;
1539 }; 1617 };
1540 1618
1541 template<typename T, typename Traits> 1619 template<typename T, typename Traits>
1542 const GCInfo GCInfoTrait<HeapVectorBacking<T, Traits> >::info = { 1620 const GCInfo GCInfoTrait<HeapVectorBacking<T, Traits> >::info = {
1543 TraceTrait<HeapVectorBacking<T, Traits> >::trace, 1621 TraceTrait<HeapVectorBacking<T, Traits> >::trace,
1544 FinalizerTrait<HeapVectorBacking<T, Traits> >::finalize, 1622 FinalizerTrait<HeapVectorBacking<T, Traits> >::finalize,
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1687 { 1765 {
1688 TraceTrait<T>::trace(visitor, &t); 1766 TraceTrait<T>::trace(visitor, &t);
1689 } 1767 }
1690 }; 1768 };
1691 1769
1692 template<typename T, typename Traits> 1770 template<typename T, typename Traits>
1693 struct TraceTrait<HeapVectorBacking<T, Traits> > { 1771 struct TraceTrait<HeapVectorBacking<T, Traits> > {
1694 typedef HeapVectorBacking<T, Traits> Backing; 1772 typedef HeapVectorBacking<T, Traits> Backing;
1695 static void trace(WebCore::Visitor* visitor, void* self) 1773 static void trace(WebCore::Visitor* visitor, void* self)
1696 { 1774 {
1697 COMPILE_ASSERT(!Traits::isWeak, WeDontSupportWeaknessInHeapVectors); 1775 COMPILE_ASSERT(!Traits::isWeak, WeDontSupportWeaknessInHeapVectorsOrDequ es);
1698 if (WTF::ShouldBeTraced<Traits>::value) 1776 if (WTF::ShouldBeTraced<Traits>::value)
1699 CollectionBackingTraceTrait<WTF::ShouldBeTraced<Traits>::value, fals e, false, HeapVectorBacking<T, Traits>, void>::mark(visitor, self); 1777 CollectionBackingTraceTrait<WTF::ShouldBeTraced<Traits>::value, fals e, false, HeapVectorBacking<T, Traits>, void>::mark(visitor, self);
1700 } 1778 }
1701 static void mark(Visitor* visitor, const Backing* backing) 1779 static void mark(Visitor* visitor, const Backing* backing)
1702 { 1780 {
1703 visitor->mark(backing, &trace); 1781 visitor->mark(backing, &trace);
1704 } 1782 }
1705 static void checkGCInfo(Visitor* visitor, const Backing* backing) 1783 static void checkGCInfo(Visitor* visitor, const Backing* backing)
1706 { 1784 {
1707 #ifndef NDEBUG 1785 #ifndef NDEBUG
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 table[i].~Value(); 1833 table[i].~Value();
1756 } 1834 }
1757 } 1835 }
1758 1836
1759 template<typename T, typename U, typename V, typename W, typename X> 1837 template<typename T, typename U, typename V, typename W, typename X>
1760 struct GCInfoTrait<HeapHashMap<T, U, V, W, X> > : public GCInfoTrait<HashMap<T, U, V, W, X, HeapAllocator> > { }; 1838 struct GCInfoTrait<HeapHashMap<T, U, V, W, X> > : public GCInfoTrait<HashMap<T, U, V, W, X, HeapAllocator> > { };
1761 template<typename T, typename U, typename V> 1839 template<typename T, typename U, typename V>
1762 struct GCInfoTrait<HeapHashSet<T, U, V> > : public GCInfoTrait<HashSet<T, U, V, HeapAllocator> > { }; 1840 struct GCInfoTrait<HeapHashSet<T, U, V> > : public GCInfoTrait<HashSet<T, U, V, HeapAllocator> > { };
1763 template<typename T, size_t inlineCapacity> 1841 template<typename T, size_t inlineCapacity>
1764 struct GCInfoTrait<HeapVector<T, inlineCapacity> > : public GCInfoTrait<Vector<T , inlineCapacity, HeapAllocator> > { }; 1842 struct GCInfoTrait<HeapVector<T, inlineCapacity> > : public GCInfoTrait<Vector<T , inlineCapacity, HeapAllocator> > { };
1843 template<typename T, size_t inlineCapacity>
1844 struct GCInfoTrait<HeapDeque<T, inlineCapacity> > : public GCInfoTrait<Deque<T, inlineCapacity, HeapAllocator> > { };
1765 1845
1766 template<typename T> 1846 template<typename T>
1767 struct IfWeakMember; 1847 struct IfWeakMember;
1768 1848
1769 template<typename T> 1849 template<typename T>
1770 struct IfWeakMember { 1850 struct IfWeakMember {
1771 template<typename U> 1851 template<typename U>
1772 static bool isDead(Visitor*, const U&) { return false; } 1852 static bool isDead(Visitor*, const U&) { return false; }
1773 }; 1853 };
1774 1854
1775 template<typename T> 1855 template<typename T>
1776 struct IfWeakMember<WeakMember<T> > { 1856 struct IfWeakMember<WeakMember<T> > {
1777 static bool isDead(Visitor* visitor, const WeakMember<T>& t) { return !visit or->isAlive(t.get()); } 1857 static bool isDead(Visitor* visitor, const WeakMember<T>& t) { return !visit or->isAlive(t.get()); }
1778 }; 1858 };
1779 1859
1780 #if COMPILER(CLANG) 1860 #if COMPILER(CLANG)
1781 // Clang does not export the symbols that we have explicitly asked it 1861 // Clang does not export the symbols that we have explicitly asked it
1782 // to export. This forces it to export all the methods from ThreadHeap. 1862 // to export. This forces it to export all the methods from ThreadHeap.
1783 template<> void ThreadHeap<FinalizedHeapObjectHeader>::addPageToHeap(const GCInf o*); 1863 template<> void ThreadHeap<FinalizedHeapObjectHeader>::addPageToHeap(const GCInf o*);
1784 template<> void ThreadHeap<HeapObjectHeader>::addPageToHeap(const GCInfo*); 1864 template<> void ThreadHeap<HeapObjectHeader>::addPageToHeap(const GCInfo*);
1785 extern template class PLATFORM_EXPORT ThreadHeap<FinalizedHeapObjectHeader>; 1865 extern template class PLATFORM_EXPORT ThreadHeap<FinalizedHeapObjectHeader>;
1786 extern template class PLATFORM_EXPORT ThreadHeap<HeapObjectHeader>; 1866 extern template class PLATFORM_EXPORT ThreadHeap<HeapObjectHeader>;
1787 #endif 1867 #endif
1788 1868
1789 } 1869 }
1790 1870
1791 #endif // Heap_h 1871 #endif // Heap_h
OLDNEW
« no previous file with comments | « Source/platform/heap/Handle.h ('k') | Source/platform/heap/HeapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698