Skip to main content

clr_assembler/program/
pool.rs

1use std::collections::HashMap;
2
3/// CLR constant pool for storing various metadata.
4#[derive(Debug, Clone, Default)]
5pub struct ClrConstantPool {
6    /// String pool (#Strings heap).
7    pub strings: HashMap<String, u32>,
8    /// Binary large object pool (#Blob heap).
9    pub blobs: HashMap<Vec<u8>, u32>,
10    /// GUID pool (#GUID heap).
11    pub guids: HashMap<[u8; 16], u32>,
12    /// User string pool (#US heap).
13    pub user_strings: HashMap<String, u32>,
14}
15
16impl ClrConstantPool {
17    /// Creates a new empty constant pool.
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Adds a string to the string pool and returns its index.
23    pub fn add_string(&mut self, s: String) -> u32 {
24        let next_index = self.strings.len() as u32;
25        *self.strings.entry(s).or_insert(next_index)
26    }
27
28    /// Adds a binary blob to the blob pool and returns its index.
29    pub fn add_blob(&mut self, b: Vec<u8>) -> u32 {
30        let next_index = self.blobs.len() as u32;
31        *self.blobs.entry(b).or_insert(next_index)
32    }
33
34    /// Adds a GUID to the GUID pool and returns its index.
35    pub fn add_guid(&mut self, g: [u8; 16]) -> u32 {
36        let next_index = self.guids.len() as u32;
37        *self.guids.entry(g).or_insert(next_index)
38    }
39
40    /// Adds a user string to the user string pool and returns its index.
41    pub fn add_user_string(&mut self, s: String) -> u32 {
42        let next_index = self.user_strings.len() as u32;
43        *self.user_strings.entry(s).or_insert(next_index)
44    }
45}