File: jmmmap.m4

package info (click to toggle)
prcs 1.2.15-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 3,129
  • sloc: cpp: 16,742; ansic: 5,725; sh: 2,545; lisp: 1,812; perl: 642; lex: 351; makefile: 165; pascal: 85
file content (137 lines) | stat: -rw-r--r-- 3,030 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
dnl I don't like the standard mmap test, its broken in
dnl several ways.

AC_DEFUN(JM_FUNC_MMAP,
[AC_CACHE_CHECK(for working mmap read private, ac_cv_func_mmap,
[AC_TRY_RUN([
/* Josh's READ ONLY mmap test.  This is taken from, but heavily
 * modified, the autoconf 2.10 distribution.  This only tests the
 * features of mmap required by PRCS.  It is advisable to run
 * this test on an NFS filesystem if you intend to use PRCS with
 * a repository on NFS. */
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int
main()
{
  char *buf1;
  caddr_t     buf2;
  char *buf3;
  int ps = 5000;  /* This is purposely not a pagesize. */
  int ps2 = 2*ps;
  int ps3 = 3*ps;
  int j;
  int fd;

  /* Allocate 3 pages of buffer. */
  if ((buf1 = (char *)malloc(ps3)) == NULL ||
      (buf3 = (char *)malloc(ps3)) == NULL)
    {
      perror ("malloc failed");
      exit(1);
    }

  /* Generate 3 pages of random numbers into buf1. */
  for (j = 0; j < ps3; ++j)
    *(buf1 + j) = rand();

  /* Open for read/write. */
  if ((fd = open("conftestmmap", O_CREAT | O_RDWR, 0666)) < 0)
    {
      perror ("open 1 failed");
      exit(1);
    }

  /* Write 2 pages of the random numbers to the file. */
  if (write(fd, buf1, ps2) != ps2)
    {
      perror ("write 1 failed");
      exit(1);
    }

  /* Now map the file onto buf2. */
  if((buf2=mmap(NULL, ps2, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL)
    {
      perror ("mmap 1 failed");
      exit (1);
    }

  /* Compare buf1 and buf2. */
  for (j = 0; j < ps2; ++j)
    if (*(buf1 + j) != *(buf2 + j))
      {
	fprintf (stderr, "read/mmap comparison failed\n");
	exit(1);
      }

  /* Write the 3rd page to the file. */
  if (write(fd, buf1+ps2, ps) != ps)
    {
      perror ("write 2 failed");
      exit(1);
    }

  /* Unamp the memory. */
  if (munmap (buf2, ps2) < 0)
    {
      perror ("munmap failed");
      exit(1);
    }

  /* Close the file. */
  if (close (fd) < 0)
    {
      perror ("close failed");
      exit(1);
    }

  /* Reopen the file. */
  if ((fd = open("conftestmmap", O_RDWR, 0666)) < 0)
    {
      perror ("open 2 failed");
      exit(1);
    }

  /* Recompare all 3 pages. */
  /* ... map the file onto buf2. */
  if((buf2=mmap(NULL, ps3, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL)
    {
      perror ("mmap 1 failed");
      exit (1);
    }

  /* ... compare buf1 and buf2. */
  for (j = 0; j < ps3; ++j)
    if (*(buf1 + j) != *(buf2 + j))
      {
	fprintf (stderr, "read/mmap/unmap/append/mmap comparison failed");
	exit(1);
      }

  /* Now read it. */
  if (read(fd, buf3, ps3) != ps3)
    {
      perror ("read 1 failed");
      exit(1);
    }

  /* And compare again. */
  for (j = 0; j < ps3; ++j)
    if (*(buf1 + j) != *(buf3 + j))
      {
	fprintf (stderr, "read/mmap/unmap/append/read comparison failed");
	exit(1);
      }

  exit(0);
}
], ac_cv_func_mmap=yes, ac_cv_func_mmap=no, ac_cv_func_mmap=no)])
if test $ac_cv_func_mmap = yes; then
  AC_DEFINE(HAVE_MMAP)
fi
])