• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef API_BASE_CONTAINERS_ALLOCATOR_H
17 #define API_BASE_CONTAINERS_ALLOCATOR_H
18 
19 #include <cstdint>
20 #include <cstdlib> // malloc, free
21 #include <securec.h>
22 
23 #include <base/namespace.h>
24 #include <base/util/log.h>
25 
26 BASE_BEGIN_NAMESPACE()
27 struct allocator {
28     using size_type = size_t;
29     void* instance { nullptr };
30     void* (*alloc)(void* instance, size_type size) { nullptr };
31     void (*free)(void* instance, void* ptr) { nullptr };
32 };
33 
CloneData(void * const dst,const size_t dstSize,const void * const src,const size_t srcSize)34 inline bool CloneData(void* const dst, const size_t dstSize, const void* const src, const size_t srcSize)
35 {
36     if (dst && src && srcSize <= dstSize) {
37         // Note: arguments for memcpy have been verified.
38         if (memcpy_s(dst, dstSize, src, srcSize) != 0) {
39             return false;
40         }
41     } else {
42         BASE_LOG_E("CloneData invalid arguments.");
43     }
44     return (dst && src && srcSize <= dstSize);
45 }
46 
MoveData(void * const dst,const size_t dstSize,const void * const src,const size_t srcSize)47 inline bool MoveData(void* const dst, const size_t dstSize, const void* const src, const size_t srcSize)
48 {
49     if (dst && src && srcSize <= dstSize) {
50         // Note: arguments for memmove have been verified.
51         if (memmove_s(dst, dstSize, src, srcSize) != 0) {
52             return false;
53         }
54     } else {
55         BASE_LOG_E("MoveData invalid arguments.");
56     }
57     return (dst && src && srcSize <= dstSize);
58 }
59 
ClearToValue(void * dst,size_t dstSize,uint8_t val,size_t count)60 inline bool ClearToValue(void* dst, size_t dstSize, uint8_t val, size_t count)
61 {
62     if (dst == nullptr) {
63         BASE_LOG_E("ClearToValue invalid arguments");
64         return false;
65     } else if (count > dstSize) {
66         count = dstSize;
67     }
68     // Note: arguments for memset have been verified.
69     if (memset_s(dst, dstSize, val, count) != 0) {
70         return false;
71     }
72     return true;
73 }
74 
default_allocator()75 inline allocator& default_allocator()
76 {
77     static allocator DefaultAllocInstance { nullptr,
78         [](void* /* instance */, allocator::size_type size) -> void* { return ::malloc(size); },
79         [](void* /* instance */, void* aPtr) { ::free(aPtr); } };
80     return DefaultAllocInstance;
81 }
82 BASE_END_NAMESPACE()
83 
84 #endif // API_BASE_CONTAINERS_ALLOCATOR_H
85