Skip to content

Commit 802d41f

Browse files
committed
libc: switch free to the proper signature
This does not attempt to fully propagate the mutability everywhere, but gives new code a hint to avoid the same issues.
1 parent fce7922 commit 802d41f

File tree

11 files changed

+37
-37
lines changed

11 files changed

+37
-37
lines changed

doc/guide-ffi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<T: Send> Drop for Unique<T> {
230230
// We need to move the object out of the box, so that
231231
// the destructor is called (at the end of this scope.)
232232
ptr::replace_ptr(self.ptr, x);
233-
free(self.ptr as *c_void)
233+
free(self.ptr as *mut c_void)
234234
}
235235
}
236236
}

src/libextra/c_vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ mod tests {
172172
let mem = malloc_raw(n);
173173

174174
CVec::new_with_dtor(mem as *mut u8, n,
175-
proc() { libc::free(mem as *c_void); })
175+
proc() { libc::free(mem as *mut c_void); })
176176
}
177177
}
178178

src/libextra/flate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
5353
assert!(res as int != 0);
5454
let out = vec::raw::from_buf_raw(res as *u8,
5555
outsz as uint);
56-
libc::free(res);
56+
libc::free(res as *mut c_void);
5757
out
5858
}
5959
}
@@ -76,7 +76,7 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
7676
assert!(res as int != 0);
7777
let out = vec::raw::from_buf_raw(res as *u8,
7878
outsz as uint);
79-
libc::free(res);
79+
libc::free(res as *mut c_void);
8080
out
8181
}
8282
}

src/libnative/io/file.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,13 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
548548
let p = Path::new(p);
549549
let star = p.join("*");
550550
as_utf16_p(star.as_str().unwrap(), |path_ptr| {
551-
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint) as *c_void;
551+
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
552552
let find_handle = FindFirstFileW(path_ptr, wfd_ptr as HANDLE);
553553
if find_handle as libc::c_int != INVALID_HANDLE_VALUE {
554554
let mut paths = ~[];
555555
let mut more_files = 1 as libc::c_int;
556556
while more_files != 0 {
557-
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
557+
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr as *c_void);
558558
if fp_buf as uint == 0 {
559559
fail!("os::list_dir() failure: got null ptr from wfd");
560560
}
@@ -567,7 +567,7 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
567567
more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE);
568568
}
569569
FindClose(find_handle);
570-
free(wfd_ptr);
570+
free(wfd_ptr as *mut c_void);
571571
Ok(paths)
572572
} else {
573573
Err(super::last_error())

src/librustc/lib/llvm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ impl TypeNames {
18361836
unsafe {
18371837
let s = llvm::LLVMTypeToString(ty.to_ref());
18381838
let ret = from_c_str(s);
1839-
free(s as *c_void);
1839+
free(s as *mut c_void);
18401840
ret
18411841
}
18421842
}
@@ -1850,7 +1850,7 @@ impl TypeNames {
18501850
unsafe {
18511851
let s = llvm::LLVMValueToString(val);
18521852
let ret = from_c_str(s);
1853-
free(s as *c_void);
1853+
free(s as *mut c_void);
18541854
ret
18551855
}
18561856
}

src/librustuv/uvll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
373373
}
374374

375375
pub unsafe fn free_handle(v: *c_void) {
376-
free(v)
376+
free(v as *mut c_void)
377377
}
378378

379379
pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
@@ -383,7 +383,7 @@ pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
383383
}
384384

385385
pub unsafe fn free_req(v: *c_void) {
386-
free(v)
386+
free(v as *mut c_void)
387387
}
388388

389389
#[test]

src/libstd/c_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Drop for CString {
183183
fn drop(&mut self) {
184184
if self.owns_buffer_ {
185185
unsafe {
186-
libc::free(self.buf as *libc::c_void)
186+
libc::free(self.buf as *mut libc::c_void)
187187
}
188188
}
189189
}
@@ -459,7 +459,7 @@ mod tests {
459459
#[test]
460460
fn test_unwrap() {
461461
let c_str = "hello".to_c_str();
462-
unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
462+
unsafe { libc::free(c_str.unwrap() as *mut libc::c_void) }
463463
}
464464

465465
#[test]

src/libstd/libc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3225,7 +3225,7 @@ pub mod funcs {
32253225
pub fn calloc(nobj: size_t, size: size_t) -> *c_void;
32263226
pub fn malloc(size: size_t) -> *mut c_void;
32273227
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
3228-
pub fn free(p: *c_void);
3228+
pub fn free(p: *mut c_void);
32293229
pub fn exit(status: c_int) -> !;
32303230
// Omitted: atexit.
32313231
pub fn system(s: *c_char) -> c_int;

src/libstd/rt/global_heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
5252
// `realloc(ptr, 0)` may allocate, but it may also return a null pointer
5353
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
5454
if size == 0 {
55-
free(ptr as *c_void);
55+
free(ptr);
5656
mut_null()
5757
} else {
5858
let p = realloc(ptr as *mut c_void, size as size_t);
@@ -107,7 +107,7 @@ pub unsafe fn exchange_free_(ptr: *u8) {
107107

108108
#[inline]
109109
pub unsafe fn exchange_free(ptr: *u8) {
110-
free(ptr as *c_void);
110+
free(ptr as *mut c_void);
111111
}
112112

113113
#[cfg(test)]

src/libstd/sync/deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<T: Send> Buffer<T> {
389389
impl<T: Send> Drop for Buffer<T> {
390390
fn drop(&mut self) {
391391
// It is assumed that all buffers are empty on drop.
392-
unsafe { libc::free(self.storage as *libc::c_void) }
392+
unsafe { libc::free(self.storage as *mut libc::c_void) }
393393
}
394394
}
395395

src/libstd/unstable/mutex.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -173,49 +173,49 @@ mod imp {
173173
type pthread_condattr_t = libc::c_void;
174174

175175
pub unsafe fn init_lock() -> uint {
176-
let block = malloc_raw(rust_pthread_mutex_t_size() as uint) as *pthread_mutex_t;
176+
let block = malloc_raw(rust_pthread_mutex_t_size() as uint) as *mut pthread_mutex_t;
177177
let n = pthread_mutex_init(block, ptr::null());
178178
assert_eq!(n, 0);
179179
return block as uint;
180180
}
181181

182182
pub unsafe fn init_cond() -> uint {
183-
let block = malloc_raw(rust_pthread_cond_t_size() as uint) as *pthread_cond_t;
183+
let block = malloc_raw(rust_pthread_cond_t_size() as uint) as *mut pthread_cond_t;
184184
let n = pthread_cond_init(block, ptr::null());
185185
assert_eq!(n, 0);
186186
return block as uint;
187187
}
188188

189189
pub unsafe fn free_lock(h: uint) {
190-
let block = h as *libc::c_void;
190+
let block = h as *mut libc::c_void;
191191
assert_eq!(pthread_mutex_destroy(block), 0);
192192
libc::free(block);
193193
}
194194

195195
pub unsafe fn free_cond(h: uint) {
196-
let block = h as *pthread_cond_t;
196+
let block = h as *mut pthread_cond_t;
197197
assert_eq!(pthread_cond_destroy(block), 0);
198198
libc::free(block);
199199
}
200200

201201
pub unsafe fn lock(l: uint) {
202-
assert_eq!(pthread_mutex_lock(l as *pthread_mutex_t), 0);
202+
assert_eq!(pthread_mutex_lock(l as *mut pthread_mutex_t), 0);
203203
}
204204

205205
pub unsafe fn trylock(l: uint) -> bool {
206-
pthread_mutex_trylock(l as *pthread_mutex_t) == 0
206+
pthread_mutex_trylock(l as *mut pthread_mutex_t) == 0
207207
}
208208

209209
pub unsafe fn unlock(l: uint) {
210-
assert_eq!(pthread_mutex_unlock(l as *pthread_mutex_t), 0);
210+
assert_eq!(pthread_mutex_unlock(l as *mut pthread_mutex_t), 0);
211211
}
212212

213213
pub unsafe fn wait(cond: uint, m: uint) {
214-
assert_eq!(pthread_cond_wait(cond as *pthread_cond_t, m as *pthread_mutex_t), 0);
214+
assert_eq!(pthread_cond_wait(cond as *mut pthread_cond_t, m as *mut pthread_mutex_t), 0);
215215
}
216216

217217
pub unsafe fn signal(cond: uint) {
218-
assert_eq!(pthread_cond_signal(cond as *pthread_cond_t), 0);
218+
assert_eq!(pthread_cond_signal(cond as *mut pthread_cond_t), 0);
219219
}
220220

221221
extern {
@@ -224,19 +224,19 @@ mod imp {
224224
}
225225

226226
extern {
227-
fn pthread_mutex_init(lock: *pthread_mutex_t,
227+
fn pthread_mutex_init(lock: *mut pthread_mutex_t,
228228
attr: *pthread_mutexattr_t) -> libc::c_int;
229-
fn pthread_mutex_destroy(lock: *pthread_mutex_t) -> libc::c_int;
230-
fn pthread_cond_init(cond: *pthread_cond_t,
229+
fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> libc::c_int;
230+
fn pthread_cond_init(cond: *mut pthread_cond_t,
231231
attr: *pthread_condattr_t) -> libc::c_int;
232-
fn pthread_cond_destroy(cond: *pthread_cond_t) -> libc::c_int;
233-
fn pthread_mutex_lock(lock: *pthread_mutex_t) -> libc::c_int;
234-
fn pthread_mutex_trylock(lock: *pthread_mutex_t) -> libc::c_int;
235-
fn pthread_mutex_unlock(lock: *pthread_mutex_t) -> libc::c_int;
232+
fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> libc::c_int;
233+
fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> libc::c_int;
234+
fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> libc::c_int;
235+
fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> libc::c_int;
236236

237-
fn pthread_cond_wait(cond: *pthread_cond_t,
238-
lock: *pthread_mutex_t) -> libc::c_int;
239-
fn pthread_cond_signal(cond: *pthread_cond_t) -> libc::c_int;
237+
fn pthread_cond_wait(cond: *mut pthread_cond_t,
238+
lock: *mut pthread_mutex_t) -> libc::c_int;
239+
fn pthread_cond_signal(cond: *mut pthread_cond_t) -> libc::c_int;
240240
}
241241
}
242242

@@ -263,7 +263,7 @@ mod imp {
263263

264264
pub unsafe fn free_lock(h: uint) {
265265
DeleteCriticalSection(h as LPCRITICAL_SECTION);
266-
libc::free(h as *c_void);
266+
libc::free(h as *mut c_void);
267267
}
268268

269269
pub unsafe fn free_cond(h: uint) {

0 commit comments

Comments
 (0)