9
9
#include <zephyr/sys/spsc_pbuf.h>
10
10
#include <zephyr/random/rand32.h>
11
11
12
+ #define HDR_LEN sizeof(uint32_t)
13
+ #define TLEN (len ) ROUND_UP(HDR_LEN + len, sizeof(uint32_t))
14
+
12
15
/* The buffer size itself would be 199 bytes.
13
16
* 212 - sizeof(struct spsc_pbuf) - 1 = 199.
14
17
* -1 because internal rd/wr_idx is reserved to mean the buffer is empty.
@@ -191,7 +194,7 @@ ZTEST(test_spsc_pbuf, test_0cpy)
191
194
PACKET_WRITE (pb , len1 , len1 , 0 , len1 );
192
195
193
196
/* Remaining space. */
194
- len2 = capacity - ROUND_UP (len1 , sizeof ( uint32_t )) - 2 * sizeof ( uint32_t ) ;
197
+ len2 = capacity - TLEN (len1 ) - HDR_LEN ;
195
198
/* Request exceeding capacity*/
196
199
PACKET_WRITE (pb , len2 + 1 , 0 , 1 , len2 );
197
200
@@ -235,7 +238,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_discard)
235
238
len1 = 14 ;
236
239
PACKET_WRITE (pb , len1 , len1 , 0 , len1 );
237
240
238
- len2 = capacity - len1 - sizeof ( uint32_t ) - 10 ;
241
+ len2 = capacity - TLEN ( len1 ) - 10 ;
239
242
PACKET_WRITE (pb , len2 , len2 , 1 , len2 );
240
243
241
244
/* Consume first packet */
@@ -276,7 +279,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner1)
276
279
len2 = capacity ;
277
280
len2 = spsc_pbuf_alloc (pb , len2 , & buf );
278
281
279
- uint16_t exp_len2 = capacity - ROUND_UP (len1 , sizeof ( uint32_t )) - 2 * sizeof ( uint32_t ) ;
282
+ uint16_t exp_len2 = capacity - TLEN (len1 ) - HDR_LEN ;
280
283
281
284
zassert_equal (len2 , exp_len2 , "got %d, exp: %d" , len2 , exp_len2 );
282
285
@@ -303,11 +306,11 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner2)
303
306
capacity = spsc_pbuf_capacity (pb );
304
307
305
308
/* Commit 5 byte packet. */
306
- len1 = 10 ;
309
+ len1 = 16 ;
307
310
PACKET_WRITE (pb , len1 , len1 , 0 , len1 );
308
311
309
312
/* Attempt to allocate packet that will leave 5 bytes at the end. */
310
- len2 = capacity - ROUND_UP (len1 , sizeof ( uint32_t )) - 2 * sizeof ( uint16_t ) - 5 ;
313
+ len2 = capacity - TLEN (len1 ) - HDR_LEN - 5 ;
311
314
PACKET_WRITE (pb , len2 , len2 , 1 , len2 );
312
315
313
316
/* Free first packet. */
@@ -326,7 +329,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner2)
326
329
/* Get longest available. As now there is only one packet at the beginning
327
330
* that should be remaining space decremented by length fields.
328
331
*/
329
- uint16_t exp_len = capacity - ROUND_UP (len1 , sizeof ( uint32_t )) - 2 * sizeof ( uint32_t ) ;
332
+ uint16_t exp_len = capacity - TLEN (len1 ) - HDR_LEN ;
330
333
331
334
PACKET_WRITE (pb , capacity , 0 , 2 , exp_len );
332
335
}
@@ -389,21 +392,20 @@ ZTEST(test_spsc_pbuf, test_utilization)
389
392
390
393
PACKET_CONSUME (pb , len1 , 0 );
391
394
u = spsc_pbuf_get_utilization (pb );
392
- zassert_equal (u , ROUND_UP (len1 , sizeof ( uint32_t )) + sizeof ( uint32_t ), NULL );
395
+ zassert_equal (u , TLEN (len1 ), NULL );
393
396
394
397
len2 = 11 ;
395
398
PACKET_WRITE (pb , len2 , len2 , 1 , len2 );
396
399
PACKET_CONSUME (pb , len2 , 1 );
397
400
u = spsc_pbuf_get_utilization (pb );
398
- zassert_equal (u , ROUND_UP (len2 , sizeof ( uint32_t )) + sizeof ( uint32_t ), NULL );
401
+ zassert_equal (u , TLEN (len2 ), NULL );
399
402
400
- len3 = capacity - ROUND_UP (len1 , sizeof (uint32_t )) - ROUND_UP (len2 , sizeof (uint32_t ))
401
- - 3 * sizeof (uint32_t ) + sizeof (uint32_t );
403
+ len3 = capacity - TLEN (len1 ) - TLEN (len2 );
402
404
PACKET_WRITE (pb , SPSC_PBUF_MAX_LEN , len3 , 2 , len3 );
403
405
PACKET_CONSUME (pb , len3 , 2 );
404
406
405
407
u = spsc_pbuf_get_utilization (pb );
406
- int exp_u = ROUND_UP (len3 , sizeof ( uint32_t )) + sizeof ( uint32_t );
408
+ int exp_u = TLEN (len3 );
407
409
408
410
zassert_equal (u , exp_u , NULL );
409
411
}
0 commit comments