• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 SHARE_MEMORY_BLOCK_H
17 #define SHARE_MEMORY_BLOCK_H
18 
19 #ifndef NO_PROTOBUF
20 #include "google/protobuf/message.h"
21 #endif
22 #include <cstdint>
23 #include <functional>
24 #include <string>
25 #include "logging.h"
26 #include "plugin_module_api.h"
27 
28 class EXPORT_API ShareMemoryBlock {
29 public:
30     ShareMemoryBlock(const std::string& name, uint32_t size);
31     ShareMemoryBlock(const std::string& name, uint32_t size, int fd);
32     ~ShareMemoryBlock();
33 
34     bool PutRaw(const int8_t* data, uint32_t size);
35     bool PutRawTimeout(const int8_t* data, uint32_t size);
36     bool PutWithPayloadTimeout(const int8_t* header, uint32_t headerSize, const int8_t* payload, uint32_t payloadSize);
37     bool PutWithPayloadSync(const int8_t* header, uint32_t headerSize, const int8_t* payload, uint32_t payloadSize,
38                             const std::function<bool()>& callback);
39 #ifndef NO_PROTOBUF
40     EXPORT_API bool PutMessage(const google::protobuf::Message& pmsg, const std::string& pluginName);
41 #endif
42     using DataHandler = std::function<bool(const int8_t*, uint32_t)>;
43     EXPORT_API bool TakeData(const DataHandler& func, bool isProtobufSerialize = true);
44     bool TakeDataOptimize(const DataHandler& func);
45 
46     std::string GetName();
47     uint32_t GetSize();
48     EXPORT_API int GetfileDescriptor();
49 
50     bool Valid() const;
51 
52     enum ReusePolicy {
53         DROP_NONE, // buffer满时,不丢弃老数据,不放入新数据
54         DROP_OLD,  // buffer满时,丢弃最老的数据
55     };
SetReusePolicy(enum ReusePolicy dt)56     void SetReusePolicy(enum ReusePolicy dt)
57     {
58         reusePloicy_ = dt;
59     }
60 
61     void ResetPos();
62     void UseMemory(int32_t size);
63     bool GetMemory(uint32_t size, uint8_t** memory, uint32_t* offset);
64     bool Seek(uint32_t pos);
65 
66     struct ShareMemoryBlockCtx {
67         RandomWriteCtx ctx;
68         ShareMemoryBlock* block = nullptr;
69     };
GetCtx()70     ShareMemoryBlockCtx* GetCtx()
71     {
72         return &smbCtx_;
73     }
74 
75 private:
76     int8_t* GetFreeMemory(uint32_t size);
77     bool UseFreeMemory(int8_t* pmem, uint32_t size);
78 
79     uint32_t GetDataSize();
80     const int8_t* GetDataPoint();
81     bool Next();
82 
83     struct BlockHeader {
84         struct alignas(sizeof(uintptr_t)) {
85             std::atomic<uint32_t> writeOffset_;
86             std::atomic<uint32_t> readOffset_;
87             uint32_t memorySize_;
88             pthread_mutex_t mutex_;
89             std::atomic<uint32_t> bytesCount_;
90             std::atomic<uint32_t> chunkCount_;
91         } info;
92         int8_t data[0];
93     };
94 
95     ShareMemoryBlock();
96     int8_t* GetCurrentFreeMemory(uint32_t size);
97 
98     bool CreateBlock(std::string name, uint32_t size);
99     bool CreateBlockWithFd(std::string name, uint32_t size, int fd);
100     bool ReleaseBlock();
101 
102     int fileDescriptor_;
103     void* memoryPoint_;
104     uint32_t memorySize_;
105     std::string memoryName_;
106     uint32_t messageWriteOffset_ = 0;
107     ShareMemoryBlockCtx smbCtx_ = {
108         .ctx = {nullptr, nullptr},
109         .block = nullptr,
110     };
111 
112     BlockHeader* header_;
113     ReusePolicy reusePloicy_;
114 };
115 
116 #endif
117