Bump allocator
This commit is contained in:
		
							parent
							
								
									19077d69db
								
							
						
					
					
						commit
						9f0819dafb
					
				
					 4 changed files with 61 additions and 0 deletions
				
			
		|  | @ -15,6 +15,7 @@ arch_test = { path = "arch/test" } | ||||||
| 
 | 
 | ||||||
| [target.'cfg(target_arch = "x86_64")'.dependencies] | [target.'cfg(target_arch = "x86_64")'.dependencies] | ||||||
| arch_x86_64 = { path = "arch/x86_64" } | arch_x86_64 = { path = "arch/x86_64" } | ||||||
|  | bump_allocator = { path = "alloc/bump_allocator"} | ||||||
| 
 | 
 | ||||||
| [profile.dev] | [profile.dev] | ||||||
| panic = "abort" | panic = "abort" | ||||||
|  |  | ||||||
							
								
								
									
										3
									
								
								alloc/bump_allocator/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								alloc/bump_allocator/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | ||||||
|  | [package] | ||||||
|  | name = "bump_allocator" | ||||||
|  | version = "0.1.0" | ||||||
							
								
								
									
										47
									
								
								alloc/bump_allocator/src/lib.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								alloc/bump_allocator/src/lib.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,47 @@ | ||||||
|  | //! A simple allocator that never frees, for testing
 | ||||||
|  | 
 | ||||||
|  | #![feature(allocator)] | ||||||
|  | #![allocator] | ||||||
|  | #![no_std] | ||||||
|  | 
 | ||||||
|  | pub static mut HEAP: usize = 10*1024*1024; | ||||||
|  | 
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 { | ||||||
|  |     unsafe { | ||||||
|  |         let mut ret = HEAP; | ||||||
|  |         if align.is_power_of_two() { | ||||||
|  |             ret += (align - 1); | ||||||
|  |             ret &= !(align - 1); | ||||||
|  |         } else { | ||||||
|  |             assert_eq!(align, 0); | ||||||
|  |         } | ||||||
|  |         HEAP = ret + size; | ||||||
|  |         ret as *mut u8 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub extern fn __rust_usable_size(size: usize, align: usize) -> usize { | ||||||
|  |     size | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) { | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize, align: usize) -> *mut u8 { | ||||||
|  |     use core::{ptr, cmp}; | ||||||
|  | 
 | ||||||
|  |     let new_ptr = __rust_allocate(new_size, align); | ||||||
|  |     unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) }; | ||||||
|  |     __rust_deallocate(ptr, size, align); | ||||||
|  |     new_ptr | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub extern fn __rust_reallocate_inplace(ptr: *mut u8, size: usize, new_size: usize, align: usize) -> usize { | ||||||
|  |     size | ||||||
|  | } | ||||||
|  | @ -64,6 +64,8 @@ | ||||||
| //! An error will be returned, `ENOBUFS`, if the buffer is not long enough for the name.
 | //! An error will be returned, `ENOBUFS`, if the buffer is not long enough for the name.
 | ||||||
| //! In this case, it is recommended to add one page, 4096 bytes, to the buffer and retry.
 | //! In this case, it is recommended to add one page, 4096 bytes, to the buffer and retry.
 | ||||||
| 
 | 
 | ||||||
|  | #![feature(alloc)] | ||||||
|  | #![feature(collections)] | ||||||
| #![feature(const_fn)] | #![feature(const_fn)] | ||||||
| #![feature(lang_items)] | #![feature(lang_items)] | ||||||
| #![feature(question_mark)] | #![feature(question_mark)] | ||||||
|  | @ -84,6 +86,14 @@ extern crate arch_test as arch; | ||||||
| #[macro_use] | #[macro_use] | ||||||
| extern crate arch_x86_64 as arch; | extern crate arch_x86_64 as arch; | ||||||
| 
 | 
 | ||||||
|  | /// Bump allocator
 | ||||||
|  | #[cfg(all(not(test), target_arch = "x86_64"))] | ||||||
|  | extern crate bump_allocator; | ||||||
|  | 
 | ||||||
|  | extern crate alloc; | ||||||
|  | #[macro_use] | ||||||
|  | extern crate collections; | ||||||
|  | 
 | ||||||
| /// Context management
 | /// Context management
 | ||||||
| pub mod context; | pub mod context; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jeremy Soller
						Jeremy Soller