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 SetWaitTime(int waitTime)61 void SetWaitTime(int waitTime) 62 { 63 waitTime_ = waitTime; 64 } 65 66 void ResetPos(); 67 void UseMemory(int32_t size); 68 bool GetMemory(uint32_t size, uint8_t** memory, uint32_t* offset); 69 bool Seek(uint32_t pos); 70 71 struct ShareMemoryBlockCtx { 72 RandomWriteCtx ctx; 73 ShareMemoryBlock* block = nullptr; 74 }; GetCtx()75 ShareMemoryBlockCtx* GetCtx() 76 { 77 return &smbCtx_; 78 } 79 80 private: 81 int8_t* GetFreeMemory(uint32_t size); 82 bool UseFreeMemory(int8_t* pmem, uint32_t size); 83 int waitTime_ = 60000000; 84 uint32_t GetDataSize(); 85 const int8_t* GetDataPoint(); 86 bool Next(); 87 88 struct BlockHeader { 89 struct alignas(sizeof(uintptr_t)) { 90 std::atomic<uint32_t> writeOffset_; 91 std::atomic<uint32_t> readOffset_; 92 uint32_t memorySize_; 93 pthread_mutex_t mutex_; 94 std::atomic<uint32_t> bytesCount_; 95 std::atomic<uint32_t> chunkCount_; 96 } info; 97 int8_t data[0]; 98 }; 99 100 ShareMemoryBlock(); 101 int8_t* GetCurrentFreeMemory(uint32_t size); 102 103 bool CreateBlock(std::string name, uint32_t size); 104 bool CreateBlockWithFd(std::string name, uint32_t size, int fd); 105 bool ReleaseBlock(); 106 int fileDescriptor_; 107 void* memoryPoint_; 108 uint32_t memorySize_; 109 std::string memoryName_; 110 uint32_t messageWriteOffset_ = 0; 111 ShareMemoryBlockCtx smbCtx_ = { 112 .ctx = {nullptr, nullptr}, 113 .block = nullptr, 114 }; 115 116 BlockHeader* header_; 117 ReusePolicy reusePloicy_; 118 }; 119 120 #endif 121