DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
DCS_ModuleCore.h
Go to the documentation of this file.
1 #ifndef _DCS_CORE_H
2 #define _DCS_CORE_H
3 
4 #pragma once
5 #include "../../config/exports.h"
6 #include "../../DCS_Utils/include/DCS_ModuleUtils.h"
7 
8 #include <functional>
9 
21 namespace DCS
22 {
23  namespace Memory
24  {
25  // class CircularBuffer
26  // {
27  // public:
28  // CircularBuffer(u64 block_size, u64 num_blocks);
29  // ~CircularBuffer();
30 
31  // inline u8* write(u8* data, u64 sz)
32  // {
33  // if(sz > block_size)
34  // {
35  // LOG_CRITICAL("CircularBuffer: Write operation size exceeded block_size.");
36  // return nullptr;
37  // }
38 
39  // while(read_tag.load() & (1 << o_write))
40  // {
41  // std::this_thread::yield();
42  // }
43 
44  // std::unique_lock<std::mutex> lck(m);
45 
46  // cv.wait(lck, []() { return (read_tag.load() & (1 << o_write)) == 0; });
47 
48  // lck.unlock();
49  // u8* p = data + (o_write++ * block_size);
50  // memcpy(p, data, sz);
51 
52  // read_tag |= (1 << (o_write-1));
53 
54  // o_write %= num_blocks;
55  // write_cycle++;
56  // return p;
57  // }
58 
59  // inline void read(u8* dst_buffer, u64 sz)
60  // {
61  // if(dst_buffer == nullptr)
62  // {
63  // LOG_CRITICAL("CircularBuffer: Read operation destination buffer is NULL.");
64  // return;
65  // }
66 
67  // std::unique_lock<std::mutex> lck(m);
68 
69  // cv.wait(lck, []() { return (read_tag.load() & (1 << o_read)) == 1; });
70 
71 
72  // u8* p = data + (o_read++ * block_size);
73  // memcpy(dst_buffer, p, sz);
74  // read_tag &= ~(1 << (o_read-1));
75 
76  // o_read %= num_blocks;
77  // read_cycle++;
78 
79  // lck.unlock();
80  // }
81 
82  // private:
83  // u8* data = nullptr;
84  // u64 block_size = 0;
85  // u64 num_blocks = 0;
86  // u64 total_size = 0;
87 
88  // u64 o_read = 0;
89  // u64 o_write = 0;
90 
91  // std::atomic<u64> read_tag = 0xFFFFFFFFFFFFFFFF;
92 
93  // std::condition_variable cv;
94  // std::mutex m;
95 
96  // u64 read_cycle = 0;
97  // u64 write_cycle = 0;
98  // };
99 
100 
108  {
109  char* data_start = nullptr;
110  char* data = nullptr;
111  u64 size;
112  u64 usedSize;
113  u64 alignment;
114  };
115 
122  {
123  public:
129  LinearAllocator(u64 size, u64 align);
130 
136  template<typename T, typename... Args>
137  T* allocate(Args... args)
138  {
139  buffer.usedSize += sizeof(T);
140 
141  if (buffer.usedSize > buffer.size)
142  {
143  buffer.usedSize -= sizeof(T);
144  LOG_CRITICAL("Allocating %u bytes would result in buffer overrun. [Available: %u]", sizeof(T), buffer.size - buffer.usedSize);
145  return nullptr;
146  }
147 
148  T* rb = nullptr;
149  rb = new(buffer.data) T(args...);
150 
151  if (rb == nullptr)
152  {
153  LOG_CRITICAL("Failed to allocated %u bytes in allocator.", sizeof(T));
154  return nullptr;
155  }
156 
157  buffer.data += sizeof(T);
158  return rb;
159  }
160 
165  template<typename T>
166  T* allocate()
167  {
168  buffer.usedSize += sizeof(T);
169 
170  if (buffer.usedSize > buffer.size)
171  {
172  buffer.usedSize -= sizeof(T);
173  LOG_CRITICAL("Allocating %u bytes would result in buffer overrun. [Available: %u]", sizeof(T), buffer.size - buffer.usedSize);
174  return nullptr;
175  }
176 
177  T* rb = nullptr;
178  rb = new(buffer.data) T();
179 
180  if (rb == nullptr)
181  {
182  LOG_CRITICAL("Failed to allocated %u bytes in allocator.", sizeof(T));
183  return nullptr;
184  }
185 
186  buffer.data += sizeof(T);
187  return rb;
188  }
189 
195  void release();
196 
202  void reset();
203 
204  private:
205  LinearDataPointer buffer;
206  };
207  }
208 
212  namespace Threading
213  {
220  }
221 
222  namespace Auth
223  {
224  DCS_API void Encrypt(DCS::u8* to_encrypt, int size, DCS::u8* key, DCS::u8* iv, DCS::u8* encrypted_out, DCS::u8* tag);
225  }
226 
227  namespace Math
228  {
229  struct CountResult
230  {
231  u64 num_detected;
232  u64* maximizers = nullptr;
233  f64* maxima = nullptr;
234 
235  ~CountResult()
236  {
237  if(maximizers != nullptr)
238  {
239  delete[] maximizers;
240  }
241 
242  if(maxima != nullptr)
243  {
244  delete[] maxima;
245  }
246  }
247  };
248 
249  CountResult countArrayPeak(f64* arr, u64 size, f64 vlo, f64 vhi, f64 vth);
250  }
251 }
252 
253 #endif _DCS_CORE_H
#define DCS_API
Defines the export interface acessible via the dll-interface. Only important for SHARED LIB Compile M...
Definition: exports.h:116
Definition: DCS_ModuleCore.h:229
Linearly allocates memory for a single use.
Definition: DCS_ModuleCore.h:121
Definition: DCS_ModuleCore.h:107
double f64
Equivalent to double.
Definition: DCS_ModuleUtils.h:61
const DCS_API u16 GetMaxHardwareConcurrency()
Get current machine maximum hardware concurrency (Number of physical threads supported by the current...
Definition: threading.cpp:6
T * allocate()
Allocates a new section of the pool for a new type T.
Definition: DCS_ModuleCore.h:166
unsigned short u16
Equivalent to uint_16t.
Definition: DCS_ModuleUtils.h:56
T * allocate(Args...args)
Allocates a new section of the pool for a new type T.
Definition: DCS_ModuleCore.h:137
#define DCS_REGISTER_CALL(...)
Append this definition before function declarations to register it as a tcp connection callable...
Definition: exports.h:74
Definition: registry.h:70
#define LOG_CRITICAL(msg,...)
Alias to LOG_LVL(Critical, msg, VA_ARGS)
Definition: DCS_ModuleUtils.h:38
unsigned long long u64
Equivalent to uint_64t.
Definition: DCS_ModuleUtils.h:52
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58