1 /* 2 * Copyright (c) 2025 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 * @addtogroup MuslMalloc 17 * @{ 18 * 19 * @brief Provide some methods related to memory management. 20 * @since 12 21 */ 22 /** 23 * @file malloc.h 24 * 25 * @brief Includes some memory-related methods and structures, 26 * such as: malloc, calloc, realloc, and so on. 27 * 28 * @library libc.so 29 * @kit CstandardLibrary 30 * @since 12 31 */ 32 33 #ifndef _MALLOC_H 34 #define _MALLOC_H 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #define __NEED_size_t 41 42 #include <bits/alltypes.h> 43 44 #define M_SET_THREAD_CACHE (-1001) 45 #define M_THREAD_CACHE_ENABLE 1 46 #define M_THREAD_CACHE_DISABLE 0 47 48 #define M_FLUSH_THREAD_CACHE (-1002) 49 50 #define M_DELAYED_FREE (-1003) 51 #define M_DELAYED_FREE_ENABLE 1 52 #define M_DELAYED_FREE_DISABLE 0 53 54 #define M_OHOS_CONFIG (-1004) 55 #define M_DISABLE_OPT_TCACHE 100 56 #define M_ENABLE_OPT_TCACHE 101 57 #define M_TCACHE_PERFORMANCE_MODE 102 58 #define M_TCACHE_NORMAL_MODE 103 59 60 void *malloc (size_t); 61 void *calloc (size_t, size_t); 62 void *realloc (void *, size_t); 63 void free (void *); 64 void *valloc (size_t); 65 void *memalign(size_t, size_t); 66 67 size_t malloc_usable_size(void *); 68 int mallopt(int param, int value); 69 70 /** 71 * @brief This function determines whether a given memory block was allocated using 72 * Standard C library Memory Allocator. 73 * This function is MT-Safe(multi-thread safe) but not signal-safe. 74 * @param {void *} ptr A pointer to the memory block to be checked. 75 * @return 1 - The memory block was allocated using Standard C library Memory Allocator. 76 * 0 - The memory block was not allocated using Standard C library Memory Allocator. 77 * -1 - The function is not implemented or other error. 78 * @since 19 79 */ 80 int malloc_check_from_ptr(void *ptr); 81 82 struct mallinfo { 83 int arena; 84 int ordblks; 85 int smblks; 86 int hblks; 87 int hblkhd; 88 int usmblks; 89 int fsmblks; 90 int uordblks; 91 int fordblks; 92 int keepcost; 93 }; 94 95 struct mallinfo2 { 96 size_t arena; 97 size_t ordblks; 98 size_t smblks; 99 size_t hblks; 100 size_t hblkhd; 101 size_t usmblks; 102 size_t fsmblks; 103 size_t uordblks; 104 size_t fordblks; 105 size_t keepcost; 106 }; 107 108 /** 109 * @brief Obtains the memory information allocated by malloc-related operations. 110 * @return A mallinfo struct containing details about memory allocation. 111 * @since 20 112 */ 113 struct mallinfo mallinfo(void); 114 115 /** 116 * @brief Obtains the memory information allocated by malloc-related operations. 117 * @return A mallinfo2 struct containing details about memory allocation. Unlike mallinfo, this struct uses 118 * size_t for its counters, providing a larger range. 119 * @since 20 120 */ 121 struct mallinfo2 mallinfo2(void); 122 123 #ifdef __cplusplus 124 } 125 #endif 126 127 #endif 128 /** @} */