Skip to content

Commit fb14d03

Browse files
sisciagavrie
authored andcommitted
complete migration to better redis allocator
1 parent 45d0180 commit fb14d03

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

src/alloc.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::alloc::{GlobalAlloc, Layout};
22
use std::os::raw::c_void;
3-
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
43

54
use crate::raw;
65

76
pub struct RedisAlloc;
87

9-
static USE_REDIS_ALLOC: AtomicBool = AtomicBool::new(false);
10-
118
unsafe impl GlobalAlloc for RedisAlloc {
129
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
1310
let size = (layout.size() + layout.align() - 1) & (!(layout.align() - 1));
@@ -18,8 +15,3 @@ unsafe impl GlobalAlloc for RedisAlloc {
1815
return raw::RedisModule_Free.unwrap()(ptr as *mut c_void);
1916
}
2017
}
21-
22-
pub fn use_redis_alloc() {
23-
USE_REDIS_ALLOC.store(true, SeqCst);
24-
eprintln!("Now using Redis allocator");
25-
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod key;
2828
pub use crate::context::Context;
2929
pub use crate::redismodule::*;
3030

31+
#[cfg(not(test))]
3132
#[global_allocator]
3233
static ALLOC: crate::alloc::RedisAlloc = crate::alloc::RedisAlloc;
3334

src/macros.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,16 @@ macro_rules! redis_module {
9595
dest = dest.add(1);
9696
}
9797
}
98-
// We use an explicit block here to make sure all memory allocated before we
99-
// switch to the Redis allocator will be out of scope and thus deallocated.
100-
let module_version = $module_version as c_int;
98+
// We use an explicit block here to make sure all memory allocated before we
99+
// switch to the Redis allocator will be out of scope and thus deallocated.
100+
let module_version = $module_version as c_int;
101101

102-
if unsafe { raw::Export_RedisModule_Init(
103-
ctx,
104-
name_buffer.as_ptr() as *const std::os::raw::c_char,
105-
module_version,
106-
raw::REDISMODULE_APIVER_1 as c_int,
107-
) } == raw::Status::Err as c_int { return raw::Status::Err as c_int; }
108-
109-
if true {
110-
redis_module::alloc::use_redis_alloc();
111-
} else {
112-
eprintln!("*** NOT USING Redis allocator ***");
113-
}
102+
if unsafe { raw::Export_RedisModule_Init(
103+
ctx,
104+
name_buffer.as_ptr() as *const std::os::raw::c_char,
105+
module_version,
106+
raw::REDISMODULE_APIVER_1 as c_int,
107+
) } == raw::Status::Err as c_int { return raw::Status::Err as c_int; }
114108

115109
$(
116110
if $init_func(ctx) == raw::Status::Err as c_int {

0 commit comments

Comments
 (0)