1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "osTest.h"
33 #include "It_los_mem.h"
34
35
36 #define LOOP_NUM 500
37
TestCase(VOID)38 static UINT32 TestCase(VOID)
39 {
40 UINT32 ret;
41 void *p0 = NULL;
42 void *f0 = NULL;
43 UINT32 size, sizeadd;
44 int count;
45 UINT32 freesize[2] = {0}; // 2, array size.
46
47 MemInit();
48
49 freesize[0] = MemGetFreeSize(g_memPool);
50
51 size = 0x8;
52 for (count = 0; count < LOOP_NUM; count++) {
53 p0 = LOS_MemAlloc(g_memPool, size);
54 ICUNIT_GOTO_NOT_EQUAL(p0, NULL, count, EXIT);
55
56 f0 = LOS_MemRealloc(g_memPool, p0, size / 2); // 2, The reallocated memory is half of its previous size.
57 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
58
59 f0 = LOS_MemRealloc(g_memPool, p0, size * 2); // 2, The reallocated memory is 2 times of its previous size.
60 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
61
62 ret = LOS_MemFree(g_memPool, f0);
63 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
64 }
65
66 freesize[1] = MemGetFreeSize(g_memPool);
67 ICUNIT_GOTO_EQUAL(freesize[1], freesize[0], freesize[1], EXIT);
68
69 size = 0x800;
70 for (count = 0; count < LOOP_NUM; count++) {
71 p0 = LOS_MemAlloc(g_memPool, size);
72 ICUNIT_GOTO_NOT_EQUAL(p0, NULL, count, EXIT);
73
74 f0 = LOS_MemRealloc(g_memPool, p0, size / 2); // 2, The reallocated memory is half of its previous size.
75 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
76
77 f0 = LOS_MemRealloc(g_memPool, p0, size * 2); // 2, The reallocated memory is 2 times of its previous size.
78 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
79
80 ret = LOS_MemFree(g_memPool, f0);
81 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
82 }
83
84 freesize[1] = MemGetFreeSize(g_memPool);
85 ICUNIT_GOTO_EQUAL(freesize[1], freesize[0], freesize[1], EXIT);
86
87 sizeadd = 4; // 4, set 4 to sizeadd.
88 while (1) {
89 size = 1 << sizeadd;
90 for (count = 0; count < LOOP_NUM; count++) {
91 p0 = LOS_MemAlloc(g_memPool, size);
92 ICUNIT_GOTO_NOT_EQUAL(p0, NULL, count, EXIT);
93
94 f0 = LOS_MemRealloc(g_memPool, p0, size / 2); // 2, The reallocated memory is half of its previous size.
95 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
96
97 f0 = LOS_MemRealloc(g_memPool, p0, size * 2); // 2, The reallocated memory is 2 times of its previous size.
98 ICUNIT_GOTO_NOT_EQUAL(f0, NULL, count, EXIT);
99
100 ret = LOS_MemFree(g_memPool, f0);
101 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
102 }
103
104 sizeadd++;
105 if (sizeadd == 9) { // 9, the limit of sizeadd.
106 break;
107 }
108 }
109
110 freesize[1] = MemGetFreeSize(g_memPool);
111 ICUNIT_GOTO_EQUAL(freesize[1], freesize[0], freesize[1], EXIT);
112
113 EXIT:
114 MemFree();
115 return LOS_OK;
116 }
117
ItLosMem037(void)118 VOID ItLosMem037(void)
119 {
120 TEST_ADD_CASE("ItLosMem037", TestCase, TEST_LOS, TEST_MEM, TEST_LEVEL3, TEST_PRESSURE);
121 }
122
123