Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef SIMPLE_BUFFER_POOL_H_
21 : #define SIMPLE_BUFFER_POOL_H_
22 :
23 : #include <map>
24 : #include <memory>
25 : #include <mutex>
26 :
27 : class SimpleBufferPool
28 : {
29 : public:
30 : SimpleBufferPool();
31 : ~SimpleBufferPool();
32 : SimpleBufferPool(const SimpleBufferPool &) = delete;
33 : SimpleBufferPool(SimpleBufferPool &&) = delete;
34 : SimpleBufferPool &operator=(const SimpleBufferPool &) = delete;
35 : SimpleBufferPool &operator=(SimpleBufferPool &&) = delete;
36 :
37 33 : template <class T = uint8_t> T *allocate(size_t count)
38 : {
39 33 : return reinterpret_cast<T *>(allocateImpl(count * sizeof(T)));
40 : }
41 :
42 33 : template <class T = uint8_t> std::shared_ptr<T> allocateShared(size_t count)
43 : {
44 66 : return std::shared_ptr<T>(allocate(count), [this](T *p) { deallocate(p); });
45 : }
46 :
47 : void deallocate(void *p);
48 :
49 : private:
50 : void *allocateImpl(size_t bytes);
51 :
52 : private:
53 : uint8_t *m_staticBuf;
54 : size_t m_staticBufSize;
55 :
56 : struct BufInfo
57 : {
58 : size_t size;
59 : bool free;
60 : };
61 :
62 : std::mutex m_staticBufLock;
63 : std::map<void *, BufInfo> m_staticBufMetaData;
64 : };
65 :
66 : #endif // SIMPLE_BUFFER_POOL_H_
|