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
+
1
13
/*
2
14
** About the realloc function:
3
15
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
15
27
** (any reallocation to an equal or smaller size cannot fail!)
16
28
*/
17
29
18
- pub const MINSIZEARRAY : libc :: c_int = 4 as libc :: c_int ;
30
+ pub const MINSIZEARRAY : c_int = 4 ;
19
31
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 {
31
43
/* cannot double it? */
32
44
if * size >= limit {
33
45
/* cannot grow even a little? */
34
46
luaG_runerror (
35
47
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 ,
37
49
what,
38
50
limit,
39
51
) ;
40
52
}
41
- newsize = limit;
53
+ newsize = limit; /* still have at least one free place */
42
54
} else {
43
- newsize = * size * 2 as libc :: c_int ;
55
+ newsize = * size * 2 ;
44
56
if newsize < MINSIZEARRAY {
45
- /* minimum size */
46
- newsize = MINSIZEARRAY ;
57
+ newsize = MINSIZEARRAY ; /* minimum size */
47
58
}
48
59
}
49
- newblock = luaM_realloc_ (
60
+ let newblock = luaM_realloc_ (
50
61
L ,
51
62
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,
54
65
) ;
55
- /* update only when everything else is OK */
56
- * size = newsize;
66
+ * size = newsize; /* update only when everything else is OK */
57
67
return newblock;
58
68
}
59
69
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
+
60
78
/*
61
79
** generic allocation routine.
62
80
*/
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( ) ) ;
77
91
// 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 */
82
96
if !( ( * g) . version ) . is_null ( ) {
83
97
/* is state fully built? */
84
98
/* try to free some memory... */
85
- luaC_fullgc ( L , 1 as libc :: c_int ) ;
99
+ luaC_fullgc ( L , 1 ) ;
86
100
/* try again */
87
101
newblock =
88
102
( ( ( * g) . frealloc ) . expect ( "non-null function pointer" ) ) ( ( * g) . ud , block, osize, nsize) ;
@@ -92,8 +106,6 @@ pub unsafe fn luaM_realloc_(
92
106
}
93
107
}
94
108
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 ;
98
110
return newblock;
99
111
}
0 commit comments