Skip to content

Commit 03f9496

Browse files
committed
Migrate lmem
1 parent 480f3a9 commit 03f9496

File tree

5 files changed

+63
-145
lines changed

5 files changed

+63
-145
lines changed

build/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() {
1919
.file("build/liolib.c")
2020
.file("build/llex.c")
2121
.file("build/lmathlib.c")
22-
.file("build/lmem.c")
2322
.file("build/loadlib.c")
2423
.file("build/lobject.c")
2524
.file("build/lopcodes.c")

build/lmem.c

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub(crate) mod ldo;
44
pub(crate) mod lfunc;
55
pub(crate) mod llimits;
6+
pub(crate) mod lmem;
67
pub(crate) mod lobject;
78
pub(crate) mod lstate;
89
pub(crate) mod ltemp;

src/lmem.rs

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
use libc::{c_char, c_int, c_void, size_t};
2+
3+
use crate::llimits::l_mem;
4+
use crate::lstate::lua_State;
5+
use crate::types::LUA_ERRMEM;
6+
7+
extern "C" {
8+
pub fn luaG_runerror(L: *mut lua_State, fmt: *const c_char, args: ...) -> !;
9+
pub fn luaC_fullgc(L: *mut lua_State, isemergency: c_int);
10+
pub fn luaD_throw(L: *mut lua_State, errcode: c_int) -> !;
11+
}
12+
113
/*
214
** About the realloc function:
315
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
@@ -15,74 +27,76 @@
1527
** (any reallocation to an equal or smaller size cannot fail!)
1628
*/
1729

18-
pub const MINSIZEARRAY: libc::c_int = 4 as libc::c_int;
30+
pub const MINSIZEARRAY: c_int = 4;
1931

20-
pub unsafe fn luaM_growaux_(
21-
mut L: *mut lua_State,
22-
mut block: *mut libc::c_void,
23-
mut size: *mut libc::c_int,
24-
mut size_elems: size_t,
25-
mut limit: libc::c_int,
26-
mut what: *const libc::c_char,
27-
) -> *mut libc::c_void {
28-
let mut newblock = 0 as *mut libc::c_void;
29-
let mut newsize: libc::c_int = 0;
30-
if *size >= limit / 2 as libc::c_int {
32+
#[no_mangle]
33+
pub unsafe extern "C" fn luaM_growaux_(
34+
L: *mut lua_State,
35+
block: *mut c_void,
36+
size: *mut c_int,
37+
size_elems: size_t,
38+
limit: c_int,
39+
what: *const c_char,
40+
) -> *mut c_void {
41+
let mut newsize;
42+
if *size >= limit / 2 {
3143
/* cannot double it? */
3244
if *size >= limit {
3345
/* cannot grow even a little? */
3446
luaG_runerror(
3547
L,
36-
b"too many %s (limit is %d)\0" as *const u8 as *const libc::c_char,
48+
b"too many %s (limit is %d)\0" as *const u8 as *const c_char,
3749
what,
3850
limit,
3951
);
4052
}
41-
newsize = limit;
53+
newsize = limit; /* still have at least one free place */
4254
} else {
43-
newsize = *size * 2 as libc::c_int;
55+
newsize = *size * 2;
4456
if newsize < MINSIZEARRAY {
45-
/* minimum size */
46-
newsize = MINSIZEARRAY;
57+
newsize = MINSIZEARRAY; /* minimum size */
4758
}
4859
}
49-
newblock = luaM_realloc_(
60+
let newblock = luaM_realloc_(
5061
L,
5162
block,
52-
(*size as libc::c_ulong).checked_mul(size_elems),
53-
(newsize as libc::c_ulong).checked_mul(size_elems),
63+
(*size as size_t) * size_elems,
64+
(newsize as size_t) * size_elems,
5465
);
55-
/* update only when everything else is OK */
56-
*size = newsize;
66+
*size = newsize; /* update only when everything else is OK */
5767
return newblock;
5868
}
5969

70+
#[no_mangle]
71+
pub unsafe extern "C" fn luaM_toobig(L: *mut lua_State) -> ! {
72+
luaG_runerror(
73+
L,
74+
b"memory allocation error: block too big\0" as *const u8 as *const c_char,
75+
);
76+
}
77+
6078
/*
6179
** generic allocation routine.
6280
*/
63-
64-
pub unsafe fn luaM_realloc_(
65-
mut L: *mut lua_State,
66-
mut block: *mut libc::c_void,
67-
mut osize: size_t,
68-
mut nsize: size_t,
69-
) -> *mut libc::c_void {
70-
let mut newblock = 0 as *mut libc::c_void;
71-
let mut g = (*L).l_G;
72-
let mut realosize = if !block.is_null() {
73-
osize
74-
} else {
75-
0 as libc::c_int as libc::c_ulong
76-
};
81+
#[no_mangle]
82+
pub unsafe extern "C" fn luaM_realloc_(
83+
L: *mut lua_State,
84+
block: *mut c_void,
85+
osize: size_t,
86+
nsize: size_t,
87+
) -> *mut c_void {
88+
let g = (*L).l_G;
89+
let realosize = if !block.is_null() { osize } else { 0 };
90+
debug_assert!((realosize == 0) == block.is_null());
7791
// TODO: HARDMEMTESTS
78-
newblock = (((*g).frealloc).expect("non-null function pointer"))((*g).ud, block, osize, nsize);
79-
if newblock.is_null() && nsize > 0 as libc::c_int as libc::c_ulong {
80-
/* cannot fail when shrinking a block */
81-
debug_assert!(nsize > realosize);
92+
let mut newblock =
93+
(((*g).frealloc).expect("non-null function pointer"))((*g).ud, block, osize, nsize);
94+
if newblock.is_null() && nsize > 0 {
95+
debug_assert!(nsize > realosize); /* cannot fail when shrinking a block */
8296
if !((*g).version).is_null() {
8397
/* is state fully built? */
8498
/* try to free some memory... */
85-
luaC_fullgc(L, 1 as libc::c_int);
99+
luaC_fullgc(L, 1);
86100
/* try again */
87101
newblock =
88102
(((*g).frealloc).expect("non-null function pointer"))((*g).ud, block, osize, nsize);
@@ -92,8 +106,6 @@ pub unsafe fn luaM_realloc_(
92106
}
93107
}
94108
debug_assert!((nsize == 0) == newblock.is_null());
95-
(*g).GCdebt = ((*g).GCdebt as libc::c_ulong)
96-
.wrapping_add(nsize)
97-
.wrapping_sub(realosize) as l_mem;
109+
(*g).GCdebt = ((*g).GCdebt + nsize as l_mem) - realosize as l_mem;
98110
return newblock;
99111
}

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ pub type lua_Number = c_double;
99
/* type for integer functions */
1010
pub type lua_Integer = c_longlong;
1111

12+
/* thread status */
13+
pub const LUA_ERRMEM: c_int = 4;
14+
15+
/*
16+
** basic types
17+
*/
1218
pub const LUA_NUMTAGS: usize = 9;
1319

1420
/*

0 commit comments

Comments
 (0)