• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 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 
27 class ShareMemoryBlock {
28 public:
29     ShareMemoryBlock(const std::string& name, uint32_t size);
30     ShareMemoryBlock(const std::string& name, uint32_t size, int fd);
31     ~ShareMemoryBlock();
32 
33     bool PutRaw(const int8_t* data, uint32_t size);
34     bool PutRawTimeout(const int8_t* data, uint32_t size);
35     bool PutWithPayloadTimeout(const int8_t* header, uint32_t headerSize, const int8_t* payload, uint32_t payloadSize);
36     bool PutWithPayloadSync(const int8_t* header, uint32_t headerSize, const int8_t* payload, uint32_t payloadSize);
37 #ifndef NO_PROTOBUF
38     EXPORT_API bool PutMessage(const google::protobuf::Message& pmsg, const std::string& pluginName);
39 #endif
40     using DataHandler = std::function<bool(const int8_t*, uint32_t)>;
41     EXPORT_API bool TakeData(const DataHandler& func);
42 
43     std::string GetName();
44     uint32_t GetSize();
45     EXPORT_API int GetfileDescriptor();
46     EXPORT_API void ClearShareMemoryBlock();
47 
48     bool Valid() const;
49 
50     enum ReusePolicy {
51         DROP_NONE, // buffer满时,不丢弃老数据,不放入新数据
52         DROP_OLD,  // buffer满时,丢弃最老的数据
53     };
SetReusePolicy(enum ReusePolicy dt)54     void SetReusePolicy(enum ReusePolicy dt)
55     {
56         reusePloicy_ = dt;
57     }
58 
59 private:
60     int8_t* GetFreeMemory(uint32_t size);
61     bool UseFreeMemory(int8_t* pmem, uint32_t size);
62 
63     uint32_t GetDataSize();
64     const int8_t* GetDataPoint();
65     bool Next();
66 
67     struct BlockHeader {
68         struct alignas(sizeof(uintptr_t)) {
69             uint32_t writeOffset_;
70             uint32_t readOffset_;
71             uint32_t memorySize_;
72             pthread_mutex_t mutex_;
73             uint32_t bytesCount_;
74             uint32_t chunkCount_;
75         } info;
76         int8_t data[0];
77     };
78 
79     ShareMemoryBlock();
80     int8_t* GetCurrentFreeMemory(uint32_t size);
81 
82     bool CreateBlock(std::string name, uint32_t size);
83     bool CreateBlockWithFd(std::string name, uint32_t size, int fd);
84     bool ReleaseBlock();
85 
86     int fileDescriptor_;
87     void* memoryPoint_;
88     uint32_t memorySize_;
89     std::string memoryName_;
90 
91     BlockHeader* header_;
92     ReusePolicy reusePloicy_;
93 };
94 
95 #endif
96