removed CRs

This commit is contained in:
Remzi Arpaci-Dusseau
2021-08-23 07:13:51 -05:00
parent bf8bf14d43
commit 0cd7a8be1a

View File

@@ -1,74 +1,74 @@
// Persistent stack example of how mmap() naturally creates a software abstraction of persistent memory. // Persistent stack example of how mmap() naturally creates a software abstraction of persistent memory.
// Author: Terence Kelly // Author: Terence Kelly
// tpkelly @ { acm.org, cs.princeton.edu, eecs.umich.edu } // tpkelly @ { acm.org, cs.princeton.edu, eecs.umich.edu }
// Note: Use 'truncate' to make the initial (empty) backing file, // Note: Use 'truncate' to make the initial (empty) backing file,
// whose size should be a multiple of the system page size: // whose size should be a multiple of the system page size:
// //
// prompt> getconf PAGESIZE // prompt> getconf PAGESIZE
// 4096 // 4096
// prompt> truncate -s 4096 ps.img // prompt> truncate -s 4096 ps.img
// //
// makes a 4096-byte empty file. The first sizeof(size_t) bytes will // makes a 4096-byte empty file. The first sizeof(size_t) bytes will
// be interprted as the number of items on the stack; the remainder // be interprted as the number of items on the stack; the remainder
// of the file will contain the integers contained in the stack. // of the file will contain the integers contained in the stack.
// Now you can run the program: // Now you can run the program:
// //
// prompt> ./pstack 7 13 47 pop // prompt> ./pstack 7 13 47 pop
// 47 // 47
// prompt> ./pstack pop pop 99 // prompt> ./pstack pop pop 99
// 13 // 13
// 7 // 7
// prompt> ./pstack pop // prompt> ./pstack pop
// 99 // 99
// //
// Notice that items push'd in one invocation of the program persist // Notice that items push'd in one invocation of the program persist
// until the next invocation: The stack is persistent. // until the next invocation: The stack is persistent.
// //
// You can use hexdump to examine the contents of the backing file: // You can use hexdump to examine the contents of the backing file:
// //
// prompt> hexdump ps.img // prompt> hexdump ps.img
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
typedef struct { typedef struct {
size_t n; size_t n;
int stack[]; // zero-length per C99 int stack[]; // zero-length per C99
} pstack_t; } pstack_t;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int fd, rc; int fd, rc;
struct stat s; struct stat s;
size_t file_size; size_t file_size;
pstack_t *p; pstack_t *p;
fd = open("ps.img", O_RDWR); fd = open("ps.img", O_RDWR);
assert(fd > -1); assert(fd > -1);
rc = fstat(fd, &s); rc = fstat(fd, &s);
assert(rc == 0); assert(rc == 0);
file_size = (size_t) s.st_size; file_size = (size_t) s.st_size;
assert(file_size >= sizeof(pstack_t) && file_size % sizeof(int) == 0); assert(file_size >= sizeof(pstack_t) && file_size % sizeof(int) == 0);
p = (pstack_t *) mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); p = (pstack_t *) mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
assert(p != MAP_FAILED); assert(p != MAP_FAILED);
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
if (strcmp(argv[i], "pop") == 0) { if (strcmp(argv[i], "pop") == 0) {
if (p->n > 0) // stack not empty if (p->n > 0) // stack not empty
printf("%d\n", p->stack[--p->n]); printf("%d\n", p->stack[--p->n]);
} else { // push } else { // push
if (sizeof(pstack_t) + (1 + p->n) * sizeof(int) <= file_size) // stack not full if (sizeof(pstack_t) + (1 + p->n) * sizeof(int) <= file_size) // stack not full
p->stack[p->n++] = atoi(argv[i]); p->stack[p->n++] = atoi(argv[i]);
} }
(void) close(fd); (void) close(fd);
return 0; return 0;
} }