diff options
Diffstat (limited to 'src/lj_buf.c')
-rw-r--r-- | src/lj_buf.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/lj_buf.c b/src/lj_buf.c index 1786c10d..1f6e97bf 100644 --- a/src/lj_buf.c +++ b/src/lj_buf.c | |||
@@ -3,8 +3,6 @@ | |||
3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h | 3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include <stdio.h> | ||
7 | |||
8 | #define lj_buf_c | 6 | #define lj_buf_c |
9 | #define LUA_CORE | 7 | #define LUA_CORE |
10 | 8 | ||
@@ -18,23 +16,37 @@ | |||
18 | 16 | ||
19 | /* -- Buffer management --------------------------------------------------- */ | 17 | /* -- Buffer management --------------------------------------------------- */ |
20 | 18 | ||
21 | LJ_NOINLINE void LJ_FASTCALL lj_buf_grow(SBuf *sb, char *en) | 19 | static void buf_grow(SBuf *sb, MSize sz) |
22 | { | 20 | { |
23 | lua_State *L = sbufL(sb); | 21 | MSize osz = sbufsz(sb), len = sbuflen(sb), nsz = osz; |
24 | char *b = sbufB(sb); | 22 | char *b; |
25 | MSize sz = (MSize)(en - b); | ||
26 | MSize osz = (MSize)(sbufE(sb) - b), nsz = osz; | ||
27 | MSize n = (MSize)(sbufP(sb) - b); | ||
28 | if (LJ_UNLIKELY(sz > LJ_MAX_MEM)) | ||
29 | lj_err_mem(L); | ||
30 | if (nsz < LJ_MIN_SBUF) nsz = LJ_MIN_SBUF; | 23 | if (nsz < LJ_MIN_SBUF) nsz = LJ_MIN_SBUF; |
31 | while (nsz < sz) nsz += nsz; | 24 | while (nsz < sz) nsz += nsz; |
32 | b = (char *)lj_mem_realloc(L, b, osz, nsz); | 25 | b = (char *)lj_mem_realloc(sbufL(sb), sbufB(sb), osz, nsz); |
33 | setmref(sb->b, b); | 26 | setmref(sb->b, b); |
34 | setmref(sb->p, b + n); | 27 | setmref(sb->p, b + len); |
35 | setmref(sb->e, b + nsz); | 28 | setmref(sb->e, b + nsz); |
36 | } | 29 | } |
37 | 30 | ||
31 | LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz) | ||
32 | { | ||
33 | lua_assert(sz > sbufsz(sb)); | ||
34 | if (LJ_UNLIKELY(sz > LJ_MAX_MEM)) | ||
35 | lj_err_mem(sbufL(sb)); | ||
36 | buf_grow(sb, sz); | ||
37 | return sbufB(sb); | ||
38 | } | ||
39 | |||
40 | LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz) | ||
41 | { | ||
42 | MSize len = sbuflen(sb); | ||
43 | lua_assert(sz > sbufleft(sb)); | ||
44 | if (LJ_UNLIKELY(sz > LJ_MAX_MEM || len + sz > LJ_MAX_MEM)) | ||
45 | lj_err_mem(sbufL(sb)); | ||
46 | buf_grow(sb, len + sz); | ||
47 | return sbufP(sb); | ||
48 | } | ||
49 | |||
38 | void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb) | 50 | void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb) |
39 | { | 51 | { |
40 | char *b = sbufB(sb); | 52 | char *b = sbufB(sb); |