forked from tree-sitter/tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.rs
108 lines (93 loc) · 3.21 KB
/
util.rs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::os::raw::c_void;
extern "C" {
/// In *Release* builds, the C library links directly against `malloc` and `free`.
///
/// When freeing memory that was allocated by C code, use `free` directly.
#[cfg(not(debug_assertions))]
#[link_name = "free"]
pub fn free_ptr(ptr: *mut c_void);
/// In *Test* builds, the C library is compiled with the `TREE_SITTER_TEST` macro,
/// so all calls to `malloc`, `free`, etc are linked against wrapper functions
/// called `ts_record_malloc`, `ts_record_free`, etc. These symbols are defined
/// in the `tree_sitter_cli::tests::helpers::allocations` module.
///
/// When freeing memory that was allocated by C code, use the `free` function
/// from that module.
#[cfg(debug_assertions)]
#[link_name = "ts_record_free"]
pub fn free_ptr(ptr: *mut c_void);
/// In *Debug* builds, the C library is compiled the same as in test builds: using
/// the wrapper functions. This prevents the C library from having to be recompiled
/// constantly when switching between running tests and compiling with RLS.
///
/// But we don't want to actually record allocations when running the library in
/// debug mode, so we define symbols like `ts_record_malloc` to just delegate to
/// the normal `malloc` functions.
#[cfg(all(debug_assertions, not(test)))]
fn malloc(size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn calloc(count: usize, size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn free(ptr: *mut c_void);
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_malloc(size: usize) -> *const c_void {
malloc(size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_calloc(count: usize, size: usize) -> *const c_void {
calloc(count, size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_realloc(ptr: *mut c_void, size: usize) -> *const c_void {
realloc(ptr, size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_free(ptr: *mut c_void) {
free(ptr)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
extern "C" fn ts_toggle_allocation_recording(_: bool) -> bool {
false
}
pub struct CBufferIter<T> {
ptr: *mut T,
count: usize,
i: usize,
}
impl<T> CBufferIter<T> {
pub unsafe fn new(ptr: *mut T, count: usize) -> Self {
Self { ptr, count, i: 0 }
}
}
impl<T: Copy> Iterator for CBufferIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let i = self.i;
if i >= self.count {
None
} else {
self.i += 1;
Some(unsafe { *self.ptr.offset(i as isize) })
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.count - self.i;
(remaining, Some(remaining))
}
}
impl<T: Copy> ExactSizeIterator for CBufferIter<T> {}
impl<T> Drop for CBufferIter<T> {
fn drop(&mut self) {
unsafe {
free_ptr(self.ptr as *mut c_void);
}
}
}