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