Skip to content

Commit a13408d

Browse files
Remove libc dependency in cg_gcc alloc_system example
1 parent 1075b80 commit a13408d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

example/alloc_system.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#![no_std]
55
#![feature(allocator_api, rustc_private)]
6-
#![cfg_attr(any(unix, target_os = "redox"), feature(libc))]
76

87
// The minimum alignment guaranteed by the architecture. This value is used to
98
// add fast paths for low alignment values.
@@ -48,7 +47,18 @@ mod realloc_fallback {
4847
}
4948
#[cfg(any(unix, target_os = "redox"))]
5049
mod platform {
51-
extern crate libc;
50+
mod libc {
51+
use core::ffi::{c_void, c_int};
52+
53+
#[link(name = "c")]
54+
extern "C" {
55+
pub fn malloc(size: usize) -> *mut c_void;
56+
pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
57+
pub fn calloc(nmemb: usize, size: usize) -> *mut c_void;
58+
pub fn free(ptr: *mut u8);
59+
pub fn posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
60+
}
61+
}
5262
use core::ptr;
5363
use MIN_ALIGN;
5464
use System;
@@ -82,12 +92,12 @@ mod platform {
8292
}
8393
#[inline]
8494
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
85-
libc::free(ptr as *mut libc::c_void)
95+
libc::free(ptr as *mut _)
8696
}
8797
#[inline]
8898
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
8999
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
90-
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
100+
libc::realloc(ptr as *mut _, new_size) as *mut u8
91101
} else {
92102
self.realloc_fallback(ptr, layout, new_size)
93103
}

0 commit comments

Comments
 (0)