MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / sprintf_auto_resize

Function sprintf_auto_resize

util/logmsg.c:93–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91}
92
93static int sprintf_auto_resize(char **headp, int *offp, int len, const char *fmt, const char *content)
94{
95 char *head = *headp;
96 int off = *offp;
97 char dummy[1];
98 int size = snprintf(dummy, 1, fmt, content);
99 int need = off + size + 1;
100
101 if (need > len) { /* Resize the buffer to fit more content. */
102 if (off < len) { /* The buffer on the stack isn't wide enough. Switch to malloc. */
103 head = malloc(need);
104 memcpy(head, *headp, off);
105 } else {
106 head = realloc(head, need);
107 }
108 if (head == NULL)
109 return 0;
110 }
111
112 off += snprintf(head + off, size + 1, fmt, content);
113
114 *headp = head;
115 *offp = off;
116 return size;
117}
118
119int logmsgv(loglvl lvl, const char *fmt, va_list args)
120{

Callers 1

logmsgvFunction · 0.85

Calls 2

mallocFunction · 0.85
reallocFunction · 0.85

Tested by

no test coverage detected