From 841fd77530238457ae595aa84d6bb49ce6d660be Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 14 Dec 2020 19:42:05 +0500 Subject: [PATCH] Add buffer overrun concept to README --- README.md | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8c6be61..2c6519e 100644 --- a/README.md +++ b/README.md @@ -141,13 +141,13 @@ Consumer pops more: V ``` -Producer pushes long message: +Producer pushes one short and one long message: ``` | ___ ___ ___ ___ ___ ___ ___ ___ _V_ ___ -| | | |14 |15 | | | -|___|___|___|___|___ ___ ___ ___|___|___| +| | | |14 |15 |16 | | | +|___|___|___|___|___ ___ ___|___|___|___| | V ``` @@ -157,10 +157,39 @@ Consumer pops one short and one long message: ``` | ___ ___ ___ ___ ___ ___ ___ ___ _V_ ___ -| | | | | | | | | | | +| | | | | | | |16 | | | |___|___|___|___|___|___|___|___|___|___| - | - V + | + V +``` + +Producer is in trouble now. He wants to send a long message, but he is +positioned at the second half of the buffer. The situation is complicated by the +fact that the size of the message may not be known in advance (stream-like +message queuing). To reduce the likelihood of it stalling due to lack of space, +it chooses whether to write to the current position or to the beginning of the +buffer, depending on where there is more free space at the moment. He compares +the following values: + +* `Size of the buffer - Current position of the producer` +* `Current position of the consumer` + +In our example, the second one is greater (remember that indices start with +zero): + +``` + 10 - 8 = 2 + _A_ + / \ + | | + ___ ___ ___ ___ ___ ___ ___ ___ _V_ _|_ +| | | | | | | |16 | | | +|___|___|___|___|___|___|___|___|___|___| + | | | + | | V + \____________ __________/ + V + 7 - 0 = 7 ```