aboutsummaryrefslogtreecommitdiff
path: root/src/lj_bcwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_bcwrite.c')
-rw-r--r--src/lj_bcwrite.c24
1 files changed, 8 insertions, 16 deletions
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