| 1 | |
|---|
| 2 | |
|---|
| 3 | AC_DEFUN([MAGICK_FUNC_MMAP_FILEIO], |
|---|
| 4 | [AC_CHECK_HEADERS(stdlib.h unistd.h) |
|---|
| 5 | AC_CHECK_FUNCS(getpagesize) |
|---|
| 6 | AC_CACHE_CHECK(for working mmap file i/o, magick_cv_func_mmap_fileio, |
|---|
| 7 | [AC_RUN_IFELSE([AC_LANG_SOURCE([AC_INCLUDES_DEFAULT] |
|---|
| 8 | [[/* malloc might have been renamed as rpl_malloc. */ |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | /* |
|---|
| 12 | This test is derived from GNU Autoconf's similar macro. |
|---|
| 13 | The purpose of this test is to verify that files may be memory |
|---|
| 14 | mapped, and that memory mapping and file I/O are coherent. |
|---|
| 15 | |
|---|
| 16 | The test creates a test file, memory maps the file, updates |
|---|
| 17 | the file using the memory map, and then reads the file using |
|---|
| 18 | file I/O to verify that the file contains the updates. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | char *malloc (); |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | /* This mess was copied from the GNU getpagesize.h. */ |
|---|
| 29 | |
|---|
| 30 | /* Assume that all systems that can run configure have sys/param.h. */ |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | int |
|---|
| 66 | main () |
|---|
| 67 | { |
|---|
| 68 | char *data, *data2, *data3; |
|---|
| 69 | int i, pagesize; |
|---|
| 70 | int fd; |
|---|
| 71 | |
|---|
| 72 | pagesize = getpagesize (); |
|---|
| 73 | |
|---|
| 74 | /* First, make a file with some known garbage in it. */ |
|---|
| 75 | data = (char *) malloc (pagesize); |
|---|
| 76 | if (!data) |
|---|
| 77 | exit (1); |
|---|
| 78 | for (i = 0; i < pagesize; ++i) |
|---|
| 79 | *(data + i) = rand (); |
|---|
| 80 | umask (0); |
|---|
| 81 | fd = creat ("conftest.mmap", 0600); |
|---|
| 82 | if (fd < 0) |
|---|
| 83 | exit (1); |
|---|
| 84 | if (write (fd, data, pagesize) != pagesize) |
|---|
| 85 | exit (1); |
|---|
| 86 | close (fd); |
|---|
| 87 | |
|---|
| 88 | /* Mmap the file as read/write/shared and verify that we see the |
|---|
| 89 | same garbage. */ |
|---|
| 90 | fd = open ("conftest.mmap", O_RDWR); |
|---|
| 91 | if (fd < 0) |
|---|
| 92 | exit (1); |
|---|
| 93 | data2 = mmap (0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
|---|
| 94 | if (data2 == 0) |
|---|
| 95 | exit (1); |
|---|
| 96 | for (i = 0; i < pagesize; ++i) |
|---|
| 97 | if (*(data + i) != *(data2 + i)) |
|---|
| 98 | exit (1); |
|---|
| 99 | |
|---|
| 100 | /* Finally, make sure that changes to the mapped area |
|---|
| 101 | percolate back to the file as seen by read(). */ |
|---|
| 102 | for (i = 0; i < pagesize; ++i) |
|---|
| 103 | *(data2 + i) = *(data2 + i) + 1; |
|---|
| 104 | data3 = (char *) malloc (pagesize); |
|---|
| 105 | if (!data3) |
|---|
| 106 | exit (1); |
|---|
| 107 | if (read (fd, data3, pagesize) != pagesize) |
|---|
| 108 | exit (1); |
|---|
| 109 | for (i = 0; i < pagesize; ++i) |
|---|
| 110 | if (*(data2 + i) != *(data3 + i)) |
|---|
| 111 | exit (1); |
|---|
| 112 | close (fd); |
|---|
| 113 | exit (0); |
|---|
| 114 | }]])], |
|---|
| 115 | [magick_cv_func_mmap_fileio=yes], |
|---|
| 116 | [magick_cv_func_mmap_fileio=no], |
|---|
| 117 | [magick_cv_func_mmap_fileio=no])]) |
|---|
| 118 | if test $magick_cv_func_mmap_fileio = yes; then |
|---|
| 119 | AC_DEFINE(HAVE_MMAP_FILEIO, 1, |
|---|
| 120 | [Define to 1 if you have a working `mmap' system call.]) |
|---|
| 121 | fi |
|---|
| 122 | rm -f conftest.mmap |
|---|
| 123 | ]) |
|---|