Skip to content

Commit cc54b11

Browse files
authored
allign the allocations to the given layout.align() (#60)
* allign the allocations to the given layout.align()
1 parent 0af3a46 commit cc54b11

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

src/alloc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ unsafe impl GlobalAlloc for RedisAlloc {
1212
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
1313
let use_redis = USE_REDIS_ALLOC.load(SeqCst);
1414
if use_redis {
15-
return raw::RedisModule_Alloc.unwrap()(layout.size()) as *mut u8;
15+
// complete the requested size to be aligned with the requested layout.align()
16+
let size = (layout.size() + layout.align() - 1) & (!(layout.align() - 1));
17+
return raw::RedisModule_Alloc.unwrap()(size) as *mut u8;
1618
}
1719
System.alloc(layout)
1820
}

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Context {
177177
raw::replicate_verbatim(self.ctx);
178178
}
179179

180-
pub fn create_string(&self, s: &str) -> RedisString{
180+
pub fn create_string(&self, s: &str) -> RedisString {
181181
RedisString::create(self.ctx, s)
182182
}
183183
}

src/key.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ fn read_key(key: *mut raw::RedisModuleKey) -> Result<String, Utf8Error> {
240240
)
241241
}
242242

243-
fn hash_get_key(ctx: *mut raw::RedisModuleCtx, key: *mut raw::RedisModuleKey, field: &str) -> Option<RedisString> {
243+
fn hash_get_key(
244+
ctx: *mut raw::RedisModuleCtx,
245+
key: *mut raw::RedisModuleKey,
246+
field: &str,
247+
) -> Option<RedisString> {
244248
let res = raw::hash_get(key, field);
245249
if res.is_null() {
246250
None

src/raw.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,27 @@ pub fn string_dma(key: *mut RedisModuleKey, len: *mut size_t, mode: KeyMode) ->
239239
pub fn hash_get(key: *mut RedisModuleKey, field: &str) -> *mut RedisModuleString {
240240
let res: *mut RedisModuleString = ptr::null_mut();
241241
unsafe {
242-
RedisModule_HashGet.unwrap()(key, REDISMODULE_HASH_CFIELDS as i32, CString::new(field).unwrap().as_ptr(), &res, 0);
242+
RedisModule_HashGet.unwrap()(
243+
key,
244+
REDISMODULE_HASH_CFIELDS as i32,
245+
CString::new(field).unwrap().as_ptr(),
246+
&res,
247+
0,
248+
);
243249
}
244250
res
245251
}
246252

247253
pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleString) -> Status {
248254
unsafe {
249-
RedisModule_HashSet.unwrap()(key, REDISMODULE_HASH_CFIELDS as i32, CString::new(field).unwrap().as_ptr(), value, 0).into()
255+
RedisModule_HashSet.unwrap()(
256+
key,
257+
REDISMODULE_HASH_CFIELDS as i32,
258+
CString::new(field).unwrap().as_ptr(),
259+
value,
260+
0,
261+
)
262+
.into()
250263
}
251264
}
252265

@@ -334,6 +347,12 @@ pub fn save_unsigned(rdb: *mut RedisModuleIO, val: u64) {
334347
unsafe { RedisModule_SaveUnsigned.unwrap()(rdb, val) };
335348
}
336349

337-
pub fn string_append_buffer(ctx: *mut RedisModuleCtx, s: *mut RedisModuleString, buff: &str) -> Status {
338-
unsafe { RedisModule_StringAppendBuffer.unwrap()(ctx, s, buff.as_ptr() as *mut i8, buff.len()).into() }
339-
}
350+
pub fn string_append_buffer(
351+
ctx: *mut RedisModuleCtx,
352+
s: *mut RedisModuleString,
353+
buff: &str,
354+
) -> Status {
355+
unsafe {
356+
RedisModule_StringAppendBuffer.unwrap()(ctx, s, buff.as_ptr() as *mut i8, buff.len()).into()
357+
}
358+
}

src/redismodule.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ pub struct RedisString {
7272

7373
impl RedisString {
7474
pub fn new(ctx: *mut raw::RedisModuleCtx, inner: *mut raw::RedisModuleString) -> RedisString {
75-
RedisString{
76-
ctx,
77-
inner
78-
}
75+
RedisString { ctx, inner }
7976
}
8077

8178
pub fn create(ctx: *mut raw::RedisModuleCtx, s: &str) -> RedisString {
@@ -93,7 +90,7 @@ impl RedisString {
9390
}
9491

9592
pub fn append(&mut self, s: &str) -> raw::Status {
96-
raw::string_append_buffer(self.ctx, self.inner, s)
93+
raw::string_append_buffer(self.ctx, self.inner, s)
9794
}
9895

9996
pub fn len(&self) -> usize {

0 commit comments

Comments
 (0)