chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*************************************************************************************************/
/*!
 *  \file   wsf_buf.c
 *
 *  \brief  Buffer pool service.
 *
 *  Copyright (c) 2009-2018 Arm Ltd. All Rights Reserved.
 *
 *  Copyright (c) 2019-2020 Packetcraft, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/*************************************************************************************************/
 
#include "wsf_types.h"
#include "wsf_buf.h"
 
#include "wsf_assert.h"
#include "wsf_cs.h"
#include "wsf_trace.h"
#include "wsf_heap.h"
#include "wsf_math.h"
#include "wsf_os.h"
// #include "mk_trace.h"
 
/**************************************************************************************************
  Macros
**************************************************************************************************/
 
/* Magic number used to check for free buffer. */
#define WSF_BUF_FREE_NUM 0xFAABD00D
 
/**************************************************************************************************
  Data Types
**************************************************************************************************/
 
/* Unit of memory storage-- a structure containing a pointer. */
typedef struct wsfBufMem_tag
{
    struct wsfBufMem_tag *pNext;
#if WSF_BUF_FREE_CHECK_ASSERT == TRUE
    uint32_t free;
#endif
} wsfBufMem_t;
 
/* Internal buffer pool. */
typedef struct
{
    wsfBufPoolDesc_t desc; /* Number of buffers and length. */
    wsfBufMem_t *pStart;   /* Start of pool. */
    wsfBufMem_t *pFree;    /* First free buffer in pool. */
#if WSF_BUF_STATS == TRUE
    uint8_t numAlloc;   /* Number of buffers currently allocated from pool. */
    uint8_t maxAlloc;   /* Maximum buffers ever allocated from pool. */
    uint16_t maxReqLen; /* Maximum request length from pool. */
#endif
} wsfBufPool_t;
 
/**************************************************************************************************
  Global Variables
**************************************************************************************************/
 
/* Number of pools. */
static uint8_t wsfBufNumPools;
 
/* Memory used for pools. */
static wsfBufMem_t *wsfBufMem = NULL;
 
/* Currently use for debugging only. */
static uint32_t wsfBufMemLen;
 
#if WSF_BUF_STATS_HIST == TRUE
/* Buffer allocation counter. */
uint8_t wsfBufAllocCount[WSF_BUF_STATS_MAX_LEN];
 
/* Pool Overflow counter. */
uint8_t wsfPoolOverFlowCount[WSF_BUF_STATS_MAX_POOL];
#endif
 
#if WSF_OS_DIAG == TRUE
/* WSF buffer diagnostic callback function. */
static WsfBufDiagCback_t wsfBufDiagCback = NULL;
#endif
 
/*************************************************************************************************/
/*!
 *  \brief  Calculate size required by the buffer pool.
 *
 *  \param  numPools  Number of buffer pools.
 *  \param  pDesc     Array of buffer pool descriptors, one for each pool.
 *
 *  \return Amount of pBufMem used.
 */
/*************************************************************************************************/
uint32_t WsfBufCalcSize(uint8_t numPools, wsfBufPoolDesc_t *pDesc)
{
    uint32_t len;
    uint32_t descLen;
    wsfBufPool_t *pPool;
    wsfBufMem_t *pStart;
    uint8_t i;
 
    wsfBufMem = (wsfBufMem_t *)0;
    pPool = (wsfBufPool_t *)wsfBufMem;
 
    /* Buffer storage starts after the pool structs. */
    pStart = (wsfBufMem_t *)(pPool + numPools);
 
    /* Create each pool; see loop exit condition below. */
    while (TRUE)
    {
        /* Exit loop after verification check. */
        if (numPools-- == 0)
        {
            break;
        }
 
        /* Adjust pool lengths for minimum size and alignment. */
        if (pDesc->len < sizeof(wsfBufMem_t))
        {
            descLen = sizeof(wsfBufMem_t);
        }
        else if ((pDesc->len % sizeof(wsfBufMem_t)) != 0)
        {
            descLen = pDesc->len + sizeof(wsfBufMem_t) - (pDesc->len % sizeof(wsfBufMem_t));
        }
        else
        {
            descLen = pDesc->len;
        }
 
        len = descLen / sizeof(wsfBufMem_t);
        for (i = pDesc->num; i > 0; i--)
        {
            /* Pointer to the next free buffer is stored in the buffer itself. */
            pStart += len;
        }
        pDesc++;
    }
 
    return (uint32_t)((uint8_t *)pStart - (uint8_t *)wsfBufMem);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Initialize the buffer pool service.  This function should only be called once
 *          upon system initialization.
 *
 *  \param  numPools  Number of buffer pools.
 *  \param  pDesc     Array of buffer pool descriptors, one for each pool.
 *
 *  \return Amount of pBufMem used or 0 for failures.
 */
/*************************************************************************************************/
uint32_t WsfBufInit(uint8_t numPools, wsfBufPoolDesc_t *pDesc)
{
    wsfBufPool_t *pPool;
    wsfBufMem_t *pStart;
    uint16_t len;
    uint8_t i;
 
    wsfBufMem = (wsfBufMem_t *)WsfHeapGetFreeStartAddress();
    pPool = (wsfBufPool_t *)wsfBufMem;
 
    /* Buffer storage starts after the pool structs. */
    pStart = (wsfBufMem_t *)(pPool + numPools);
 
    wsfBufNumPools = numPools;
 
    /* Create each pool; see loop exit condition below. */
    while (TRUE)
    {
        /* Verify we didn't overrun memory; if we did, abort. */
        if (pStart > &wsfBufMem[WsfHeapCountAvailable() / sizeof(wsfBufMem_t)])
        {
            WSF_ASSERT(FALSE);
            return 0;
        }
 
        /* Exit loop after verification check. */
        if (numPools-- == 0)
        {
            break;
        }
 
        /* Adjust pool lengths for minimum size and alignment. */
        if (pDesc->len < sizeof(wsfBufMem_t))
        {
            pPool->desc.len = sizeof(wsfBufMem_t);
        }
        else if ((pDesc->len % sizeof(wsfBufMem_t)) != 0)
        {
            pPool->desc.len = pDesc->len + sizeof(wsfBufMem_t) - (pDesc->len % sizeof(wsfBufMem_t));
        }
        else
        {
            pPool->desc.len = pDesc->len;
        }
 
        pPool->desc.num = pDesc->num;
        pDesc++;
 
        pPool->pStart = pStart;
        pPool->pFree = pStart;
#if WSF_BUF_STATS == TRUE
        pPool->numAlloc = 0;
        pPool->maxAlloc = 0;
        pPool->maxReqLen = 0;
#endif
 
        WSF_TRACE_INFO2("Creating pool len=%u num=%u", pPool->desc.len, pPool->desc.num);
        WSF_TRACE_INFO1("              pStart=0x%x", (uint32_t)pPool->pStart);
 
        /* Initialize free list. */
        len = pPool->desc.len / sizeof(wsfBufMem_t);
        for (i = pPool->desc.num; i > 1; i--)
        {
            /* Verify we didn't overrun memory; if we did, abort. */
            if (pStart > &wsfBufMem[WsfHeapCountAvailable() / sizeof(wsfBufMem_t)])
            {
                WSF_ASSERT(FALSE);
                return 0;
            }
            /* Pointer to the next free buffer is stored in the buffer itself. */
            pStart->pNext = pStart + len;
            pStart += len;
        }
 
        /* Verify we didn't overrun memory; if we did, abort. */
        if (pStart > &wsfBufMem[WsfHeapCountAvailable() / sizeof(wsfBufMem_t)])
        {
            WSF_ASSERT(FALSE);
            return 0;
        }
        /* Last one in list points to NULL. */
        pStart->pNext = NULL;
        pStart += len;
 
        /* Next pool. */
        pPool++;
    }
 
    wsfBufMemLen = (uint32_t)((uint8_t *)pStart - (uint8_t *)wsfBufMem);
    WSF_TRACE_INFO1("Created buffer pools; using %u bytes", wsfBufMemLen);
 
    return wsfBufMemLen;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Allocate a buffer.
 *
 *  \param  len     Length of buffer to allocate.
 *
 *  \return Pointer to allocated buffer or NULL if allocation fails.
 */
/*************************************************************************************************/
void *WsfBufAlloc(uint16_t len)
{
    wsfBufPool_t *pPool;
    wsfBufMem_t *pBuf;
    uint8_t i;
 
    WSF_CS_INIT(cs);
 
    WSF_ASSERT(len > 0);
 
    pPool = (wsfBufPool_t *)wsfBufMem;
 
    for (i = wsfBufNumPools; i > 0; i--, pPool++)
    {
        /* Check if buffer is big enough. */
        if (len <= pPool->desc.len)
        {
            /* Enter critical section. */
            uint32_t lock = WSF_CS_ENTER();
 
            /* Check if buffers are available. */
            if (pPool->pFree != NULL)
            {
                /* Allocation succeeded. */
                pBuf = pPool->pFree;
 
                /* Next free buffer is stored inside current free buffer. */
                pPool->pFree = pBuf->pNext;
 
#if WSF_BUF_FREE_CHECK_ASSERT == TRUE
                pBuf->free = 0;
#endif
#if WSF_BUF_STATS_HIST == TRUE
                /* Increment count for buffers of this length. */
                if (len < WSF_BUF_STATS_MAX_LEN)
                {
                    wsfBufAllocCount[len]++;
                }
                else
                {
                    wsfBufAllocCount[0]++;
                }
#endif
#if WSF_BUF_STATS == TRUE
                if (++pPool->numAlloc > pPool->maxAlloc)
                {
                    pPool->maxAlloc = pPool->numAlloc;
                }
                pPool->maxReqLen = WSF_MAX(pPool->maxReqLen, len);
#endif
                /* Exit critical section. */
                WSF_CS_EXIT(lock);
 
                WSF_TRACE_ALLOC2("WsfBufAlloc len:%u pBuf:%08x", pPool->desc.len, pBuf);
                // LOG_INFO(TRACE_MODULE_OS, "WsfBufAlloc  %u %u %08x\r\n", i, len, (uint32_t)pBuf);
 
                return pBuf;
            }
#if WSF_BUF_STATS_HIST == TRUE
            else
            {
                /* Pool overflow: increment count of overflow for current pool. */
                wsfPoolOverFlowCount[wsfBufNumPools - i]++;
            }
#endif
            /* Exit critical section. */
            WSF_CS_EXIT(lock);
 
#if WSF_BUF_ALLOC_BEST_FIT_FAIL_ASSERT == TRUE
            WSF_ASSERT(FALSE);
#endif
        }
    }
 
    /* Allocation failed. */
#if WSF_OS_DIAG == TRUE
    if (wsfBufDiagCback != NULL)
    {
        WsfBufDiag_t info;
 
        info.type = WSF_BUF_ALLOC_FAILED;
        info.param.alloc.taskId = WSF_OS_GET_ACTIVE_HANDLER_ID();
        info.param.alloc.len = len;
 
        wsfBufDiagCback(&info);
    }
    else
    {
        WSF_TRACE_ALLOC2("WsfBufAlloc failed len:%u - task:%u", len, WSF_OS_GET_ACTIVE_HANDLER_ID());
    }
#else
    WSF_TRACE_ALLOC1("WsfBufAlloc failed len:%u", len);
#endif
 
#if WSF_BUF_ALLOC_FAIL_ASSERT == TRUE
    WSF_ASSERT(FALSE);
#endif
 
    return NULL;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Free a buffer.
 *
 *  \param  pBuf    Buffer to free.
 */
/*************************************************************************************************/
void WsfBufFree(void *pBuf)
{
    wsfBufPool_t *pPool;
    wsfBufMem_t *p = pBuf;
 
    WSF_CS_INIT(cs);
 
    /* Verify pointer is within range. */
#if WSF_BUF_FREE_CHECK_ASSERT == TRUE
    WSF_ASSERT(p >= ((wsfBufPool_t *)wsfBufMem)->pStart);
    WSF_ASSERT(p < (wsfBufMem_t *)(((uint8_t *)wsfBufMem) + wsfBufMemLen));
#endif
 
    /* Iterate over pools starting from last pool. */
    pPool = (wsfBufPool_t *)wsfBufMem + (wsfBufNumPools - 1);
    while (pPool >= (wsfBufPool_t *)wsfBufMem)
    {
        /* Check if the buffer memory is located inside this pool. */
        if (p >= pPool->pStart)
        {
            /* Enter critical section. */
            uint32_t lock = WSF_CS_ENTER();
 
#if WSF_BUF_FREE_CHECK_ASSERT == TRUE
            WSF_ASSERT(p->free != WSF_BUF_FREE_NUM);
            p->free = WSF_BUF_FREE_NUM;
#endif
#if WSF_BUF_STATS == TRUE
            pPool->numAlloc--;
#endif
 
            /* Pool found; put buffer back in free list. */
            p->pNext = pPool->pFree;
            pPool->pFree = p;
 
            /* Exit critical section. */
            WSF_CS_EXIT(lock);
 
            WSF_TRACE_FREE2("WsfBufFree len:%u pBuf:%08x", pPool->desc.len, pBuf);
 
            return;
        }
 
        /* Next pool. */
        pPool--;
    }
 
    /* Should never get here. */
    WSF_ASSERT(FALSE);
 
    return;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Diagnostic function to get the buffer allocation statistics.
 *
 *  \return Buffer allocation statistics array.
 */
/*************************************************************************************************/
uint8_t *WsfBufGetAllocStats(void)
{
#if WSF_BUF_STATS_HIST == TRUE
    return wsfBufAllocCount;
#else
    return NULL;
#endif
}
 
/*************************************************************************************************/
/*!
 *  \brief  Diagnostic function to get the number of overflow times for each pool.
 *
 *  \return Overflow times statistics array
 */
/*************************************************************************************************/
uint8_t *WsfBufGetPoolOverFlowStats(void)
{
#if WSF_BUF_STATS_HIST == TRUE
    return wsfPoolOverFlowCount;
#else
    return NULL;
#endif
}
 
/*************************************************************************************************/
/*!
 *  \brief  Get number of pools.
 *
 *  \return Number of pools.
 */
/*************************************************************************************************/
uint8_t WsfBufGetNumPool(void)
{
    return wsfBufNumPools;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Get statistics for each pool.
 *
 *  \param  pBuf    Buffer to store the statistics.
 *  \param  poolId  Pool ID.
 */
/*************************************************************************************************/
void WsfBufGetPoolStats(WsfBufPoolStat_t *pStat, uint8_t poolId)
{
    wsfBufPool_t *pPool;
 
    if (poolId >= wsfBufNumPools)
    {
        pStat->bufSize = 0;
        return;
    }
 
    WSF_CS_INIT(cs);
    uint32_t lock = WSF_CS_ENTER();
 
    pPool = (wsfBufPool_t *)wsfBufMem;
 
    pStat->bufSize = pPool[poolId].desc.len;
    pStat->numBuf = pPool[poolId].desc.num;
#if WSF_BUF_STATS == TRUE
    pStat->numAlloc = pPool[poolId].numAlloc;
    pStat->maxAlloc = pPool[poolId].maxAlloc;
    pStat->maxReqLen = pPool[poolId].maxReqLen;
#else
    pStat->numAlloc = 0;
    pStat->maxAlloc = 0;
    pStat->maxReqLen = 0;
#endif
 
    /* Exit critical section. */
    WSF_CS_EXIT(lock);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Called to register the buffer diagnostics callback function.
 *
 *  \param  pCallback   Pointer to the callback function.
 */
/*************************************************************************************************/
void WsfBufDiagRegister(WsfBufDiagCback_t callback)
{
#if WSF_OS_DIAG == TRUE
    wsfBufDiagCback = callback;
#else
    /* Unused parameter */
    (void)callback;
#endif
}