ic-stable-structures 0.7.2

A collection of data structures for fearless canister upgrades.
Documentation
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
use crate::{
    read_struct,
    types::{Address, Bytes, NULL},
    write_struct, Memory,
};

const ALLOCATOR_LAYOUT_VERSION: u8 = 1;
const CHUNK_LAYOUT_VERSION: u8 = 1;

const ALLOCATOR_MAGIC: &[u8; 3] = b"BTA"; // btree allocator
const CHUNK_MAGIC: &[u8; 3] = b"CHK"; // btree allocator

/// A free list constant-size chunk allocator.
///
/// The allocator allocates chunks of size `allocation_size` from the given `memory`.
///
/// # Properties
///
/// * The allocator tries to minimize its memory footprint, growing the memory in
///   size only when all the available memory is allocated.
///
/// * The allocator makes no assumptions on the size of the memory and will
///   continue growing so long as the provided `memory` allows it.
///
/// The allocator divides the memory into "chunks" of equal size. Each chunk contains:
///     a) A `ChunkHeader` with metadata about the chunk.
///     b) A blob of length `allocation_size` that can be used freely by the user.
///
/// # Assumptions:
///
/// * The given memory is not being used by any other data structure.
pub struct Allocator<M: Memory> {
    // The address in memory where the `AllocatorHeader` is stored.
    header_addr: Address,

    // The size of the chunk to allocate in bytes.
    allocation_size: Bytes,

    // The number of chunks currently allocated.
    num_allocated_chunks: u64,

    // A linked list of unallocated chunks.
    free_list_head: Address,

    memory: M,
}

#[repr(C, packed)]
#[derive(PartialEq, Debug)]
struct AllocatorHeader {
    magic: [u8; 3],
    version: u8,
    // Empty space to memory-align the following fields.
    _alignment: [u8; 4],
    allocation_size: Bytes,
    num_allocated_chunks: u64,
    free_list_head: Address,
    // Additional space reserved to add new fields without breaking backward-compatibility.
    _buffer: [u8; 16],
}

impl AllocatorHeader {
    fn size() -> Bytes {
        Bytes::from(core::mem::size_of::<Self>() as u64)
    }
}

impl<M: Memory> Allocator<M> {
    /// Initialize an allocator and store it in address `addr`.
    ///
    /// The allocator assumes that all memory from `addr` onwards is free.
    ///
    /// When initialized, the allocator has the following memory layout:
    ///
    /// [   AllocatorHeader       | ChunkHeader ]
    ///      ..   free_list_head  ↑      next
    ///                |__________|       |____ NULL
    ///
    pub fn new(memory: M, addr: Address, allocation_size: Bytes) -> Self {
        let mut allocator = Self {
            header_addr: addr,
            allocation_size,
            num_allocated_chunks: 0,
            free_list_head: NULL, // Will be set in `allocator.clear()` below.
            memory,
        };

        allocator.clear();
        allocator
    }

    /// Deallocate all allocated chunks.
    pub fn clear(&mut self) {
        // Create the initial memory chunk and save it directly after the allocator's header.
        self.free_list_head = self.header_addr + AllocatorHeader::size();
        let chunk = ChunkHeader::null();
        chunk.save(self.free_list_head, &self.memory);

        self.num_allocated_chunks = 0;

        self.save()
    }

    /// Load an allocator from memory at the given `addr`.
    pub fn load(memory: M, addr: Address) -> Self {
        let header: AllocatorHeader = read_struct(addr, &memory);
        assert_eq!(&header.magic, ALLOCATOR_MAGIC, "Bad magic.");
        assert_eq!(
            header.version, ALLOCATOR_LAYOUT_VERSION,
            "Unsupported version."
        );

        Self {
            header_addr: addr,
            allocation_size: header.allocation_size,
            num_allocated_chunks: header.num_allocated_chunks,
            free_list_head: header.free_list_head,
            memory,
        }
    }

    /// Allocates a new chunk from memory with size `allocation_size`.
    ///
    /// Internally, there are two cases:
    ///
    /// 1) The list of free chunks (`free_list_head`) has only one element.
    ///    This case happens when we initialize a new allocator, or when
    ///    all of the previously allocated chunks are still in use.
    ///
    ///    Example memory layout:
    ///
    ///    [   AllocatorHeader       | ChunkHeader ]
    ///         ..   free_list_head  ↑      next
    ///                   |__________↑       |____ NULL
    ///
    ///    In this case, the chunk in the free list is allocated to the user
    ///    and a new `ChunkHeader` is appended to the allocator's memory,
    ///    growing the memory if necessary.
    ///
    ///    [   AllocatorHeader       | ChunkHeader | ... | ChunkHeader2 ]
    ///         ..   free_list_head      (allocated)     ↑      next
    ///                   |______________________________↑       |____ NULL
    ///
    /// 2) The list of free chunks (`free_list_head`) has more than one element.
    ///
    ///    Example memory layout:
    ///
    ///    [   AllocatorHeader       | ChunkHeader1 | ... | ChunkHeader2 ]
    ///         ..   free_list_head  ↑       next         ↑       next
    ///                   |__________↑        |___________↑         |____ NULL
    ///
    ///    In this case, the first chunk in the free list is allocated to the
    ///    user, and the head of the list is updated to point to the next free
    ///    block.
    ///
    ///    [   AllocatorHeader       | ChunkHeader1 | ... | ChunkHeader2 ]
    ///         ..   free_list_head      (allocated)      ↑       next
    ///                   |_______________________________↑         |____ NULL
    ///
    pub fn allocate(&mut self) -> Address {
        // Get the next available chunk.
        let chunk_addr = self.free_list_head;
        let mut chunk = ChunkHeader::load(chunk_addr, &self.memory);

        // The available chunk must not be allocated.
        assert!(
            !chunk.allocated,
            "Attempting to allocate an already allocated chunk."
        );

        // Allocate the chunk.
        chunk.allocated = true;
        chunk.save(chunk_addr, &self.memory);

        // Update the head of the free list.
        if chunk.next != NULL {
            // The next chunk becomes the new head of the list.
            self.free_list_head = chunk.next;
        } else {
            // There is no next chunk. Shift everything by chunk size.
            self.free_list_head += self.chunk_size();

            // Write new chunk to that location.
            ChunkHeader::null().save(self.free_list_head, &self.memory);
        }

        self.num_allocated_chunks += 1;
        self.save();

        // Return the chunk's address offset by the chunk's header.
        chunk_addr + ChunkHeader::size()
    }

    /// Deallocates a previously allocated chunk.
    pub fn deallocate(&mut self, address: Address) {
        let chunk_addr = address - ChunkHeader::size();

        let mut chunk = ChunkHeader::load(chunk_addr, &self.memory);

        assert!(chunk.allocated);

        chunk.allocated = false;
        chunk.next = self.free_list_head;

        chunk.save(chunk_addr, &self.memory);

        self.free_list_head = chunk_addr;

        self.num_allocated_chunks -= 1;
        self.save();
    }

    /// Saves the allocator to memory.
    pub fn save(&self) {
        let header = AllocatorHeader {
            magic: *ALLOCATOR_MAGIC,
            version: ALLOCATOR_LAYOUT_VERSION,
            _alignment: [0; 4],
            num_allocated_chunks: self.num_allocated_chunks,
            allocation_size: self.allocation_size,
            free_list_head: self.free_list_head,
            _buffer: [0; 16],
        };

        write_struct(&header, self.header_addr, &self.memory);
    }

    #[cfg(test)]
    pub fn num_allocated_chunks(&self) -> u64 {
        self.num_allocated_chunks
    }

    // The full size of a chunk, which is the size of the header + the `allocation_size` that's
    // available to the user.
    fn chunk_size(&self) -> Bytes {
        self.allocation_size + ChunkHeader::size()
    }

    /// Destroys the allocator and returns the underlying memory.
    #[inline]
    pub fn into_memory(self) -> M {
        self.memory
    }

    /// Returns a reference to the underlying memory.
    #[inline]
    pub fn memory(&self) -> &M {
        &self.memory
    }
}

#[derive(Debug)]
#[repr(C, packed)]
struct ChunkHeader {
    magic: [u8; 3],
    version: u8,
    allocated: bool,
    // Empty space to memory-align the following fields.
    _alignment: [u8; 3],
    next: Address,
}

impl ChunkHeader {
    // Initializes an unallocated chunk that doesn't point to another chunk.
    fn null() -> Self {
        Self {
            magic: *CHUNK_MAGIC,
            version: CHUNK_LAYOUT_VERSION,
            allocated: false,
            _alignment: [0; 3],
            next: NULL,
        }
    }

    fn save<M: Memory>(&self, address: Address, memory: &M) {
        write_struct(self, address, memory);
    }

    fn load<M: Memory>(address: Address, memory: &M) -> Self {
        let header: ChunkHeader = read_struct(address, memory);
        assert_eq!(&header.magic, CHUNK_MAGIC, "Bad magic.");
        assert_eq!(header.version, CHUNK_LAYOUT_VERSION, "Unsupported version.");

        header
    }

    fn size() -> Bytes {
        Bytes::from(core::mem::size_of::<Self>() as u64)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{Memory, WASM_PAGE_SIZE};
    use std::cell::RefCell;
    use std::rc::Rc;

    fn make_memory() -> Rc<RefCell<Vec<u8>>> {
        Rc::new(RefCell::new(Vec::new()))
    }

    #[test]
    fn new_and_load() {
        let mem = make_memory();
        let allocator_addr = Address::from(0);
        let allocation_size = Bytes::from(16u64);

        // Create a new allocator.
        Allocator::new(mem.clone(), allocator_addr, allocation_size);

        // Load it from memory.
        let allocator = Allocator::load(mem.clone(), allocator_addr);

        assert_eq!(allocator.allocation_size, allocation_size);
        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size()
        );

        // Load the first memory chunk.
        let chunk = ChunkHeader::load(allocator.free_list_head, &mem);
        assert_eq!(chunk.next, NULL);
    }

    #[test]
    fn allocate() {
        let mem = make_memory();
        let allocation_size = Bytes::from(16u64);

        let mut allocator = Allocator::new(mem, Address::from(0), allocation_size);

        let original_free_list_head = allocator.free_list_head;

        // Each allocation should push the `head` by `chunk_size`.
        for i in 1..=3 {
            allocator.allocate();
            assert_eq!(
                allocator.free_list_head,
                original_free_list_head + allocator.chunk_size() * i
            );
        }
    }

    #[test]
    fn allocate_large() {
        // Allocate large chunks to verify that we are growing the memory.
        let mem = make_memory();
        assert_eq!(mem.size(), 0);
        let allocator_addr = Address::from(0);
        let allocation_size = Bytes::from(WASM_PAGE_SIZE);

        let mut allocator = Allocator::new(mem.clone(), allocator_addr, allocation_size);
        assert_eq!(mem.size(), 1);

        allocator.allocate();
        assert_eq!(mem.size(), 2);

        allocator.allocate();
        assert_eq!(mem.size(), 3);

        allocator.allocate();
        assert_eq!(mem.size(), 4);

        // Each allocation should push the `head` by `chunk_size`.
        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size() + allocator.chunk_size() * 3
        );
        assert_eq!(allocator.num_allocated_chunks, 3);

        // Load and reload to verify that the data is the same.
        let allocator = Allocator::load(mem, Address::from(0));
        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size() + allocator.chunk_size() * 3
        );
        assert_eq!(allocator.num_allocated_chunks, 3);
    }

    #[test]
    fn allocate_then_deallocate() {
        let mem = make_memory();
        let allocation_size = Bytes::from(16u64);
        let allocator_addr = Address::from(0);

        let mut allocator = Allocator::new(mem.clone(), allocator_addr, allocation_size);

        let chunk_addr = allocator.allocate();

        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size() + allocator.chunk_size()
        );
        allocator.deallocate(chunk_addr);
        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size()
        );
        assert_eq!(allocator.num_allocated_chunks, 0);

        // Load and reload to verify that the data is the same.
        let allocator = Allocator::load(mem, allocator_addr);
        assert_eq!(
            allocator.free_list_head,
            allocator_addr + AllocatorHeader::size()
        );
        assert_eq!(allocator.num_allocated_chunks, 0);
    }

    #[test]
    fn clear_deallocates_all_allocated_chunks() {
        let mem = make_memory();
        let allocation_size = Bytes::from(16u64);
        let allocator_addr = Address::from(0);

        let mut allocator = Allocator::new(mem.clone(), allocator_addr, allocation_size);

        allocator.allocate();
        allocator.allocate();

        assert_eq!(
            allocator.free_list_head,
            allocator_addr
                + AllocatorHeader::size()
                + allocator.chunk_size()
                + allocator.chunk_size()
        );
        allocator.clear();

        let header_actual: AllocatorHeader = read_struct(allocator_addr, &mem);

        Allocator::new(mem.clone(), allocator_addr, allocation_size);

        let header_expected: AllocatorHeader = read_struct(allocator_addr, &mem);

        assert_eq!(header_actual, header_expected);
    }

    #[test]
    fn allocate_deallocate_2() {
        let mem = make_memory();
        let allocation_size = Bytes::from(16u64);

        let mut allocator = Allocator::new(mem, Address::from(0), allocation_size);

        let _chunk_addr_1 = allocator.allocate();
        let chunk_addr_2 = allocator.allocate();

        assert_eq!(allocator.free_list_head, chunk_addr_2 + allocation_size);
        allocator.deallocate(chunk_addr_2);
        assert_eq!(allocator.free_list_head, chunk_addr_2 - ChunkHeader::size());

        let chunk_addr_3 = allocator.allocate();
        assert_eq!(chunk_addr_3, chunk_addr_2);
        assert_eq!(allocator.free_list_head, chunk_addr_3 + allocation_size);
    }

    #[test]
    #[should_panic]
    fn deallocate_free_chunk() {
        let mem = make_memory();
        let allocation_size: u64 = 16;

        let mut allocator = Allocator::new(mem, Address::from(0), Bytes::from(allocation_size));

        let chunk_addr = allocator.allocate();
        allocator.deallocate(chunk_addr);

        // Try deallocating the free chunk - should panic.
        allocator.deallocate(chunk_addr);
    }
}