Last active
June 23, 2019 22:38
-
-
Save jerrinsg/e68b1ff3e88ead4da1a424ac7807fbd5 to your computer and use it in GitHub Desktop.
A persistent memory linked list application written using PMDK library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ex_common.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include "pmemobj_list.h" | |
POBJ_LAYOUT_BEGIN(list); | |
POBJ_LAYOUT_ROOT(list, struct root); | |
POBJ_LAYOUT_TOID(list, struct entry); | |
POBJ_LAYOUT_END(list); | |
static PMEMobjpool *pop; | |
void addNode(void); | |
void printNodes(void); | |
// The root object that stores pointers to the elements in the linked list | |
struct root { | |
TOID(struct entry) head; | |
TOID(struct entry) tail; | |
}; | |
// Structure of each node in the linked list | |
struct entry { | |
int id; | |
TOID(struct entry) next; | |
}; | |
void addNode() { | |
TOID(struct root) r = POBJ_ROOT(pop, struct root); | |
TOID(struct entry) e; | |
struct root *rp = D_RW(r); | |
struct entry *tp = D_RW(rp->tail); | |
TX_BEGIN(pop) { | |
e = TX_NEW(struct entry); | |
TX_ADD_DIRECT(rp); | |
D_RW(e)->id = rand(); | |
D_RW(e)->next = TOID_NULL(struct entry); | |
if(TOID_IS_NULL(rp->head)) { | |
rp->head = e; | |
} else { | |
TX_ADD_DIRECT(&tp->next); | |
tp->next = e; | |
} | |
rp->tail = e; | |
} TX_ONABORT { | |
abort(); | |
} TX_END | |
} | |
void printNodes() { | |
TOID(struct root) r = POBJ_ROOT(pop, struct root); | |
TOID(struct entry) e = D_RO(r)->head; | |
while(!TOID_IS_NULL(e)) { | |
printf("ID = %d\n", D_RO(e)->id); | |
e = D_RO(e)->next; | |
} | |
} | |
int main() { | |
const char *path = "/mnt/ext4-pmem0/fifo"; | |
if (file_exists(path) != 0) { | |
if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(list), | |
PMEMOBJ_MIN_POOL, 0666)) == NULL) { | |
perror("failed to create pool\n"); | |
return -1; | |
} | |
} else { | |
if ((pop = pmemobj_open(path, | |
POBJ_LAYOUT_NAME(list))) == NULL) { | |
perror("failed to open pool\n"); | |
return -1; | |
} | |
} | |
addNode(); | |
printNodes(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment