Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x86_64::paging): Implement paging & kernel remap #103

Merged
merged 223 commits into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
223 commits
Select commit Hold shift + click to select a range
5c8ca5f
[notes]
hawkw Jun 11, 2016
d3bdafb
Merge pull request #33 from hawkw/master
hawkw Jun 11, 2016
eb0f99d
[kernel,x86_64] tweak type signature for Addr slightly
hawkw Jun 11, 2016
06b5ec6
[memory] re-export stuff from arch
hawkw Jun 11, 2016
564a8ec
[x86_64, kernel] make Page trait actually work
hawkw Jun 11, 2016
cd3eace
[kernel] start on making allocator API smarter
hawkw Jun 11, 2016
0cd5ea6
[kernel] start implementing smart allocator API
hawkw Jun 11, 2016
f04fad5
[kernel] remove an unused import
hawkw Jun 11, 2016
04f5b03
[kernel] trying to make new paging types play nice
hawkw Jun 11, 2016
fdd533e
[memory] Fix unsafe invocations in alloc
hawkw Jun 13, 2016
d3ed299
[kernel] actually add memory::alloc module
hawkw Jun 13, 2016
eaa09ad
[kernel] Add Arrayvec crate
hawkw Jun 13, 2016
f76af96
[kernel] start on first-fit page frame allocator
hawkw Jun 13, 2016
789d552
[kernel, x86_64] made pages Copy + Clone again
hawkw Jun 13, 2016
08bc27c
[kernel] start on first fit alloc for page frames
hawkw Jun 13, 2016
e51e9d0
[kernel] add const fns for making addrs into ptrs
hawkw Jun 14, 2016
856ea3b
[kernel, x86_64] rename method on mapper, add unmap method
hawkw Jun 14, 2016
0dce0ef
[x86_64] tweaks to pml4
hawkw Jun 14, 2016
75de965
[x86_64] work on paging
hawkw Jun 14, 2016
4547235
[x86_64] paging tweaks
hawkw Jun 14, 2016
4f486f9
[kernel, x86_64] remove lots of unneeded generics in paging
hawkw Jun 14, 2016
3ec675b
[x86_64] implement Mapper::unmap() for PML4
hawkw Jun 14, 2016
82792c9
[x86_64] ActivePML4::unmap() flushes the TLB
hawkw Jun 15, 2016
0d476a7
[x86_64] add functions for testing paging
hawkw Jun 15, 2016
15a2d6d
[x86_all] paging tests work better now
hawkw Jun 15, 2016
72af564
[kernel, x86_64] we can now allocate frames using the kernel heap
hawkw Jun 15, 2016
1089048
[kernel, x86_64] address types stringify nicely
hawkw Jun 15, 2016
66045b3
[x86_64] add stack space
hawkw Jun 15, 2016
e1babd1
[x86_64] add more tests for unmap
hawkw Jun 15, 2016
9381896
[kernel] remove unused variables warnings for unimplemented fns
hawkw Jun 15, 2016
91b6c7a
[kernel] remove unused imports
hawkw Jun 15, 2016
8db3ec7
Merge branch 'master' into paging-dev
hawkw Jun 15, 2016
9fcb002
[kernel] fix issues after merge
hawkw Jun 15, 2016
7a3643d
[kernel] use page shift rather than division
hawkw Jun 15, 2016
27b9740
[x86_all] add cr3::pd_base_frame() function
hawkw Jun 15, 2016
605b8b3
[x86_64] implement convert::From<elf::Section> for EntryFlags
hawkw Jun 15, 2016
e17ee41
Merge branch 'master' into paging-dev
hawkw Jun 16, 2016
c8bdc04
[x86_64, kernel] Page types can be implemented with custom_derive
hawkw Jun 16, 2016
d0eaf25
[kernel] Page/Frame ranges can be core::ops::Range now
hawkw Jun 16, 2016
784337c
[kernel] remove an unused import
hawkw Jun 17, 2016
39dd701
[kernel] start on temporary pages for kernel remap
hawkw Jun 17, 2016
4c7fe37
[x86_64] make GDT type pub to suppress warning
hawkw Jun 17, 2016
0c0033b
[x86_all, x86_64] move GDT to x86_all, impl DTable for GDT
hawkw Jun 17, 2016
a71397c
[x86_64] improve how pages are flushed from the TLB
hawkw Jun 17, 2016
ae877d2
[x86_64, kernel] more work on temp pages
hawkw Jun 17, 2016
3f5e910
[kernel] add function for checking if addresses are page aligned
hawkw Jun 17, 2016
b1f9671
[x86_64, kernel] remove arch page table indexing from kernel
hawkw Jun 17, 2016
8af2f51
[kernel] add docs to temp page
hawkw Jun 17, 2016
b8c6d91
[x86_64] add tab::flush_all()
hawkw Jun 17, 2016
36c1b68
[kernel] remove unused import
hawkw Jun 17, 2016
57d1d6a
[x86_64] first pass on MSR
hawkw Jun 18, 2016
be6f401
[x86_all] made control register %cr3 API much more fluent
hawkw Jun 18, 2016
75cfc63
[x86_all] fix pointer size issues in control_regs::cr3
hawkw Jun 18, 2016
b5e3876
[x86_64, kernel] add all needed functions for kernel remap
hawkw Jun 18, 2016
376dde9
[x86_64] more work on kernel remap for x86_64
hawkw Jun 21, 2016
5a80a0c
[kernel] add convert::From implementations for PageRange
hawkw Jun 21, 2016
594ac52
[kernel] fix issues in FrameRange::from(elf::Section)
hawkw Jun 21, 2016
6e3198b
[x86_64] kernel_remap identity maps ELF sections
hawkw Jun 21, 2016
e6c1d8a
[x86_64] finish paging::kernel_remap()
hawkw Jun 21, 2016
671402e
[x86_64] change linker script to put multiboot info in .rodata
hawkw Jun 21, 2016
ab03b08
[x86_64] arch_init() sets flags for paging
hawkw Jun 21, 2016
89dad82
[kernel] kernel_init() remaps the kernel; needs refactoring later
hawkw Jun 21, 2016
1a779e3
[x86_64] add missing colon in linker script
hawkw Jun 21, 2016
492a84a
[x86_64] finish implementing InactivePageTable::new()
hawkw Jun 21, 2016
2443c2c
[multiboot2] fix u32 overflow in Info::has_end()
hawkw Jun 21, 2016
99e51a2
[kernel] made panic handler not clear the screen
hawkw Jun 21, 2016
1dfe8e1
[kernel] can use multiboot to get kernel frame range
hawkw Jun 24, 2016
7a3cf81
[kernel, x86_64] misc attempting to fix remap
hawkw Jun 24, 2016
997c5cf
Merge branch 'master' into paging-dev
hawkw Oct 6, 2016
537f4e0
[kernel] start updating paging to build against latest nightly
hawkw Oct 7, 2016
729c0c1
Merge pull request #46 from hawkw/master
hawkw Oct 7, 2016
c63800d
Merge remote-tracking branch 'origin/master' into paging-dev
hawkw Jan 18, 2017
284ad0a
[cpu] cpu crate depends on memory crate now
hawkw Jan 20, 2017
da12ab5
[memory, paging] start moving paging stuff into paging crate
hawkw Jan 20, 2017
eaa8873
[memory, paging] move base page types back to mem module (for alloc)
hawkw Jan 20, 2017
b7c5a05
[cpu] fix unused import in x86_64 segment code
hawkw Jan 20, 2017
d33655d
[elf] add missing imports in elf
hawkw Jan 20, 2017
2d57292
[alloc] put FrameAllocator type back into alloc
hawkw Jan 20, 2017
4161a73
[alloc] put base Allocator trait back into Alloc
hawkw Jan 20, 2017
e2e79e8
[alloc] first fit allocator compiles now
hawkw Jan 20, 2017
e1a7a26
[cpu, paging] move cr3 stuff to paging module
hawkw Jan 21, 2017
4eed2c0
[paging] move x86_64 paging code to more reasonable module
hawkw Jan 21, 2017
b3f6716
[paging] fix import issues in paging module
hawkw Jan 21, 2017
483c0ee
[kernel] fix merge conflicts in kernel main.rs
hawkw Jan 21, 2017
6737e37
[kernel] fix merge conflicts in x86_64 arch_init
hawkw Jan 21, 2017
04ce6d7
[kernel] remove unused imports in kernel
hawkw Jan 21, 2017
2acdb45
[kernel] fix syntax in some kinfoln!s
hawkw Jan 21, 2017
85a541f
[kernel] fix imports in x86 multiboot2 module
hawkw Jan 21, 2017
88cc704
[alloc, kernel] temp. disable buddy frame allocator
hawkw Jan 21, 2017
0fca32e
[alloc, kernel] fix accidentally nuked system buddy block allocator
hawkw Jan 21, 2017
2161514
[alloc] re-add old, bad buddy frame allocator code
hawkw Jan 21, 2017
a9f566d
[paging, alloc, kernel] fix imports in paging, etc
hawkw Jan 21, 2017
0a2f532
[kernel, memory] move init params to memory module
hawkw Jan 22, 2017
ad0ef3d
[cpu, paging] move some cr3 code from paging back to cpu
hawkw Jan 22, 2017
3490ac2
[memory] memory module re-exports PAGE_SIZE constant as well
hawkw Jan 22, 2017
4dd2087
[paging] fix use of struct as immediate in tlb::invlpg
hawkw Jan 22, 2017
a4ddf1f
[kernel, params, memory, paging] factor out init params to own crate
hawkw Jan 22, 2017
cdf1c21
[params] add functions to init params
hawkw Jan 22, 2017
77fe0ef
[cpu, paging, kernel] remove unused imports & feature flags
hawkw Jan 22, 2017
9bee77f
[alloc] fix tests for buddy-block allocator
hawkw Jan 22, 2017
1703e30
[memory] remove implementation of deprecated trait `num::Zero`
hawkw Jan 22, 2017
7de1dcb
[kernel] fix unresolved merge conflict in linker script
hawkw Jan 22, 2017
ac0ab66
[params] quick pass on params knowing about the multiboot info
hawkw Jan 22, 2017
39ab0c6
[params] params now has default impl
hawkw Jan 22, 2017
83ed142
[paging] kernel_remap function can borrow init params
hawkw Jan 22, 2017
7abd575
[kernel] move kernel remap out of arch_init
hawkw Jan 22, 2017
3eac4f6
[kernel] made bad ASCII graphic different (better???)
hawkw Jan 22, 2017
013297f
[kernel] add more information to bad ASCII graphic
hawkw Jan 22, 2017
e4fc0b8
[alloc] reduce namespace "stuttering" for buddy allocator
hawkw Jan 23, 2017
63af2d0
[kernel] removed left-over heap initialization code from arch_init
hawkw Jan 23, 2017
a8f242e
[memory] implement fmt::Pointer for Addr types
hawkw Jan 23, 2017
75124dc
[kernel] refactor arch_init a little
hawkw Jan 23, 2017
440df2a
[kernel] remove unused imports
hawkw Jan 23, 2017
383c9e6
[cpu] add documentation to GDT
hawkw Jan 23, 2017
dc40269
[paging] add implementation to current_pml4 function
hawkw Jan 23, 2017
ac387f6
[docs] use conventional name for safety section in RustDocs
hawkw Jan 23, 2017
9c68fca
[paging] add more safety docs to paging cr3 module
hawkw Jan 23, 2017
96a315b
[alloc, cpu, paging, kernel] misc. documentation improvements
hawkw Jan 23, 2017
ce53dff
[alloc] start on a borrowed allocation handle for the heap alloc
hawkw Jan 23, 2017
d563bbb
Merge remote-tracking branch 'origin' into paging-dev
hawkw Jan 31, 2017
860d7e2
[memory, params] fix linkage of stack base/top addrs
hawkw Jan 31, 2017
e83bde2
[alloc] add allocator API from RFC 1398
hawkw Jan 31, 2017
e22cc59
[alloc] implement the RFC1398 Allocator trait for the buddy heap
hawkw Feb 2, 2017
c21c75a
[alloc] rename HeapAllocator to Heap
hawkw Feb 2, 2017
5337ceb
[alloc] remove "framesque" trait
hawkw Feb 2, 2017
25e53aa
[memory, paging] addresses can now be aligned up/down
hawkw Feb 3, 2017
71a5eb1
[alloc][test] fix tests for buddy allocator to conform to new API
hawkw Feb 3, 2017
037d8b1
[alloc] start adding bump pointer allocator
hawkw Feb 3, 2017
a4ba770
[alloc] working bump pointer impl
hawkw Feb 3, 2017
86e4535
[x86_64] skip non-allocated kernel ELF sections
hawkw Feb 3, 2017
8dbf1f5
Merge branch 'master' into paging-dev
hawkw Feb 4, 2017
e7da0eb
[memory, params, kernel] fix bad handling of heap addresses in params
hawkw Feb 4, 2017
1af54fa
[x86_64] reserve a non-tiny amount of heap space for now
hawkw Feb 4, 2017
873d95e
[alloc] start rewriting system allocators
hawkw Feb 4, 2017
82a30d8
[alloc] more work on tiered allocators
hawkw Feb 6, 2017
1c000cc
[alloc] refactor alloc::system module
hawkw Feb 7, 2017
f89ed2f
[alloc] system allocator can now lend out borrowed handles
hawkw Feb 7, 2017
e9fc99d
[alloc] slightly more readable lifetime parameters
hawkw Feb 7, 2017
20fdf5b
[alloc] moved borrowed handles into their own module
hawkw Feb 7, 2017
73a97f6
[alloc] more work on borrowed handles
hawkw Feb 21, 2017
57fb3a1
[alloc] move mutex outside of FrameAllocator API
hawkw Feb 21, 2017
ebf681a
[alloc] fix missing import for borrowed pointers
hawkw Feb 21, 2017
cbd2191
[memory, params] add notes
hawkw Feb 28, 2017
296e94b
[alloc] add benchmarks for `next_pow2`
hawkw Mar 4, 2017
21756b4
[alloc] add benchmarks for is_pow2
hawkw Mar 4, 2017
605ccc9
[alloc] use standard lib next_power_of_two function
hawkw Mar 4, 2017
7ae4288
[alloc] fixed attempting to compile benches without bench flag
hawkw Mar 4, 2017
001c402
feat(alloc): additional work on allocator places
hawkw Mar 17, 2017
b592ab3
Merge branch 'master' into paging-dev
hawkw May 25, 2017
018ccef
fix(util::Align): replace num::One with iter::Step
hawkw May 25, 2017
8595a43
fix(memory): delete reference to module removed in merge
hawkw May 25, 2017
49012f5
fix(elf): temporary hacky fix for convert::Into<FrameRange> for Section
hawkw May 25, 2017
f2e70a7
refac(memory::Page): make Page conversion traits a little more ergonomic
hawkw May 25, 2017
0b7a514
fix(elf): make converting elf Sections into FrameRanges cross platform
hawkw May 25, 2017
607ba69
fix(alloc): update a bunch of uses of ptr::Unique to use new API
hawkw May 25, 2017
2ebb183
fix(paging): fix type of elf section header -> PT entry flags conversion
hawkw May 25, 2017
9ce0c87
fix(paging): update paging module to use changed ELF API from 9d2eb03…
hawkw May 25, 2017
c0ae079
refac(elf): elf Section.address() and .end_address() now return PAddr
hawkw May 25, 2017
c4e727d
fix(params): elf sections iterator should live for the static lfietime
hawkw May 25, 2017
50b60cc
fix(multiboot2): use changed ELF API from 9d2eb03cd444cfa89867b1ffe20…
hawkw May 25, 2017
03ab2b7
fix(multiboot2): ensure that the ELF tag lives for the static lifetime
hawkw May 25, 2017
5f1058b
fix(x86_64::arch_init): update to match APIs that changed on master
hawkw May 25, 2017
21f9b2d
fix(x86_64::arch_init): format the multiboot tag address as a pointer
hawkw May 25, 2017
5686bc6
fix(elf): ELF section header name indices are ALWAYS 32-bits
hawkw May 25, 2017
81442f7
refac(elf): ELF sections iterator now uses ptr::offset()
hawkw May 25, 2017
1be805e
Merge branch 'master' into paging-dev
hawkw May 25, 2017
2e6d30b
refac(buddy::Heap): use Unique pointer rather than raw ptr
hawkw May 26, 2017
70b350a
style(heap::Buddy): use Address type alias instead of *mut u8
hawkw May 26, 2017
4277152
fix(alloc): temporarily re-enable old heap glue
hawkw May 26, 2017
ee38d74
feat(memory): add const constructor to {V, P}Addr
hawkw May 26, 2017
3f7a676
feat(x86_64::task): make TSS slightly fancier
hawkw May 26, 2017
a7c771c
feat(alloc::Stack): start on an allocator for new stack frames
hawkw May 26, 2017
de69522
refac(alloc): move frame allocation to its own module
hawkw May 26, 2017
fdf37bb
feat(params): start on cross-platform representation of memory maps
hawkw May 26, 2017
8ee19b6
feat(alloc): start bringing back frame allocators that work
hawkw May 26, 2017
0911b05
Merge branch 'double-fault-tss' into paging-dev
hawkw May 26, 2017
c8e778f
feat(alloc::frame::MemMapAllocator): add working mem-map allocator
hawkw May 26, 2017
52903b6
fix(alloc::frame::MemMapAllocator): fix issues with pattern bindings
hawkw May 26, 2017
63ec370
refac(paging::x86_64): rewrite paging code to use new frame alloc API
hawkw May 26, 2017
5a6d8db
fix(multiboot2): fix issues in convert::Into<params::mem::Area> impl
hawkw May 26, 2017
0b68256
fix(x86_64::arch_init): fix missing mutability for InitParams
hawkw May 26, 2017
da5e945
style(alloc::frame::mem_map): make next_area() more ideomatic
hawkw May 26, 2017
fd5ffa2
fix(params): fix stack overflow caused by too-large memory areas array
hawkw May 26, 2017
d2d9fc4
Merge remote-tracking branch 'origin' into paging-dev
hawkw May 26, 2017
e6d5ca8
feat(memory::Page): add function for getting end address of a Page
hawkw May 27, 2017
daf9421
feat(paging::Stack): first pass on stack alloc, moved to paging crate
hawkw May 27, 2017
0967138
refac(paging::stack::StackAllocator): StackAllocator can be a trait
hawkw May 27, 2017
dd94320
docs(alloc::frame::mem_map): add missing module docs
hawkw May 27, 2017
69e1cb1
style(alloc): remove dead code, unused imports, etc
hawkw May 27, 2017
6b06afa
style(paging): remove dead code, unused imports, etc
hawkw May 27, 2017
7ce8319
feat(memory::Page): add function for getting end address of a Page
hawkw May 27, 2017
6071ed4
feat(paging::Stack): first pass on stack alloc, moved to paging crate
hawkw May 27, 2017
69a2b41
refac(paging::stack::StackAllocator): StackAllocator can be a trait
hawkw May 27, 2017
9b1a974
chore(Makefile): make exception task logs to serial
hawkw May 27, 2017
7aeda4a
fix(params): fix issue where ranges in mem params weren't inclusive
hawkw May 27, 2017
70fb2ca
feat(memory): nicer fmt::Debug impls for Frame/Page
hawkw May 27, 2017
8b41838
fix(x86_64::arch_init): use ELF section END addr for finding kernel end
hawkw May 27, 2017
16de6bb
feat(paging::x86_64): kernel remapping actually works on x86_64 now!
hawkw May 27, 2017
3729510
feat(memory::x86_64::PhysicalPage): nicer frame debug messages
hawkw May 27, 2017
fe069ff
feat(params::InitParams): init params now carries ELF sections iterator
hawkw May 27, 2017
cee1676
fix(x86_64/linker.ld): page align the multiboot header section
hawkw May 27, 2017
5f1c28f
fix(paging): fix issue where `using` didn't remap the temp page
hawkw May 27, 2017
d0b8d05
feat(paging): add fmt::Debug instances for page tables &c
hawkw May 27, 2017
1800c7a
fix(cpu::x86_64::msr): fix issues setting MSR
hawkw May 27, 2017
0bc86bb
Merge branch 'double-fault-tss' of github.com:hawkw/sos-kernel into d…
hawkw May 27, 2017
3681b6f
chore(paging): fix messed up merge (oops)
hawkw May 27, 2017
bc2748b
fix(x86_64/linker.ld): put boot/GDT in a page-aligned ELF section
hawkw May 28, 2017
d50a001
feat(kernel_init): add the new attempt!() macro for nicer init logging
hawkw May 28, 2017
47d8901
fix(x86_64::arch_init): temporary fix for writing NXE bit to IA32_EFER
hawkw May 28, 2017
c067607
fix(alloc::frame::mem_map): MemMapFrameAllocator no longer clones
hawkw May 28, 2017
6faec05
feat(vga): panic messages also written to serial, if available
hawkw May 28, 2017
2e9299a
fix(cpu::x86_all::control_regs::cr3::write()): add `memory` clobber
hawkw May 28, 2017
e1f6633
fix(boot): temporarily remove uses of `set_flags!` macro
hawkw May 28, 2017
bce2c25
feat(paging::x86_64): add tracing logging to kernel_remap, unmap
hawkw May 28, 2017
994c61b
fix(paging::x86_64::kernel_remap): minor bugfixes
hawkw May 28, 2017
c6d5367
feat(paging::kernel_remap): log important stuff to the VGA log
hawkw May 28, 2017
1c74e0e
feat(kernel_init): nicer VGA logging for kernel remap
hawkw May 28, 2017
85d501d
fix(paging::x86_64::kernel_remap): fix extra whitespace in log message
hawkw May 28, 2017
2ad5d41
feat(memory::macros): add PartialEq/PartialOrd<Repr> instances to Addrs
hawkw May 28, 2017
0413e36
fix(Page): remove assertions in phys page that only needed to be in virt
hawkw May 28, 2017
7f0375e
fix(kernel_init): fix some wonky formatting in kernel_init vga log
hawkw May 28, 2017
575c5bc
chore: remove unused files
hawkw May 28, 2017
4bd062f
chore: remove a bunch of dead code
hawkw May 28, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,21 @@ trace = []

[dependencies]
rlibc = "0.1.4"
spin = "0.3.5"
spin = "0.4.5"
once = "0.3.2"
bitflags = "0.7"
cpu = { path = "cpu" }
memory = { path = "memory" }
util = { path = "util" }
elf = { path = "elf" }
paging = { path = "paging" }
params = { path = "params" }

[dependencies.log]
version = "0.3.6"
default-features = false
features = ["release_max_level_info"]

# [dependencies.custom_derive]
# git = "https://github.com/DanielKeep/rust-custom-derive.git"
# version = "0.1.6"
# default-features = false

[dependencies.lazy_static]
version = "0.2.*"
features = ["spin_no_std"]
Expand All @@ -66,7 +63,7 @@ features = ["kinfo", "system_term"]

[dependencies.alloc]
path = "alloc"
features = ["buddy_as_system"]
features = ["buddy", "system", "borrow", "buddy_as_system"]

[dependencies.clippy]
version = "0.0.60"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ HELP_FUN = \
.PHONY: all clean kernel run iso cargo help gdb test doc release-iso release-run release-kernel

exception: $(iso) ##@build Run the kernel, dumping the state from QEMU if an exception occurs
@qemu-system-x86_64 -s -hda $(iso) -d int -no-reboot
@qemu-system-x86_64 -s -hda $(iso) -d int -no-reboot -serial file:$(CURDIR)/target/$(target)/serial-$(TIMESTAMP).log

doc: ##@utilities Make RustDoc documentation
@xargo doc
Expand Down
26 changes: 21 additions & 5 deletions alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ version = "0.1.0"
authors = ["Eliza Weisman <[email protected]>"]

[features]
default = ["buddy"]
default = ["buddy", "bump_ptr", "borrow"]
buddy = ["sos_intrusive"]
as_system = []
buddy_as_system = ["spin", "as_system"]
buddy_as_system = ["buddy", "once"]
system = []
bump_ptr = []
placement_in = ["system"]
borrow = []
first_fit = ["arrayvec"]
bench = []

[dependencies.log]
version = "0.3.6"
Expand All @@ -18,9 +23,20 @@ features = ["release_max_level_info"]
path = "../memory"

[dependencies.spin]
version = "0.3.5"
optional = true
version = "^0.4.5"

[dependencies.sos_intrusive]
path = "../sos_intrusive"
optional = true

[dependencies.arrayvec]
version = "0.3.16"
default-features = false
optional = true

[dependencies.once]
version = "^0.3.2"
optional = true

[dependencies.params]
path = "../params"
121 changes: 121 additions & 0 deletions alloc/src/borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use super::{Address, Allocator, Layout};
use ptr::Unique;
use ops::{Deref, DerefMut};
use spin::Mutex;

pub trait Lender {
type Borrowed;
fn borrow(&self) -> Self::Borrowed;
}

/// A borrowed handle on a heap allocation with a specified lifetime.
///
/// This automatically deallocates the allocated object when the borrow's
/// lifetime ends. It also ensures that the borrow only lives as long as the
/// allocator that provided it, and that the borrow is dropped if the allocator
/// is dropped.
// TODO: can we replace this with one generic implementation that works for
// borrowed pointers, objects, and arrays? can that code be reused for
// BorrowedFrame/BorrowedFrameRange as well?
// - eliza, 02/21/2017
pub struct BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
ptr: Unique<u8>
, layout: Layout
, allocator: &'alloc Mutex<A>
}

impl<'alloc, A> BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {

#[inline]
pub fn new( ptr: Address
, layout: Layout
, allocator: &'alloc Mutex<A>)
-> Self {
BorrowedPtr { ptr: unsafe { Unique::new(ptr) }
, layout: layout
, allocator: allocator
}

}
}

impl<'alloc, A> Deref for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
type Target = Unique<u8>;
fn deref(&self) -> &Self::Target { &self.ptr }
}

impl<'alloc, A> Drop for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
unsafe {
self.allocator.lock().dealloc(self.ptr.as_ptr(), self.layout.clone())
}
}
}

/// A borrowed handle on a heap allocation with a specified lifetime.
///
/// This automatically deallocates the allocated object when the borrow's
/// lifetime ends. It also ensures that the borrow only lives as long as the
/// allocator that provided it, and that the borrow is dropped if the allocator
/// is dropped.
pub struct Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
value: Unique<T>
, allocator: &'alloc Mutex<A>
}

impl<'alloc, A, T> Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {

#[inline]
pub fn new( value: Unique<T>, allocator: &'alloc Mutex<A>)
-> Self {
Borrowed { value: value
, allocator: allocator
}

}
}

impl<'alloc, A, T> Deref for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
type Target = T;
fn deref(&self) -> &Self::Target { unsafe { self.value.as_ref() } }
}

impl<'alloc, A, T> DerefMut for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.value.as_mut() }
}
}

impl<'alloc, A, T> Drop for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
use mem::drop;
let address = self.value.as_ptr() as Address;
// ensure we drop the object _before_ deallocating it, so that
// the object's destructor gets run first
// i hope this is correct...
drop(self.value.as_ptr());
unsafe {
self.allocator.lock()
.dealloc( address
, Layout::for_value(self.value.as_ref()))
}
}
}
50 changes: 50 additions & 0 deletions alloc/src/buddy/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl PowersOf2 for usize {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "bench")]
use test::{self, Bencher};

#[test]
fn test_next_pow2() {
Expand Down Expand Up @@ -118,4 +120,52 @@ mod tests {
assert_eq!(5, 32.log2());
assert_eq!(10, 1024.log2());
}

#[cfg(feature = "bench")]
#[bench]
fn our_next_pow2(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_pow2())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn std_next_power_of_two(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_power_of_two())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn our_is_pow2(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|ref x: usize| x.is_pow2())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn std_is_power_of_two(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_power_of_two())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}
}
Loading