| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Assertions.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-13 11:01:44 +01:00
										 |  |  | #include <AK/NonnullOwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-27 19:01:26 +02:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:20:30 +01:00
										 |  |  | #include <LibJS/Heap/HeapBlock.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-21 13:12:16 +01:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-13 11:01:44 +01:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-13 11:01:44 +01:00
										 |  |  | NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, size_t cell_size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-21 13:12:16 +01:00
										 |  |  |     char name[64]; | 
					
						
							|  |  |  |     snprintf(name, sizeof(name), "LibJS: HeapBlock(%zu)", cell_size); | 
					
						
							| 
									
										
										
										
											2021-05-27 19:01:26 +02:00
										 |  |  |     auto* block = static_cast<HeapBlock*>(heap.block_allocator().allocate_block(name)); | 
					
						
							| 
									
										
										
										
											2020-03-13 11:01:44 +01:00
										 |  |  |     new (block) HeapBlock(heap, cell_size); | 
					
						
							|  |  |  |     return NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HeapBlock::HeapBlock(Heap& heap, size_t cell_size) | 
					
						
							|  |  |  |     : m_heap(heap) | 
					
						
							|  |  |  |     , m_cell_size(cell_size) | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(cell_size >= sizeof(FreelistEntry)); | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void HeapBlock::deallocate(Cell* cell) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(is_valid_cell_pointer(cell)); | 
					
						
							|  |  |  |     VERIFY(!m_freelist || is_valid_cell_pointer(m_freelist)); | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |     VERIFY(cell->state() == Cell::State::Live); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(!cell->is_marked()); | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |     cell->~Cell(); | 
					
						
							| 
									
										
										
										
											2020-06-01 17:01:04 +03:00
										 |  |  |     auto* freelist_entry = new (cell) FreelistEntry(); | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |     freelist_entry->set_state(Cell::State::Dead); | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |     freelist_entry->next = m_freelist; | 
					
						
							|  |  |  |     m_freelist = freelist_entry; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |