Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
cleanup, uniformity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Rudoff committed May 10, 2019
1 parent de448e0 commit be471a4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 49 deletions.
34 changes: 2 additions & 32 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions chapter3/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mmap_example
pmem_map_file
testfile
26 changes: 18 additions & 8 deletions chapter3/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
17 changes: 15 additions & 2 deletions chapter3/map_file_windows_example.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -117,14 +130,14 @@ 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);
}

CloseHandle(fmh);
CloseHandle(fh);

printf("Done.\n");

exit(0);
}
23 changes: 16 additions & 7 deletions chapter3/pmem_map_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit be471a4

Please sign in to comment.