LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
locked_buffer.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_APPS_LOCKED_BUFFER_H
7#define DEEPLIMA_APPS_LOCKED_BUFFER_H
8
9#include <cassert>
10#include <cstddef>
11#include <cstdint>
12#include <cstring>
13#include <iostream>
14#include <stdexcept>
15#include <vector>
16
22{
24 : m_data(nullptr),
25 m_len(0),
26 m_lock_count(0),
27 m_char_aligned_data(nullptr)
28 {
29 // std::cerr << "locked_buffer_t::locked_buffer_t()"
30 // << (void*)this << std::endl;
31 }
32
34 {
35 // std::cerr << "locked_buffer_t::~locked_buffer_t() "
36 // << (void*)this << std::endl;
37 m_data = nullptr;
38 m_char_aligned_data = nullptr;
39 }
40
42 {
43 // std::cerr << "locked_buffer_t::locked_buffer_t(other)"
44 // << (void*)this << std::endl;
45 m_data = other.m_data;
47 m_len = other.m_len;
49 }
50
52
53 inline uint8_t* end() const
54 {
55 if (nullptr == m_char_aligned_data)
56 {
57 return nullptr;
58 }
59 return (uint8_t*)m_char_aligned_data + m_len;
60 }
61
62 inline bool locked() const
63 {
64 return m_lock_count > 0;
65 }
66
67 inline void lock()
68 {
69 // std::cerr << "locked_buffer_t::lock " << (void*)this << " " << m_lock_count;
71 // std::cerr << " -> " << m_lock_count << std::endl;
72 }
73
74 inline void unlock()
75 {
76 // std::cerr << "locked_buffer_t::unlock " << (void*)this << " " << m_lock_count;
77 m_len = 0;
78 m_char_aligned_data = nullptr;
79 assert(m_lock_count > 0);
81 // std::cerr << " -> " << m_lock_count << std::endl;
82 }
83
84 inline void set_read_start(const char* new_start)
85 {
86 assert((uint8_t*)new_start < end());
87 m_len -= (new_start - m_char_aligned_data);
88 m_char_aligned_data = new_start;
89 }
90
91 // m_data and m_char_aligned_data are owned by the container of this buffer
92 uint8_t* m_data = nullptr;
93 uint32_t m_len = 0;
94 uint32_t m_lock_count = 0;
95 const char* m_char_aligned_data = nullptr;
96
97};
98
100{
101 std::vector<locked_buffer_t> m_data;
102 uint8_t* m_mem = nullptr;
104
105 locked_buffer_set_t(size_t /* currently unused. should be 2 times the number of threads */,
106 uint32_t buffer_size = 0)
107 : m_mem(nullptr),
108 m_max_buff_size(buffer_size)
109 {}
110
112 {
113 if (nullptr != m_mem)
114 {
115 delete[] m_mem;
116 }
117 }
118
119 void init(size_t n, uint32_t buffer_size)
120 {
121 // std::cerr << "locked_buffer_set_t::init" << std::endl;
122 assert(n > 0);
123 assert(buffer_size > 0);
124
125 m_max_buff_size = buffer_size;
126
127 if (nullptr != m_mem)
128 {
129 delete[] m_mem;
130 }
131 m_mem = new uint8_t [ 8 + n * buffer_size ];
132
133 if (nullptr == m_mem)
134 {
135 throw std::runtime_error("Out of memory.");
136 }
137
138 uint8_t* p_base = m_mem + 8;
139
140 m_data.resize(n);
141 for (size_t i = 0; i < m_data.size(); i++)
142 {
143 m_data[i].m_data = p_base + (i * buffer_size);
144 m_data[i].m_len = buffer_size;
145 }
146 }
147
148 inline uint32_t max_buff_size() const
149 {
150 return m_max_buff_size;
151 }
152
153 inline size_t next(size_t n) const
154 {
155 return (m_data.size() == (n + 1)) ? 0 : (n + 1);
156 }
157
158 inline uint32_t bytes_available(size_t n)
159 {
160 uint32_t s = 0;
161 for (size_t i = n; i < m_data.size(); i++)
162 {
163 s += m_data[i].m_len;
164 }
165 return s;
166 }
167
168 inline size_t size() const
169 {
170 return m_data.size();
171 }
172
173 inline locked_buffer_t& get(size_t n)
174 {
175 assert(n < m_data.size());
176 return m_data[n];
177 }
178
180 {
181 std::cerr << (void*)this << " BUFFS: ";
182 for (size_t i = 0; i < m_data.size(); i++)
183 {
184 std::cerr << " | " << m_data[i].m_lock_count;
185 }
186 std::cerr << " |" << std::endl;
187 }
188};
189
190#endif
void init(size_t n, uint32_t buffer_size)
uint32_t bytes_available(size_t n)
std::vector< locked_buffer_t > m_data
size_t next(size_t n) const
locked_buffer_t & get(size_t n)
uint32_t max_buff_size() const
locked_buffer_set_t(size_t, uint32_t buffer_size=0)
size_t size() const
locketd_buffer_t is the character-level buffer shared between the source file reader and the tokenize...
const char * m_char_aligned_data
uint32_t m_lock_count
bool locked() const
void set_read_start(const char *new_start)
locked_buffer_t(const locked_buffer_t &other)
uint8_t * end() const
locked_buffer_t & operator=(const locked_buffer_t &)=delete