Skip to content

Commit 53cc7e5

Browse files
author
michalbiesek
committed
Use pmem memset/memcpy function in sds implementation
1 parent 99c1740 commit 53cc7e5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/sds.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const char *SDS_NOINIT = "SDS_NOINIT";
4444
#define SDS_GENERAL_VARIANT 0
4545
#define SDS_DRAM_VARIANT 1
4646

47+
#define SDS_ON_PMEM 0
48+
#define SDS_ON_DRAM 1
49+
4750
static inline int sdsHdrSize(char type) {
4851
switch(type&SDS_TYPE_MASK) {
4952
case SDS_TYPE_5:
@@ -93,18 +96,24 @@ static sds _sdsnewlen(const void *init, size_t initlen, int on_dram) {
9396
void *sh;
9497
sds s;
9598
char type = sdsReqType(initlen);
99+
int sds_place;
100+
96101
/* Empty strings are usually created in order to append. Use type 8
97102
* since type 5 is not good at this. */
98103
if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
99104
int hdrlen = sdsHdrSize(type);
100105
unsigned char *fp; /* flags pointer. */
106+
if ((on_dram == SDS_GENERAL_VARIANT) && (hdrlen+initlen+1) >= zmalloc_get_threshold()) {
107+
sds_place = SDS_ON_PMEM;
108+
} else {
109+
sds_place = SDS_ON_DRAM;
110+
}
101111

102-
sh = (on_dram == SDS_DRAM_VARIANT) ? s_dram_malloc(hdrlen+initlen+1)
103-
: s_malloc(hdrlen+initlen+1);
112+
sh = (sds_place == SDS_ON_DRAM) ? s_dram_malloc(hdrlen+initlen+1) : s_pmem_malloc(hdrlen+initlen+1);
104113
if (init==SDS_NOINIT)
105114
init = NULL;
106115
else if (!init)
107-
memset(sh, 0, hdrlen+initlen+1);
116+
(sds_place == SDS_ON_DRAM) ? memset(sh, 0, hdrlen+initlen+1) : zmemset_pmem(sh, 0, hdrlen+initlen+1);
108117
if (sh == NULL) return NULL;
109118
s = (char*)sh+hdrlen;
110119
fp = ((unsigned char*)s)-1;
@@ -143,7 +152,7 @@ static sds _sdsnewlen(const void *init, size_t initlen, int on_dram) {
143152
}
144153
}
145154
if (initlen && init)
146-
memcpy(s, init, initlen);
155+
(sds_place == SDS_ON_DRAM) ? memcpy(s, init, initlen) : zmemcpy_pmem(s, init, initlen);
147156
s[initlen] = '\0';
148157
return s;
149158
}

src/sdsalloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
#include "zmalloc.h"
4343
#define s_malloc zmalloc
44+
#define s_dram_malloc zmalloc_dram
45+
#define s_pmem_malloc zmalloc_pmem
4446
#define s_realloc zrealloc
4547
#define s_free zfree
4648
#define s_dram_malloc zmalloc_dram

0 commit comments

Comments
 (0)