1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::alloc::{alloc_zeroed, dealloc, Layout, LayoutError};

/// Sized Buffer that can be used in multi-threaded environment, with reference count and lock.
pub struct Buffer<T : Clone + Default + Send + Sync>
{
    element : * mut T,
    len : *mut usize,
    lock : *mut bool,
    locked_here : bool,
    count : *mut usize,
    default : T
}
impl<T : Clone + Default + Send + Sync> Buffer<T>
{
    /// Create new Buffer with length.
    pub fn new(len : usize) -> Self
    {
        unsafe
        {
            let array_layout = std::alloc::Layout::array::<T>(len).unwrap();
            Self
            {
                element : std::alloc::alloc_zeroed(array_layout) as * mut T,
                len : std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as * mut usize,
                lock : std::alloc::alloc_zeroed(std::alloc::Layout::new::<bool>()) as * mut bool,
                locked_here : false,
                count : std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as * mut usize,
                default : T::default()
            }
        }
    }
    /// Get length of the Buffer.
    pub fn len(&self) -> usize { unsafe { *self.len } }
    /// resize the Buffer.
    pub fn resize(&mut self, new_len : usize)
    {
        let alloc_layout = std::alloc::Layout::array::<T>(new_len).unwrap();
        let dealloc_layout = std::alloc::Layout::array::<T>(self.len()).unwrap();
        unsafe
        {
            let element = std::alloc::alloc_zeroed(alloc_layout) as * mut T;
            std::ptr::copy_nonoverlapping(self.element, element, std::cmp::min(self.len(), new_len));
            std::alloc::dealloc(self.element as * mut u8, dealloc_layout);
            self.element = element;
            *self.len = new_len;
        }
    }
    /// New Buffer from raw pointer.
    pub fn from_raw(ptr : * mut T, length : usize) -> Self
    {
        unsafe
        {
            let len = std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as * mut usize;
            *len = length;
            let lock = std::alloc::alloc_zeroed(std::alloc::Layout::new::<bool>()) as *mut bool;
            *lock = false;
            Self { element : ptr, len, lock, locked_here : false, count : std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as *mut usize, default : T::default() }
        }
    }
    /// Try to lock in time. True if success and false if failed.
    pub fn try_lock(&mut self) -> bool
    {
        unsafe
        {
            if !*self.lock
            {
                *self.lock = true;
                self.locked_here = true;
                return true
            }
            false
        }
    }
    /// Lock the buffer.
    pub fn lock(&mut self)
    {
        unsafe
        {
            while *self.lock { std::thread::yield_now(); }
            if !*self.lock
            {
                *self.lock = true;
                self.locked_here = true;
            }
        }    
    }
    /// unlock the buffer.
    pub fn unlock(&mut self)
    {
        if self.locked_here
        {
            unsafe { *self.lock = false; }
            self.locked_here = false;
        }
    }
}
impl<T : Clone + Default + Send + Sync> std::ops::Index<usize> for Buffer<T>
{
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output { unsafe { self.element.offset((index % *self.len) as isize).as_ref().expect("No value assigned.") } }
}
impl<T : Clone + Default + Send + Sync> std::ops::IndexMut<usize> for Buffer<T>
{
    fn index_mut(& mut self, index: usize) -> & mut Self::Output
    {
        unsafe
        {
            if *self.lock && self.locked_here { return self.element.offset((index % *self.len) as isize).as_mut().expect("No value assigned.") }
            else if !self.locked_here
            {
                eprintln!("Buffer was locked somewhere else.");
                return &mut self.default
            }
            eprintln!("Buffer was not locked.");
            &mut self.default
        }
    }
}
impl<T : Clone + Default + Send + Sync> AsRef<[T]> for Buffer<T> { fn as_ref(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self.element, *self.len) } } }
impl<T : Clone + Default + Send + Sync> AsMut<[T]> for Buffer<T>
{
    fn as_mut(&mut self) -> &mut [T]
    {
        unsafe
        {
            if *self.lock && self.locked_here { return std::slice::from_raw_parts_mut(self.element, *self.len) }
            else if !self.locked_here
            {
                eprintln!("Buffer was locked somewhere else.");
                return std::slice::from_mut(&mut self.default)
            }
            eprintln!("Buffer was not locked.");
            std::slice::from_mut(&mut self.default)
        }
    }
}
impl<T : Clone + Default + Send + Sync> std::ops::Deref for Buffer<T>
{
    type Target = [T];
    fn deref(&self) -> &Self::Target { self.as_ref() }
}
impl<T : Clone + Default + Send + Sync> std::ops::DerefMut for Buffer<T> { fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut() } }
impl<T : Clone + Default + Send + Sync> Clone for Buffer<T>
{
    fn clone(&self) -> Self
    {
        unsafe 
        {
            *self.count += 1;
            Self
            {
                element : self.element,
                len : self.len,
                lock : self.lock,
                locked_here : false,
                count : self.count,
                default : T::default()
            }
        }
    }
}
impl<T : Clone + Default + Send + Sync> Default for Buffer<T>
{
    fn default() -> Self
    {
        unsafe
        {
            let lock = std::alloc::alloc_zeroed(std::alloc::Layout::new::<bool>()) as * mut bool;
            *lock = false;
            Self
            {
                element : std::ptr::null_mut(),
                len : std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as * mut usize,
                lock,
                locked_here : false,
                count : std::alloc::alloc_zeroed(std::alloc::Layout::new::<usize>()) as * mut usize,
                default : T::default()
            }
        }
        
    }
}
unsafe impl<T : Clone + Default + Send + Sync> Send for Buffer<T> {}
unsafe impl<T : Clone + Default + Send + Sync> Sync for Buffer<T> {}
impl<T : Clone + Default + Send + Sync> Drop for Buffer<T>
{
    fn drop(&mut self)
    {
        unsafe
        {
            if *self.count > 0
            {
                if self.locked_here { *self.lock = false; }
                *self.count -= 1;
                return
            }
            let array_layout = std::alloc::Layout::array::<T>(*self.len).unwrap();
            std::alloc::dealloc(self.element as *mut u8, array_layout);
            std::alloc::dealloc(self.len as *mut u8, std::alloc::Layout::new::<usize>());
            std::alloc::dealloc(self.lock as *mut u8, std::alloc::Layout::new::<bool>());
            std::alloc::dealloc(self.count as *mut u8, std::alloc::Layout::new::<usize>());
        }
    }
}

///The buffer that pushes the whole buffer when index meets the size of the buffer. Generic T must be either f32 or f64.
#[derive(Clone)]
pub struct PushBuffer<T>
{
    buffer : * mut T,
    index : usize,
    len : usize
}
impl<T : Copy> PushBuffer<T>
{
    ///New PushBuffer with length.

    pub fn new(len : usize) -> Result<Self, LayoutError>
    {
        let layout = Layout::array::<T>(len)?;
        unsafe { Ok(PushBuffer { buffer : alloc_zeroed(layout) as * mut T , index : 0, len : len }) }
    }
    ///New PushBuffer from raw pointer.

    pub fn from_raw(ptr : * mut T, len : usize) -> Self
    {
        Self { buffer : ptr, index : 0, len }
    }
    ///Resizes the buffer.

    pub fn resize(&mut self, len : usize) -> Result<(), LayoutError>
    {
        let dealloc_layout = std::alloc::Layout::array::<T>(self.len)?;
        let alloc_layout = std::alloc::Layout::array::<T>(len)?;
        unsafe
        {
            std::alloc::dealloc(self.buffer as * mut u8, dealloc_layout);
            self.buffer = std::alloc::alloc_zeroed(alloc_layout) as * mut T;
        }
        Ok(())
    }
    ///Converts internal data chunk as silce

    pub fn into_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self.buffer, self.len) } }
    ///Converts internal data chunk as mutable silce

    pub fn into_slice_mut(&self) -> &mut[T] { unsafe{ std::slice::from_raw_parts_mut(self.buffer, self.len) } }
    ///Pushes data to buffer.

    pub fn push(& mut self, value : T)
    {
        if self.index <= self.len
        {
            unsafe { * self.buffer.offset(self.index as isize) = value; }
            self.index += 1;
        }
        else
        {
            unsafe
                {
                    (1..self.len).for_each(|x| * self.buffer.offset(x as isize - 1) = * self.buffer.offset(x as isize));
                    * self.buffer.offset(self.index as isize) = value;
                }
        }
    }
    ///Get index.
    pub fn get_index(&self) -> usize { self.index }
    ///Set index.
    pub fn set_index(&mut self, index : usize) { self.index = index; }
    ///Returns the length of the buffer.
    pub fn len(& self) -> usize { return self.len; }
}
impl<T> std::ops::Index<usize> for PushBuffer<T>
{
    type Output = T;

    fn index(& self, index : usize) -> & Self::Output
    {
        let real_index = if index > self.len
        {
            eprintln!("Index out of range. Indexing to remain of given index divided by size of buffer");
            index % self.len
        } else { index };
        let data = unsafe { self.buffer.offset(real_index as isize).as_ref() };
        match data
        {
            None => { panic!("Access to invalid memory!"); }
            Some(reference) => { return reference; }
        }
    }
}
impl<T> std::ops::IndexMut<usize> for PushBuffer<T>
{
    fn index_mut(& mut self, index : usize) -> & mut Self::Output
    {
        let real_index = if index > self.len
        {
            eprintln!("Index out of range. Indexing to remain of given index divided by size of buffer");
            index % self.len
        } else { index };
        let data = unsafe { self.buffer.offset(real_index as isize).as_mut() };
        match data
        {
            None => { panic!("Access to invalid memory!"); }
            Some(reference) => { return reference; }
        }
    }
}
impl<T : Copy> std::ops::Deref for PushBuffer<T>
{
    type Target = [T];
    fn deref(&self) -> &Self::Target { self.into_slice() }
}
impl<T : Copy> std::ops::DerefMut for PushBuffer<T>
{
    fn deref_mut(&mut self) -> &mut Self::Target { self.into_slice_mut() }
}
impl<T> Drop for PushBuffer<T>
{
    fn drop(&mut self)
    {
        let layout = Layout::array::<T>(self.len as usize);
        match layout
        {
            Ok(layout) => { unsafe { dealloc(self.buffer as * mut u8, layout); } }
            Err(error) => { eprintln!("drop failed : {}", error); }
        }
    }
}

///Circular buffer or ring buffer. When index returns to 0 when index meets the size of the buffer. Generic T must be either f32 or f64.
#[derive(Clone)]
pub struct CircularBuffer<T>
{
    buffer : * mut T,
    read : usize,
    write : usize,
    len : usize
}
impl<T : Copy> CircularBuffer<T>
{
    ///New CircularBuffer with length.

    pub fn new(len : usize) -> Result<Self, LayoutError>
    {
        let layout = Layout::array::<T>(len)?;
        unsafe { Ok(CircularBuffer { buffer : alloc_zeroed(layout) as * mut T, read : 0, write : 0, len : len }) }
    }
    ///New CircularBuffer from raw pointer.

    pub fn from_raw(ptr : * mut T, len : usize) -> Self { Self { buffer : ptr, read : 0, write : 0, len } }
    ///Resizes the buffer.

    pub fn resize(&mut self, len : usize) -> Result<(), LayoutError>
    {
        let dealloc_layout = std::alloc::Layout::array::<T>(self.len)?;
        let alloc_layout = std::alloc::Layout::array::<T>(len)?;
        unsafe
        {
            std::alloc::dealloc(self.buffer as * mut u8, dealloc_layout);
            self.buffer = std::alloc::alloc_zeroed(alloc_layout) as * mut T;
        }
        self.read = 0;
        self.write = 0;

        Ok(())
    }
    ///Converts internal data chunk as silce
    pub fn into_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self.buffer, self.len) } }
    ///Converts internal data chunk as mutable silce
    pub fn into_slice_mut(&self) -> &mut[T] { unsafe{ std::slice::from_raw_parts_mut(self.buffer, self.len) } }
    ///Pushes data to buffer.
    pub fn push(& mut self, value : T)
    {
        unsafe { * self.buffer.offset(self.write as isize) = value; }
        if self.write < self.len() { self.write += 1; } else { self.write = 0; }
    }
    ///Reads next data of the buffer.
    pub fn next(& mut self) -> T
    {
        let value = unsafe { *self.buffer.offset(self.read as isize) };
        if self.write < self.len() { self.read += 1; } else { self.read = 0; }
        value
    }
    ///Initializes write index.
    pub fn init_write(& mut self, index : usize) { self.write = index; }
    ///Initializes read index.
    pub fn init_read(& mut self, index : usize) { self.write = index; }
    ///Returns the length of the buffer.
    pub fn len(& self) -> usize { return self.len; }
}
impl<T: Copy> std::ops::Deref for CircularBuffer<T>
{
    type Target = [T];
    fn deref(&self) -> &Self::Target { self.into_slice() }
}
impl<T: Copy> std::ops::DerefMut for CircularBuffer<T> { fn deref_mut(&mut self) -> &mut Self::Target { self.into_slice_mut() } }
impl<T> Drop for CircularBuffer<T>
{
    fn drop(&mut self)
    {
        let layout = Layout::array::<T>(self.len as usize);
        match layout
        {
            Ok(layout) => { unsafe { dealloc(self.buffer as * mut u8, layout); } }
            Err(error) => { eprintln!("drop failed : {}", error); }
        }
    }
}