diff --git a/.gitignore b/.gitignore index 259148f..287255b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,32 +1,2 @@ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app +*.lst +a.out diff --git a/chapter3/.gitignore b/chapter3/.gitignore index 18da2f4..52e7977 100644 --- a/chapter3/.gitignore +++ b/chapter3/.gitignore @@ -1,2 +1,3 @@ mmap_example +pmem_map_file testfile diff --git a/chapter3/Makefile b/chapter3/Makefile index 64b0120..a96419d 100644 --- a/chapter3/Makefile +++ b/chapter3/Makefile @@ -1,21 +1,21 @@ # Copyright (c) 2019, Intel Corporation -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: -# +# # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. -# +# # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. -# +# # * Neither the name of Intel Corporation nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -32,15 +32,25 @@ # Makefile for chapter3 examples # -all: mmap_example +.SUFFIXES: .lst + +all: mmap_example pmem_map_file listings + +listings: mmap_example.lst pmem_map_file.lst map_file_windows_example.lst + +.c.lst: + cat -n $^ > $@ mmap_example: mmap_example.c - $(CC) -o $@ $^ + $(CC) -o mmap_example mmap_example.c + +pmem_map_file: pmem_map_file.c + $(CC) -o pmem_map_file pmem_map_file.c -lpmem clean: $(RM) *.o core a.out testfile clobber: clean - $(RM) mmap_example + $(RM) mmap_example pmem_map_file *.lst .PHONY: all clean clobber diff --git a/chapter3/map_file_windows_example.c b/chapter3/map_file_windows_example.c old mode 100755 new mode 100644 index 969c7ae..46151e4 --- a/chapter3/map_file_windows_example.c +++ b/chapter3/map_file_windows_example.c @@ -29,6 +29,19 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +/* + * map_file_windows_example.c -- windows memory mapped file example + * + * This file memory-maps a file and stores a string to it. It is + * a quick example of how to use MapViewOfFileEx() and how to flush + * changes to the file. + * + * To run this example, provide a single argument, which is the name of + * a test file that is at least 4k in length. This program will overwrite + * the file contents! + */ + #include #include #include @@ -117,7 +130,8 @@ main(int argc, char *argv[]) /* Explicitly unmap before closing the file */ if (UnmapViewOfFile(pmaddr) == FALSE) { - fprintf(stderr, "UnmapViewOfFile, gle: 0x%08x", GetLastError()); + fprintf(stderr, "UnmapViewOfFile, gle: 0x%08x", + GetLastError()); exit(1); } @@ -125,6 +139,5 @@ main(int argc, char *argv[]) CloseHandle(fh); printf("Done.\n"); - exit(0); } diff --git a/chapter3/pmem_map_file.c b/chapter3/pmem_map_file.c index 5d1b48c..8fe33ae 100644 --- a/chapter3/pmem_map_file.c +++ b/chapter3/pmem_map_file.c @@ -56,22 +56,31 @@ main(int argc, char *argv[]) size_t mapped_len; int is_pmem; - /* create a pmem file and memory map it */ - if ((pmemaddr = pmem_map_file(PATH, PMEM_LEN, PMEM_FILE_CREATE, + if (argc != 2) { + fprintf(stderr, "Usage: %s filename\n", argv[0]); + exit(1); + } + + /* Create a pmem file and memory map it. */ + if ((pmemaddr = pmem_map_file(argv[1], PMEM_LEN, PMEM_FILE_CREATE, 0666, &mapped_len, &is_pmem)) == NULL) { perror("pmem_map_file"); exit(1); } - /* store a string to the persistent memory */ - strcpy(pmemaddr, "hello, persistent memory"); + /* Store a string to the persistent memory. */ + char s[] = "This is new data written to the file"; + strcpy(pmemaddr, s); - /* flush our string to persistence */ + /* Flush our string to persistence. */ if (is_pmem) - pmem_persist(pmemaddr, mapped_len); + pmem_persist(pmemaddr, sizeof(s)); else - pmem_msync(pmemaddr, mapped_len); + pmem_msync(pmemaddr, sizeof(s)); /* Delete the mappings. */ pmem_unmap(pmemaddr, mapped_len); + + printf("Done.\n"); + exit(0); }