• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "os/mem.h"
17 #include "utils/asan_interface.h"
18 #include "utils/tsan_interface.h"
19 
20 #include <gtest/gtest.h>
21 
22 namespace panda::test::mem {
23 
TEST(Mem,GetNativeBytesFromMallinfoTest)24 TEST(Mem, GetNativeBytesFromMallinfoTest)
25 {
26 #if (!defined(PANDA_ASAN_ON)) && (!defined(PANDA_TSAN_ON)) && (defined(__GLIBC__) || defined(PANDA_TARGET_MOBILE))
27     size_t old_bytes = panda::os::mem::GetNativeBytesFromMallinfo();
28     size_t new_bytes = old_bytes;
29 
30     void *p1[1000];
31     for (int i = 0; i < 1000; i++) {
32         p1[i] = malloc(64);
33         ASSERT_NE(p1[i], nullptr);
34     }
35     new_bytes = panda::os::mem::GetNativeBytesFromMallinfo();
36     ASSERT_TRUE(new_bytes > old_bytes);
37 
38     old_bytes = new_bytes;
39     void *p2[10];
40     for (int i = 0; i < 10; i++) {
41         p2[i] = malloc(4 * 1024 * 1024);
42         ASSERT_NE(p2[i], nullptr);
43     }
44     new_bytes = panda::os::mem::GetNativeBytesFromMallinfo();
45     ASSERT_TRUE(new_bytes > old_bytes);
46 
47     old_bytes = new_bytes;
48     for (int i = 0; i < 1000; i++) {
49         free(p1[i]);
50         p1[i] = nullptr;
51     }
52     new_bytes = panda::os::mem::GetNativeBytesFromMallinfo();
53     ASSERT_TRUE(new_bytes < old_bytes);
54 
55     old_bytes = new_bytes;
56     for (int i = 0; i < 10; i++) {
57         free(p2[i]);
58         p2[i] = nullptr;
59     }
60     new_bytes = panda::os::mem::GetNativeBytesFromMallinfo();
61     ASSERT_TRUE(new_bytes < old_bytes);
62 #else
63     size_t bytes = panda::os::mem::GetNativeBytesFromMallinfo();
64     ASSERT_EQ(bytes, panda::os::mem::DEFAULT_NATIVE_BYTES_FROM_MALLINFO);
65 #endif
66 }
67 
68 }  // namespace panda::test::mem
69