MCPcopy Create free account
hub / github.com/MegEngine/MegCC / svsprintf

Function svsprintf

compiler/tools/kernel_exporter/utils.cpp:9–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7}
8
9std::string svsprintf(const char* fmt, va_list ap_orig) {
10 fmt = convert_fmt_str(fmt);
11 int size = 100; /* Guess we need no more than 100 bytes */
12 char* p;
13
14 if ((p = (char*)malloc(size)) == nullptr)
15 goto err;
16
17 for (;;) {
18 va_list ap;
19 va_copy(ap, ap_orig);
20 int n = vsnprintf(p, size, fmt, ap);
21 va_end(ap);
22
23 if (n < 0)
24 goto err;
25
26 if (n < size) {
27 std::string rst(p);
28 free(p);
29 return rst;
30 }
31
32 size = n + 1;
33
34 char* np = (char*)realloc(p, size);
35 if (!np) {
36 free(p);
37 goto err;
38 } else
39 p = np;
40 }
41
42err:
43 fprintf(stderr, "could not allocate memory for svsprintf; fmt=%s\n", fmt);
44 __builtin_trap();
45}
46
47std::string ssprintf(const char* fmt, ...) {
48 va_list ap;

Callers 1

ssprintfFunction · 0.70

Calls 1

convert_fmt_strFunction · 0.85

Tested by

no test coverage detected