Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 #endif | 270 #endif |
| 271 }; | 271 }; |
| 272 | 272 |
| 273 | 273 |
| 274 // ---------------------------------------------------------------------------- | 274 // ---------------------------------------------------------------------------- |
| 275 // Space is the abstract superclass for all allocation spaces. | 275 // Space is the abstract superclass for all allocation spaces. |
| 276 class Space : public Malloced { | 276 class Space : public Malloced { |
| 277 public: | 277 public: |
| 278 Space(AllocationSpace id, Executability executable) | 278 Space(AllocationSpace id, Executability executable) |
| 279 : id_(id), executable_(executable) {} | 279 : id_(id), executable_(executable) {} |
| 280 | |
| 280 virtual ~Space() {} | 281 virtual ~Space() {} |
| 282 | |
| 281 // Does the space need executable memory? | 283 // Does the space need executable memory? |
| 282 Executability executable() { return executable_; } | 284 Executability executable() { return executable_; } |
| 285 | |
| 283 // Identity used in error reporting. | 286 // Identity used in error reporting. |
| 284 AllocationSpace identity() { return id_; } | 287 AllocationSpace identity() { return id_; } |
| 288 | |
| 285 virtual int Size() = 0; | 289 virtual int Size() = 0; |
| 290 | |
| 286 #ifdef DEBUG | 291 #ifdef DEBUG |
| 287 virtual void Verify() = 0; | 292 virtual void Verify() = 0; |
| 288 virtual void Print() = 0; | 293 virtual void Print() = 0; |
| 289 #endif | 294 #endif |
| 295 | |
| 290 private: | 296 private: |
| 291 AllocationSpace id_; | 297 AllocationSpace id_; |
| 292 Executability executable_; | 298 Executability executable_; |
| 293 }; | 299 }; |
| 294 | 300 |
| 295 | 301 |
| 296 // ---------------------------------------------------------------------------- | 302 // ---------------------------------------------------------------------------- |
| 297 // A space acquires chunks of memory from the operating system. The memory | 303 // A space acquires chunks of memory from the operating system. The memory |
| 298 // allocator manages chunks for the paged heap spaces (old space and map | 304 // allocator manages chunks for the paged heap spaces (old space and map |
| 299 // space). A paged chunk consists of pages. Pages in a chunk have contiguous | 305 // space). A paged chunk consists of pages. Pages in a chunk have contiguous |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 // Checks whether a page belongs to a space. | 395 // Checks whether a page belongs to a space. |
| 390 static inline bool IsPageInSpace(Page* p, PagedSpace* space); | 396 static inline bool IsPageInSpace(Page* p, PagedSpace* space); |
| 391 | 397 |
| 392 // Returns the space that owns the given page. | 398 // Returns the space that owns the given page. |
| 393 static inline PagedSpace* PageOwner(Page* page); | 399 static inline PagedSpace* PageOwner(Page* page); |
| 394 | 400 |
| 395 // Finds the first/last page in the same chunk as a given page. | 401 // Finds the first/last page in the same chunk as a given page. |
| 396 static Page* FindFirstPageInSameChunk(Page* p); | 402 static Page* FindFirstPageInSameChunk(Page* p); |
| 397 static Page* FindLastPageInSameChunk(Page* p); | 403 static Page* FindLastPageInSameChunk(Page* p); |
| 398 | 404 |
| 405 #ifdef ENABLE_HEAP_PROTECTION | |
| 406 // Protect/unprotect a block of memory by marking it read-only/writable. | |
| 407 static inline void Protect(Address start, size_t size); | |
| 408 static inline void Unprotect(Address start, size_t size, | |
| 409 Executability executable); | |
| 410 | |
| 411 // Protect/unprotect a chunk given a page in the chunk. | |
| 412 static inline void ProtectChunkFromPage(Page* page); | |
| 413 static inline void UnprotectChunkFromPage(Page* page); | |
| 414 #endif | |
| 415 | |
| 399 #ifdef DEBUG | 416 #ifdef DEBUG |
| 400 // Reports statistic info of the space. | 417 // Reports statistic info of the space. |
| 401 static void ReportStatistics(); | 418 static void ReportStatistics(); |
| 402 #endif | 419 #endif |
| 403 | 420 |
| 404 // Due to encoding limitation, we can only have 8K chunks. | 421 // Due to encoding limitation, we can only have 8K chunks. |
| 405 static const int kMaxNofChunks = 1 << Page::kPageSizeBits; | 422 static const int kMaxNofChunks = 1 << Page::kPageSizeBits; |
| 406 // If a chunk has at least 32 pages, the maximum heap size is about | 423 // If a chunk has at least 32 pages, the maximum heap size is about |
| 407 // 8 * 1024 * 32 * 8K = 2G bytes. | 424 // 8 * 1024 * 32 * 8K = 2G bytes. |
| 408 static const int kPagesPerChunk = 64; | 425 static const int kPagesPerChunk = 64; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 | 470 |
| 454 // Basic check whether a chunk id is in the valid range. | 471 // Basic check whether a chunk id is in the valid range. |
| 455 static inline bool IsValidChunkId(int chunk_id); | 472 static inline bool IsValidChunkId(int chunk_id); |
| 456 | 473 |
| 457 // Checks whether a chunk id identifies an allocated chunk. | 474 // Checks whether a chunk id identifies an allocated chunk. |
| 458 static inline bool IsValidChunk(int chunk_id); | 475 static inline bool IsValidChunk(int chunk_id); |
| 459 | 476 |
| 460 // Returns the chunk id that a page belongs to. | 477 // Returns the chunk id that a page belongs to. |
| 461 static inline int GetChunkId(Page* p); | 478 static inline int GetChunkId(Page* p); |
| 462 | 479 |
| 480 // True if the address lies in the initial chunk. | |
| 481 static inline bool InInitialChunk(Address address); | |
| 482 | |
| 463 // Initializes pages in a chunk. Returns the first page address. | 483 // Initializes pages in a chunk. Returns the first page address. |
| 464 // This function and GetChunkId() are provided for the mark-compact | 484 // This function and GetChunkId() are provided for the mark-compact |
| 465 // collector to rebuild page headers in the from space, which is | 485 // collector to rebuild page headers in the from space, which is |
| 466 // used as a marking stack and its page headers are destroyed. | 486 // used as a marking stack and its page headers are destroyed. |
| 467 static Page* InitializePagesInChunk(int chunk_id, int pages_in_chunk, | 487 static Page* InitializePagesInChunk(int chunk_id, int pages_in_chunk, |
| 468 PagedSpace* owner); | 488 PagedSpace* owner); |
| 469 }; | 489 }; |
| 470 | 490 |
| 471 | 491 |
| 472 // ----------------------------------------------------------------------------- | 492 // ----------------------------------------------------------------------------- |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 | 682 |
| 663 private: | 683 private: |
| 664 int capacity_; | 684 int capacity_; |
| 665 int available_; | 685 int available_; |
| 666 int size_; | 686 int size_; |
| 667 int waste_; | 687 int waste_; |
| 668 }; | 688 }; |
| 669 | 689 |
| 670 | 690 |
| 671 class PagedSpace : public Space { | 691 class PagedSpace : public Space { |
| 672 friend class PageIterator; | |
| 673 public: | 692 public: |
| 674 // Creates a space with a maximum capacity, and an id. | 693 // Creates a space with a maximum capacity, and an id. |
| 675 PagedSpace(int max_capacity, AllocationSpace id, Executability executable); | 694 PagedSpace(int max_capacity, AllocationSpace id, Executability executable); |
| 676 | 695 |
| 677 virtual ~PagedSpace() {} | 696 virtual ~PagedSpace() {} |
| 678 | 697 |
| 679 // Set up the space using the given address range of virtual memory (from | 698 // Set up the space using the given address range of virtual memory (from |
| 680 // the memory allocator's initial chunk) if possible. If the block of | 699 // the memory allocator's initial chunk) if possible. If the block of |
| 681 // addresses is not big enough to contain a single page-aligned page, a | 700 // addresses is not big enough to contain a single page-aligned page, a |
| 682 // fresh chunk will be allocated. | 701 // fresh chunk will be allocated. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 // Updates the allocation pointer to the relocation top after a mark-compact | 776 // Updates the allocation pointer to the relocation top after a mark-compact |
| 758 // collection. | 777 // collection. |
| 759 virtual void MCCommitRelocationInfo() = 0; | 778 virtual void MCCommitRelocationInfo() = 0; |
| 760 | 779 |
| 761 // Releases half of unused pages. | 780 // Releases half of unused pages. |
| 762 void Shrink(); | 781 void Shrink(); |
| 763 | 782 |
| 764 // Ensures that the capacity is at least 'capacity'. Returns false on failure. | 783 // Ensures that the capacity is at least 'capacity'. Returns false on failure. |
| 765 bool EnsureCapacity(int capacity); | 784 bool EnsureCapacity(int capacity); |
| 766 | 785 |
| 786 #ifdef ENABLE_HEAP_PROTECTION | |
| 787 // Protect/unprotect the space by marking it read-only/writable. | |
| 788 void Protect(); | |
| 789 void Unprotect(); | |
| 790 #endif | |
| 791 | |
| 767 #ifdef DEBUG | 792 #ifdef DEBUG |
| 768 // Print meta info and objects in this space. | 793 // Print meta info and objects in this space. |
| 769 virtual void Print(); | 794 virtual void Print(); |
| 770 | 795 |
| 771 // Report code object related statistics | 796 // Report code object related statistics |
| 772 void CollectCodeStatistics(); | 797 void CollectCodeStatistics(); |
| 773 static void ReportCodeStatistics(); | 798 static void ReportCodeStatistics(); |
| 774 static void ResetCodeStatistics(); | 799 static void ResetCodeStatistics(); |
| 775 #endif | 800 #endif |
| 776 | 801 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 827 // Returns the page of the allocation pointer. | 852 // Returns the page of the allocation pointer. |
| 828 Page* AllocationTopPage() { return TopPageOf(allocation_info_); } | 853 Page* AllocationTopPage() { return TopPageOf(allocation_info_); } |
| 829 | 854 |
| 830 // Returns a pointer to the page of the relocation pointer. | 855 // Returns a pointer to the page of the relocation pointer. |
| 831 Page* MCRelocationTopPage() { return TopPageOf(mc_forwarding_info_); } | 856 Page* MCRelocationTopPage() { return TopPageOf(mc_forwarding_info_); } |
| 832 | 857 |
| 833 #ifdef DEBUG | 858 #ifdef DEBUG |
| 834 // Returns the number of total pages in this space. | 859 // Returns the number of total pages in this space. |
| 835 int CountTotalPages(); | 860 int CountTotalPages(); |
| 836 #endif | 861 #endif |
| 862 | |
| 863 friend class PageIterator; | |
| 837 }; | 864 }; |
| 838 | 865 |
| 839 | 866 |
| 840 #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) | 867 #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 841 // HistogramInfo class for recording a single "bar" of a histogram. This | 868 // HistogramInfo class for recording a single "bar" of a histogram. This |
| 842 // class is used for collecting statistics to print to stdout (when compiled | 869 // class is used for collecting statistics to print to stdout (when compiled |
| 843 // with DEBUG) or to the log file (when compiled with | 870 // with DEBUG) or to the log file (when compiled with |
| 844 // ENABLE_LOGGING_AND_PROFILING). | 871 // ENABLE_LOGGING_AND_PROFILING). |
| 845 class HistogramInfo BASE_EMBEDDED { | 872 class HistogramInfo BASE_EMBEDDED { |
| 846 public: | 873 public: |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1110 | 1137 |
| 1111 // True if the object is a heap object in the address range of the | 1138 // True if the object is a heap object in the address range of the |
| 1112 // respective semispace (not necessarily below the allocation pointer of the | 1139 // respective semispace (not necessarily below the allocation pointer of the |
| 1113 // semispace). | 1140 // semispace). |
| 1114 bool ToSpaceContains(Object* o) { return to_space_.Contains(o); } | 1141 bool ToSpaceContains(Object* o) { return to_space_.Contains(o); } |
| 1115 bool FromSpaceContains(Object* o) { return from_space_.Contains(o); } | 1142 bool FromSpaceContains(Object* o) { return from_space_.Contains(o); } |
| 1116 | 1143 |
| 1117 bool ToSpaceContains(Address a) { return to_space_.Contains(a); } | 1144 bool ToSpaceContains(Address a) { return to_space_.Contains(a); } |
| 1118 bool FromSpaceContains(Address a) { return from_space_.Contains(a); } | 1145 bool FromSpaceContains(Address a) { return from_space_.Contains(a); } |
| 1119 | 1146 |
| 1147 #ifdef ENABLE_HEAP_PROTECTION | |
| 1148 // Protect/unprotect the space by marking it read-only/writable. | |
| 1149 virtual void Protect(); | |
| 1150 virtual void Unprotect(); | |
| 1151 #endif | |
| 1152 | |
| 1120 #ifdef DEBUG | 1153 #ifdef DEBUG |
| 1121 // Verify the active semispace. | 1154 // Verify the active semispace. |
| 1122 virtual void Verify(); | 1155 virtual void Verify(); |
| 1123 // Print the active semispace. | 1156 // Print the active semispace. |
| 1124 virtual void Print() { to_space_.Print(); } | 1157 virtual void Print() { to_space_.Print(); } |
| 1125 #endif | 1158 #endif |
| 1126 | 1159 |
| 1127 #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) | 1160 #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1128 // Iterates the active semispace to collect statistics. | 1161 // Iterates the active semispace to collect statistics. |
| 1129 void CollectStatistics(); | 1162 void CollectStatistics(); |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1547 | 1580 |
| 1548 // The size of this chunk. | 1581 // The size of this chunk. |
| 1549 size_t size_; | 1582 size_t size_; |
| 1550 | 1583 |
| 1551 public: | 1584 public: |
| 1552 TRACK_MEMORY("LargeObjectChunk") | 1585 TRACK_MEMORY("LargeObjectChunk") |
| 1553 }; | 1586 }; |
| 1554 | 1587 |
| 1555 | 1588 |
| 1556 class LargeObjectSpace : public Space { | 1589 class LargeObjectSpace : public Space { |
| 1557 friend class LargeObjectIterator; | |
| 1558 public: | 1590 public: |
| 1559 explicit LargeObjectSpace(AllocationSpace id); | 1591 explicit LargeObjectSpace(AllocationSpace id); |
| 1560 virtual ~LargeObjectSpace() {} | 1592 virtual ~LargeObjectSpace() {} |
| 1561 | 1593 |
| 1562 // Initializes internal data structures. | 1594 // Initializes internal data structures. |
| 1563 bool Setup(); | 1595 bool Setup(); |
| 1564 | 1596 |
| 1565 // Releases internal resources, frees objects in this space. | 1597 // Releases internal resources, frees objects in this space. |
| 1566 void TearDown(); | 1598 void TearDown(); |
| 1567 | 1599 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1599 | 1631 |
| 1600 // Frees unmarked objects. | 1632 // Frees unmarked objects. |
| 1601 void FreeUnmarkedObjects(); | 1633 void FreeUnmarkedObjects(); |
| 1602 | 1634 |
| 1603 // Checks whether a heap object is in this space; O(1). | 1635 // Checks whether a heap object is in this space; O(1). |
| 1604 bool Contains(HeapObject* obj); | 1636 bool Contains(HeapObject* obj); |
| 1605 | 1637 |
| 1606 // Checks whether the space is empty. | 1638 // Checks whether the space is empty. |
| 1607 bool IsEmpty() { return first_chunk_ == NULL; } | 1639 bool IsEmpty() { return first_chunk_ == NULL; } |
| 1608 | 1640 |
| 1641 #ifdef ENABLE_LOGGING_AND_PROFILING | |
|
Kevin Millikin (Chromium)
2009/03/24 11:42:43
Ooops. Fixed.
| |
| 1642 // Protect/unprotect the space by marking it read-only/writable. | |
| 1643 void Protect(); | |
| 1644 void Unprotect(); | |
| 1645 #endif | |
| 1646 | |
| 1609 #ifdef DEBUG | 1647 #ifdef DEBUG |
| 1610 virtual void Verify(); | 1648 virtual void Verify(); |
| 1611 virtual void Print(); | 1649 virtual void Print(); |
| 1612 void ReportStatistics(); | 1650 void ReportStatistics(); |
| 1613 void CollectCodeStatistics(); | 1651 void CollectCodeStatistics(); |
| 1614 // Dump the remembered sets in the space to stdout. | 1652 // Dump the remembered sets in the space to stdout. |
| 1615 void PrintRSet(); | 1653 void PrintRSet(); |
| 1616 #endif | 1654 #endif |
| 1617 // Checks whether an address is in the object area in this space. It | 1655 // Checks whether an address is in the object area in this space. It |
| 1618 // iterates all objects in the space. May be slow. | 1656 // iterates all objects in the space. May be slow. |
| 1619 bool SlowContains(Address addr) { return !FindObject(addr)->IsFailure(); } | 1657 bool SlowContains(Address addr) { return !FindObject(addr)->IsFailure(); } |
| 1620 | 1658 |
| 1621 private: | 1659 private: |
| 1622 // The head of the linked list of large object chunks. | 1660 // The head of the linked list of large object chunks. |
| 1623 LargeObjectChunk* first_chunk_; | 1661 LargeObjectChunk* first_chunk_; |
| 1624 int size_; // allocated bytes | 1662 int size_; // allocated bytes |
| 1625 int page_count_; // number of chunks | 1663 int page_count_; // number of chunks |
| 1626 | 1664 |
| 1627 | 1665 |
| 1628 // Shared implementation of AllocateRaw, AllocateRawCode and | 1666 // Shared implementation of AllocateRaw, AllocateRawCode and |
| 1629 // AllocateRawFixedArray. | 1667 // AllocateRawFixedArray. |
| 1630 Object* AllocateRawInternal(int requested_size, | 1668 Object* AllocateRawInternal(int requested_size, |
| 1631 int object_size, | 1669 int object_size, |
| 1632 Executability executable); | 1670 Executability executable); |
| 1633 | 1671 |
| 1634 // Returns the number of extra bytes (rounded up to the nearest full word) | 1672 // Returns the number of extra bytes (rounded up to the nearest full word) |
| 1635 // required for extra_object_bytes of extra pointers (in bytes). | 1673 // required for extra_object_bytes of extra pointers (in bytes). |
| 1636 static inline int ExtraRSetBytesFor(int extra_object_bytes); | 1674 static inline int ExtraRSetBytesFor(int extra_object_bytes); |
| 1637 | 1675 |
| 1676 friend class LargeObjectIterator; | |
| 1677 | |
| 1638 public: | 1678 public: |
| 1639 TRACK_MEMORY("LargeObjectSpace") | 1679 TRACK_MEMORY("LargeObjectSpace") |
| 1640 }; | 1680 }; |
| 1641 | 1681 |
| 1642 | 1682 |
| 1643 class LargeObjectIterator: public ObjectIterator { | 1683 class LargeObjectIterator: public ObjectIterator { |
| 1644 public: | 1684 public: |
| 1645 explicit LargeObjectIterator(LargeObjectSpace* space); | 1685 explicit LargeObjectIterator(LargeObjectSpace* space); |
| 1646 LargeObjectIterator(LargeObjectSpace* space, HeapObjectCallback size_func); | 1686 LargeObjectIterator(LargeObjectSpace* space, HeapObjectCallback size_func); |
| 1647 | 1687 |
| 1648 bool has_next() { return current_ != NULL; } | 1688 bool has_next() { return current_ != NULL; } |
| 1649 HeapObject* next(); | 1689 HeapObject* next(); |
| 1650 | 1690 |
| 1651 // implementation of ObjectIterator. | 1691 // implementation of ObjectIterator. |
| 1652 virtual bool has_next_object() { return has_next(); } | 1692 virtual bool has_next_object() { return has_next(); } |
| 1653 virtual HeapObject* next_object() { return next(); } | 1693 virtual HeapObject* next_object() { return next(); } |
| 1654 | 1694 |
| 1655 private: | 1695 private: |
| 1656 LargeObjectChunk* current_; | 1696 LargeObjectChunk* current_; |
| 1657 HeapObjectCallback size_func_; | 1697 HeapObjectCallback size_func_; |
| 1658 }; | 1698 }; |
| 1659 | 1699 |
| 1660 | 1700 |
| 1661 } } // namespace v8::internal | 1701 } } // namespace v8::internal |
| 1662 | 1702 |
| 1663 #endif // V8_SPACES_H_ | 1703 #endif // V8_SPACES_H_ |
| OLD | NEW |