source: svn/trunk/newcon3bcm2_21bu/magnum/basemodules/mem/test/bmem_test.c

Last change on this file was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 11.4 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2009, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bmem_test.c $
11 * $brcm_Revision: Hydra_Software_Devel/11 $
12 * $brcm_Date: 5/26/09 4:24p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/basemodules/mem/test/bmem_test.c $
19 *
20 * Hydra_Software_Devel/11   5/26/09 4:24p albertl
21 * PR55389: Replaced uintptr_t with uint32_t.
22 *
23 * Hydra_Software_Devel/10   4/27/05 6:04p jasonh
24 * PR 15059: Changed debug application to remove newlines and add some
25 * more information.
26 *
27 * Hydra_Software_Devel/9   5/28/04 11:37a hongtaoz
28 * PR10059: added alignment test; took heap error counts into test result
29 * judgement;
30 *
31 * Hydra_Software_Devel/8   5/27/04 7:38p hongtaoz
32 * PR10059: added more debug messages; enhanced with random free/alloc
33 * loops to test complex scenarioes.
34 *
35 * Hydra_Software_Devel/7   5/26/04 6:59p hongtaoz
36 * PR10059: sorted the freed heap in size ascending order; fixed compile
37 * error for bmem test; dump heap map before destroying the heap for bmem
38 * test;
39 *
40 * Hydra_Software_Devel/6   12/31/03 12:02p jasonh
41 * PR 8940: Changed return type of destroyheap to void. Removed use of
42 * DEBUG macro and printf.
43 *
44 * Hydra_Software_Devel/5   11/11/03 11:19a jasonh
45 * PR 8589: removed leaked resource check and added offset to sample heap.
46 *
47 * Hydra_Software_Devel/4   9/15/03 5:25p jasonh
48 * Re-wrote test from scratch.
49 *
50 * Hydra_Software_Devel/3   3/21/03 6:03p erickson
51 * added printInfo for debug
52 *
53 * Hydra_Software_Devel/2   3/20/03 3:51p erickson
54 * renamed all MEM_ to BMEM_
55 *
56 * Hydra_Software_Devel/1   3/20/03 3:24p erickson
57 * initial bmem work, taken from SetTop/memorylib
58 *
59 ***************************************************************************/
60#include "bstd.h"
61#include "bkni.h"
62#include "bkni_multi.h"
63#include "bdbg.h"
64
65BDBG_MODULE(memorymanagertest);
66
67#include "bmem.h"
68#include "bmem_debug.h"
69#include "bmem_config.h"
70#include "bmem_test.h"
71
72static uint8_t  s_aBytes[1024 * 1024];
73
74static BERR_Code BMEM_Test_CheckAlloc(
75        BMEM_Handle   heap,
76        unsigned int  uiHeapAlignment,
77        void         *pAddr,
78        size_t        ulAddrSize,
79        unsigned int  uiAddrAlignment,
80        unsigned int  uiAddrBoundary
81        )
82{
83        BERR_Code  err = BERR_SUCCESS;
84        uint32_t   ulAddr = (uint32_t)pAddr;
85        uint32_t   ulConvertedOffset;
86        void      *pConvertedAddr;
87
88        /* fill allocated block with non-guardbanding value */
89        BKNI_Memset(pAddr, 0xFF, ulAddrSize);
90
91        /* doesn't match heap alignment? */
92        if ((((UINT32_C(1) << uiHeapAlignment) - 1) & ulAddr) != 0)
93        {
94                /* failed to match heap alignment */
95                err = BERR_TRACE(BERR_UNKNOWN);
96                BDBG_ERR(("BMEM_Test_CheckAlloc: Failed heap alignment"));
97                goto done;
98        }
99
100        /* doesn't match address alignment? */
101        if ((((UINT32_C(1) << uiAddrAlignment) - 1) & ulAddr) != 0)
102        {
103                /* failed to match address alignment */
104                err = BERR_TRACE(BERR_UNKNOWN);
105                BDBG_ERR(("BMEM_Test_CheckAlloc: Failed address alignment"));
106                goto done;
107        }
108
109        /* check boundary */
110
111        /* convert address -> offset */
112        err = BERR_TRACE(BMEM_ConvertAddressToOffset(heap, pAddr, 
113                &ulConvertedOffset));
114
115        if (err != BERR_SUCCESS)
116        {
117                /* unable to convert address -> offset */
118                BDBG_ERR(("BMEM_Test_CheckAlloc: Failed address -> offset conversion"));
119                goto done;
120        }
121
122        /* convert offset -> address */
123        err = BERR_TRACE(BMEM_ConvertOffsetToAddress(heap, ulConvertedOffset,
124                &pConvertedAddr));
125        if (err != BERR_SUCCESS)
126        {
127                /* unable to convert address -> offset */
128                BDBG_ERR(("BMEM_Test_CheckAlloc: Failed offset -> address conversion"));
129                goto done;
130        }
131
132        /* insure that addresses are the same */
133        if (pAddr != pConvertedAddr)
134        {
135                /* addresses did not match */
136                err = BERR_TRACE(BERR_UNKNOWN);
137                BDBG_ERR(("BMEM_Test_CheckAlloc: Failed address match"));
138                goto done;
139        }
140
141        /* validate heap */
142        err = BERR_TRACE(BMEM_ValidateHeap(heap));
143        if (err != BERR_SUCCESS)
144        {
145                /* invalid heap */
146                BDBG_ERR(("BMEM_Test_CheckAlloc: Invalid heap"));
147                goto done;
148        }
149
150done:
151        /* return status */
152        return err;
153}
154
155void BMEM_Test_DisplayConfig(
156        void )
157{
158        /* header */
159        BKNI_Printf("BMEM compile-time configuration:\n\n");
160
161        /* safety config */
162        BKNI_Printf("BMEM_SAFETY_CONFIG      = ");
163#if (BMEM_SAFETY_CONFIG == BMEM_CONFIG_FASTEST)
164        BKNI_Printf("BMEM_CONFIG_FASTEST\n");
165#elif (BMEM_SAFETY_CONFIG == BMEM_CONFIG_NORMAL)
166        BKNI_Printf("BMEM_CONFIG_NORMAL\n");
167#elif (BMEM_SAFETY_CONFIG == BMEM_CONFIG_TRACK)
168        BKNI_Printf("BMEM_CONFIG_TRACK\n");
169#elif (BMEM_SAFETY_CONFIG == BMEM_CONFIG_SAFE)
170        BKNI_Printf("BMEM_CONFIG_SAFE\n");
171#elif (BMEM_SAFETY_CONFIG == BMEM_CONFIG_SAFEST)
172        BKNI_Printf("BMEM_CONFIG_SAFEST\n");
173#else
174        BKNI_Printf("***UNKNOWN***\n");
175#endif
176
177        /* bookkeeping config */
178        BKNI_Printf("BMEM_BOOKKEEPING_CONFIG = ");
179#if (BMEM_BOOKKEEPING_CONFIG == BMEM_BOOKKEEPING_LOCAL)
180        BKNI_Printf("BMEM_BOOKKEEPING_LOCAL\n");
181#elif (BMEM_BOOKKEEPING_CONFIG == BMEM_BOOKKEEPING_SYSTEM)
182        BKNI_Printf("BMEM_BOOKKEEPING_SYSTEM\n");
183#else
184        BKNI_Printf("***UNKNOWN***\n");
185#endif
186
187        /* reentrant config */
188        BKNI_Printf("BMEM_REENTRANT          = ");
189#if (BMEM_REENTRANT_CONFIG == BMEM_REENTRANT)
190        BKNI_Printf("BMEM_REENTRANT\n");
191#elif (BMEM_REENTRANT_CONFIG == BMEM_NOT_REENTRANT)
192        BKNI_Printf("BMEM_NOT_REENTRANT\n");
193#else
194        BKNI_Printf("***UNKNOWN***\n");
195#endif
196
197        /* test header */
198        BKNI_Printf("------------------------------------------------\n");
199}
200
201BERR_Code BMEM_Test_DoTest1(
202        void )
203{
204        BERR_Code     err = BERR_SUCCESS;
205        BMEM_Handle   heap;
206        void         *apvAlloced[1024 * 128];
207        unsigned int  uiNumAlloc = 0;
208        size_t        zSize;
209        unsigned int  i;
210        uint32_t      ulConvertOffset;
211        uint32_t      ulErrCnt;
212
213        /* set global debug level */
214        BDBG_SetLevel(BDBG_eWrn);
215
216#ifdef BMEM_TEST_DEBUG
217        BDBG_SetModuleLevel("memorymanagertest", BDBG_eMsg);
218        BDBG_SetModuleLevel("BMEM", BDBG_eMsg);
219#else
220        BDBG_SetModuleLevel("BMEM", BDBG_eWrn);
221#endif 
222
223        /* Create heap */
224        BKNI_Printf("Allocate 1024x1024 bytes at offset 0x2000000 as the heap.\n");
225        err = BERR_TRACE(BMEM_CreateHeap(&heap, (void *)s_aBytes,
226                0x2000000, 1024*1024, 0));
227        if (err != BERR_SUCCESS)
228        {
229                /* error creating heap */
230                BDBG_ERR(("BMEM_Test_DoTest1: Failed heap create"));
231                goto done;
232        }
233
234        do {
235                /* allocate various sizes until failure */
236                while (uiNumAlloc + 3 < 1024 * 128)
237                {
238                        /* allocate tiny */
239                        apvAlloced[uiNumAlloc] = BMEM_Alloc(heap, 4);
240                        if (!apvAlloced[uiNumAlloc])
241                        {
242                                /* unable to allocate any more of this size */
243                                break;
244                        }
245
246                        /* check tiny */
247                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
248                                apvAlloced[uiNumAlloc], 4, 0, 0));
249                        if (err != BERR_SUCCESS)
250                        {
251                                /* error */
252                                goto done;
253                        }
254                        BDBG_MSG(("---- tiny alloc(4-byte) #%d.", uiNumAlloc));
255                        uiNumAlloc++;
256
257                        /* allocate small */
258                        apvAlloced[uiNumAlloc] = BMEM_AllocAligned(heap, 64, 2, 0);
259                        if (!apvAlloced[uiNumAlloc])
260                        {
261                                /* unable to allocate any more of this size */
262                                break;
263                        }
264
265                        /* check small */
266                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
267                                apvAlloced[uiNumAlloc], 64, 2, 0));
268                        if (err != BERR_SUCCESS)
269                        {
270                                /* error */
271                                goto done;
272                        }
273                        BDBG_MSG(("---- small alloc(64-byte) #%d.", uiNumAlloc));
274                        uiNumAlloc++;
275
276                        /* allocate medium */
277                        apvAlloced[uiNumAlloc] = BMEM_AllocAligned(heap, 1024, 4, 0);
278                        if (!apvAlloced[uiNumAlloc])
279                        {
280                                /* unable to allocate any more of this size */
281                                break;
282                        }
283
284                        /* check medium */
285                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
286                                apvAlloced[uiNumAlloc], 1024, 4, 0));
287                        if (err != BERR_SUCCESS)
288                        {
289                                /* error */
290                                goto done;
291                        }
292                        BDBG_MSG(("---- medium alloc(1024-byte) #%d.", uiNumAlloc));
293                        uiNumAlloc++;
294
295                        /* allocate large */
296                        apvAlloced[uiNumAlloc] = BMEM_AllocAligned(heap, 16 * 1024, 5, 0);
297                        if (!apvAlloced[uiNumAlloc])
298                        {
299                                /* unable to allocate any more of this size */
300                                break;
301                        }
302
303                        /* check large */
304                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
305                                apvAlloced[uiNumAlloc], 16 * 1024, 5, 0));
306                        if (err != BERR_SUCCESS)
307                        {
308                                /* error */
309                                goto done;
310                        }
311                        BDBG_MSG(("---- large alloc(16kbyte) #%d.", uiNumAlloc));
312                        uiNumAlloc++;
313
314                        /* allocate huge */
315                        apvAlloced[uiNumAlloc] = BMEM_AllocAligned(heap, 64 * 1024, 8, 0);
316                        if (!apvAlloced[uiNumAlloc])
317                        {
318                                /* unable to allocate any more of this size */
319                                break;
320                        }
321
322                        /* check huge */
323                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
324                                apvAlloced[uiNumAlloc], 64 * 1024, 8, 0));
325                        if (err != BERR_SUCCESS)
326                        {
327                                /* error */
328                                goto done;
329                        }
330                        BDBG_MSG(("---- huge alloc(64kbyte) #%d.", uiNumAlloc));
331                        uiNumAlloc++;
332
333                        BMEM_ConvertAddressToOffset(heap, apvAlloced[uiNumAlloc - 4], &ulConvertOffset);
334                        BDBG_MSG(("==== free #%d at 0x%x.", uiNumAlloc-4, ulConvertOffset));
335                        BMEM_Free(heap, apvAlloced[uiNumAlloc - 4]);
336                        apvAlloced[uiNumAlloc - 4] = NULL;
337
338                        BMEM_ConvertAddressToOffset(heap, apvAlloced[uiNumAlloc - 2], &ulConvertOffset);
339                        BDBG_MSG(("==== free #%d at 0x%x.\n", uiNumAlloc-2, ulConvertOffset));
340                        BMEM_Free(heap, apvAlloced[uiNumAlloc - 2]);
341                        apvAlloced[uiNumAlloc - 2] = NULL;
342                }
343
344                /* allocate remaining slots */
345                while (uiNumAlloc < 1024 * 128)
346                {
347                        /* get largest available block */
348                        zSize = BMEM_GetLargestAvailableBlockSize(heap);
349
350                        /* no blocks to allocate? */
351                        if (zSize == 0)
352                        {
353                                /* no more blocks to allocate */
354                                BDBG_MSG(("No more blocks to allocate"));
355                                break;
356                        }
357
358                        /* allocate block */
359                        apvAlloced[uiNumAlloc] = BMEM_Alloc(heap, zSize);
360                        if (!apvAlloced[uiNumAlloc])
361                        {
362                                /* unable to allocate (should have) */
363                                err = BERR_TRACE(BERR_UNKNOWN);
364                                BDBG_ERR(("BMEM_Test_DoTest1: Failed largest block alloc"));
365                                goto done;
366                        }
367
368                        /* check allocation */
369                        err = BERR_TRACE(BMEM_Test_CheckAlloc(heap, 0,
370                                apvAlloced[uiNumAlloc], zSize, 0, 0));
371                        if (err != BERR_SUCCESS)
372                        {
373                                /* error */
374                                goto done;
375                        }
376                        BDBG_MSG(("---- remaining block alloc (%d-byte) #%d.", zSize, uiNumAlloc));
377                        uiNumAlloc++;
378                }
379               
380                /* free random allocations */
381                for (i=0; i<uiNumAlloc; ++i)
382                {
383                        /* free random allocation */
384                        if((rand() % 2) && apvAlloced[i])
385                        {
386                                BMEM_ConvertAddressToOffset(heap, apvAlloced[i], &ulConvertOffset);
387                                BDBG_MSG(("==== free #%d at 0x%x.", i, ulConvertOffset));
388                                BMEM_Free(heap, apvAlloced[i]);
389                                apvAlloced[i] = NULL;
390                        }
391                }
392
393                BMEM_ValidateHeap(heap);
394                BMEM_Dbg_DumpHeap(heap);
395                BKNI_Printf("\nNumber of heap errors: %d.\n", BMEM_Dbg_GetErrorCount(heap));
396
397                BKNI_Printf("Press any key to continue; press'q' to exit.\n");
398        }while((getchar() != 'q') && (uiNumAlloc < 1024*128));
399
400        /* free all allocations */
401        for (i=0; i<uiNumAlloc; ++i)
402        {
403                /* free allocation */
404                if(apvAlloced[i])
405                {
406                        BMEM_ConvertAddressToOffset(heap, apvAlloced[i], &ulConvertOffset);
407                        BDBG_MSG(("==== free #%d at 0x%x.", i, ulConvertOffset));
408                        BMEM_Free(heap, apvAlloced[i]);
409                }
410        }
411        /* dump heap map */
412        BMEM_ValidateHeap(heap);
413        BDBG_SetModuleLevel("BMEM", BDBG_eMsg);
414        BMEM_Dbg_DumpHeap(heap);
415        BDBG_SetModuleLevel("BMEM", BDBG_eWrn);
416        ulErrCnt = BMEM_Dbg_GetErrorCount(heap);
417        BKNI_Printf("Number of heap errors: %d.", ulErrCnt);
418        /* Destroy heap */
419        BMEM_DestroyHeap(heap);
420
421done:
422        /* display status */
423        if ((err == BERR_SUCCESS) && (ulErrCnt == 0))
424                BKNI_Printf("Test 1: SUCCESS");
425        else
426                BKNI_Printf("Test 1: FAILED (%ul)", err);
427
428        /* return status */
429        return err;
430}
431
432/* End of File */
Note: See TracBrowser for help on using the repository browser.