aboutsummaryrefslogtreecommitdiff
path: root/src/alloc/alloc.rs
blob: 6408f23c3231a0b61c51844008f8d4ca60839c9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Kernel Mode Allocator

#![crate_name = "alloc_system"]
#![crate_type = "rlib"]

// Allocators are not allowed to depend on the standard library which in turn
// requires an allocator in order to avoid circular dependencies. This crate,
// however, can use all of libcore.
#![no_std]

// The compiler needs to be instructed that this crate is an allocator in order
// to realize that when this is linked in another allocator like jemalloc should
// not be linked in
#![feature(global_allocator)]
#![feature(default_lib_allocator)]
//#![allocator]



mod pool;

// Listed below are the five allocation functions currently required by custom
// allocators. Their signatures and symbol names are not currently typechecked
// by the compiler, but this is a future extension and are required to match
// what is found below.
//
// Note that the standard `malloc` and `realloc` functions do not provide a way
// to communicate alignment so this implementation would need to be improved
// with respect to alignment in that aspect.

const KMRS_TAG: u32 = 0x4B4D5253; // 'KMRS'
use pool::{ExAllocatePoolWithTag, ExFreePoolWithTag, POOL_TYPE};

extern "C"
{
	// from ntoskrnl
	pub fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
}

#[no_mangle]
pub extern "C" fn __rust_allocate(size: usize, _align: usize) -> *mut u8
{
	unsafe { ExAllocatePoolWithTag(POOL_TYPE::PagedPool, size, KMRS_TAG) }
}

#[no_mangle]
pub extern "C" fn __rust_deallocate(ptr: *mut u8, _old_size: usize, _align: usize)
{
	unsafe { ExFreePoolWithTag(ptr, KMRS_TAG) };
}

#[no_mangle]
pub extern "C" fn __rust_reallocate(old: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8
{
	unsafe {
		// http://en.cppreference.com/w/c/memory/realloc
		let minsize = if size < old_size { size } else { old_size };
		let new = __rust_allocate(size, align);
		if new.is_null() {
			return new;
		}
		if !old.is_null() && old_size > 0 {
			memcpy(new, old, minsize);
			__rust_deallocate(old, old_size, align);
		}
		return new;
	}
}

#[no_mangle]
pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize, _align: usize) -> usize
{
	old_size // this api is not supported
}

#[no_mangle]
pub extern "C" fn __rust_usable_size(size: usize, _align: usize) -> usize
{
	size
}