Skip to content

Commit 092e276

Browse files
committed
Use Redis allocator for native types
1 parent e56ed89 commit 092e276

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

examples/data_type.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ impl Command for AllocSetCommand {
5050
// 1. Open key [OK]
5151
// 2. Allocate data [OK]
5252
// 3. Set the key to the data [OK]
53-
// 4. Activate custom allocator and compare Redis memory usage
53+
// 4. Activate custom allocator and compare Redis memory usage [OK]
54+
// 5. Handle deallocation of existing value
5455

5556
let key = r.open_key_writable(key);
5657
key.check_type(&MY_TYPE)?;
5758

59+
// TODO: If there is an existing value, reuse it or deallocate it.
60+
5861
let my = Box::into_raw(Box::new(
5962
MyType {
6063
data: "A".repeat(size as usize)
@@ -176,6 +179,9 @@ pub extern "C" fn AllocDelCommand_Redis(
176179
fn module_on_load(ctx: *mut raw::RedisModuleCtx) -> Result<(), &'static str> {
177180
module_init(ctx, MODULE_NAME, MODULE_VERSION)?;
178181

182+
// TODO: Call this from inside module_init
183+
redismodule::use_redis_alloc();
184+
179185
MY_TYPE.create_data_type(ctx, "mytype123")?;
180186

181187
AllocSetCommand::create(ctx)?;

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ unsafe impl GlobalAlloc for RedisAlloc {
6767

6868
#[global_allocator]
6969
static ALLOC: RedisAlloc = RedisAlloc;
70+
71+
pub fn use_redis_alloc() {
72+
eprintln!("Using Redis allocator");
73+
USE_REDIS_ALLOC.store(true, SeqCst);
74+
}
7075
////////////////////////////////////////////////////////////
7176

7277

0 commit comments

Comments
 (0)