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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use crate::{Memory, DropFn};
use crate::droplist::{DropList, DropListWriteResult, DropItem};
use std::ptr::{null_mut};
use crate::block::{Block, PlacementError};
use std::fmt::Debug;

/// Error while trying to place data in arena block.

#[derive(Debug)]
pub enum UploadError {
    /// Droplist does not fit in a block.

    ///

    /// Arena has drop lists to efficiently execute item drop functions, and they hold around 1000

    /// items at minimum.

    ///

    /// Solution: increase block size.

    DropListDoesNotFit,

    /// Item does not fit in a block.

    ///

    /// If there is not enough space in a block, then another block is allocated. This error occurs only

    /// if the item is bigger than the maximum possible free space in a block.

    ///

    /// Solution: handle this error and do not store items that are too big or increase block size.

    ItemDoesNotFit,

    /// Metadata does not fit in a block.

    ///

    /// Arena stores its metadata in the first block. This metadata contains pointers to first/last

    /// droplists, memory block (yeah a bit circular here), weak and total reference counts.

    /// This error occurs when this metadata does not fit in a block, and should happen on `Arena`

    /// initialization only.

    ///

    /// Solution: increase block size.

    MetadataDoesNotFit,

    /// Arena was dropped.

    ///

    /// The main `Arena` is dropped and the drop function may have been executed for any containing item.

    /// This action is not available.

    ///

    /// Solution: ensure arena objects are not accessed after the arena is dropped and handle this error.

    ArenaIsNotAlive,
}

impl std::fmt::Display for UploadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UploadError::DropListDoesNotFit => std::fmt::Display::fmt("Drop list does not fit in a block", f),
            UploadError::ItemDoesNotFit => std::fmt::Display::fmt("Item is bigger than a block", f),
            UploadError::MetadataDoesNotFit => std::fmt::Display::fmt("Metadata does not fit in a first arena block", f),
            UploadError::ArenaIsNotAlive => std::fmt::Display::fmt("Arena is not alive", f),
        }
    }
}

impl std::error::Error for UploadError {}

/// Information about arena injected in first allocated arena memory block.

struct ArenaMetadata {
    memory: Memory,
    last_block: Option<Block>,
    first_drop_list: *mut DropList,
    last_drop_list: *mut DropList,
    strong_rc: i64,
    rc: i64,
}

impl ArenaMetadata {
    #[inline(always)]
    pub fn inc_rc(&mut self) {
        self.strong_rc += 1;
        self.rc += 1;
        trace!("inc_rc s {} t {}", self.strong_rc, self.rc);
    }

    #[inline(always)]
    pub fn dec_rc(&mut self) {
        self.strong_rc -= 1;
        self.rc -= 1;
        trace!("dec_rc s {} t {}", self.strong_rc, self.rc);
    }

    #[inline(always)]
    pub fn inc_weak(&mut self) {
        self.rc += 1;
        trace!("inc_wk s {} t {}", self.strong_rc, self.rc);
    }

    #[inline(always)]
    pub fn dec_weak(&mut self) {
        self.rc -= 1;
        trace!("dec_wk s {} t {}", self.strong_rc, self.rc);
    }

    unsafe fn push_drop_fn<T>(&mut self, data: *const u8) -> Result<*const Option<DropItem>, UploadError> {
        debug_assert_ne!(null_mut(), self.first_drop_list, "push: drop list not null (1)");
        debug_assert_ne!(null_mut(), self.last_drop_list, "push: drop list not null (2)");

        Ok(match (*self.last_drop_list).push_drop_fn::<T>(data) {
            (DropListWriteResult::ListFull, item) => {
                self.push_next_drop_list()?;
                item
            },
            (DropListWriteResult::ListNotFull, item) => item,
        })
    }

    pub unsafe fn push_custom_drop_fn(&mut self, fun: DropFn, data: *const u8) -> Result<*const Option<DropItem>, UploadError> {
        debug_assert_ne!(null_mut(), self.first_drop_list, "push: drop list not null (3)");
        debug_assert_ne!(null_mut(), self.last_drop_list, "push: drop list not null (4)");

        Ok(match (*self.last_drop_list).push_custom_drop_fn(fun, data) {
            (DropListWriteResult::ListFull, item) => {
                self.push_next_drop_list()?;
                item
            },
            (DropListWriteResult::ListNotFull, item) => item,
        })
    }

    unsafe fn push_next_drop_list(&mut self) -> Result<(), UploadError> {
        let next_drop_list = match self.upload_no_drop(DropList::empty()) {
            Ok(v) => v,
            Err(_) => return Err(UploadError::DropListDoesNotFit),
        };
        debug_assert_ne!(self.last_drop_list, null_mut(), "last drop list not null");
        (*self.last_drop_list).set_next_list(next_drop_list);
        self.last_drop_list = next_drop_list;
        Ok(())
    }

    /// Place item to arena and return a pointer to it, and also add drop function to drop list to drop this

    /// item when there are no remaining `Arena` instances.

    pub unsafe fn upload_auto_drop<T>(&mut self, value: T) -> Result<(*mut T, *const Option<DropItem>), UploadError> {
        let last_block = self.last_block.as_mut().unwrap();
        match last_block.push_copy::<T>(&value) {
            Ok(value_ptr) => {
                std::mem::forget(value);
                let drop_item = self.push_drop_fn::<T>(value_ptr as *const u8)?;
                return Ok((value_ptr, drop_item));
            },
            Err(e) => match e {
                PlacementError::NotEnoughSpaceInBlock => (),
                PlacementError::ItemTooBig => return Err(UploadError::ItemDoesNotFit),
            }
        }

        let mut block = Some(Block::new(self.memory.take_block()));
        std::mem::swap(&mut block, &mut self.last_block);
        let last_block = self.last_block.as_mut().unwrap();
        last_block.set_previous_block(block.unwrap());

        let value_ptr = last_block.push_copy::<T>(&value).ok().expect("fits into subsequent block (1)");
        std::mem::forget(value);
        let drop_item = self.push_drop_fn::<T>(value_ptr as *const u8)?;
        Ok((value_ptr, drop_item))
    }

    /// Place item to arena and return a pointer to it, without adding a drop function.

    pub unsafe fn upload_no_drop<T>(&mut self, value: T) -> Result<*mut T, UploadError> {
        let last_block = self.last_block.as_mut().unwrap();
        match last_block.push_copy::<T>(&value) {
            Ok(value_ptr) => {
                std::mem::forget(value);
                return Ok(value_ptr);
            },
            Err(e) => match e {
                PlacementError::NotEnoughSpaceInBlock => (),
                PlacementError::ItemTooBig => return Err(UploadError::ItemDoesNotFit),
            }
        }

        let mut block = Some(Block::new(self.memory.take_block()));
        std::mem::swap(&mut block, &mut self.last_block);
        let last_block = self.last_block.as_mut().unwrap();
        last_block.set_previous_block(block.unwrap());

        let value_ptr = last_block.push_copy::<T>(&value).ok().expect("fits into subsequent block (2)");
        std::mem::forget(value);
        Ok(value_ptr)
    }

    /// Place a chunk of bytes to arena and return a pointer to the first byte.

    pub unsafe fn upload_no_drop_bytes(&mut self, len: usize, value: impl Iterator<Item=u8>) -> Result<*mut u8, UploadError> {
        let last_block = self.last_block.as_mut().unwrap();
        let (remaining_bytes_for_alignment, aligned_start) = last_block.remaining_bytes_for_alignment::<[u8; 1]>();
        if remaining_bytes_for_alignment >= len as isize {
            return Ok(last_block.upload_bytes_unchecked(aligned_start, len, value));
        }

        if len > last_block.largest_item_size() {
            return Err(UploadError::ItemDoesNotFit);
        }

        let mut block = Some(Block::new(self.memory.take_block()));
        std::mem::swap(&mut block, &mut self.last_block);
        let last_block = self.last_block.as_mut().unwrap();
        last_block.set_previous_block(block.unwrap());

        let (remaining_bytes_for_alignment, aligned_start) = last_block.remaining_bytes_for_alignment::<[u8; 1]>();
        if remaining_bytes_for_alignment >= len as isize {
            return Ok(last_block.upload_bytes_unchecked(aligned_start, len, value));
        }

        unreachable!("upload_no_drop_bytes failed after acquiring the next block")
    }

    pub unsafe fn alloc_no_drop_items_aligned_uninit<T>(&mut self, len: usize, offset_between_items: usize) -> Result<*mut T, UploadError> {
        let last_block = self.last_block.as_mut().unwrap();
        let (remaining_bytes_for_alignment, aligned_start) = last_block.remaining_bytes_for_alignment::<T>();
        let total_array_len = len * offset_between_items;
        if remaining_bytes_for_alignment >= total_array_len as isize {
            return Ok(last_block.upload_bytes_unchecked_uninit(aligned_start, total_array_len) as *mut T);
        }

        if total_array_len > last_block.largest_item_size() {
            return Err(UploadError::ItemDoesNotFit);
        }

        let mut block = Some(Block::new(self.memory.take_block()));
        std::mem::swap(&mut block, &mut self.last_block);
        let last_block = self.last_block.as_mut().unwrap();
        last_block.set_previous_block(block.unwrap());

        let (remaining_bytes_for_alignment, aligned_start) = last_block.remaining_bytes_for_alignment::<T>();
        if remaining_bytes_for_alignment >= total_array_len as isize {
            return Ok(last_block.upload_bytes_unchecked_uninit(aligned_start, total_array_len) as *mut T);
        }

        unreachable!("alloc_no_drop_items_aligned_uninit failed after acquiring the next block")
    }

    pub unsafe fn drop_objects(&mut self) {
        debug_assert_ne!(null_mut(), self.first_drop_list, "drop_objects: drop list not null");
        (*self.first_drop_list).execute_drop_chain();
        self.first_drop_list = null_mut();
        self.last_drop_list = null_mut();
    }

    /// After the call to this function metadata must not be used

    pub unsafe fn reclaim_memory(&mut self) {
        let mut block = None;
        std::mem::swap(&mut block, &mut self.last_block);
        while block.is_some() {
            let (previous_block, data) = block.unwrap().into_previous_block_and_data();
            self.memory.return_block(data);
            block = previous_block;
        }
    }
}

/// A weak `Arena` reference that holds a pointer to valid memory until dropped.

///

/// As long as the original strong `Arena` is alive, this reference can be upgraded to `Arena`.

/// If the `Arena` is no longer alive, the memory can no longer be mutated, and the drop

/// functions for all root objects in the `Arena` are already executed.

///

/// However, if your structure does not need to be dropped (i.e. bunch of bytes), the bytes

/// can be safely accessed as long as you hold the `WeakArena` reference, even if `is_alive` returns `false`.

///

/// The `WeakArena`, like `Arena`, can not be shared between threads.

pub struct WeakArena {
    metadata: *mut ArenaMetadata,
}

/// `Arena` is a memory block container that executes `drop` for your objects when it goes out of scope.

///

/// It can not be used between threads. Create a separate `Arena` for each thread.

///

/// You can use this `Arena` to upload data to memory and receive a pointer to this data.

/// Behind the scenes, the `Arena` will bump the pointer inside the current block, copy

/// your structure into that location, and return this pointer back. If the `Arena` runs out of blocks,

/// it will request new blocks from the main `Memory`.

///

/// You should keep a `WeakArena` reference together with your pointer, and check if it `is_alive`

/// before every pointer access. As long as you have a copy of `WeakArena`, the memory won't be

/// deallocated, however if the `Arena` is dropped and you have uploaded the structure with `_auto_drop`

/// function, the structure is dropped as well and should not be used.

///

/// `Arena` and `WeakArena` are not intended to be used directly, instead, you should create

/// wrappers around them for your structures. Inspect `List`, `UStr` or `N` structures for the example code.

/// You will find that these structures do not keep `Arena` inside, because doing that would produce

/// scenario where the `Arena` is never dropped when the structures are nested.

///

/// When all `WeakArena` and `Arena` instances are gone, the memory blocks are returned back to `Memory`.

pub struct Arena {
    metadata: *mut ArenaMetadata,
}

impl Arena {
    pub fn new(memory: &Memory) -> Result<Arena, UploadError> {
        let mut memory = memory.clone();
        let mut block = Block::new(memory.take_block());
        let drop_list = unsafe { block.push(DropList::empty()) }.map_err(|_| UploadError::DropListDoesNotFit)?;
        let metadata = unsafe { block.push(ArenaMetadata {
            memory,
            last_block: None,
            first_drop_list: drop_list,
            last_drop_list: drop_list,
            strong_rc: 1,
            rc: 1
        }) }.map_err(|_| UploadError::MetadataDoesNotFit)?;
        unsafe { (*metadata).last_block = Some(block) };

        Ok(Arena {
            metadata,
        })
    }

    #[inline(always)]
    unsafe fn md(&self) -> &mut ArenaMetadata {
        std::mem::transmute::<*mut ArenaMetadata, &mut ArenaMetadata>(self.metadata)
    }

    /// Place item to arena and return a pointer to it, and also add drop function to drop list to drop this

    /// item when there are no remaining `Arena` instances. Result also contains a pointer to drop item that is valid while arena is alive.

    #[inline(always)]
    pub unsafe fn upload_auto_drop<T>(&self, value: T) -> Result<(*mut T, *const Option<DropItem>), UploadError> {
        self.md().upload_auto_drop::<T>(value)
    }

    /// Place item to arena and return a pointer to it, without adding a drop function.

    #[inline(always)]
    pub unsafe fn upload_no_drop<T>(&self, value: T) -> Result<*mut T, UploadError> {
        self.md().upload_no_drop::<T>(value)
    }

    /// Place a chunk of bytes to arena and return a pointer to the first byte.

    #[inline(always)]
    pub unsafe fn upload_no_drop_bytes(&self, len: usize, value: impl Iterator<Item=u8>) -> Result<*mut u8, UploadError> {
        self.md().upload_no_drop_bytes(len, value)
    }

    /// Place uninitialized items to arena and return a pointer to the first item. This ensures the alignment of the first item.

    #[inline(always)]
    pub unsafe fn alloc_no_drop_items_aligned_uninit<T>(&self, len: usize, offset_between_items: usize) -> Result<*mut T, UploadError> {
        self.md().alloc_no_drop_items_aligned_uninit::<T>(len, offset_between_items)
    }

    /// Place custom drop function that will be executed on arena drop.

    ///

    /// The data pointer should point to a memory location inside the arena.

    #[inline(always)]
    pub unsafe fn push_custom_drop_fn(&self, fun: DropFn, data: *const u8) -> Result<*const Option<DropItem>, UploadError> {
        self.md().push_custom_drop_fn(fun, data)
    }

    /// Clone as `WeakArena`.

    pub fn to_weak_arena(&self) -> WeakArena {
        trace!("split weak arena");
        unsafe { self.md().inc_weak() };
        WeakArena {
            metadata: self.metadata,
        }
    }
}

impl WeakArena {
    /// Returns true if drop functions for the arena structures were not yet executed (the `Arena` is not dropped).

    #[inline(always)]
    pub fn is_alive(&self) -> bool {
        unsafe { self.md().strong_rc > 0 }
    }

    #[inline(always)]
    unsafe fn md(&self) -> &mut ArenaMetadata {
        std::mem::transmute::<*mut ArenaMetadata, &mut ArenaMetadata>(self.metadata)
    }

    /// Place item to arena and return a pointer to it, and also add drop function to drop list to drop this

    /// item when there are no remaining `Arena` instances.

    #[inline(always)]
    pub unsafe fn upload_auto_drop<T>(&self, value: T) -> Result<(*mut T, *const Option<DropItem>), UploadError> {
        if self.is_alive() {
            Ok(self.md().upload_auto_drop::<T>(value)?)
        } else {
            Err(UploadError::ArenaIsNotAlive)
        }
    }

    /// Place item to arena and return a pointer to it, without adding a drop function.

    #[inline(always)]
    pub unsafe fn upload_no_drop<T>(&self, value: T) -> Result<*mut T, UploadError> {
        if self.is_alive() {
            Ok(self.md().upload_no_drop::<T>(value)?)
        } else {
            Err(UploadError::ArenaIsNotAlive)
        }
    }

    /// Place a chunk of bytes to arena and return a pointer to the first byte.

    #[inline(always)]
    pub unsafe fn upload_no_drop_bytes(&self, len: usize, value: impl Iterator<Item=u8>) -> Result<*mut u8, UploadError> {
        if self.is_alive() {
            Ok(self.md().upload_no_drop_bytes(len, value)?)
        } else {
            Err(UploadError::ArenaIsNotAlive)
        }
    }

    /// Try to upgrade `WeakArena` to `Arena`.

    pub fn arena(&self) -> Option<Arena> {
        if self.is_alive() {
            trace!("upgrade weak to strong arena");
            unsafe { self.md().inc_rc() };
            Some(Arena {
                metadata: self.metadata,
            })
        } else {
            None
        }
    }
}

impl Clone for Arena {
    fn clone(&self) -> Self {
        trace!("clone arena");
        let metadata = self.metadata;
        unsafe { (*metadata).inc_rc(); }
        Arena {
            metadata,
        }
    }
}

impl Clone for WeakArena {
    fn clone(&self) -> Self {
        trace!("clone weak");
        let metadata = self.metadata;
        unsafe { (*metadata).inc_weak(); }
        WeakArena {
            metadata,
        }
    }
}

impl Drop for Arena {
    fn drop(&mut self) {
        trace!("drop arena");

        let metadata = unsafe { self.md() };
        (*metadata).dec_rc();

        if (*metadata).strong_rc == 0 {
            trace!("drop arena objects");
            unsafe { (*metadata).drop_objects() };
        }

        if (*metadata).rc == 0 {
            trace!("reclaim memory");
            unsafe { metadata.reclaim_memory() };
            // this should be the last use of this metadata

        }
    }
}

impl Drop for WeakArena {
    fn drop(&mut self) {
        trace!("drop weak arena");

        let metadata = unsafe { self.md() };
        (*metadata).dec_weak();

        if (*metadata).rc == 0 {
            trace!("reclaim memory");
            unsafe { metadata.reclaim_memory() };
            // this should be the last use of this metadata

        }
    }
}

#[cfg(test)]
mod arena_tests {
    use crate::{Memory, Arena, N};
    use crate::dropflag::DropFlag;
    use std::cell::RefCell;

    #[derive(Debug)]
    struct Compact {
        value: DropFlag<i32>,
    }

    impl PartialEq for Compact {
        fn eq(&self, other: &Self) -> bool {
            ((*self.value).borrow()).eq(&(*other.value).borrow())
        }
    }

    impl Eq for Compact {}

    impl Drop for Compact {
        fn drop(&mut self) {
            *self.value.borrow_mut() -= 1;
        }
    }

    #[allow(dead_code)]
    struct Nested<T> {
        dropflag: DropFlag<i32>,
        inner: T,
    }

    impl<T> Drop for Nested<T> {
        fn drop(&mut self) {
            *self.dropflag.borrow_mut() -= 1;
        }
    }

    #[test]
    fn value_can_not_be_used_when_arena_goes_out_of_scope() {
        let flag = DropFlag::new(RefCell::new(1));
        let mut obj = {
            let mem = Memory::new();
            let arena = Arena::new(&mem).unwrap();
            let mut obj = N::new(&arena, Compact { value: flag.clone() }).unwrap();

            assert_eq!(1, *(*flag).borrow(), "drop was not called");
            assert_ne!(None, obj.val(), "value can be accessed");
            assert_ne!(None, obj.var(), "value can be accessed");

            obj
        };

        assert_eq!(0, *(*flag).borrow(), "drop was called");
        assert_eq!(None, obj.val(), "value can not be accessed");
        assert_eq!(None, obj.var(), "value can not be accessed");
    }

    #[test]
    fn nested_objects_are_dropped_properly() {
        let f1 = DropFlag::new(RefCell::new(1));
        let f2 = DropFlag::new(RefCell::new(1));

        let mem = Memory::new();
        let _obj = {
            let arena = Arena::new(&mem).unwrap();
            let obj = N::new(&arena,
                             Nested {
                                 dropflag: f1.clone(),
                                 inner: N::new(&arena,
                                               Nested {
                                                   dropflag: f2.clone(),
                                                   inner: ()
                                               }
                                 ).unwrap()
                             }
            ).unwrap();

            assert_eq!(1, *(*f1).borrow(), "drop was not called");
            assert_eq!(1, *(*f2).borrow(), "drop was not called");
            obj
        };

        assert_eq!(0, *(*f1).borrow(), "drop was called");
        assert_eq!(0, *(*f2).borrow(), "drop was called");
    }
}