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