@@ -42,6 +42,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
42
42
#define WARN_ALLOC_INTERVAL K_FOREVER
43
43
#endif
44
44
45
+ #define GET_ALIGN (pool ) MAX(sizeof(void *), pool->alloc->alignment)
46
+
45
47
/* Linker-defined symbol bound to the static pool structs */
46
48
STRUCT_SECTION_START_EXTERN (net_buf_pool );
47
49
@@ -95,9 +97,10 @@ void net_buf_reset(struct net_buf *buf)
95
97
96
98
static uint8_t * generic_data_ref (struct net_buf * buf , uint8_t * data )
97
99
{
100
+ struct net_buf_pool * buf_pool = net_buf_pool_get (buf -> pool_id );
98
101
uint8_t * ref_count ;
99
102
100
- ref_count = data - sizeof ( void * );
103
+ ref_count = data - GET_ALIGN ( buf_pool );
101
104
(* ref_count )++ ;
102
105
103
106
return data ;
@@ -109,9 +112,26 @@ static uint8_t *mem_pool_data_alloc(struct net_buf *buf, size_t *size,
109
112
struct net_buf_pool * buf_pool = net_buf_pool_get (buf -> pool_id );
110
113
struct k_heap * pool = buf_pool -> alloc -> alloc_data ;
111
114
uint8_t * ref_count ;
115
+ void * b ;
116
+
117
+ if (buf_pool -> alloc -> alignment == 0 ) {
118
+ /* Reserve extra space for a ref-count (uint8_t) */
119
+ b = k_heap_alloc (pool , sizeof (void * ) + * size , timeout );
120
+
121
+ } else {
122
+ if (* size < buf_pool -> alloc -> alignment ) {
123
+ NET_BUF_DBG ("Requested size %zu is smaller than alignment %zu" ,
124
+ * size , buf_pool -> alloc -> alignment );
125
+ return NULL ;
126
+ }
112
127
113
- /* Reserve extra space for a ref-count (uint8_t) */
114
- void * b = k_heap_alloc (pool , sizeof (void * ) + * size , timeout );
128
+ /* Reserve extra space for a ref-count (uint8_t) */
129
+ b = k_heap_aligned_alloc (pool ,
130
+ buf_pool -> alloc -> alignment ,
131
+ GET_ALIGN (buf_pool ) +
132
+ ROUND_UP (* size , buf_pool -> alloc -> alignment ),
133
+ timeout );
134
+ }
115
135
116
136
if (b == NULL ) {
117
137
return NULL ;
@@ -121,7 +141,7 @@ static uint8_t *mem_pool_data_alloc(struct net_buf *buf, size_t *size,
121
141
* ref_count = 1U ;
122
142
123
143
/* Return pointer to the byte following the ref count */
124
- return ref_count + sizeof ( void * );
144
+ return ref_count + GET_ALIGN ( buf_pool );
125
145
}
126
146
127
147
static void mem_pool_data_unref (struct net_buf * buf , uint8_t * data )
@@ -130,7 +150,7 @@ static void mem_pool_data_unref(struct net_buf *buf, uint8_t *data)
130
150
struct k_heap * pool = buf_pool -> alloc -> alloc_data ;
131
151
uint8_t * ref_count ;
132
152
133
- ref_count = data - sizeof ( void * );
153
+ ref_count = data - GET_ALIGN ( buf_pool );
134
154
if (-- (* ref_count )) {
135
155
return ;
136
156
}
@@ -171,23 +191,25 @@ const struct net_buf_data_cb net_buf_fixed_cb = {
171
191
static uint8_t * heap_data_alloc (struct net_buf * buf , size_t * size ,
172
192
k_timeout_t timeout )
173
193
{
194
+ struct net_buf_pool * buf_pool = net_buf_pool_get (buf -> pool_id );
174
195
uint8_t * ref_count ;
175
196
176
- ref_count = k_malloc (sizeof ( void * ) + * size );
197
+ ref_count = k_malloc (GET_ALIGN ( buf_pool ) + * size );
177
198
if (!ref_count ) {
178
199
return NULL ;
179
200
}
180
201
181
202
* ref_count = 1U ;
182
203
183
- return ref_count + sizeof ( void * );
204
+ return ref_count + GET_ALIGN ( buf_pool );
184
205
}
185
206
186
207
static void heap_data_unref (struct net_buf * buf , uint8_t * data )
187
208
{
209
+ struct net_buf_pool * buf_pool = net_buf_pool_get (buf -> pool_id );
188
210
uint8_t * ref_count ;
189
211
190
- ref_count = data - sizeof ( void * );
212
+ ref_count = data - GET_ALIGN ( buf_pool );
191
213
if (-- (* ref_count )) {
192
214
return ;
193
215
}
0 commit comments