Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OutputMemory.cpp « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d7f49728d3f577dd3a4f7829dc0b056cf1ec8fb8 (plain)
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
/*
 * OutputMemory.cpp
 *
 *  Created on: 10 Jan 2016
 *      Authors: David and Christian
 */

#include "OutputMemory.h"
#include "Platform.h"
#include "RepRap.h"
#include <cstdarg>

/*static*/ OutputBuffer * volatile OutputBuffer::freeOutputBuffers = nullptr;		// Messages may also be sent by ISRs,
/*static*/ volatile size_t OutputBuffer::usedOutputBuffers = 0;						// so make these volatile.
/*static*/ volatile size_t OutputBuffer::maxUsedOutputBuffers = 0;

//*************************************************************************************************
// OutputBuffer class implementation

void OutputBuffer::Append(OutputBuffer *other)
{
	if (other != nullptr)
	{
		last->next = other;
		last = other->last;
		if (other->hadOverflow)
		{
			hadOverflow = true;
		}

		for (OutputBuffer *item = Next(); item != other; item = item->Next())
		{
			item->last = last;
		}
	}
}

void OutputBuffer::IncreaseReferences(size_t refs)
{
	if (refs > 0)
	{
		TaskCriticalSectionLocker lock;

		for(OutputBuffer *item = this; item != nullptr; item = item->Next())
		{
			item->references += refs;
			item->isReferenced = true;
		}
	}
}

size_t OutputBuffer::Length() const
{
	size_t totalLength = 0;
	for (const OutputBuffer *current = this; current != nullptr; current = current->Next())
	{
		totalLength += current->DataLength();
	}
	return totalLength;
}

char &OutputBuffer::operator[](size_t index)
{
	// Get the right buffer to access
	OutputBuffer *itemToIndex = this;
	while (index > itemToIndex->DataLength())
	{
		index -= itemToIndex->DataLength();
		itemToIndex = itemToIndex->Next();
	}

	// Return the char reference
	return itemToIndex->data[index];
}

char OutputBuffer::operator[](size_t index) const
{
	// Get the right buffer to access
	const OutputBuffer *itemToIndex = this;
	while (index > itemToIndex->DataLength())
	{
		index -= itemToIndex->DataLength();
		itemToIndex = itemToIndex->Next();
	}

	// Return the char reference
	return itemToIndex->data[index];
}

const char *OutputBuffer::Read(size_t len)
{
	size_t offset = bytesRead;
	bytesRead += len;
	return data + offset;
}

size_t OutputBuffer::printf(const char *fmt, ...)
{
	char formatBuffer[FORMAT_STRING_LENGTH];
	va_list vargs;
	va_start(vargs, fmt);
	SafeVsnprintf(formatBuffer, ARRAY_SIZE(formatBuffer), fmt, vargs);
	va_end(vargs);

	return copy(formatBuffer);
}

size_t OutputBuffer::vprintf(const char *fmt, va_list vargs)
{
	char formatBuffer[FORMAT_STRING_LENGTH];
	SafeVsnprintf(formatBuffer, ARRAY_SIZE(formatBuffer), fmt, vargs);

	return cat(formatBuffer);
}

size_t OutputBuffer::catf(const char *fmt, ...)
{
	char formatBuffer[FORMAT_STRING_LENGTH];
	va_list vargs;
	va_start(vargs, fmt);
	SafeVsnprintf(formatBuffer, ARRAY_SIZE(formatBuffer), fmt, vargs);
	va_end(vargs);

	formatBuffer[ARRAY_UPB(formatBuffer)] = 0;
	return cat(formatBuffer);
}

size_t OutputBuffer::copy(const char c)
{
	// Unlink existing entries before starting the copy process
	if (next != nullptr)
	{
		ReleaseAll(next);
		last = this;
	}

	// Set the data
	data[0] = c;
	dataLength = 1;
	return 1;
}

size_t OutputBuffer::copy(const char *src)
{
	return copy(src, strlen(src));
}

size_t OutputBuffer::copy(const char *src, size_t len)
{
	// Unlink existing entries before starting the copy process
	if (next != nullptr)
	{
		ReleaseAll(next);
		last = this;
	}

	dataLength = 0;
	return cat(src, len);
}

size_t OutputBuffer::cat(const char c)
{
	// See if we can append a char
	if (last->dataLength == OUTPUT_BUFFER_SIZE)
	{
		// No - allocate a new item and copy the data
		OutputBuffer *nextBuffer;
		if (!Allocate(nextBuffer))
		{
			// We cannot store any more data
			hadOverflow = true;
			return 0;
		}
		nextBuffer->references = references;
		nextBuffer->copy(c);

		// Link the new item to this list
		last->next = nextBuffer;
		for (OutputBuffer *item = this; item != nextBuffer; item = item->Next())
		{
			item->last = nextBuffer;
		}
	}
	else
	{
		// Yes - we have enough space left
		last->data[last->dataLength++] = c;
	}
	return 1;
}

size_t OutputBuffer::cat(const char *src)
{
	return cat(src, strlen(src));
}

size_t OutputBuffer::cat(const char *src, size_t len)
{
	size_t copied = 0;
	while (copied < len)
	{
		if (last->dataLength == OUTPUT_BUFFER_SIZE)
		{
			// The last buffer is full
			OutputBuffer *nextBuffer;
			if (!Allocate(nextBuffer))
			{
				// We cannot store any more data, stop here
				hadOverflow = true;
				break;
			}
			nextBuffer->references = references;
			last->next = nextBuffer;
			last = nextBuffer->last;
			for (OutputBuffer *item = Next(); item != nextBuffer; item = item->Next())
			{
				item->last = last;
			}
		}
		const size_t copyLength = min<size_t>(len - copied, OUTPUT_BUFFER_SIZE - last->dataLength);
		memcpy(last->data + last->dataLength, src + copied, copyLength);
		last->dataLength += copyLength;
		copied += copyLength;
	}
	return copied;
}

size_t OutputBuffer::cat(StringRef &str)
{
	return cat(str.c_str(), str.strlen());
}

// Encode a string in JSON format and append it to a string buffer and return the number of bytes written
size_t OutputBuffer::EncodeString(const char *src, size_t srcLength, bool allowControlChars, bool encapsulateString)
{
	size_t bytesWritten = 0;
	if (encapsulateString)
	{
		bytesWritten += cat('"');
	}

	if (srcLength != 0)
	{
		size_t srcPointer = 1;
		char c = *src++;
		while (srcPointer <= srcLength && c != 0 && (c >= ' ' || allowControlChars))
		{
			char esc;
			switch (c)
			{
			case '\r':
				esc = 'r';
				break;
			case '\n':
				esc = 'n';
				break;
			case '\t':
				esc = 't';
				break;
			case '"':
			case '\\':
#if 1
			// In theory we should escape '/' as well. However, we never used to, and doing so confuses PanelDue.
			// This will be fixed in PanelDue firmware version 1.15, but in the mean time, don't escape '/'.
#else
			case '/':
#endif
				esc = c;
				break;
			default:
				esc = 0;
				break;
			}

			if (esc != 0)
			{
				bytesWritten += cat('\\');
				bytesWritten += cat(esc);
			}
			else
			{
				bytesWritten += cat(c);
			}

			c = *src++;
			srcPointer++;
		}
	}

	if (encapsulateString)
	{
		bytesWritten += cat('"');
	}
	return bytesWritten;
}

size_t OutputBuffer::EncodeString(const StringRef& str, bool allowControlChars, bool encapsulateString)
{
	return EncodeString(str.c_str(), str.Capacity(), encapsulateString);
}

size_t OutputBuffer::EncodeReply(OutputBuffer *src, bool allowControlChars)
{
	size_t bytesWritten = cat('"');

	while (src != nullptr)
	{
		bytesWritten += EncodeString(src->Data(), src->DataLength(), allowControlChars, false);
		src = Release(src);
	}

	bytesWritten += cat('"');
	return bytesWritten;
}

// Write all the data to file, but don't release the buffers
// Returns true if successful
bool OutputBuffer::WriteToFile(FileData& f) const
{
	bool endedInNewline = false;
	const OutputBuffer *current = this;
	do
	{
		if (current->dataLength != 0)
		{
			if (!f.Write(current->data, current->dataLength))
			{
				return false;
			}
			endedInNewline = current->data[current->dataLength - 1] == '\n';
		}
		current = current->Next();
	} while (current != nullptr);

	if (!endedInNewline)
	{
		return f.Write('\n');
	}
	return true;
}

// Initialise the output buffers manager
/*static*/ void OutputBuffer::Init()
{
	freeOutputBuffers = nullptr;
	for (size_t i = 0; i < OUTPUT_BUFFER_COUNT; i++)
	{
		freeOutputBuffers = new OutputBuffer(freeOutputBuffers);
	}
}

// Allocates an output buffer instance which can be used for (large) string outputs. This must be thread safe. Not safe to call from interrupts!
/*static*/ bool OutputBuffer::Allocate(OutputBuffer *&buf)
{
	{
		TaskCriticalSectionLocker lock;

		buf = freeOutputBuffers;
		if (buf != nullptr)
		{
			freeOutputBuffers = buf->next;
			usedOutputBuffers++;
			if (usedOutputBuffers > maxUsedOutputBuffers)
			{
				maxUsedOutputBuffers = usedOutputBuffers;
			}

			// Initialise the buffer before we release the lock in case another task uses it immediately
			buf->next = nullptr;
			buf->last = buf;
			buf->dataLength = buf->bytesRead = 0;
			buf->references = 1;					// assume it's only used once by default
			buf->isReferenced = false;
			buf->hadOverflow = false;

			return true;
		}
	}

	reprap.GetPlatform().LogError(ErrorCode::OutputStarvation);
	return false;
}

// Get the number of bytes left for continuous writing
/*static*/ size_t OutputBuffer::GetBytesLeft(const OutputBuffer *writingBuffer)
{
	const size_t freeOutputBuffers = OUTPUT_BUFFER_COUNT - usedOutputBuffers;
	const size_t bytesLeft = OUTPUT_BUFFER_SIZE - writingBuffer->last->DataLength();

	if (freeOutputBuffers < RESERVED_OUTPUT_BUFFERS)
	{
		// Keep some space left to encapsulate the responses (e.g. via an HTTP header)
		return bytesLeft;
	}

	return bytesLeft + (freeOutputBuffers - RESERVED_OUTPUT_BUFFERS) * OUTPUT_BUFFER_SIZE;
}

// Truncate an output buffer to free up more memory. Returns the number of released bytes.
/*static */ size_t OutputBuffer::Truncate(OutputBuffer *buffer, size_t bytesNeeded)
{
	// Can we free up space from this chain? Don't break it up if it's referenced anywhere else
	if (buffer == nullptr || buffer->Next() == nullptr || buffer->IsReferenced())
	{
		// No
		return 0;
	}

	// Yes - free up the last entries
	size_t releasedBytes = 0;
	OutputBuffer *previousItem;
	do {
		// Get two the last entries from the chain
		previousItem = buffer;
		OutputBuffer *lastItem = previousItem->Next();
		while (lastItem->Next() != nullptr)
		{
			previousItem = lastItem;
			lastItem = lastItem->Next();
		}

		// Unlink and free the last entry
		previousItem->next = nullptr;
		Release(lastItem);
		releasedBytes += OUTPUT_BUFFER_SIZE;
	} while (previousItem != buffer && releasedBytes < bytesNeeded);

	// Update all the references to the last item
	for(OutputBuffer *item = buffer; item != nullptr; item = item->Next())
	{
		item->last = previousItem;
	}
	return releasedBytes;
}

// Releases an output buffer instance and returns the next entry from the chain
/*static */ OutputBuffer *OutputBuffer::Release(OutputBuffer *buf)
{
	TaskCriticalSectionLocker lock;
	OutputBuffer * const nextBuffer = buf->next;

	// If this one is reused by another piece of code, don't free it up
	if (buf->references > 1)
	{
		buf->references--;
		buf->bytesRead = 0;
	}
	else
	{
		// Otherwise prepend it to the list of free output buffers again
		buf->next = freeOutputBuffers;
		freeOutputBuffers = buf;
		usedOutputBuffers--;
	}
	return nextBuffer;
}

/*static */ void OutputBuffer::ReleaseAll(OutputBuffer * volatile &buf)
{
	while (buf != nullptr)
	{
		buf = Release(buf);
	}
}

/*static*/ void OutputBuffer::Diagnostics(MessageType mtype)
{
	reprap.GetPlatform().MessageF(mtype, "Used output buffers: %d of %d (%d max)\n",
			usedOutputBuffers, OUTPUT_BUFFER_COUNT, maxUsedOutputBuffers);
}

//*************************************************************************************************
// OutputStack class implementation

// Push an OutputBuffer chain to the stack
void OutputStack::Push(OutputBuffer *buffer) volatile
{
	if (buffer != nullptr)
	{
		{
			TaskCriticalSectionLocker lock;

			if (count < OUTPUT_STACK_DEPTH)
			{
				buffer->whenQueued = millis();
				items[count++] = buffer;
				return;
			}
		}
		OutputBuffer::ReleaseAll(buffer);
		reprap.GetPlatform().LogError(ErrorCode::OutputStackOverflow);
	}
}

// Pop an OutputBuffer chain or return nullptr if none is available
OutputBuffer *OutputStack::Pop() volatile
{
	TaskCriticalSectionLocker lock;

	if (count == 0)
	{
		return nullptr;
	}

	OutputBuffer *item = items[0];
	for (size_t i = 1; i < count; i++)
	{
		items[i - 1] = items[i];
	}
	count--;

	return item;
}

// Returns the first item from the stack or nullptr if none is available
OutputBuffer *OutputStack::GetFirstItem() const volatile
{
	return (count == 0) ? nullptr : items[0];
}

// Update the first item of the stack
void OutputStack::SetFirstItem(OutputBuffer *buffer) volatile
{
	if (buffer == nullptr)
	{
		(void)Pop();
	}
	else
	{
		items[0] = buffer;
		buffer->whenQueued = millis();
	}
}

// Returns the last item from the stack or nullptr if none is available
OutputBuffer *OutputStack::GetLastItem() const volatile
{
	return (count == 0) ? nullptr : items[count - 1];
}

// Get the total length of all queued buffers
size_t OutputStack::DataLength() const volatile
{
	size_t totalLength = 0;

	TaskCriticalSectionLocker lock;
	for (size_t i = 0; i < count; i++)
	{
		totalLength += items[i]->Length();
	}

	return totalLength;
}

// Append another OutputStack to this instance. If no more space is available,
// all OutputBuffers that can't be added are automatically released
void OutputStack::Append(volatile OutputStack& stack) volatile
{
	for(size_t i = 0; i < stack.count; i++)
	{
		if (count < OUTPUT_STACK_DEPTH)
		{
			items[count++] = stack.items[i];
		}
		else
		{
			reprap.GetPlatform().LogError(ErrorCode::OutputStackOverflow);
			OutputBuffer::ReleaseAll(stack.items[i]);
		}
	}
}

// Increase the number of references for each OutputBuffer on the stack
void OutputStack::IncreaseReferences(size_t num) volatile
{
	TaskCriticalSectionLocker lock;
	for(size_t i = 0; i < count; i++)
	{
		items[i]->IncreaseReferences(num);
	}
}

// Release all buffers and clean up
void OutputStack::ReleaseAll() volatile
{
	for(size_t i = 0; i < count; i++)
	{
		OutputBuffer::ReleaseAll(items[i]);
	}
	count = 0;
}

// End