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
//! Synchronized memory pools.

pub use self::owned::{SliceBox, SlicePool, Sliceable};
use std::sync::Mutex;
use Chunk;

mod owned;

enum Order {
  Preceding,
  Following,
}

/// A thread-safe chunk chain.
struct ChunkChain(Mutex<Vec<Chunk>>);

impl ChunkChain {
  pub fn new(size: usize) -> Self {
    ChunkChain(Mutex::new(vec![Chunk::new(size)]))
  }

  pub fn allocate(&self, size: usize) -> Option<Chunk> {
    let mut chunks = self.0.lock().expect("poisoned chain");

    // Find a chunk with the least amount of memory required
    let (index, _) = chunks
      .iter()
      .enumerate()
      .filter(|(_, chunk)| chunk.free && chunk.size >= size)
      .min_by_key(|(_, chunk)| chunk.size)?;

    // Determine whether there is any memory surplus
    let delta = chunks[index].size - size;

    if delta > 0 {
      // Deduct the left over memory from the allocation
      chunks[index].size -= delta;

      if Self::has_free_adjacent(&chunks, index, Order::Preceding) {
        // Increase the size of the preceding chunk
        chunks[index - 1].size += delta;

        // Shift the offset of the allocated chunk
        chunks[index].offset += delta;
      } else if Self::has_free_adjacent(&chunks, index, Order::Following) {
        // Update the size and offset of the next chunk
        chunks[index + 1].offset -= delta;
        chunks[index + 1].size += delta;
      } else {
        // Insert a new chunk representing the surplus memory
        let offset = chunks[index].offset + size;
        chunks.insert(index + 1, Chunk::with_offset(delta, offset));
        chunks[index].free = false;
      }
    } else {
      // The allocation covers a single chunk
      chunks[index].free = false;
    }

    Some(chunks[index])
  }

  pub fn release(&self, offset: usize) {
    let mut chunks = self.0.lock().expect("poisoned chain");

    let index = chunks
      .binary_search_by_key(&offset, |chunk| chunk.offset)
      .expect("releasing chunk");
    let size = chunks[index].size;

    if Self::has_free_adjacent(&chunks, index, Order::Preceding) {
      // Increase the preceding chunk's size
      chunks[index - 1].size += size;
    } else if Self::has_free_adjacent(&chunks, index, Order::Following) {
      // Increase the extent of the next chunk
      chunks[index + 1].offset -= size;
      chunks[index + 1].size += size;
    } else {
      // No free adjacent chunks, simply mark this one as free
      chunks[index].free = true;
      return;
    }

    chunks.remove(index);
  }

  fn has_free_adjacent(chunks: &[Chunk], index: usize, order: Order) -> bool {
    match order {
      Order::Preceding => index > 0 && chunks[index - 1].free,
      Order::Following => index + 1 < chunks.len() && chunks[index + 1].free,
    }
  }
}