Skip to content

Commit

Permalink
lib: os: spsc_pbuf: Minor code cleanup
Browse files Browse the repository at this point in the history
Minor cleanup in allocation function. Using define instead of
sizeof(uint32_t) to better explain the purpose. Adding few
comments.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
  • Loading branch information
nordic-krch authored and fabiobaltieri committed Aug 24, 2022
1 parent 716fa44 commit 7303ac1
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions lib/os/spsc_pbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <zephyr/sys/byteorder.h>

#define LEN_SZ sizeof(uint32_t)
/* Amount of data that is left unused to distinguish between empty and full. */
#define FREE_SPACE_DISTANCE sizeof(uint32_t)

#define PADDING_MARK 0xFF

#define GET_UTILIZATION(flags) \
Expand Down Expand Up @@ -165,26 +168,28 @@ int spsc_pbuf_alloc(struct spsc_pbuf *pb, uint16_t len, char **buf)
space = LEN_SZ + 1;
}

if (remaining >= space) {
/* Packet will fit at the end */
free_space = remaining - ((rd_idx > 0) ? 0 : sizeof(uint32_t));
if ((remaining >= space) || (rd_idx <= space)) {
/* Packet will fit at the end. Free space depends on
* presence of data at the beginning of the buffer since
* there must be one word not used to distinguish between
* empty and full state.
*/
free_space = remaining - ((rd_idx > 0) ? 0 : FREE_SPACE_DISTANCE);
} else {
if (rd_idx > space) {
/* Padding must be added. */
data_loc[wr_idx] = PADDING_MARK;
__sync_synchronize();
cache_wb(&data_loc[wr_idx], sizeof(uint8_t), flags);

wr_idx = 0;
*wr_idx_loc = wr_idx;

free_space = rd_idx - sizeof(uint32_t);
} else {
free_space = remaining - (rd_idx > 0 ? 0 : sizeof(uint32_t));
}
/* Padding must be added. */
data_loc[wr_idx] = PADDING_MARK;
__sync_synchronize();
cache_wb(&data_loc[wr_idx], sizeof(uint8_t), flags);

wr_idx = 0;
*wr_idx_loc = wr_idx;

/* Obligatory one word empty space. */
free_space = rd_idx - FREE_SPACE_DISTANCE;
}
} else {
free_space = rd_idx - wr_idx - sizeof(uint32_t);
/* Obligatory one word empty space. */
free_space = rd_idx - wr_idx - FREE_SPACE_DISTANCE;
}

len = MIN(len, MAX(free_space - (int32_t)LEN_SZ, 0));
Expand Down

0 comments on commit 7303ac1

Please sign in to comment.