@@ -67,11 +67,29 @@ static int destroy_called;
67
67
static void buf_destroy (struct net_buf * buf );
68
68
static void fixed_destroy (struct net_buf * buf );
69
69
static void var_destroy (struct net_buf * buf );
70
+ static void var_destroy_aligned (struct net_buf * buf );
71
+ static void var_destroy_aligned_small (struct net_buf * buf );
72
+
73
+ #define VAR_POOL_ALIGN 8
74
+ #define VAR_POOL_ALIGN_SMALL 4
75
+ #define VAR_POOL_DATA_COUNT 4
76
+ #define VAR_POOL_DATA_SIZE (VAR_POOL_DATA_COUNT * 64)
70
77
71
78
NET_BUF_POOL_HEAP_DEFINE (bufs_pool , 10 , USER_DATA_HEAP , buf_destroy );
72
79
NET_BUF_POOL_FIXED_DEFINE (fixed_pool , 10 , FIXED_BUFFER_SIZE , USER_DATA_FIXED , fixed_destroy );
73
80
NET_BUF_POOL_VAR_DEFINE (var_pool , 10 , 1024 , USER_DATA_VAR , var_destroy );
74
81
82
+ /* Two pools, one with aligned to 8 bytes and one with aligned to 4 bytes
83
+ * buffers. The aligned pools are used to test that the alignment works
84
+ * correctly.
85
+ */
86
+ NET_BUF_POOL_VAR_ALIGN_DEFINE (var_pool_aligned , VAR_POOL_DATA_COUNT ,
87
+ VAR_POOL_DATA_SIZE , USER_DATA_VAR ,
88
+ var_destroy_aligned , VAR_POOL_ALIGN );
89
+ NET_BUF_POOL_VAR_ALIGN_DEFINE (var_pool_aligned_small , VAR_POOL_DATA_COUNT ,
90
+ VAR_POOL_DATA_SIZE , USER_DATA_VAR ,
91
+ var_destroy_aligned_small , VAR_POOL_ALIGN_SMALL );
92
+
75
93
static void buf_destroy (struct net_buf * buf )
76
94
{
77
95
struct net_buf_pool * pool = net_buf_pool_get (buf -> pool_id );
@@ -99,6 +117,24 @@ static void var_destroy(struct net_buf *buf)
99
117
net_buf_destroy (buf );
100
118
}
101
119
120
+ static void var_destroy_aligned (struct net_buf * buf )
121
+ {
122
+ struct net_buf_pool * pool = net_buf_pool_get (buf -> pool_id );
123
+
124
+ destroy_called ++ ;
125
+ zassert_equal (pool , & var_pool_aligned , "Invalid free pointer in buffer" );
126
+ net_buf_destroy (buf );
127
+ }
128
+
129
+ static void var_destroy_aligned_small (struct net_buf * buf )
130
+ {
131
+ struct net_buf_pool * pool = net_buf_pool_get (buf -> pool_id );
132
+
133
+ destroy_called ++ ;
134
+ zassert_equal (pool , & var_pool_aligned_small , "Invalid free pointer in buffer" );
135
+ net_buf_destroy (buf );
136
+ }
137
+
102
138
static const char example_data [] = "0123456789"
103
139
"abcdefghijklmnopqrstuvxyz"
104
140
"!#¤%&/()=?" ;
@@ -1084,4 +1120,43 @@ ZTEST(net_buf_tests, test_net_buf_linearize)
1084
1120
zassert_equal (destroy_called , 4 , "Incorrect destroy callback count" );
1085
1121
}
1086
1122
1123
+ ZTEST (net_buf_tests , test_net_buf_var_pool_aligned )
1124
+ {
1125
+ struct net_buf * buf1 , * buf2 , * buf3 ;
1126
+
1127
+ destroy_called = 0 ;
1128
+
1129
+ zassert_equal (var_pool_aligned .alloc -> alignment , VAR_POOL_ALIGN ,
1130
+ "Expected %d-byte alignment for variable pool" ,
1131
+ VAR_POOL_ALIGN );
1132
+
1133
+ buf1 = net_buf_alloc_len (& var_pool_aligned , 20 , K_NO_WAIT );
1134
+ zassert_not_null (buf1 , "Failed to get buffer" );
1135
+
1136
+ zassert_true (IS_ALIGNED ((uintptr_t )buf1 -> data , VAR_POOL_ALIGN ),
1137
+ "Buffer data pointer is not aligned to %d bytes" ,
1138
+ VAR_POOL_ALIGN );
1139
+
1140
+ buf2 = net_buf_alloc_len (& var_pool_aligned_small , 29 , K_NO_WAIT );
1141
+ zassert_not_null (buf2 , "Failed to get buffer" );
1142
+
1143
+ zassert_true (IS_ALIGNED ((uintptr_t )buf2 -> data , VAR_POOL_ALIGN_SMALL ),
1144
+ "Buffer data pointer is not aligned to %d bytes" ,
1145
+ VAR_POOL_ALIGN_SMALL );
1146
+
1147
+ buf3 = net_buf_alloc_len (& var_pool_aligned , VAR_POOL_ALIGN_SMALL , K_NO_WAIT );
1148
+ zassert_is_null (buf3 ,
1149
+ "Managed to get buffer even if alignment %d is larger than size %d" ,
1150
+ VAR_POOL_ALIGN , VAR_POOL_ALIGN_SMALL );
1151
+
1152
+ buf3 = net_buf_alloc_len (& var_pool_aligned , VAR_POOL_ALIGN , K_NO_WAIT );
1153
+ zassert_not_null (buf3 , "Failed to get buffer" );
1154
+
1155
+ net_buf_unref (buf1 );
1156
+ net_buf_unref (buf2 );
1157
+ net_buf_unref (buf3 );
1158
+
1159
+ zassert_equal (destroy_called , 3 , "Incorrect destroy callback count" );
1160
+ }
1161
+
1087
1162
ZTEST_SUITE (net_buf_tests , NULL , NULL , NULL , NULL , NULL );
0 commit comments