-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathproxy_lib.cpp
86 lines (67 loc) · 1.79 KB
/
proxy_lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#if defined(__APPLE__)
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <umf/proxy_lib_new_delete.h>
#include "base.hpp"
#include "test_helpers.h"
#include "utils_common.h"
using umf_test::test;
#define SIZE_64 64
#define ALIGN_1024 1024
TEST_F(test, proxyLib_basic) {
::free(::malloc(SIZE_64));
// a check to verify we are running the proxy library
void *ptr = (void *)0x01;
#ifdef _WIN32
size_t size = _msize(ptr);
#elif __APPLE__
size_t size = ::malloc_size(ptr);
#else
size_t size = ::malloc_usable_size(ptr);
#endif
ASSERT_EQ(size, 0xDEADBEEF);
}
TEST_F(test, proxyLib_realloc_size0) {
// realloc(ptr, 0) == free (ptr)
// realloc(ptr, 0) returns NULL
ASSERT_EQ(::realloc(::malloc(SIZE_64), 0), nullptr);
}
TEST_F(test, proxyLib_malloc_usable_size) {
void *ptr = ::malloc(SIZE_64);
ASSERT_NE(ptr, nullptr);
if (ptr == nullptr) {
// Fix for the following CodeQL's warning on Windows:
// 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
return;
}
#ifdef _WIN32
size_t size = _msize(ptr);
#elif __APPLE__
size_t size = ::malloc_size(ptr);
#else
size_t size = ::malloc_usable_size(ptr);
#endif
ASSERT_EQ((int)(size == 0 || size >= SIZE_64), 1);
::free(ptr);
}
TEST_F(test, proxyLib_aligned_alloc) {
#ifdef _WIN32
void *ptr = _aligned_malloc(SIZE_64, ALIGN_1024);
#else
void *ptr = ::aligned_alloc(ALIGN_1024, SIZE_64);
#endif
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr, ALIGN_1024)), 1);
#ifdef _WIN32
_aligned_free(ptr);
#else
::free(ptr);
#endif
}