Atlas Runtime
circular_buffer.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 
17 #ifndef _LOG_ALLOC_H
18 #define _LOG_ALLOC_H
19 
20 #include <atomic>
21 
22 namespace Atlas {
23 
24 template<class T>
25 struct CbLog
26 {
27  explicit CbLog(uint32_t sz, uint32_t is_filled,
28  uint32_t start_cb, uint32_t end_cb)
29  : Size{sz},
30  isFilled{is_filled},
31  Start{start_cb},
32  End{end_cb},
33  LogArray{nullptr} {}
34  CbLog() = delete;
35  CbLog(const CbLog&) = delete;
36  CbLog(CbLog&&) = delete;
37  CbLog& operator=(const CbLog&) = delete;
38  CbLog& operator=(CbLog&&) = delete;
39 
40  uint32_t Size;
41  std::atomic<uint32_t> isFilled;
42  std::atomic<uint32_t> Start;
43  std::atomic<uint32_t> End;
45 
46  bool isFull() {
47  return (End.load(std::memory_order_acquire)+1) % Size ==
48  Start.load(std::memory_order_acquire);
49  }
50 
51  bool isEmpty() {
52  return Start.load(std::memory_order_acquire) ==
53  End.load(std::memory_order_acquire);
54  }
55 };
56 
57 template<class T>
58 struct CbListNode
59 {
60  explicit CbListNode(CbLog<T> *cb, char *start_addr, char *end_addr)
61  : Cb{cb},
62  StartAddr{start_addr},
63  EndAddr{end_addr},
64  Next{nullptr},
65  Tid{pthread_self()},
66  isAvailable{false} {}
67  CbListNode() = delete;
68  CbListNode(const CbListNode&) = delete;
69  CbListNode(CbListNode&&) = delete;
70  CbListNode& operator=(const CbListNode&) = delete;
71  CbListNode& operator=(CbListNode&&) = delete;
72 
74  char *StartAddr;
75  char *EndAddr;
77  pthread_t Tid;
78  std::atomic<uint32_t> isAvailable;
79 };
80 
81 // CbList is a data structure shared among threads. When a new slot is
82 // requested, if the current buffer is found full, the current thread
83 // creates a new buffer, adds it to the CbList and
84 // return the first slot from this new buffer. If a buffer ever becomes
85 // empty, it can be reused. A partially empty buffer cannot be reused.
86 
87 // TODO eventual GC on cb_list
88 
89 } // namespace Atlas
90 
91 #endif
CbListNode(CbLog< T > *cb, char *start_addr, char *end_addr)
Definition: circular_buffer.hpp:60
uint32_t Size
Definition: circular_buffer.hpp:40
char * StartAddr
Definition: circular_buffer.hpp:74
CbLog< T > * Cb
Definition: circular_buffer.hpp:73
std::atomic< uint32_t > Start
Definition: circular_buffer.hpp:42
T * LogArray
Definition: circular_buffer.hpp:44
std::atomic< uint32_t > isFilled
Definition: circular_buffer.hpp:41
CbLog(uint32_t sz, uint32_t is_filled, uint32_t start_cb, uint32_t end_cb)
Definition: circular_buffer.hpp:27
Definition: circular_buffer.hpp:25
bool isFull()
Definition: circular_buffer.hpp:46
Definition: circular_buffer.hpp:58
CbListNode< T > * Next
Definition: circular_buffer.hpp:76
std::atomic< uint32_t > End
Definition: circular_buffer.hpp:43
CbLog & operator=(const CbLog &)=delete
bool isEmpty()
Definition: circular_buffer.hpp:51
char * EndAddr
Definition: circular_buffer.hpp:75
std::atomic< uint32_t > isAvailable
Definition: circular_buffer.hpp:78
Definition: atlas_alloc_cpp.hpp:21
pthread_t Tid
Definition: circular_buffer.hpp:77
CbLog()=delete