• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 11774765987

11 Nov 2024 08:22AM UTC coverage: 92.931% (-0.008%) from 92.939%
11774765987

push

github

mandesero
tmp commit for ci check

5694 of 6033 branches covered (94.38%)

Branch coverage included in aggregate %.

21704 of 23449 relevant lines covered (92.56%)

2961521.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.62
/src/lj_wbuf.c
1
/*
2
** Low-level writer for LuaJIT.
3
**
4
** Major portions taken verbatim or adapted from the LuaVela.
5
** Copyright (C) 2015-2019 IPONWEB Ltd.
6
*/
7

8
#define lj_wbuf_c
9
#define LUA_CORE
10

11
#include <errno.h>
12

13
#include "lj_obj.h"
14
#include "lj_wbuf.h"
15
#include "lj_utils.h"
16

17
static LJ_AINLINE void wbuf_set_flag(struct lj_wbuf *buf, uint8_t flag)
×
18
{
19
  buf->flags |= flag;
×
20
}
21

22
static LJ_AINLINE void wbuf_save_errno(struct lj_wbuf *buf)
×
23
{
24
  buf->saved_errno = errno;
×
25
}
×
26

27
static LJ_AINLINE size_t wbuf_len(const struct lj_wbuf *buf)
1,555,531✔
28
{
29
  return (size_t)(buf->pos - buf->buf);
1,555,531✔
30
}
31

32
static LJ_AINLINE size_t wbuf_left(const struct lj_wbuf *buf)
1,555,511✔
33
{
34
  return buf->size - wbuf_len(buf);
1,555,511✔
35
}
36

37
void lj_wbuf_init(struct lj_wbuf *buf, lj_wbuf_writer writer,
40✔
38
                  void *ctx, uint8_t *mem, size_t size)
39
{
40
  buf->ctx = ctx;
40✔
41
  buf->writer = writer;
40✔
42
  buf->buf = mem;
40✔
43
  buf->pos = mem;
40✔
44
  buf->size = size;
40✔
45
  buf->flags = 0;
40✔
46
  buf->saved_errno = 0;
40✔
47
}
20✔
48

49
void LJ_FASTCALL lj_wbuf_terminate(struct lj_wbuf *buf)
20✔
50
{
51
  lj_wbuf_init(buf, NULL, NULL, NULL, 0);
20✔
52
}
20✔
53

54
static LJ_AINLINE void wbuf_reserve(struct lj_wbuf *buf, size_t n)
1,555,511✔
55
{
56
  lj_assertX(n <= buf->size, "wbuf overflow");
1,555,511✔
57
  if (LJ_UNLIKELY(wbuf_left(buf) < n))
1,555,511✔
58
    lj_wbuf_flush(buf);
×
59
}
60

61
/* Writes a byte to the output buffer. */
62
void LJ_FASTCALL lj_wbuf_addbyte(struct lj_wbuf *buf, uint8_t b)
389,124✔
63
{
64
  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP)))
389,124✔
65
    return;
66
  wbuf_reserve(buf, sizeof(b));
389,124✔
67
  *buf->pos++ = b;
389,124✔
68
}
69

70
/* Writes an unsigned integer which is at most 64 bits long to the output. */
71
void LJ_FASTCALL lj_wbuf_addu64(struct lj_wbuf *buf, uint64_t n)
780,224✔
72
{
73
  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP)))
780,224✔
74
    return;
75
  wbuf_reserve(buf, LEB128_U64_MAXSIZE);
780,224✔
76
  buf->pos += (ptrdiff_t)lj_utils_write_uleb128(buf->pos, n);
780,224✔
77
}
78

79
/* Writes n bytes from an arbitrary buffer src to the buffer. */
80
void lj_wbuf_addn(struct lj_wbuf *buf, const void *src, size_t n)
386,163✔
81
{
82
  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP)))
386,163✔
83
    return;
84
  /*
85
  ** Very unlikely: We are told to write a large buffer at once.
86
  ** Buffer doesn't belong to us so we must to pump data
87
  ** through the buffer.
88
  */
89
  while (LJ_UNLIKELY(n > buf->size)) {
386,163✔
90
    const size_t left = wbuf_left(buf);
×
91
    memcpy(buf->pos, src, left);
×
92
    buf->pos += (ptrdiff_t)left;
×
93
    lj_wbuf_flush(buf);
×
94
    src = (uint8_t *)src + (ptrdiff_t)left;
×
95
    n -= left;
×
96
  }
97

98
  wbuf_reserve(buf, n);
386,163✔
99
  memcpy(buf->pos, src, n);
386,163✔
100
  buf->pos += (ptrdiff_t)n;
386,163✔
101
}
102

103
/* Writes a \0-terminated C string to the output buffer. */
104
void LJ_FASTCALL lj_wbuf_addstring(struct lj_wbuf *buf, const char *s)
386,123✔
105
{
106
  const size_t l = strlen(s);
386,123✔
107

108
  /* Check that profiling is still active is made in the callee's scope. */
109
  lj_wbuf_addu64(buf, (uint64_t)l);
386,123✔
110
  lj_wbuf_addn(buf, s, l);
386,123✔
111
}
386,123✔
112

113
void LJ_FASTCALL lj_wbuf_flush(struct lj_wbuf *buf)
20✔
114
{
115
  const size_t len = wbuf_len(buf);
20✔
116
  size_t written;
20✔
117

118
  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP)))
20✔
119
    return;
120

121
  written = buf->writer((const void **)&buf->buf, len, buf->ctx);
20✔
122

123
  if (LJ_UNLIKELY(written < len)) {
20✔
124
    wbuf_set_flag(buf, STREAM_ERRIO);
×
125
    wbuf_save_errno(buf);
×
126
  }
127
  if (LJ_UNLIKELY(buf->buf == NULL)) {
20✔
128
    wbuf_set_flag(buf, STREAM_STOP);
×
129
    wbuf_save_errno(buf);
×
130
  }
131
  buf->pos = buf->buf;
20✔
132
}
133

134
int LJ_FASTCALL lj_wbuf_test_flag(const struct lj_wbuf *buf, uint8_t flag)
1,558,295✔
135
{
136
  return buf->flags & flag;
1,555,531✔
137
}
138

139
int LJ_FASTCALL lj_wbuf_errno(const struct lj_wbuf *buf)
×
140
{
141
  return buf->saved_errno;
×
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc