aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2013-02-27 17:11:31 +0100
committerMike Pall <mike>2013-02-27 17:29:35 +0100
commit28cfcf77445e144335961a020e3e08d84cf0091f (patch)
tree1a769d0ee0fab26a79073a118ba4f9e1557b081a
parentd44337a566bb3de06a6ac4ecf2d2a77767b86029 (diff)
downloadluajit-28cfcf77445e144335961a020e3e08d84cf0091f.tar.gz
luajit-28cfcf77445e144335961a020e3e08d84cf0091f.tar.bz2
luajit-28cfcf77445e144335961a020e3e08d84cf0091f.zip
String buffer refactoring, part 1.
Move string buffer handling to lj_buf.*. Use common buffer resizing function.
-rw-r--r--src/Makefile2
-rw-r--r--src/Makefile.dep101
-rw-r--r--src/lib_io.c7
-rw-r--r--src/lib_string.c41
-rw-r--r--src/lj_bcread.c23
-rw-r--r--src/lj_bcwrite.c24
-rw-r--r--src/lj_buf.c40
-rw-r--r--src/lj_buf.h27
-rw-r--r--src/lj_cparse.c12
-rw-r--r--src/lj_gc.c4
-rw-r--r--src/lj_lex.c15
-rw-r--r--src/lj_lex.h1
-rw-r--r--src/lj_load.c3
-rw-r--r--src/lj_meta.c9
-rw-r--r--src/lj_obj.h2
-rw-r--r--src/lj_opt_loop.c4
-rw-r--r--src/lj_opt_split.c3
-rw-r--r--src/lj_parse.c13
-rw-r--r--src/lj_state.c5
-rw-r--r--src/lj_str.c34
-rw-r--r--src/lj_str.h10
-rw-r--r--src/ljamalg.c1
22 files changed, 197 insertions, 184 deletions
diff --git a/src/Makefile b/src/Makefile
index 95671792..0065b8c2 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -436,7 +436,7 @@ LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \
436 lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o 436 lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o
437LJLIB_C= $(LJLIB_O:.o=.c) 437LJLIB_C= $(LJLIB_O:.o=.c)
438 438
439LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \ 439LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
440 lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \ 440 lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \
441 lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \ 441 lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \
442 lj_api.o lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \ 442 lj_api.o lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \
diff --git a/src/Makefile.dep b/src/Makefile.dep
index 10118c5e..56594704 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -17,8 +17,8 @@ lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
17 lj_ccallback.h lj_clib.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h 17 lj_ccallback.h lj_clib.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
18lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h 18lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h
19lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ 19lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
20 lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_ff.h lj_ffdef.h \ 20 lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h lj_ff.h \
21 lj_lib.h lj_libdef.h 21 lj_ffdef.h lj_lib.h lj_libdef.h
22lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \ 22lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \
23 lj_obj.h lj_def.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \ 23 lj_obj.h lj_def.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \
24 lj_bc.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_target.h \ 24 lj_bc.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_target.h \
@@ -32,8 +32,8 @@ lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
32 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h 32 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h
33lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 33lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
34 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \ 34 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \
35 lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_char.h \ 35 lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_buf.h \
36 lj_lib.h lj_libdef.h 36 lj_char.h lj_lib.h lj_libdef.h
37lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 37lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
38 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \ 38 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \
39 lj_libdef.h 39 lj_libdef.h
@@ -50,11 +50,13 @@ lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
50lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ 50lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
51 lj_bcdef.h 51 lj_bcdef.h
52lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 52lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
53 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_bc.h lj_ctype.h \ 53 lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_bc.h \
54 lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h 54 lj_ctype.h lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h
55lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 55lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
56 lj_gc.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h lj_ir.h \ 56 lj_gc.h lj_buf.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h \
57 lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h 57 lj_ir.h lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h
58lj_buf.o: lj_buf.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
59 lj_err.h lj_errmsg.h lj_buf.h
58lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 60lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
59 lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ctype.h lj_cconv.h \ 61 lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ctype.h lj_cconv.h \
60 lj_cdata.h lj_carith.h 62 lj_cdata.h lj_carith.h
@@ -78,8 +80,8 @@ lj_clib.o: lj_clib.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
78 lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \ 80 lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \
79 lj_cdata.h lj_clib.h 81 lj_cdata.h lj_clib.h
80lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 82lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
81 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \ 83 lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_ctype.h lj_cparse.h \
82 lj_bc.h lj_vm.h lj_char.h lj_strscan.h 84 lj_frame.h lj_bc.h lj_vm.h lj_char.h lj_strscan.h
83lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 85lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
84 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h \ 86 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h \
85 lj_gc.h lj_cdata.h lj_cparse.h lj_cconv.h lj_clib.h lj_ccall.h lj_ff.h \ 87 lj_gc.h lj_cdata.h lj_cparse.h lj_cconv.h lj_clib.h lj_ccall.h lj_ff.h \
@@ -109,9 +111,9 @@ lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
109 lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ 111 lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \
110 lj_traceerr.h lj_vm.h 112 lj_traceerr.h lj_vm.h
111lj_gc.o: lj_gc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 113lj_gc.o: lj_gc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
112 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_udata.h lj_meta.h \ 114 lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_func.h lj_udata.h \
113 lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h lj_jit.h \ 115 lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h \
114 lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h 116 lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h
115lj_gdbjit.o: lj_gdbjit.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 117lj_gdbjit.o: lj_gdbjit.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
116 lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_frame.h lj_bc.h lj_jit.h \ 118 lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_frame.h lj_bc.h lj_jit.h \
117 lj_ir.h lj_dispatch.h 119 lj_ir.h lj_dispatch.h
@@ -121,20 +123,20 @@ lj_ir.o: lj_ir.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
121 lj_vm.h lj_strscan.h lj_lib.h 123 lj_vm.h lj_strscan.h lj_lib.h
122lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 124lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
123 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h lualib.h \ 125 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h lualib.h \
124 lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h 126 lj_state.h lj_lex.h lj_buf.h lj_parse.h lj_char.h lj_strscan.h
125lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ 127lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \
126 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ 128 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \
127 lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lex.h lj_bcdump.h \ 129 lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lex.h lj_buf.h \
128 lj_lib.h 130 lj_bcdump.h lj_lib.h
129lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ 131lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \
130 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_func.h lj_frame.h \ 132 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_func.h \
131 lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h 133 lj_frame.h lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h
132lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 134lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
133 lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \ 135 lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \
134 lj_traceerr.h lj_vm.h 136 lj_traceerr.h lj_vm.h
135lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 137lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
136 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ 138 lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_meta.h lj_frame.h \
137 lj_vm.h lj_strscan.h lj_lib.h 139 lj_bc.h lj_vm.h lj_strscan.h lj_lib.h
138lj_obj.o: lj_obj.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h 140lj_obj.o: lj_obj.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h
139lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 141lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
140 lj_ir.h lj_jit.h lj_iropt.h 142 lj_ir.h lj_jit.h lj_iropt.h
@@ -143,8 +145,8 @@ lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
143 lj_bc.h lj_traceerr.h lj_ctype.h lj_gc.h lj_carith.h lj_vm.h \ 145 lj_bc.h lj_traceerr.h lj_ctype.h lj_gc.h lj_carith.h lj_vm.h \
144 lj_strscan.h lj_folddef.h 146 lj_strscan.h lj_folddef.h
145lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 147lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
146 lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h \ 148 lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h \
147 lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h 149 lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h
148lj_opt_mem.o: lj_opt_mem.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 150lj_opt_mem.o: lj_opt_mem.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
149 lj_tab.h lj_ir.h lj_jit.h lj_iropt.h 151 lj_tab.h lj_ir.h lj_jit.h lj_iropt.h
150lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \ 152lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \
@@ -153,11 +155,12 @@ lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \
153lj_opt_sink.o: lj_opt_sink.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 155lj_opt_sink.o: lj_opt_sink.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
154 lj_ir.h lj_jit.h lj_iropt.h lj_target.h lj_target_*.h 156 lj_ir.h lj_jit.h lj_iropt.h lj_target.h lj_target_*.h
155lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \ 157lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \
156 lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_ircall.h \ 158 lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_ir.h lj_jit.h \
157 lj_iropt.h lj_vm.h 159 lj_ircall.h lj_iropt.h lj_vm.h
158lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 160lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
159 lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \ 161 lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \
160 lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h 162 lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_buf.h lj_parse.h lj_vm.h \
163 lj_vmevent.h
161lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 164lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
162 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ 165 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \
163 lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h \ 166 lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h \
@@ -168,11 +171,11 @@ lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
168 lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \ 171 lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \
169 lj_target_*.h lj_ctype.h lj_cdata.h 172 lj_target_*.h lj_ctype.h lj_cdata.h
170lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 173lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
171 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_meta.h \ 174 lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_func.h \
172 lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h lj_ir.h \ 175 lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h \
173 lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h 176 lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h
174lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 177lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
175 lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_char.h 178 lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h lj_char.h
176lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 179lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
177 lj_char.h lj_strscan.h 180 lj_char.h lj_strscan.h
178lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 181lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
@@ -190,26 +193,26 @@ lj_vmevent.o: lj_vmevent.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
190lj_vmmath.o: lj_vmmath.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 193lj_vmmath.o: lj_vmmath.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
191 lj_ir.h lj_vm.h 194 lj_ir.h lj_vm.h
192ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \ 195ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
193 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h \ 196 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h \
194 lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h \ 197 lj_func.h lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h \
195 lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c \ 198 lj_cdata.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h \
196 lj_debug.h lj_ff.h lj_ffdef.h lj_char.c lj_char.h lj_bc.c lj_bcdef.h \ 199 lj_vm.h lj_err.c lj_debug.h lj_ff.h lj_ffdef.h lj_char.c lj_char.h \
197 lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h \ 200 lj_bc.c lj_bcdef.h lj_obj.c lj_buf.c lj_str.c lj_tab.c lj_func.c \
198 lj_lib.h lj_debug.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c \ 201 lj_udata.c lj_meta.c lj_strscan.h lj_lib.h lj_debug.c lj_state.c \
199 lj_ccallback.h luajit.h lj_vmevent.c lj_vmevent.h lj_vmmath.c \ 202 lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h luajit.h lj_vmevent.c \
200 lj_strscan.c lj_api.c lj_lex.c lualib.h lj_parse.h lj_parse.c \ 203 lj_vmevent.h lj_vmmath.c lj_strscan.c lj_api.c lj_lex.c lualib.h \
201 lj_bcread.c lj_bcdump.h lj_bcwrite.c lj_load.c lj_ctype.c lj_cdata.c \ 204 lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h lj_bcwrite.c lj_load.c \
202 lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h lj_ccallback.c lj_target.h \ 205 lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h \
203 lj_target_*.h lj_mcode.h lj_carith.c lj_carith.h lj_clib.c lj_clib.h \ 206 lj_ccallback.c lj_target.h lj_target_*.h lj_mcode.h lj_carith.c \
204 lj_cparse.c lj_cparse.h lj_lib.c lj_ir.c lj_ircall.h lj_iropt.h \ 207 lj_carith.h lj_clib.c lj_clib.h lj_cparse.c lj_cparse.h lj_lib.c lj_ir.c \
205 lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ 208 lj_ircall.h lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h \
206 lj_opt_loop.c lj_snap.h lj_opt_split.c lj_opt_sink.c lj_mcode.c \ 209 lj_opt_narrow.c lj_opt_dce.c lj_opt_loop.c lj_snap.h lj_opt_split.c \
207 lj_snap.c lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \ 210 lj_opt_sink.c lj_mcode.c lj_snap.c lj_record.c lj_record.h lj_ffrecord.h \
208 lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h \ 211 lj_crecord.c lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h \
209 lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ 212 lj_emit_*.h lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c \
210 lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c \ 213 lib_aux.c lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c \
211 lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c \ 214 lib_io.c lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c \
212 lib_init.c 215 lib_ffi.c lib_init.c
213luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h 216luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h
214host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ 217host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \
215 lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ 218 lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \
diff --git a/src/lib_io.c b/src/lib_io.c
index 8858683f..e9472ba5 100644
--- a/src/lib_io.c
+++ b/src/lib_io.c
@@ -18,6 +18,7 @@
18 18
19#include "lj_obj.h" 19#include "lj_obj.h"
20#include "lj_err.h" 20#include "lj_err.h"
21#include "lj_buf.h"
21#include "lj_str.h" 22#include "lj_str.h"
22#include "lj_state.h" 23#include "lj_state.h"
23#include "lj_ff.h" 24#include "lj_ff.h"
@@ -144,7 +145,7 @@ static int io_file_readline(lua_State *L, FILE *fp, MSize chop)
144 MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0; 145 MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0;
145 char *buf; 146 char *buf;
146 for (;;) { 147 for (;;) {
147 buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); 148 buf = lj_buf_tmp(L, m);
148 if (fgets(buf+n, m-n, fp) == NULL) break; 149 if (fgets(buf+n, m-n, fp) == NULL) break;
149 n += (MSize)strlen(buf+n); 150 n += (MSize)strlen(buf+n);
150 ok |= n; 151 ok |= n;
@@ -159,7 +160,7 @@ static void io_file_readall(lua_State *L, FILE *fp)
159{ 160{
160 MSize m, n; 161 MSize m, n;
161 for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) { 162 for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) {
162 char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); 163 char *buf = lj_buf_tmp(L, m);
163 n += (MSize)fread(buf+n, 1, m-n, fp); 164 n += (MSize)fread(buf+n, 1, m-n, fp);
164 if (n != m) { 165 if (n != m) {
165 setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); 166 setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));
@@ -171,7 +172,7 @@ static void io_file_readall(lua_State *L, FILE *fp)
171static int io_file_readlen(lua_State *L, FILE *fp, MSize m) 172static int io_file_readlen(lua_State *L, FILE *fp, MSize m)
172{ 173{
173 if (m) { 174 if (m) {
174 char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); 175 char *buf = lj_buf_tmp(L, m);
175 MSize n = (MSize)fread(buf, 1, m, fp); 176 MSize n = (MSize)fread(buf, 1, m, fp);
176 setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); 177 setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));
177 return (n > 0 || m == 0); 178 return (n > 0 || m == 0);
diff --git a/src/lib_string.c b/src/lib_string.c
index 9e8ab900..5fdfcd91 100644
--- a/src/lib_string.c
+++ b/src/lib_string.c
@@ -64,7 +64,7 @@ LJLIB_ASM(string_byte) LJLIB_REC(string_range 0)
64LJLIB_ASM(string_char) 64LJLIB_ASM(string_char)
65{ 65{
66 int i, nargs = (int)(L->top - L->base); 66 int i, nargs = (int)(L->top - L->base);
67 char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, (size_t)nargs); 67 char *buf = lj_buf_tmp(L, (size_t)nargs);
68 for (i = 1; i <= nargs; i++) { 68 for (i = 1; i <= nargs; i++) {
69 int32_t k = lj_lib_checkint(L, i); 69 int32_t k = lj_lib_checkint(L, i);
70 if (!checku8(k)) 70 if (!checku8(k))
@@ -91,8 +91,6 @@ LJLIB_ASM(string_rep)
91 int32_t len = (int32_t)s->len; 91 int32_t len = (int32_t)s->len;
92 global_State *g = G(L); 92 global_State *g = G(L);
93 int64_t tlen; 93 int64_t tlen;
94 const char *src;
95 char *buf;
96 if (k <= 0) { 94 if (k <= 0) {
97 empty: 95 empty:
98 setstrV(L, L->base-1, &g->strempty); 96 setstrV(L, L->base-1, &g->strempty);
@@ -110,31 +108,34 @@ LJLIB_ASM(string_rep)
110 if (tlen > LJ_MAX_STR) 108 if (tlen > LJ_MAX_STR)
111 lj_err_caller(L, LJ_ERR_STROV); 109 lj_err_caller(L, LJ_ERR_STROV);
112 } 110 }
113 if (tlen == 0) goto empty; 111 if (tlen == 0) {
114 buf = lj_str_needbuf(L, &g->tmpbuf, (MSize)tlen); 112 goto empty;
115 src = strdata(s); 113 } else {
116 if (sep) { 114 char *buf = lj_buf_tmp(L, (MSize)tlen), *p = buf;
117 tlen -= sep->len; /* Ignore trailing separator. */ 115 const char *src = strdata(s);
118 if (k > 1) { /* Paste one string and one separator. */ 116 if (sep) {
119 int32_t i; 117 tlen -= sep->len; /* Ignore trailing separator. */
120 i = 0; while (i < len) *buf++ = src[i++]; 118 if (k > 1) { /* Paste one string and one separator. */
121 src = strdata(sep); len = sep->len; 119 int32_t i;
122 i = 0; while (i < len) *buf++ = src[i++]; 120 i = 0; while (i < len) *p++ = src[i++];
123 src = g->tmpbuf.buf; len += s->len; k--; /* Now copy that k-1 times. */ 121 src = strdata(sep); len = sep->len;
122 i = 0; while (i < len) *p++ = src[i++];
123 src = buf; len += s->len; k--; /* Now copy that k-1 times. */
124 }
124 } 125 }
126 do {
127 int32_t i = 0;
128 do { *p++ = src[i++]; } while (i < len);
129 } while (--k > 0);
130 setstrV(L, L->base-1, lj_str_new(L, buf, (size_t)tlen));
125 } 131 }
126 do {
127 int32_t i = 0;
128 do { *buf++ = src[i++]; } while (i < len);
129 } while (--k > 0);
130 setstrV(L, L->base-1, lj_str_new(L, g->tmpbuf.buf, (size_t)tlen));
131 return FFH_RES(1); 132 return FFH_RES(1);
132} 133}
133 134
134LJLIB_ASM(string_reverse) 135LJLIB_ASM(string_reverse)
135{ 136{
136 GCstr *s = lj_lib_checkstr(L, 1); 137 GCstr *s = lj_lib_checkstr(L, 1);
137 lj_str_needbuf(L, &G(L)->tmpbuf, s->len); 138 lj_buf_tmp(L, s->len);
138 return FFH_RETRY; 139 return FFH_RETRY;
139} 140}
140LJLIB_ASM_(string_lower) 141LJLIB_ASM_(string_lower)
diff --git a/src/lj_bcread.c b/src/lj_bcread.c
index 7a8c08f5..fabe76da 100644
--- a/src/lj_bcread.c
+++ b/src/lj_bcread.c
@@ -9,6 +9,7 @@
9#include "lj_obj.h" 9#include "lj_obj.h"
10#include "lj_gc.h" 10#include "lj_gc.h"
11#include "lj_err.h" 11#include "lj_err.h"
12#include "lj_buf.h"
12#include "lj_str.h" 13#include "lj_str.h"
13#include "lj_tab.h" 14#include "lj_tab.h"
14#include "lj_bc.h" 15#include "lj_bc.h"
@@ -42,17 +43,6 @@ static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)
42 lj_err_throw(L, LUA_ERRSYNTAX); 43 lj_err_throw(L, LUA_ERRSYNTAX);
43} 44}
44 45
45/* Resize input buffer. */
46static void bcread_resize(LexState *ls, MSize len)
47{
48 if (ls->sb.sz < len) {
49 MSize sz = ls->sb.sz * 2;
50 while (len > sz) sz = sz * 2;
51 lj_str_resizebuf(ls->L, &ls->sb, sz);
52 /* Caveat: this may change ls->sb.buf which may affect ls->p. */
53 }
54}
55
56/* Refill buffer if needed. */ 46/* Refill buffer if needed. */
57static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) 47static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
58{ 48{
@@ -68,8 +58,7 @@ static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
68 if (ls->n != ls->sb.n) 58 if (ls->n != ls->sb.n)
69 memmove(ls->sb.buf, ls->p, ls->n); 59 memmove(ls->sb.buf, ls->p, ls->n);
70 } else { /* Copy from buffer provided by reader. */ 60 } else { /* Copy from buffer provided by reader. */
71 bcread_resize(ls, len); 61 memcpy(lj_buf_need(ls->L, &ls->sb, len), ls->p, ls->n);
72 memcpy(ls->sb.buf, ls->p, ls->n);
73 } 62 }
74 ls->p = ls->sb.buf; 63 ls->p = ls->sb.buf;
75 } 64 }
@@ -82,10 +71,10 @@ static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
82 } 71 }
83 if (ls->sb.n) { /* Append to buffer. */ 72 if (ls->sb.n) { /* Append to buffer. */
84 MSize n = ls->sb.n + (MSize)size; 73 MSize n = ls->sb.n + (MSize)size;
85 bcread_resize(ls, n < len ? len : n); 74 char *p = lj_buf_need(ls->L, &ls->sb, n < len ? len : n);
86 memcpy(ls->sb.buf + ls->sb.n, buf, size); 75 memcpy(p + ls->sb.n, buf, size);
87 ls->n = ls->sb.n = n; 76 ls->n = ls->sb.n = n;
88 ls->p = ls->sb.buf; 77 ls->p = p;
89 } else { /* Return buffer provided by reader. */ 78 } else { /* Return buffer provided by reader. */
90 ls->n = (MSize)size; 79 ls->n = (MSize)size;
91 ls->p = buf; 80 ls->p = buf;
@@ -442,7 +431,7 @@ GCproto *lj_bcread(LexState *ls)
442 lua_State *L = ls->L; 431 lua_State *L = ls->L;
443 lua_assert(ls->current == BCDUMP_HEAD1); 432 lua_assert(ls->current == BCDUMP_HEAD1);
444 bcread_savetop(L, ls, L->top); 433 bcread_savetop(L, ls, L->top);
445 lj_str_resetbuf(&ls->sb); 434 lj_buf_reset(&ls->sb);
446 /* Check for a valid bytecode dump header. */ 435 /* Check for a valid bytecode dump header. */
447 if (!bcread_header(ls)) 436 if (!bcread_header(ls))
448 bcread_error(ls, LJ_ERR_BCFMT); 437 bcread_error(ls, LJ_ERR_BCFMT);
diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c
index 4805d515..474234c5 100644
--- a/src/lj_bcwrite.c
+++ b/src/lj_bcwrite.c
@@ -8,6 +8,7 @@
8 8
9#include "lj_obj.h" 9#include "lj_obj.h"
10#include "lj_gc.h" 10#include "lj_gc.h"
11#include "lj_buf.h"
11#include "lj_str.h" 12#include "lj_str.h"
12#include "lj_bc.h" 13#include "lj_bc.h"
13#if LJ_HASFFI 14#if LJ_HASFFI
@@ -33,19 +34,10 @@ typedef struct BCWriteCtx {
33 34
34/* -- Output buffer handling ---------------------------------------------- */ 35/* -- Output buffer handling ---------------------------------------------- */
35 36
36/* Resize buffer if needed. */ 37/* Ensure a certain amount of buffer space. */
37static LJ_NOINLINE void bcwrite_resize(BCWriteCtx *ctx, MSize len)
38{
39 MSize sz = ctx->sb.sz * 2;
40 while (ctx->sb.n + len > sz) sz = sz * 2;
41 lj_str_resizebuf(ctx->L, &ctx->sb, sz);
42}
43
44/* Need a certain amount of buffer space. */
45static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len) 38static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len)
46{ 39{
47 if (LJ_UNLIKELY(ctx->sb.n + len > ctx->sb.sz)) 40 lj_buf_need(ctx->L, &ctx->sb, ctx->sb.n + len);
48 bcwrite_resize(ctx, len);
49} 41}
50 42
51/* Add memory block to buffer. */ 43/* Add memory block to buffer. */
@@ -285,7 +277,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
285 } 277 }
286 278
287 /* Start writing the prototype info to a buffer. */ 279 /* Start writing the prototype info to a buffer. */
288 lj_str_resetbuf(&ctx->sb); 280 lj_buf_reset(&ctx->sb);
289 ctx->sb.n = 5; /* Leave room for final size. */ 281 ctx->sb.n = 5; /* Leave room for final size. */
290 bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); 282 bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
291 283
@@ -338,7 +330,7 @@ static void bcwrite_header(BCWriteCtx *ctx)
338 GCstr *chunkname = proto_chunkname(ctx->pt); 330 GCstr *chunkname = proto_chunkname(ctx->pt);
339 const char *name = strdata(chunkname); 331 const char *name = strdata(chunkname);
340 MSize len = chunkname->len; 332 MSize len = chunkname->len;
341 lj_str_resetbuf(&ctx->sb); 333 lj_buf_reset(&ctx->sb);
342 bcwrite_need(ctx, 5+5+len); 334 bcwrite_need(ctx, 5+5+len);
343 bcwrite_byte(ctx, BCDUMP_HEAD1); 335 bcwrite_byte(ctx, BCDUMP_HEAD1);
344 bcwrite_byte(ctx, BCDUMP_HEAD2); 336 bcwrite_byte(ctx, BCDUMP_HEAD2);
@@ -368,7 +360,7 @@ static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
368{ 360{
369 BCWriteCtx *ctx = (BCWriteCtx *)ud; 361 BCWriteCtx *ctx = (BCWriteCtx *)ud;
370 UNUSED(dummy); 362 UNUSED(dummy);
371 lj_str_resizebuf(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */ 363 lj_buf_grow(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */
372 bcwrite_header(ctx); 364 bcwrite_header(ctx);
373 bcwrite_proto(ctx, ctx->pt); 365 bcwrite_proto(ctx, ctx->pt);
374 bcwrite_footer(ctx); 366 bcwrite_footer(ctx);
@@ -387,10 +379,10 @@ int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
387 ctx.wdata = data; 379 ctx.wdata = data;
388 ctx.strip = strip; 380 ctx.strip = strip;
389 ctx.status = 0; 381 ctx.status = 0;
390 lj_str_initbuf(&ctx.sb); 382 lj_buf_init(&ctx.sb);
391 status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); 383 status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
392 if (status == 0) status = ctx.status; 384 if (status == 0) status = ctx.status;
393 lj_str_freebuf(G(ctx.L), &ctx.sb); 385 lj_buf_free(G(ctx.L), &ctx.sb);
394 return status; 386 return status;
395} 387}
396 388
diff --git a/src/lj_buf.c b/src/lj_buf.c
new file mode 100644
index 00000000..5d901d2a
--- /dev/null
+++ b/src/lj_buf.c
@@ -0,0 +1,40 @@
1/*
2** Buffer handling.
3** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#include <stdio.h>
7
8#define lj_buf_c
9#define LUA_CORE
10
11#include "lj_obj.h"
12#include "lj_gc.h"
13#include "lj_err.h"
14#include "lj_buf.h"
15
16LJ_NOINLINE void lj_buf_grow(lua_State *L, SBuf *sb, MSize sz)
17{
18 MSize bsz = sb->sz * 2;
19 if (LJ_UNLIKELY(sz > LJ_MAX_MEM))
20 lj_err_mem(L);
21 if (bsz < LJ_MIN_SBUF) bsz = LJ_MIN_SBUF;
22 while (bsz < sz) bsz += bsz;
23 sb->buf = lj_mem_realloc(L, sb->buf, sb->sz, bsz);
24 sb->sz = bsz;
25}
26
27char *lj_buf_tmp(lua_State *L, MSize sz)
28{
29 return lj_buf_need(L, &G(L)->tmpbuf, sz);
30}
31
32void lj_buf_shrink(lua_State *L, SBuf *sb)
33{
34 MSize sz = sb->sz;
35 if (sz > 2*LJ_MIN_SBUF) {
36 sb->buf = lj_mem_realloc(L, sb->buf, sz, (sz >> 1));
37 sb->sz = (sz >> 1);
38 }
39}
40
diff --git a/src/lj_buf.h b/src/lj_buf.h
new file mode 100644
index 00000000..19f2bb0d
--- /dev/null
+++ b/src/lj_buf.h
@@ -0,0 +1,27 @@
1/*
2** Buffer handling.
3** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#ifndef _LJ_BUF_H
7#define _LJ_BUF_H
8
9#include "lj_obj.h"
10
11/* Resizable string buffers. Struct definition in lj_obj.h. */
12LJ_FUNC char *lj_buf_tmp(lua_State *L, MSize sz);
13LJ_FUNC void lj_buf_grow(lua_State *L, SBuf *sb, MSize sz);
14LJ_FUNC void lj_buf_shrink(lua_State *L, SBuf *sb);
15
16#define lj_buf_init(sb) ((sb)->buf = NULL, (sb)->sz = 0)
17#define lj_buf_reset(sb) ((sb)->n = 0)
18#define lj_buf_free(g, sb) lj_mem_free(g, (void *)(sb)->buf, (sb)->sz)
19
20static LJ_AINLINE char *lj_buf_need(lua_State *L, SBuf *sb, MSize sz)
21{
22 if (LJ_UNLIKELY(sz > sb->sz))
23 lj_buf_grow(L, sb, sz);
24 return sb->buf;
25}
26
27#endif
diff --git a/src/lj_cparse.c b/src/lj_cparse.c
index 107c0381..b88ce5d3 100644
--- a/src/lj_cparse.c
+++ b/src/lj_cparse.c
@@ -9,6 +9,7 @@
9 9
10#include "lj_gc.h" 10#include "lj_gc.h"
11#include "lj_err.h" 11#include "lj_err.h"
12#include "lj_buf.h"
12#include "lj_str.h" 13#include "lj_str.h"
13#include "lj_ctype.h" 14#include "lj_ctype.h"
14#include "lj_cparse.h" 15#include "lj_cparse.h"
@@ -88,11 +89,9 @@ static LJ_AINLINE CPChar cp_get(CPState *cp)
88/* Grow save buffer. */ 89/* Grow save buffer. */
89static LJ_NOINLINE void cp_save_grow(CPState *cp, CPChar c) 90static LJ_NOINLINE void cp_save_grow(CPState *cp, CPChar c)
90{ 91{
91 MSize newsize;
92 if (cp->sb.sz >= CPARSE_MAX_BUF/2) 92 if (cp->sb.sz >= CPARSE_MAX_BUF/2)
93 cp_err(cp, LJ_ERR_XELEM); 93 cp_err(cp, LJ_ERR_XELEM);
94 newsize = cp->sb.sz * 2; 94 lj_buf_grow(cp->L, &cp->sb, 0);
95 lj_str_resizebuf(cp->L, &cp->sb, newsize);
96 cp->sb.buf[cp->sb.n++] = (char)c; 95 cp->sb.buf[cp->sb.n++] = (char)c;
97} 96}
98 97
@@ -296,7 +295,7 @@ static void cp_comment_cpp(CPState *cp)
296/* Lexical scanner for C. Only a minimal subset is implemented. */ 295/* Lexical scanner for C. Only a minimal subset is implemented. */
297static CPToken cp_next_(CPState *cp) 296static CPToken cp_next_(CPState *cp)
298{ 297{
299 lj_str_resetbuf(&cp->sb); 298 lj_buf_reset(&cp->sb);
300 for (;;) { 299 for (;;) {
301 if (lj_char_isident(cp->c)) 300 if (lj_char_isident(cp->c))
302 return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp); 301 return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp);
@@ -380,8 +379,7 @@ static void cp_init(CPState *cp)
380 cp->depth = 0; 379 cp->depth = 0;
381 cp->curpack = 0; 380 cp->curpack = 0;
382 cp->packstack[0] = 255; 381 cp->packstack[0] = 255;
383 lj_str_initbuf(&cp->sb); 382 lj_buf_init(&cp->sb);
384 lj_str_resizebuf(cp->L, &cp->sb, LJ_MIN_SBUF);
385 lua_assert(cp->p != NULL); 383 lua_assert(cp->p != NULL);
386 cp_get(cp); /* Read-ahead first char. */ 384 cp_get(cp); /* Read-ahead first char. */
387 cp->tok = 0; 385 cp->tok = 0;
@@ -393,7 +391,7 @@ static void cp_init(CPState *cp)
393static void cp_cleanup(CPState *cp) 391static void cp_cleanup(CPState *cp)
394{ 392{
395 global_State *g = G(cp->L); 393 global_State *g = G(cp->L);
396 lj_str_freebuf(g, &cp->sb); 394 lj_buf_free(g, &cp->sb);
397} 395}
398 396
399/* Check and consume optional token. */ 397/* Check and consume optional token. */
diff --git a/src/lj_gc.c b/src/lj_gc.c
index 79f8b720..4ce6eb19 100644
--- a/src/lj_gc.c
+++ b/src/lj_gc.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#include "lj_tab.h" 17#include "lj_tab.h"
17#include "lj_func.h" 18#include "lj_func.h"
@@ -353,8 +354,7 @@ static void gc_shrink(global_State *g, lua_State *L)
353{ 354{
354 if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1) 355 if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1)
355 lj_str_resize(L, g->strmask >> 1); /* Shrink string table. */ 356 lj_str_resize(L, g->strmask >> 1); /* Shrink string table. */
356 if (g->tmpbuf.sz > LJ_MIN_SBUF*2) 357 lj_buf_shrink(L, &g->tmpbuf); /* Shrink temp buffer. */
357 lj_str_resizebuf(L, &g->tmpbuf, g->tmpbuf.sz >> 1); /* Shrink temp buf. */
358} 358}
359 359
360/* Type of GC free functions. */ 360/* Type of GC free functions. */
diff --git a/src/lj_lex.c b/src/lj_lex.c
index 9f2b06f8..3227cadd 100644
--- a/src/lj_lex.c
+++ b/src/lj_lex.c
@@ -56,11 +56,9 @@ static int fillbuf(LexState *ls)
56 56
57static LJ_NOINLINE void save_grow(LexState *ls, int c) 57static LJ_NOINLINE void save_grow(LexState *ls, int c)
58{ 58{
59 MSize newsize;
60 if (ls->sb.sz >= LJ_MAX_STR/2) 59 if (ls->sb.sz >= LJ_MAX_STR/2)
61 lj_lex_error(ls, 0, LJ_ERR_XELEM); 60 lj_lex_error(ls, 0, LJ_ERR_XELEM);
62 newsize = ls->sb.sz * 2; 61 lj_buf_grow(ls->L, &ls->sb, 0);
63 lj_str_resizebuf(ls->L, &ls->sb, newsize);
64 ls->sb.buf[ls->sb.n++] = (char)c; 62 ls->sb.buf[ls->sb.n++] = (char)c;
65} 63}
66 64
@@ -167,7 +165,7 @@ static void read_long_string(LexState *ls, TValue *tv, int sep)
167 case '\r': 165 case '\r':
168 save(ls, '\n'); 166 save(ls, '\n');
169 inclinenumber(ls); 167 inclinenumber(ls);
170 if (!tv) lj_str_resetbuf(&ls->sb); /* avoid wasting space */ 168 if (!tv) lj_buf_reset(&ls->sb); /* Don't waste space for comments. */
171 break; 169 break;
172 default: 170 default:
173 if (tv) save_and_next(ls); 171 if (tv) save_and_next(ls);
@@ -259,7 +257,7 @@ static void read_string(LexState *ls, int delim, TValue *tv)
259 257
260static int llex(LexState *ls, TValue *tv) 258static int llex(LexState *ls, TValue *tv)
261{ 259{
262 lj_str_resetbuf(&ls->sb); 260 lj_buf_reset(&ls->sb);
263 for (;;) { 261 for (;;) {
264 if (lj_char_isident(ls->current)) { 262 if (lj_char_isident(ls->current)) {
265 GCstr *s; 263 GCstr *s;
@@ -295,10 +293,10 @@ static int llex(LexState *ls, TValue *tv)
295 next(ls); 293 next(ls);
296 if (ls->current == '[') { 294 if (ls->current == '[') {
297 int sep = skip_sep(ls); 295 int sep = skip_sep(ls);
298 lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */ 296 lj_buf_reset(&ls->sb); /* `skip_sep' may dirty the buffer */
299 if (sep >= 0) { 297 if (sep >= 0) {
300 read_long_string(ls, NULL, sep); /* long comment */ 298 read_long_string(ls, NULL, sep); /* long comment */
301 lj_str_resetbuf(&ls->sb); 299 lj_buf_reset(&ls->sb);
302 continue; 300 continue;
303 } 301 }
304 } 302 }
@@ -381,7 +379,6 @@ int lj_lex_setup(lua_State *L, LexState *ls)
381 ls->lookahead = TK_eof; /* No look-ahead token. */ 379 ls->lookahead = TK_eof; /* No look-ahead token. */
382 ls->linenumber = 1; 380 ls->linenumber = 1;
383 ls->lastline = 1; 381 ls->lastline = 1;
384 lj_str_resizebuf(ls->L, &ls->sb, LJ_MIN_SBUF);
385 next(ls); /* Read-ahead first char. */ 382 next(ls); /* Read-ahead first char. */
386 if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb && 383 if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb &&
387 char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ 384 char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */
@@ -420,7 +417,7 @@ void lj_lex_cleanup(lua_State *L, LexState *ls)
420 global_State *g = G(L); 417 global_State *g = G(L);
421 lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); 418 lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine);
422 lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); 419 lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo);
423 lj_str_freebuf(g, &ls->sb); 420 lj_buf_free(g, &ls->sb);
424} 421}
425 422
426void lj_lex_next(LexState *ls) 423void lj_lex_next(LexState *ls)
diff --git a/src/lj_lex.h b/src/lj_lex.h
index 6e18e4b0..7013f6eb 100644
--- a/src/lj_lex.h
+++ b/src/lj_lex.h
@@ -10,6 +10,7 @@
10 10
11#include "lj_obj.h" 11#include "lj_obj.h"
12#include "lj_err.h" 12#include "lj_err.h"
13#include "lj_buf.h"
13 14
14/* Lua lexer tokens. */ 15/* Lua lexer tokens. */
15#define TKDEF(_, __) \ 16#define TKDEF(_, __) \
diff --git a/src/lj_load.c b/src/lj_load.c
index 9d892678..2572e79b 100644
--- a/src/lj_load.c
+++ b/src/lj_load.c
@@ -15,6 +15,7 @@
15#include "lj_obj.h" 15#include "lj_obj.h"
16#include "lj_gc.h" 16#include "lj_gc.h"
17#include "lj_err.h" 17#include "lj_err.h"
18#include "lj_buf.h"
18#include "lj_str.h" 19#include "lj_str.h"
19#include "lj_func.h" 20#include "lj_func.h"
20#include "lj_frame.h" 21#include "lj_frame.h"
@@ -54,7 +55,7 @@ LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data,
54 ls.rdata = data; 55 ls.rdata = data;
55 ls.chunkarg = chunkname ? chunkname : "?"; 56 ls.chunkarg = chunkname ? chunkname : "?";
56 ls.mode = mode; 57 ls.mode = mode;
57 lj_str_initbuf(&ls.sb); 58 lj_buf_init(&ls.sb);
58 status = lj_vm_cpcall(L, NULL, &ls, cpparser); 59 status = lj_vm_cpcall(L, NULL, &ls, cpparser);
59 lj_lex_cleanup(L, &ls); 60 lj_lex_cleanup(L, &ls);
60 lj_gc_check(L); 61 lj_gc_check(L);
diff --git a/src/lj_meta.c b/src/lj_meta.c
index e11f1b75..db1ce928 100644
--- a/src/lj_meta.c
+++ b/src/lj_meta.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#include "lj_tab.h" 17#include "lj_tab.h"
17#include "lj_meta.h" 18#include "lj_meta.h"
@@ -283,7 +284,7 @@ TValue *lj_meta_cat(lua_State *L, TValue *top, int left)
283 ** next step: [...][CAT stack ............] 284 ** next step: [...][CAT stack ............]
284 */ 285 */
285 MSize tlen = strV(top)->len; 286 MSize tlen = strV(top)->len;
286 char *buffer; 287 char *buf;
287 int i; 288 int i;
288 for (n = 1; n <= left && tostring(L, top-n); n++) { 289 for (n = 1; n <= left && tostring(L, top-n); n++) {
289 MSize len = strV(top-n)->len; 290 MSize len = strV(top-n)->len;
@@ -291,15 +292,15 @@ TValue *lj_meta_cat(lua_State *L, TValue *top, int left)
291 lj_err_msg(L, LJ_ERR_STROV); 292 lj_err_msg(L, LJ_ERR_STROV);
292 tlen += len; 293 tlen += len;
293 } 294 }
294 buffer = lj_str_needbuf(L, &G(L)->tmpbuf, tlen); 295 buf = lj_buf_tmp(L, tlen);
295 n--; 296 n--;
296 tlen = 0; 297 tlen = 0;
297 for (i = n; i >= 0; i--) { 298 for (i = n; i >= 0; i--) {
298 MSize len = strV(top-i)->len; 299 MSize len = strV(top-i)->len;
299 memcpy(buffer + tlen, strVdata(top-i), len); 300 memcpy(buf + tlen, strVdata(top-i), len);
300 tlen += len; 301 tlen += len;
301 } 302 }
302 setstrV(L, top-n, lj_str_new(L, buffer, tlen)); 303 setstrV(L, top-n, lj_str_new(L, buf, tlen));
303 } 304 }
304 left -= n; 305 left -= n;
305 top -= n; 306 top -= n;
diff --git a/src/lj_obj.h b/src/lj_obj.h
index 6f367ea2..6faedd10 100644
--- a/src/lj_obj.h
+++ b/src/lj_obj.h
@@ -119,7 +119,7 @@ typedef int32_t BCLine; /* Bytecode line number. */
119/* Internal assembler functions. Never call these directly from C. */ 119/* Internal assembler functions. Never call these directly from C. */
120typedef void (*ASMFunction)(void); 120typedef void (*ASMFunction)(void);
121 121
122/* Resizable string buffer. Need this here, details in lj_str.h. */ 122/* Resizable string buffer. Need this here, details in lj_buf.h. */
123typedef struct SBuf { 123typedef struct SBuf {
124 char *buf; /* String buffer base. */ 124 char *buf; /* String buffer base. */
125 MSize n; /* String buffer length. */ 125 MSize n; /* String buffer length. */
diff --git a/src/lj_opt_loop.c b/src/lj_opt_loop.c
index 3a119f47..2d574089 100644
--- a/src/lj_opt_loop.c
+++ b/src/lj_opt_loop.c
@@ -11,6 +11,7 @@
11#if LJ_HASJIT 11#if LJ_HASJIT
12 12
13#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_buf.h"
14#include "lj_str.h" 15#include "lj_str.h"
15#include "lj_ir.h" 16#include "lj_ir.h"
16#include "lj_jit.h" 17#include "lj_jit.h"
@@ -271,8 +272,7 @@ static void loop_unroll(jit_State *J)
271 ** Caveat: don't call into the VM or run the GC or the buffer may be gone. 272 ** Caveat: don't call into the VM or run the GC or the buffer may be gone.
272 */ 273 */
273 invar = J->cur.nins; 274 invar = J->cur.nins;
274 subst = (IRRef1 *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, 275 subst = (IRRef1 *)lj_buf_tmp(J->L, (invar-REF_BIAS)*sizeof(IRRef1))-REF_BIAS;
275 (invar-REF_BIAS)*sizeof(IRRef1)) - REF_BIAS;
276 subst[REF_BASE] = REF_BASE; 276 subst[REF_BASE] = REF_BASE;
277 277
278 /* LOOP separates the pre-roll from the loop body. */ 278 /* LOOP separates the pre-roll from the loop body. */
diff --git a/src/lj_opt_split.c b/src/lj_opt_split.c
index 303af03c..2b04e77d 100644
--- a/src/lj_opt_split.c
+++ b/src/lj_opt_split.c
@@ -11,6 +11,7 @@
11#if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) 11#if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI))
12 12
13#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_buf.h"
14#include "lj_str.h" 15#include "lj_str.h"
15#include "lj_ir.h" 16#include "lj_ir.h"
16#include "lj_jit.h" 17#include "lj_jit.h"
@@ -201,7 +202,7 @@ static void split_ir(jit_State *J)
201 IRRef nins = J->cur.nins, nk = J->cur.nk; 202 IRRef nins = J->cur.nins, nk = J->cur.nk;
202 MSize irlen = nins - nk; 203 MSize irlen = nins - nk;
203 MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); 204 MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1));
204 IRIns *oir = (IRIns *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, need); 205 IRIns *oir = (IRIns *)lj_buf_tmp(J->L, need);
205 IRRef1 *hisubst; 206 IRRef1 *hisubst;
206 IRRef ref; 207 IRRef ref;
207 208
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 7ff7d728..3f992bec 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -1429,18 +1429,9 @@ static void fs_fixup_line(FuncState *fs, GCproto *pt,
1429 } 1429 }
1430} 1430}
1431 1431
1432/* Resize buffer if needed. */
1433static LJ_NOINLINE void fs_buf_resize(LexState *ls, MSize len)
1434{
1435 MSize sz = ls->sb.sz * 2;
1436 while (ls->sb.n + len > sz) sz = sz * 2;
1437 lj_str_resizebuf(ls->L, &ls->sb, sz);
1438}
1439
1440static LJ_AINLINE void fs_buf_need(LexState *ls, MSize len) 1432static LJ_AINLINE void fs_buf_need(LexState *ls, MSize len)
1441{ 1433{
1442 if (LJ_UNLIKELY(ls->sb.n + len > ls->sb.sz)) 1434 lj_buf_need(ls->L, &ls->sb, ls->sb.n + len);
1443 fs_buf_resize(ls, len);
1444} 1435}
1445 1436
1446/* Add string to buffer. */ 1437/* Add string to buffer. */
@@ -1469,7 +1460,7 @@ static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)
1469 VarInfo *vs =ls->vstack, *ve; 1460 VarInfo *vs =ls->vstack, *ve;
1470 MSize i, n; 1461 MSize i, n;
1471 BCPos lastpc; 1462 BCPos lastpc;
1472 lj_str_resetbuf(&ls->sb); /* Copy to temp. string buffer. */ 1463 lj_buf_reset(&ls->sb); /* Copy to temp. string buffer. */
1473 /* Store upvalue names. */ 1464 /* Store upvalue names. */
1474 for (i = 0, n = fs->nuv; i < n; i++) { 1465 for (i = 0, n = fs->nuv; i < n; i++) {
1475 GCstr *s = strref(vs[fs->uvmap[i]].name); 1466 GCstr *s = strref(vs[fs->uvmap[i]].name);
diff --git a/src/lj_state.c b/src/lj_state.c
index 8c53d37f..6c3e97b1 100644
--- a/src/lj_state.c
+++ b/src/lj_state.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#include "lj_tab.h" 17#include "lj_tab.h"
17#include "lj_func.h" 18#include "lj_func.h"
@@ -164,7 +165,7 @@ static void close_state(lua_State *L)
164 lj_ctype_freestate(g); 165 lj_ctype_freestate(g);
165#endif 166#endif
166 lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); 167 lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef);
167 lj_str_freebuf(g, &g->tmpbuf); 168 lj_buf_free(g, &g->tmpbuf);
168 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); 169 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
169 lua_assert(g->gc.total == sizeof(GG_State)); 170 lua_assert(g->gc.total == sizeof(GG_State));
170#ifndef LUAJIT_USE_SYSMALLOC 171#ifndef LUAJIT_USE_SYSMALLOC
@@ -203,7 +204,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
203 setnilV(&g->nilnode.val); 204 setnilV(&g->nilnode.val);
204 setnilV(&g->nilnode.key); 205 setnilV(&g->nilnode.key);
205 setmref(g->nilnode.freetop, &g->nilnode); 206 setmref(g->nilnode.freetop, &g->nilnode);
206 lj_str_initbuf(&g->tmpbuf); 207 lj_buf_init(&g->tmpbuf);
207 g->gc.state = GCSpause; 208 g->gc.state = GCSpause;
208 setgcref(g->gc.root, obj2gco(L)); 209 setgcref(g->gc.root, obj2gco(L));
209 setmref(g->gc.sweep, &g->gc.root); 210 setmref(g->gc.sweep, &g->gc.root);
diff --git a/src/lj_str.c b/src/lj_str.c
index 6548ee4d..623b362d 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -1,9 +1,6 @@
1/* 1/*
2** String handling. 2** String handling.
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**
5** Portions taken verbatim or adapted from the Lua interpreter.
6** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7*/ 4*/
8 5
9#include <stdio.h> 6#include <stdio.h>
@@ -14,6 +11,7 @@
14#include "lj_obj.h" 11#include "lj_obj.h"
15#include "lj_gc.h" 12#include "lj_gc.h"
16#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_buf.h"
17#include "lj_str.h" 15#include "lj_str.h"
18#include "lj_state.h" 16#include "lj_state.h"
19#include "lj_char.h" 17#include "lj_char.h"
@@ -222,33 +220,24 @@ GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
222 220
223static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len) 221static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len)
224{ 222{
225 char *p;
226 MSize i; 223 MSize i;
227 if (sb->n + len > sb->sz) { 224 char *p = lj_buf_need(L, sb, sb->n+len) + sb->n;
228 MSize sz = sb->sz * 2;
229 while (sb->n + len > sz) sz = sz * 2;
230 lj_str_resizebuf(L, sb, sz);
231 }
232 p = sb->buf + sb->n;
233 sb->n += len; 225 sb->n += len;
234 for (i = 0; i < len; i++) p[i] = str[i]; 226 for (i = 0; i < len; i++) p[i] = str[i];
235} 227}
236 228
237static void addchar(lua_State *L, SBuf *sb, int c) 229static void addchar(lua_State *L, SBuf *sb, int c)
238{ 230{
239 if (sb->n + 1 > sb->sz) { 231 char *p = lj_buf_need(L, sb, sb->n+1);
240 MSize sz = sb->sz * 2; 232 p[sb->n++] = (char)c;
241 lj_str_resizebuf(L, sb, sz);
242 }
243 sb->buf[sb->n++] = (char)c;
244} 233}
245 234
246/* Push formatted message as a string object to Lua stack. va_list variant. */ 235/* Push formatted message as a string object to Lua stack. va_list variant. */
247const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) 236const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
248{ 237{
249 SBuf *sb = &G(L)->tmpbuf; 238 SBuf *sb = &G(L)->tmpbuf;
250 lj_str_needbuf(L, sb, (MSize)strlen(fmt)); 239 lj_buf_need(L, sb, (MSize)strlen(fmt));
251 lj_str_resetbuf(sb); 240 lj_buf_reset(sb);
252 for (;;) { 241 for (;;) {
253 const char *e = strchr(fmt, '%'); 242 const char *e = strchr(fmt, '%');
254 if (e == NULL) break; 243 if (e == NULL) break;
@@ -326,14 +315,3 @@ const char *lj_str_pushf(lua_State *L, const char *fmt, ...)
326 return msg; 315 return msg;
327} 316}
328 317
329/* -- Buffer handling ----------------------------------------------------- */
330
331char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz)
332{
333 if (sz > sb->sz) {
334 if (sz < LJ_MIN_SBUF) sz = LJ_MIN_SBUF;
335 lj_str_resizebuf(L, sb, sz);
336 }
337 return sb->buf;
338}
339
diff --git a/src/lj_str.h b/src/lj_str.h
index 3aa03662..3dcd1a9b 100644
--- a/src/lj_str.h
+++ b/src/lj_str.h
@@ -37,14 +37,4 @@ LJ_FUNC const char *lj_str_pushf(lua_State *L, const char *fmt, ...)
37#endif 37#endif
38 ; 38 ;
39 39
40/* Resizable string buffers. Struct definition in lj_obj.h. */
41LJ_FUNC char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz);
42
43#define lj_str_initbuf(sb) ((sb)->buf = NULL, (sb)->sz = 0)
44#define lj_str_resetbuf(sb) ((sb)->n = 0)
45#define lj_str_resizebuf(L, sb, size) \
46 ((sb)->buf = (char *)lj_mem_realloc(L, (sb)->buf, (sb)->sz, (size)), \
47 (sb)->sz = (size))
48#define lj_str_freebuf(g, sb) lj_mem_free(g, (void *)(sb)->buf, (sb)->sz)
49
50#endif 40#endif
diff --git a/src/ljamalg.c b/src/ljamalg.c
index 962b3134..487609c4 100644
--- a/src/ljamalg.c
+++ b/src/ljamalg.c
@@ -33,6 +33,7 @@
33#include "lj_char.c" 33#include "lj_char.c"
34#include "lj_bc.c" 34#include "lj_bc.c"
35#include "lj_obj.c" 35#include "lj_obj.c"
36#include "lj_buf.c"
36#include "lj_str.c" 37#include "lj_str.c"
37#include "lj_tab.c" 38#include "lj_tab.c"
38#include "lj_func.c" 39#include "lj_func.c"