1 /*
2 * Copyright (c) 2023 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 "malloc.h"
17 #include "napi/native_api.h"
18 #include <cerrno>
19 #include <cstdlib>
20 #include <js_native_api_types.h>
21 #include <unistd.h>
22
23 #define TEST_AT_FDCWD (-100)
24 #define TEST_ERROR_AT_FDCWD 100
25 #define NO_ERR 0
26 #define SUCCESS 1
27 #define FAIL (-1)
28 #define TEN 10
29 #define TEST_MODE 0666
30 #define PARAM_0 0
31 #define PARAM_1 1
32 #define PARAM_2 2
33 #define PARAM_64 64
34 #define PARAM_10 10
35 #define PARAM_8 8
36 #define PARAM_256 256
37 #define PARAM_UNNORMAL (-1)
38 #define ERRON_0 0
39
Malloc(napi_env env,napi_callback_info info)40 static napi_value Malloc(napi_env env, napi_callback_info info)
41 {
42 errno = ERRON_0;
43 int ret = PARAM_UNNORMAL;
44 size_t memsize = PARAM_64;
45 int *memory = static_cast<int *>(malloc(memsize));
46 if (memory == nullptr) {
47 ret = PARAM_UNNORMAL;
48 } else {
49 ret = PARAM_0;
50 free(memory);
51 memory = nullptr;
52 }
53 napi_value result = nullptr;
54 napi_create_int32(env, ret, &result);
55 return result;
56 }
57
MallocUsableSize(napi_env env,napi_callback_info info)58 static napi_value MallocUsableSize(napi_env env, napi_callback_info info)
59 {
60 int mallocSize = PARAM_256;
61 void *p = malloc(mallocSize);
62 size_t ret = malloc_usable_size(p);
63 free(p);
64 if (ret == mallocSize) {
65 ret = PARAM_0;
66 } else {
67 ret = PARAM_UNNORMAL;
68 }
69 napi_value result = nullptr;
70 napi_create_int32(env, ret, &result);
71 return result;
72 }
73
Memalign(napi_env env,napi_callback_info info)74 static napi_value Memalign(napi_env env, napi_callback_info info)
75 {
76 int align = getpagesize();
77 int ret = FAIL;
78 void *buff = (void *)memalign(align, PARAM_8 * align);
79 if (buff != nullptr) {
80 ret = SUCCESS;
81 } else {
82 ret = FAIL;
83 }
84 free(buff);
85 napi_value result = nullptr;
86 napi_create_int32(env, ret, &result);
87 buff = nullptr;
88 return result;
89 }
Mallopt(napi_env env,napi_callback_info info)90 static napi_value Mallopt(napi_env env, napi_callback_info info)
91 {
92 int ret = mallopt(PARAM_0, PARAM_10);
93 napi_value result = nullptr;
94 napi_create_int32(env, ret, &result);
95 return result;
96 }
97
98 EXTERN_C_START
Init(napi_env env,napi_value exports)99 static napi_value Init(napi_env env, napi_value exports)
100 {
101 napi_property_descriptor desc[] = {
102 {"malloc", nullptr, Malloc, nullptr, nullptr, nullptr, napi_default, nullptr},
103 {"mallocUsableSize", nullptr, MallocUsableSize, nullptr, nullptr, nullptr, napi_default, nullptr},
104 {"memalign", nullptr, Memalign, nullptr, nullptr, nullptr, napi_default, nullptr},
105 {"mallopt", nullptr, Mallopt, nullptr, nullptr, nullptr, napi_default, nullptr},
106
107 };
108 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
109 return exports;
110 }
111 EXTERN_C_END
112
113 static napi_module demoModule = {
114 .nm_version = 1,
115 .nm_flags = 0,
116 .nm_filename = nullptr,
117 .nm_register_func = Init,
118 .nm_modname = "mallocndk",
119 .nm_priv = ((void *)0),
120 .reserved = {0},
121 };
122
RegisterModule(void)123 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
124