Commit 96e9477668718a85cecae19abd2fb1e7aac223c2

Kano 2014-01-14T13:42:34

api allow any size stats data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
diff --git a/Makefile.am b/Makefile.am
index bb0155e..3a20646 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -43,9 +43,7 @@ cgminer_SOURCES	+= elist.h miner.h compat.h bench_block.h	\
 
 cgminer_SOURCES	+= logging.c
 
-if HAS_BAB
 cgminer_SOURCES	+= klist.h klist.c
-endif
 
 if NEED_FPGAUTILS
 cgminer_SOURCES += fpgautils.c fpgautils.h
diff --git a/api.c b/api.c
index 83f8132..1c064ba 100644
--- a/api.c
+++ b/api.c
@@ -24,6 +24,7 @@
 #include "compat.h"
 #include "miner.h"
 #include "util.h"
+#include "klist.h"
 
 #if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || defined(USE_MINION)
 #define HAVE_AN_ASIC 1
@@ -627,6 +628,22 @@ static struct io_list *io_head = NULL;
 #define io_new(init) _io_new(init, false)
 #define sock_io_new() _io_new(SOCKBUFALLOCSIZ, true)
 
+#define ALLOC_SBITEMS 2
+#define LIMIT_SBITEMS 0
+
+typedef struct sbitem {
+	char *buf;
+	size_t siz;
+	size_t tot;
+} SBITEM;
+
+// Size to grow tot if exceeded
+#define SBEXTEND 4096
+
+#define DATASB(_item) ((SBITEM *)(_item->data))
+
+static K_LIST *strbufs;
+
 static void io_reinit(struct io_data *io_data)
 {
 	io_data->cur = io_data->ptr;
@@ -750,8 +767,10 @@ static char *escape_string(char *str, bool isjson)
 		return str;
 
 	buf = malloc(strlen(str) + count + 1);
-	if (unlikely(!buf))
-		quit(1, "Failed to malloc escape buf");
+	if (unlikely(!buf)) {
+		quithere(1, "Failed to malloc escape buf %d",
+			    (int)(strlen(str) + count + 1));
+	}
 
 	ptr = buf;
 	while (*str)
@@ -1035,115 +1054,166 @@ struct api_data *api_add_avg(struct api_data *root, char *name, float *data, boo
 	return api_add_data_full(root, name, API_AVG, (void *)data, copy_data);
 }
 
-static struct api_data *print_data(struct api_data *root, char *buf, bool isjson, bool precom)
+static void add_item_buf(K_ITEM *item, const char *str)
+{
+	size_t old_siz, new_siz, siz, ext;
+	char *buf;
+
+	buf = DATASB(item)->buf;
+	siz = (size_t)strlen(str);
+
+	old_siz = DATASB(item)->siz;
+	new_siz = old_siz + siz + 1; // include '\0'
+	if (DATASB(item)->tot < new_siz) {
+		ext = (siz + 1) + SBEXTEND - ((siz + 1) % SBEXTEND);
+		DATASB(item)->buf = buf = realloc(DATASB(item)->buf, DATASB(item)->tot + ext);
+		if (!buf) {
+			quithere(1, "OOM buf siz=%d tot=%d ext=%d",
+				    (int)siz, (int)(DATASB(item)->tot), (int)ext);
+		}
+		DATASB(item)->tot += ext;
+	}
+	memcpy(buf + old_siz, str, siz + 1);
+	DATASB(item)->siz += siz;
+}
+
+static struct api_data *print_data(struct io_data *io_data, struct api_data *root, bool isjson, bool precom)
 {
+	// N.B. strings don't use this buffer so 64 is enough (for now)
+	char buf[64];
 	struct api_data *tmp;
-	bool first = true;
+	bool done, first = true;
 	char *original, *escape;
-	char *quote;
+	K_ITEM *item;
 
-	*buf = '\0';
+	K_WLOCK(strbufs);
+	item = k_unlink_head(strbufs);
+	K_WUNLOCK(strbufs);
 
-	if (precom) {
-		*(buf++) = *COMMA;
-		*buf = '\0';
-	}
+	DATASB(item)->siz = 0;
 
-	if (isjson) {
-		strcpy(buf, JSON0);
-		buf = strchr(buf, '\0');
-		quote = JSON1;
-	} else
-		quote = (char *)BLANK;
+	if (precom)
+		add_item_buf(item, COMMA);
+
+	if (isjson)
+		add_item_buf(item, JSON0);
 
 	while (root) {
 		if (!first)
-			*(buf++) = *COMMA;
+			add_item_buf(item, COMMA);
 		else
 			first = false;
 
-		sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
+		if (isjson)
+			add_item_buf(item, JSON1);
+
+		add_item_buf(item, root->name);
 
-		buf = strchr(buf, '\0');
+		if (isjson)
+			add_item_buf(item, JSON1);
 
+		if (isjson)
+			add_item_buf(item, ":");
+		else
+			add_item_buf(item, "=");
+
+		first = false;
+
+		done = false;
 		switch(root->type) {
 			case API_STRING:
 			case API_CONST:
-				sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, (char *)(root->data));
+				if (isjson)
+					add_item_buf(item, JSON1);
+				done = true;
 				break;
 			case API_ESCAPE:
 				original = (char *)(root->data);
 				escape = escape_string((char *)(root->data), isjson);
-				sprintf(buf, "%s%s%s", quote, escape, quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, escape);
+				if (isjson)
+					add_item_buf(item, JSON1);
 				if (escape != original)
 					free(escape);
+				done = true;
 				break;
 			case API_UINT8:
-				sprintf(buf, "%u", *(uint8_t *)root->data);
+				snprintf(buf, sizeof(buf), "%u", *(uint8_t *)root->data);
 				break;
 			case API_UINT16:
-				sprintf(buf, "%u", *(uint16_t *)root->data);
+				snprintf(buf, sizeof(buf), "%u", *(uint16_t *)root->data);
 				break;
 			case API_INT:
-				sprintf(buf, "%d", *((int *)(root->data)));
+				snprintf(buf, sizeof(buf), "%d", *((int *)(root->data)));
 				break;
 			case API_UINT:
-				sprintf(buf, "%u", *((unsigned int *)(root->data)));
+				snprintf(buf, sizeof(buf), "%u", *((unsigned int *)(root->data)));
 				break;
 			case API_UINT32:
-				sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
+				snprintf(buf, sizeof(buf), "%"PRIu32, *((uint32_t *)(root->data)));
 				break;
 			case API_UINT64:
-				sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
+				snprintf(buf, sizeof(buf), "%"PRIu64, *((uint64_t *)(root->data)));
 				break;
 			case API_TIME:
-				sprintf(buf, "%lu", *((unsigned long *)(root->data)));
+				snprintf(buf, sizeof(buf), "%lu", *((unsigned long *)(root->data)));
 				break;
 			case API_DOUBLE:
-				sprintf(buf, "%f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%f", *((double *)(root->data)));
 				break;
 			case API_ELAPSED:
-				sprintf(buf, "%.0f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.0f", *((double *)(root->data)));
 				break;
 			case API_UTILITY:
 			case API_FREQ:
 			case API_MHS:
-				sprintf(buf, "%.2f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.2f", *((double *)(root->data)));
 				break;
 			case API_VOLTS:
 			case API_AVG:
-				sprintf(buf, "%.3f", *((float *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.3f", *((float *)(root->data)));
 				break;
 			case API_MHTOTAL:
-				sprintf(buf, "%.4f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.4f", *((double *)(root->data)));
 				break;
 			case API_HS:
-				sprintf(buf, "%.15f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.15f", *((double *)(root->data)));
 				break;
 			case API_DIFF:
-				sprintf(buf, "%.8f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.8f", *((double *)(root->data)));
 				break;
 			case API_BOOL:
-				sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
+				snprintf(buf, sizeof(buf), "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
 				break;
 			case API_TIMEVAL:
-				sprintf(buf, "%ld.%06ld",
+				snprintf(buf, sizeof(buf), "%ld.%06ld",
 					((struct timeval *)(root->data))->tv_sec,
 					((struct timeval *)(root->data))->tv_usec);
 				break;
 			case API_TEMP:
-				sprintf(buf, "%.2f", *((float *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.2f", *((float *)(root->data)));
 				break;
 			case API_PERCENT:
-				sprintf(buf, "%.4f", *((double *)(root->data)) * 100.0);
+				snprintf(buf, sizeof(buf), "%.4f", *((double *)(root->data)) * 100.0);
 				break;
 			default:
 				applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
-				sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, UNKNOWN);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				done = true;
 				break;
 		}
 
-		buf = strchr(buf, '\0');
+		if (!done)
+			add_item_buf(item, buf);
 
 		free(root->name);
 		if (root->data_was_malloc)
@@ -1161,7 +1231,16 @@ static struct api_data *print_data(struct api_data *root, char *buf, bool isjson
 		}
 	}
 
-	strcpy(buf, isjson ? JSON5 : SEPSTR);
+	if (isjson)
+		add_item_buf(item, JSON5);
+	else
+		add_item_buf(item, SEPSTR);
+
+	io_add(io_data, DATASB(item)->buf);
+
+	K_WLOCK(strbufs);
+	k_add_head(strbufs, item);
+	K_WUNLOCK(strbufs);
 
 	return root;
 }
@@ -1248,7 +1327,6 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 {
 	struct api_data *root = NULL;
 	char buf[TMPBUFSIZ];
-	char buf2[TMPBUFSIZ];
 	char severity[2];
 #ifdef HAVE_AN_ASIC
 	int asc;
@@ -1353,8 +1431,7 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 			root = api_add_escape(root, "Msg", buf, false);
 			root = api_add_escape(root, "Description", opt_api_description, false);
 
-			root = print_data(root, buf2, isjson, false);
-			io_add(io_data, buf2);
+			root = print_data(io_data, root, isjson, false);
 			if (isjson)
 				io_add(io_data, JSON_CLOSE);
 			return;
@@ -1369,8 +1446,7 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 	root = api_add_escape(root, "Msg", buf, false);
 	root = api_add_escape(root, "Description", opt_api_description, false);
 
-	root = print_data(root, buf2, isjson, false);
-	io_add(io_data, buf2);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson)
 		io_add(io_data, JSON_CLOSE);
 }
@@ -1775,7 +1851,6 @@ static void lockstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __ma
 static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	message(io_data, MSG_VERSION, 0, NULL, isjson);
@@ -1784,8 +1859,7 @@ static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 	root = api_add_string(root, "CGMiner", VERSION, false);
 	root = api_add_const(root, "API", APIVERSION, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -1793,7 +1867,6 @@ static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int asccount = 0;
 	int pgacount = 0;
@@ -1829,8 +1902,7 @@ static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __
 	root = api_add_const(root, "Hotplug", NONE, false);
 #endif
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -1857,7 +1929,6 @@ static const char *status2str(enum alive status)
 static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *enabled;
 	char *status;
 	int numasc = numascs();
@@ -1927,8 +1998,7 @@ static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom
 		root = api_add_percent(root, "Device Rejected%", &rejp, false);
 		root = api_add_elapsed(root, "Device Elapsed", &(dev_runtime), false);
 
-		root = print_data(root, buf, isjson, precom);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, precom);
 	}
 }
 #endif
@@ -1937,7 +2007,6 @@ static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom
 static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *enabled;
 	char *status;
 	int numpga = numpgas();
@@ -2014,8 +2083,7 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
 		root = api_add_percent(root, "Device Rejected%", &rejp, false);
 		root = api_add_elapsed(root, "Device Elapsed", &(dev_runtime), false);
 
-		root = print_data(root, buf, isjson, precom);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, precom);
 	}
 }
 #endif
@@ -2259,7 +2327,6 @@ static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
 static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	char *status, *lp;
 	int i;
@@ -2346,8 +2413,7 @@ static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 				(double)(pool->diff_stale) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
 		root = api_add_percent(root, "Pool Stale%", &stalep, false);
 
-		root = print_data(root, buf, isjson, isjson && (i > 0));
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson && (i > 0));
 	}
 
 	if (isjson && io_open)
@@ -2357,7 +2423,6 @@ static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	double utility, mhs, work_utility;
 
@@ -2409,8 +2474,7 @@ static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __mayb
 
 	mutex_unlock(&hash_lock);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -2418,7 +2482,6 @@ static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __mayb
 static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int count = 0;
 
@@ -2431,8 +2494,7 @@ static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 
 	root = api_add_int(root, "Count", &count, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -2816,7 +2878,6 @@ void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_un
 void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *reason;
 
 	if (cgpu->device_last_not_well == 0)
@@ -2874,8 +2935,7 @@ void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, b
 	root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
 	root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
 
-	root = print_data(root, buf, isjson, isjson && (device > 0));
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, isjson && (device > 0));
 }
 
 static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
@@ -2906,7 +2966,6 @@ static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
 static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	struct cgpu_info *cgpu;
 	int i;
@@ -2932,8 +2991,7 @@ static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 		root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
 		root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
 
-		root = print_data(root, buf, isjson, isjson && (i > 0));
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson && (i > 0));
 	}
 
 	if (isjson && io_open)
@@ -2974,7 +3032,6 @@ void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, b
 static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, struct cgpu_info *cgpu, bool isjson)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 
 	root = api_add_int(root, "STATS", &i, false);
 	root = api_add_string(root, "ID", id, false);
@@ -3069,8 +3126,7 @@ static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_st
 #endif
 	}
 
-	root = print_data(root, buf, isjson, isjson && (i > 0));
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, isjson && (i > 0));
 
 	return ++i;
 }
@@ -3138,7 +3194,6 @@ static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, c
 static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	message(io_data, MSG_MINECOIN, 0, NULL, isjson);
@@ -3154,8 +3209,7 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	root = api_add_bool(root, "LP", &have_longpoll, false);
 	root = api_add_diff(root, "Network Difficulty", &current_diff, true);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3163,7 +3217,6 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	if (param == NULL)
@@ -3233,8 +3286,7 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 	root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
 	root = api_add_bool(root, "WorkTime", &opt_worktime, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3281,7 +3333,6 @@ static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	struct api_data *root = NULL;
 
 #ifdef USE_USBUTILS
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	int count = 0;
 
@@ -3299,16 +3350,14 @@ static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	if (isjson)
 		io_open = io_add(io_data, COMSTR JSON_USBSTATS);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 
 	while (42) {
 		root = api_usb_stats(&count);
 		if (!root)
 			break;
 
-		root = print_data(root, buf, isjson, isjson);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson);
 	}
 
 	if (isjson && io_open)
@@ -3641,7 +3690,6 @@ static void ascidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
 static void asccount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int count = 0;
 
@@ -3654,8 +3702,7 @@ static void asccount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 
 	root = api_add_int(root, "Count", &count, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3779,7 +3826,6 @@ struct CMDS {
 static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	char cmdbuf[100];
 	bool found, access;
@@ -3810,8 +3856,7 @@ static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, c
 	root = api_add_const(root, "Exists", found ? YES : NO, false);
 	root = api_add_const(root, "Access", access ? YES : NO, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -4502,6 +4547,8 @@ void api(int api_thr_id)
 	if (opt_api_mcast)
 		mcast_init();
 
+	strbufs = k_new_list("StrBufs", sizeof(SBITEM), ALLOC_SBITEMS, LIMIT_SBITEMS, false);
+
 	while (!bye) {
 		clisiz = sizeof(cli);
 		if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {