Skip to content

Commit be471a4

Browse files
author
Andy Rudoff
committed
cleanup, uniformity.
1 parent de448e0 commit be471a4

File tree

5 files changed

+52
-49
lines changed

5 files changed

+52
-49
lines changed

.gitignore

+2-32
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,2 @@
1-
# Prerequisites
2-
*.d
3-
4-
# Compiled Object files
5-
*.slo
6-
*.lo
7-
*.o
8-
*.obj
9-
10-
# Precompiled Headers
11-
*.gch
12-
*.pch
13-
14-
# Compiled Dynamic libraries
15-
*.so
16-
*.dylib
17-
*.dll
18-
19-
# Fortran module files
20-
*.mod
21-
*.smod
22-
23-
# Compiled Static libraries
24-
*.lai
25-
*.la
26-
*.a
27-
*.lib
28-
29-
# Executables
30-
*.exe
31-
*.out
32-
*.app
1+
*.lst
2+
a.out

chapter3/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mmap_example
2+
pmem_map_file
23
testfile

chapter3/Makefile

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Copyright (c) 2019, Intel Corporation
2-
#
2+
#
33
# Redistribution and use in source and binary forms, with or without
44
# modification, are permitted provided that the following conditions
55
# are met:
6-
#
6+
#
77
# * Redistributions of source code must retain the above copyright
88
# notice, this list of conditions and the following disclaimer.
9-
#
9+
#
1010
# * Redistributions in binary form must reproduce the above copyright
1111
# notice, this list of conditions and the following disclaimer in
1212
# the documentation and/or other materials provided with the
1313
# distribution.
14-
#
14+
#
1515
# * Neither the name of Intel Corporation nor the names of its
1616
# contributors may be used to endorse or promote products derived
1717
# from this software without specific prior written permission.
18-
#
18+
#
1919
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2020
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2121
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -32,15 +32,25 @@
3232
# Makefile for chapter3 examples
3333
#
3434

35-
all: mmap_example
35+
.SUFFIXES: .lst
36+
37+
all: mmap_example pmem_map_file listings
38+
39+
listings: mmap_example.lst pmem_map_file.lst map_file_windows_example.lst
40+
41+
.c.lst:
42+
cat -n $^ > $@
3643

3744
mmap_example: mmap_example.c
38-
$(CC) -o $@ $^
45+
$(CC) -o mmap_example mmap_example.c
46+
47+
pmem_map_file: pmem_map_file.c
48+
$(CC) -o pmem_map_file pmem_map_file.c -lpmem
3949

4050
clean:
4151
$(RM) *.o core a.out testfile
4252

4353
clobber: clean
44-
$(RM) mmap_example
54+
$(RM) mmap_example pmem_map_file *.lst
4555

4656
.PHONY: all clean clobber

chapter3/map_file_windows_example.c

100755100644
+15-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
*/
32+
33+
/*
34+
* map_file_windows_example.c -- windows memory mapped file example
35+
*
36+
* This file memory-maps a file and stores a string to it. It is
37+
* a quick example of how to use MapViewOfFileEx() and how to flush
38+
* changes to the file.
39+
*
40+
* To run this example, provide a single argument, which is the name of
41+
* a test file that is at least 4k in length. This program will overwrite
42+
* the file contents!
43+
*/
44+
3245
#include <fcntl.h>
3346
#include <stdio.h>
3447
#include <stdlib.h>
@@ -117,14 +130,14 @@ main(int argc, char *argv[])
117130

118131
/* Explicitly unmap before closing the file */
119132
if (UnmapViewOfFile(pmaddr) == FALSE) {
120-
fprintf(stderr, "UnmapViewOfFile, gle: 0x%08x", GetLastError());
133+
fprintf(stderr, "UnmapViewOfFile, gle: 0x%08x",
134+
GetLastError());
121135
exit(1);
122136
}
123137

124138
CloseHandle(fmh);
125139
CloseHandle(fh);
126140

127141
printf("Done.\n");
128-
129142
exit(0);
130143
}

chapter3/pmem_map_file.c

+16-7
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,31 @@ main(int argc, char *argv[])
5656
size_t mapped_len;
5757
int is_pmem;
5858

59-
/* create a pmem file and memory map it */
60-
if ((pmemaddr = pmem_map_file(PATH, PMEM_LEN, PMEM_FILE_CREATE,
59+
if (argc != 2) {
60+
fprintf(stderr, "Usage: %s filename\n", argv[0]);
61+
exit(1);
62+
}
63+
64+
/* Create a pmem file and memory map it. */
65+
if ((pmemaddr = pmem_map_file(argv[1], PMEM_LEN, PMEM_FILE_CREATE,
6166
0666, &mapped_len, &is_pmem)) == NULL) {
6267
perror("pmem_map_file");
6368
exit(1);
6469
}
6570

66-
/* store a string to the persistent memory */
67-
strcpy(pmemaddr, "hello, persistent memory");
71+
/* Store a string to the persistent memory. */
72+
char s[] = "This is new data written to the file";
73+
strcpy(pmemaddr, s);
6874

69-
/* flush our string to persistence */
75+
/* Flush our string to persistence. */
7076
if (is_pmem)
71-
pmem_persist(pmemaddr, mapped_len);
77+
pmem_persist(pmemaddr, sizeof(s));
7278
else
73-
pmem_msync(pmemaddr, mapped_len);
79+
pmem_msync(pmemaddr, sizeof(s));
7480

7581
/* Delete the mappings. */
7682
pmem_unmap(pmemaddr, mapped_len);
83+
84+
printf("Done.\n");
85+
exit(0);
7786
}

0 commit comments

Comments
 (0)