3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
13 /* Resizable string buffers. */
15 /* The SBuf struct definition is in lj_obj.h:
16 ** char *w; Write pointer.
17 ** char *e; End pointer.
18 ** char *b; Base pointer.
19 ** MRef L; lua_State, used for buffer resizing. Extension bits in 3 LSB.
22 /* Extended string buffer. */
23 typedef struct SBufExt
{
26 GCRef cowref
; /* Copy-on-write object reference. */
27 MRef bsb
; /* Borrowed string buffer. */
29 char *r
; /* Read pointer. */
30 GCRef dict_str
; /* Serialization string dictionary table. */
31 GCRef dict_mt
; /* Serialization metatable dictionary table. */
32 int depth
; /* Remaining recursion depth. */
35 #define sbufsz(sb) ((MSize)((sb)->e - (sb)->b))
36 #define sbuflen(sb) ((MSize)((sb)->w - (sb)->b))
37 #define sbufleft(sb) ((MSize)((sb)->e - (sb)->w))
38 #define sbufxlen(sbx) ((MSize)((sbx)->w - (sbx)->r))
39 #define sbufxslack(sbx) ((MSize)((sbx)->r - (sbx)->b))
41 #define SBUF_MASK_FLAG (7)
42 #define SBUF_MASK_L (~(GCSize)SBUF_MASK_FLAG)
43 #define SBUF_FLAG_EXT 1 /* Extended string buffer. */
44 #define SBUF_FLAG_COW 2 /* Copy-on-write buffer. */
45 #define SBUF_FLAG_BORROW 4 /* Borrowed string buffer. */
48 ((lua_State *)(void *)(uintptr_t)(mrefu((sb)->L) & SBUF_MASK_L))
49 #define setsbufL(sb, l) (setmref((sb)->L, (l)))
50 #define setsbufXL(sb, l, flag) \
51 (setmrefu((sb)->L, (GCSize)(uintptr_t)(void *)(l) + (flag)))
52 #define setsbufXL_(sb, l) \
53 (setmrefu((sb)->L, (GCSize)(uintptr_t)(void *)(l) | (mrefu((sb)->L) & SBUF_MASK_FLAG)))
55 #define sbufflag(sb) (mrefu((sb)->L))
56 #define sbufisext(sb) (sbufflag((sb)) & SBUF_FLAG_EXT)
57 #define sbufiscow(sb) (sbufflag((sb)) & SBUF_FLAG_COW)
58 #define sbufisborrow(sb) (sbufflag((sb)) & SBUF_FLAG_BORROW)
59 #define sbufiscoworborrow(sb) (sbufflag((sb)) & (SBUF_FLAG_COW|SBUF_FLAG_BORROW))
61 (lj_assertG_(G(sbufL(sb)), sbufisext(sb), "not an SBufExt"), (SBufExt *)(sb))
62 #define setsbufflag(sb, flag) (setmrefu((sb)->L, (flag)))
65 (LJ_HASBUFFER && tvisudata(o) && udataV(o)->udtype == UDTYPE_BUFFER)
66 #define bufV(o) check_exp(tvisbuf(o), ((SBufExt *)uddata(udataV(o))))
68 /* Buffer management */
69 LJ_FUNC
char *LJ_FASTCALL
lj_buf_need2(SBuf
*sb
, MSize sz
);
70 LJ_FUNC
char *LJ_FASTCALL
lj_buf_more2(SBuf
*sb
, MSize sz
);
71 LJ_FUNC
void LJ_FASTCALL
lj_buf_shrink(lua_State
*L
, SBuf
*sb
);
72 LJ_FUNC
char * LJ_FASTCALL
lj_buf_tmp(lua_State
*L
, MSize sz
);
74 static LJ_AINLINE
void lj_buf_init(lua_State
*L
, SBuf
*sb
)
77 sb
->w
= sb
->e
= sb
->b
= NULL
;
80 static LJ_AINLINE
void lj_buf_reset(SBuf
*sb
)
85 static LJ_AINLINE SBuf
*lj_buf_tmp_(lua_State
*L
)
87 SBuf
*sb
= &G(L
)->tmpbuf
;
93 static LJ_AINLINE
void lj_buf_free(global_State
*g
, SBuf
*sb
)
95 lj_assertG(!sbufisext(sb
), "bad free of SBufExt");
96 lj_mem_free(g
, sb
->b
, sbufsz(sb
));
99 static LJ_AINLINE
char *lj_buf_need(SBuf
*sb
, MSize sz
)
101 if (LJ_UNLIKELY(sz
> sbufsz(sb
)))
102 return lj_buf_need2(sb
, sz
);
106 static LJ_AINLINE
char *lj_buf_more(SBuf
*sb
, MSize sz
)
108 if (LJ_UNLIKELY(sz
> sbufleft(sb
)))
109 return lj_buf_more2(sb
, sz
);
113 /* Extended buffer management */
114 static LJ_AINLINE
void lj_bufx_init(lua_State
*L
, SBufExt
*sbx
)
116 memset(sbx
, 0, sizeof(SBufExt
));
117 setsbufXL(sbx
, L
, SBUF_FLAG_EXT
);
120 static LJ_AINLINE
void lj_bufx_set_borrow(lua_State
*L
, SBufExt
*sbx
, SBuf
*sb
)
122 setsbufXL(sbx
, L
, SBUF_FLAG_EXT
| SBUF_FLAG_BORROW
);
123 setmref(sbx
->bsb
, sb
);
124 sbx
->r
= sbx
->w
= sbx
->b
= sb
->b
;
128 static LJ_AINLINE
void lj_bufx_set_cow(lua_State
*L
, SBufExt
*sbx
,
129 const char *p
, MSize len
)
131 setsbufXL(sbx
, L
, SBUF_FLAG_EXT
| SBUF_FLAG_COW
);
132 sbx
->r
= sbx
->b
= (char *)p
;
133 sbx
->w
= sbx
->e
= (char *)p
+ len
;
136 static LJ_AINLINE
void lj_bufx_reset(SBufExt
*sbx
)
138 if (sbufiscow(sbx
)) {
139 setmrefu(sbx
->L
, (mrefu(sbx
->L
) & ~(GCSize
)SBUF_FLAG_COW
));
140 setgcrefnull(sbx
->cowref
);
141 sbx
->b
= sbx
->e
= NULL
;
143 sbx
->r
= sbx
->w
= sbx
->b
;
146 static LJ_AINLINE
void lj_bufx_free(lua_State
*L
, SBufExt
*sbx
)
148 if (!sbufiscoworborrow(sbx
)) lj_mem_free(G(L
), sbx
->b
, sbufsz(sbx
));
149 setsbufXL(sbx
, L
, SBUF_FLAG_EXT
);
150 setgcrefnull(sbx
->cowref
);
151 sbx
->r
= sbx
->w
= sbx
->b
= sbx
->e
= NULL
;
154 #if LJ_HASBUFFER && LJ_HASJIT
155 LJ_FUNC
void lj_bufx_set(SBufExt
*sbx
, const char *p
, MSize len
, GCobj
*o
);
157 LJ_FUNC MSize LJ_FASTCALL
lj_bufx_more(SBufExt
*sbx
, MSize sz
);
161 /* Low-level buffer put operations */
162 LJ_FUNC SBuf
*lj_buf_putmem(SBuf
*sb
, const void *q
, MSize len
);
163 #if LJ_HASJIT || LJ_HASFFI
164 LJ_FUNC SBuf
* LJ_FASTCALL
lj_buf_putchar(SBuf
*sb
, int c
);
166 LJ_FUNC SBuf
* LJ_FASTCALL
lj_buf_putstr(SBuf
*sb
, GCstr
*s
);
168 static LJ_AINLINE
char *lj_buf_wmem(char *p
, const void *q
, MSize len
)
170 return (char *)memcpy(p
, q
, len
) + len
;
173 static LJ_AINLINE
void lj_buf_putb(SBuf
*sb
, int c
)
175 char *w
= lj_buf_more(sb
, 1);
180 /* High-level buffer put operations */
181 LJ_FUNCA SBuf
* LJ_FASTCALL
lj_buf_putstr_reverse(SBuf
*sb
, GCstr
*s
);
182 LJ_FUNCA SBuf
* LJ_FASTCALL
lj_buf_putstr_lower(SBuf
*sb
, GCstr
*s
);
183 LJ_FUNCA SBuf
* LJ_FASTCALL
lj_buf_putstr_upper(SBuf
*sb
, GCstr
*s
);
184 LJ_FUNC SBuf
*lj_buf_putstr_rep(SBuf
*sb
, GCstr
*s
, int32_t rep
);
185 LJ_FUNC SBuf
*lj_buf_puttab(SBuf
*sb
, GCtab
*t
, GCstr
*sep
,
186 int32_t i
, int32_t e
);
188 /* Miscellaneous buffer operations */
189 LJ_FUNCA GCstr
* LJ_FASTCALL
lj_buf_tostr(SBuf
*sb
);
190 LJ_FUNC GCstr
*lj_buf_cat2str(lua_State
*L
, GCstr
*s1
, GCstr
*s2
);
191 LJ_FUNC
uint32_t LJ_FASTCALL
lj_buf_ruleb128(const char **pp
);
193 static LJ_AINLINE GCstr
*lj_buf_str(lua_State
*L
, SBuf
*sb
)
195 return lj_str_new(L
, sb
->b
, sbuflen(sb
));