]> granicus.if.org Git - imagemagick/blob - m4/magick_func_mmap_fileio.m4
(no commit message)
[imagemagick] / m4 / magick_func_mmap_fileio.m4
1 # MAGICK_FUNC_MMAP_FILEIO
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 #undef malloc
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 #include <fcntl.h>
22 #include <sys/mman.h>
23
24 #if !STDC_HEADERS && !HAVE_STDLIB_H
25 char *malloc ();
26 #endif
27
28 /* This mess was copied from the GNU getpagesize.h.  */
29 #if !HAVE_GETPAGESIZE
30 /* Assume that all systems that can run configure have sys/param.h.  */
31 # if !HAVE_SYS_PARAM_H
32 #  define HAVE_SYS_PARAM_H 1
33 # endif
34
35 # ifdef _SC_PAGESIZE
36 #  define getpagesize() sysconf(_SC_PAGESIZE)
37 # else /* no _SC_PAGESIZE */
38 #  if HAVE_SYS_PARAM_H
39 #   include <sys/param.h>
40 #   ifdef EXEC_PAGESIZE
41 #    define getpagesize() EXEC_PAGESIZE
42 #   else /* no EXEC_PAGESIZE */
43 #    ifdef NBPG
44 #     define getpagesize() NBPG * CLSIZE
45 #     ifndef CLSIZE
46 #      define CLSIZE 1
47 #     endif /* no CLSIZE */
48 #    else /* no NBPG */
49 #     ifdef NBPC
50 #      define getpagesize() NBPC
51 #     else /* no NBPC */
52 #      ifdef PAGESIZE
53 #       define getpagesize() PAGESIZE
54 #      endif /* PAGESIZE */
55 #     endif /* no NBPC */
56 #    endif /* no NBPG */
57 #   endif /* no EXEC_PAGESIZE */
58 #  else /* no HAVE_SYS_PARAM_H */
59 #   define getpagesize() 8192   /* punt totally */
60 #  endif /* no HAVE_SYS_PARAM_H */
61 # endif /* no _SC_PAGESIZE */
62
63 #endif /* no HAVE_GETPAGESIZE */
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 ])# MAGICK_FUNC_MMAP_FILEIO