Optimisation: Remove duplicated code in cmdqueue_could_enqueue_back()
Code is 28 lines shorter :) Change in memory: Flash: -42 bytes SRAM: 0 bytes
This commit is contained in:
parent
6a470791d3
commit
76fb4610ea
|
|
@ -165,61 +165,33 @@ static bool cmdqueue_could_enqueue_back(size_t len_asked, bool atomic_update = f
|
||||||
// Full buffer.
|
// Full buffer.
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (serial_count > 0) {
|
// If there is some data stored starting at bufindw, len_asked is certainly smaller than
|
||||||
// If there is some data stored starting at bufindw, len_asked is certainly smaller than
|
// the allocated data buffer. Try to reserve a new buffer and to move the already received
|
||||||
// the allocated data buffer. Try to reserve a new buffer and to move the already received
|
// serial data.
|
||||||
// serial data.
|
// How much memory to reserve for the commands pushed to the front?
|
||||||
// How much memory to reserve for the commands pushed to the front?
|
// End of the queue, when pushing to the end.
|
||||||
// End of the queue, when pushing to the end.
|
size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
|
||||||
size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
|
if (bufindw < bufindr)
|
||||||
if (bufindw < bufindr)
|
// Simple case. There is a contiguous space between the write buffer and the read buffer.
|
||||||
// Simple case. There is a contiguous space between the write buffer and the read buffer.
|
return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
|
||||||
return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
|
// Otherwise the free space is split between the start and end.
|
||||||
// Otherwise the free space is split between the start and end.
|
if (// Could one fit to the end, including the reserve?
|
||||||
if (// Could one fit to the end, including the reserve?
|
endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
|
||||||
endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
|
// Could one fit to the end, and the reserve to the start?
|
||||||
// Could one fit to the end, and the reserve to the start?
|
(endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
|
||||||
(endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
|
return true;
|
||||||
return true;
|
// Could one fit both to the start?
|
||||||
// Could one fit both to the start?
|
if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
|
||||||
if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
|
// Mark the rest of the buffer as used.
|
||||||
// Mark the rest of the buffer as used.
|
memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
|
||||||
memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
|
// and point to the start.
|
||||||
// and point to the start.
|
// Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
|
||||||
// Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
|
if (atomic_update)
|
||||||
if (atomic_update)
|
cli();
|
||||||
cli();
|
bufindw = 0;
|
||||||
bufindw = 0;
|
if (atomic_update)
|
||||||
if (atomic_update)
|
sei();
|
||||||
sei();
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// How much memory to reserve for the commands pushed to the front?
|
|
||||||
// End of the queue, when pushing to the end.
|
|
||||||
size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
|
|
||||||
if (bufindw < bufindr)
|
|
||||||
// Simple case. There is a contiguous space between the write buffer and the read buffer.
|
|
||||||
return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
|
|
||||||
// Otherwise the free space is split between the start and end.
|
|
||||||
if (// Could one fit to the end, including the reserve?
|
|
||||||
endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
|
|
||||||
// Could one fit to the end, and the reserve to the start?
|
|
||||||
(endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
|
|
||||||
return true;
|
|
||||||
// Could one fit both to the start?
|
|
||||||
if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
|
|
||||||
// Mark the rest of the buffer as used.
|
|
||||||
memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
|
|
||||||
// and point to the start.
|
|
||||||
// Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
|
|
||||||
if (atomic_update)
|
|
||||||
cli();
|
|
||||||
bufindw = 0;
|
|
||||||
if (atomic_update)
|
|
||||||
sei();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue