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* p[TEST_MEM_SIZE / 0X300] = {NULL};
43 void *f0 = NULL;
44 UINT32 size;
45 int count;
46 int i, j;
47
48 MemInit();
49
50 size = 0x300;
51 for (count = 0; count < LOOP_NUM; count++) {
52 i = 0;
53 for (p0 = LOS_MemAlloc(g_memPool, size), f0 = p0; p0 != NULL; i++) {
54 p[i] = p0;
55 p0 = LOS_MemAlloc(g_memPool, size);
56 }
57
58 if (MemGetFreeSize(g_memPool) >= (size + LOS_DLNK_NODE_HEAD_SIZE)) {
59 ICUNIT_GOTO_EQUAL(1, 0, i, EXIT);
60 }
61
62 for (j = 0; j < i; j++) {
63 p0 = LOS_MemRealloc(g_memPool, p[j], size / 2); // 2, The reallocated memory is half of its previous size.
64 if (p0 == NULL) {
65 ICUNIT_GOTO_EQUAL(1, 0, j, EXIT);
66 }
67 }
68
69 for (j = 0; j < i; j++) {
70 ret = LOS_MemFree(g_memPool, f0);
71 f0 = (void *)((char *)f0 + ((UINT32)size + LOS_DLNK_NODE_HEAD_SIZE));
72 if (ret != LOS_OK) {
73 ICUNIT_GOTO_EQUAL(1, 0, j, EXIT);
74 }
75 }
76 }
77
78 for (count = 0; count < LOOP_NUM; count++) {
79 i = 0;
80 for (p0 = LOS_MemAlloc(g_memPool, size), f0 = p0; p0 != NULL; i++) {
81 p[i] = p0;
82 p0 = LOS_MemAlloc(g_memPool, size);
83 }
84
85 if (MemGetFreeSize(g_memPool) >= (size + LOS_DLNK_NODE_HEAD_SIZE)) {
86 ICUNIT_GOTO_EQUAL(1, 0, i, EXIT);
87 }
88
89 for (j = 0; j < i; j++) {
90 if (j % 2) { // 2, judge j is odd number or not.
91 ret = LOS_MemFree(g_memPool, p[j]);
92 if (ret != LOS_OK) {
93 ICUNIT_GOTO_EQUAL(1, 0, j, EXIT);
94 }
95 }
96 }
97 for (j = 0; j < i; j++) {
98 if (!(j % 2)) { // 2, judge j is even number or not.
99 p0 = LOS_MemRealloc(g_memPool, p[j], size * 2);
100 if (p0 == NULL) {
101 ICUNIT_GOTO_EQUAL(1, 0, j, EXIT);
102 }
103 }
104 }
105
106 for (j = 0; j < i; j++) {
107 if (!(j % 2)) { // 2, judge j is even number or not.
108 ret = LOS_MemFree(g_memPool, f0);
109 f0 = (void *)((char *)f0 + 2 * ((UINT32)size + LOS_DLNK_NODE_HEAD_SIZE)); // 2, set new size.
110 if (ret != LOS_OK) {
111 ICUNIT_GOTO_EQUAL(1, 0, j, EXIT);
112 }
113 }
114 }
115 }
116
117 EXIT:
118 MemFree();
119 return LOS_OK;
120 }
121
ItLosMem038(void)122 VOID ItLosMem038(void)
123 {
124 TEST_ADD_CASE("ItLosMem038", TestCase, TEST_LOS, TEST_MEM, TEST_LEVEL3, TEST_PRESSURE);
125 }
126
127