mmap revision 1
1N/Aref -D_def_map_ast=1
1N/A
1N/Asys mman
1N/A
1N/Atst lib_mmap note{ standard mmap interface that works }end execute{
1N/A #include <unistd.h>
1N/A #include <fcntl.h>
1N/A #include <sys/types.h>
1N/A #include <sys/mman.h>
1N/A #include <sys/stat.h>
1N/A #include <sys/times.h>
1N/A
1N/A #define MAPSIZE (64*1024)
1N/A #define BUFSIZE (8*1024)
1N/A #define WRITE (64)
1N/A
1N/A #define Failed(file) (remove(file),1)
1N/A
1N/A int
1N/A #if _STD_
1N/A main(int argc, char** argv)
1N/A #else
1N/A main(argc,argv)
1N/A int argc;
1N/A char** argv;
1N/A #endif
1N/A {
1N/A caddr_t mm;
1N/A char *t, *u, *f;
1N/A int i, fd, okfixed;
1N/A char file[1024], buf[MAPSIZE];
1N/A struct tms stm, etm;
1N/A clock_t rdtm, mmtm;
1N/A
1N/A /* create data file in a local fs if possible */
1N/A t = file;
1N/A if (access(f = "/tmp", 0) == 0 ||
1N/A access(f = "/usr/tmp", 0) == 0)
1N/A {
1N/A while (*t = *f++)
1N/A t++;
1N/A *t++ = '/';
1N/A }
1N/A u = t;
1N/A f = argv[0];
1N/A while (*t = *f++)
1N/A if (*t == '/')
1N/A t = u;
1N/A else if (*t != '.')
1N/A t++;
1N/A *t++ = '.'; *t++ = 'D'; *t = 0;
1N/A if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
1N/A return 1;
1N/A
1N/A for (i = 0; i < sizeof(buf); ++i)
1N/A buf[i] = '0' + (i%10);
1N/A for (i = 0; i < WRITE; ++i)
1N/A if (write(fd,buf,sizeof(buf)) != sizeof(buf))
1N/A return Failed(file);
1N/A close(fd);
1N/A
1N/A /* see if can overwrite fixed map */
1N/A #ifndef MAP_VARIABLE
1N/A #define MAP_VARIABLE 0
1N/A #endif
1N/A if ((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A
1N/A mm = mmap((caddr_t)0, sizeof(buf), (PROT_READ|PROT_WRITE),
1N/A (MAP_PRIVATE|MAP_VARIABLE), fd, 0);
1N/A if(mm == (caddr_t)0 || mm == (caddr_t)(-1))
1N/A return Failed(file);
1N/A mm = mmap(mm, sizeof(buf), (PROT_READ|PROT_WRITE),
1N/A (MAP_PRIVATE|MAP_FIXED), fd, 0);
1N/A okfixed = (mm == (caddr_t)0 || mm == (caddr_t)(-1)) ? 0 : 1;
1N/A munmap(mm, sizeof(buf));
1N/A close(fd);
1N/A
1N/A /* read time */
1N/A if((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A times(&stm);
1N/A for (i = 0; i < WRITE; ++i)
1N/A if (read(fd,buf,BUFSIZE) != BUFSIZE)
1N/A return Failed(file);
1N/A times(&etm);
1N/A close(fd);
1N/A rdtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
1N/A
1N/A /* mmap time */
1N/A if ((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A times(&stm);
1N/A for(i = 0, mm = (caddr_t)0; i < WRITE; ++i)
1N/A { if(okfixed)
1N/A { mm = (caddr_t)mmap(mm, MAPSIZE,
1N/A (PROT_READ|PROT_WRITE),
1N/A (MAP_PRIVATE | (mm ? MAP_FIXED : MAP_VARIABLE)),
1N/A fd, i*MAPSIZE );
1N/A }
1N/A else
1N/A { if(mm)
1N/A munmap(mm, MAPSIZE);
1N/A mm = (caddr_t)mmap((caddr_t)0, MAPSIZE,
1N/A (PROT_READ|PROT_WRITE),
1N/A (MAP_PRIVATE|MAP_VARIABLE),
1N/A fd, i*MAPSIZE );
1N/A }
1N/A if(mm == (caddr_t)(-1) || mm == (caddr_t)0)
1N/A return Failed(file);
1N/A }
1N/A times(&etm);
1N/A close(fd);
1N/A remove(file);
1N/A mmtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
1N/A
1N/A return rdtm+60 < mmtm ? 1 : 0;
1N/A }
1N/A}end
1N/A
1N/Atst lib_mmap64 -D_LARGEFILE64_SOURCE note{ mmap64 interface and implementation work }end execute{
1N/A #if !_lib_mmap
1N/A (
1N/A #endif
1N/A
1N/A #include <unistd.h>
1N/A #include <fcntl.h>
1N/A #include <sys/types.h>
1N/A #include <sys/mman.h>
1N/A #include <sys/stat.h>
1N/A
1N/A int
1N/A main()
1N/A {
1N/A off64_t off;
1N/A int fd;
1N/A int n;
1N/A char* s;
1N/A struct stat64 st;
1N/A char file[32] = {'/','t','m','p','/','m','m','X','X','X','X','X','X'};
1N/A
1N/A /* hey, stubs are supposed to fail! */
1N/A if (stat64(".", &st) || !st.st_mode || !st.st_mtime)
1N/A return 1;
1N/A if (!mktemp(file) || (fd = open64(file, O_CREAT|O_WRONLY, 0600)) < 0)
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A off = (1<<8);
1N/A off *= off;
1N/A if (lseek64(fd, off, SEEK_SET) != off)
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A n = strlen(file) + 1;
1N/A if (write(fd, file, n) != n)
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A if (close(fd) < 0 || (fd = open64(file, O_RDWR)) < 0)
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A if (!(s = mmap64((caddr_t)0, (size_t)n, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, off)))
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A if (strcmp(s, file))
1N/A {
1N/A remove(file);
1N/A return 1;
1N/A }
1N/A close(fd);
1N/A remove(file);
1N/A return 0;
1N/A }
1N/A}end
1N/A
1N/Atst mmap_anon note{ use mmap MAP_ANON to get raw memory }end execute{
1N/A #if !_lib_mmap
1N/A (
1N/A #endif
1N/A #include <unistd.h>
1N/A #include <fcntl.h>
1N/A #include <sys/types.h>
1N/A #include <sys/mman.h>
1N/A #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
1N/A #define MAP_ANON MAP_ANONYMOUS
1N/A #endif
1N/A int
1N/A main()
1N/A { void *addr;
1N/A addr = mmap(0,1024*1024,PROT_READ|PROT_WRITE,MAP_ANON|MAP_PRIVATE,-1,0);
1N/A return (addr && addr != (void*)(-1)) ? 0 : 1;
1N/A }
1N/A}end
1N/A
1N/Atst mmap_devzero note{ use mmap on /dev/zero to get raw memory }end execute{
1N/A #if !_lib_mmap
1N/A (
1N/A #endif
1N/A #include <unistd.h>
1N/A #include <fcntl.h>
1N/A #include <sys/types.h>
1N/A #include <sys/mman.h>
1N/A int
1N/A main()
1N/A { int fd;
1N/A void *addr;
1N/A if((fd = open("/dev/zero", O_RDWR)) < 0)
1N/A return 1;
1N/A addr = mmap(0,1024*1024,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
1N/A return (addr && addr != (void*)(-1)) ? 0 : 1;
1N/A }
1N/A}end
1N/A
1N/Atst note{ mmap is worth using }end output{
1N/A #if !_lib_mmap
1N/A (
1N/A #endif
1N/A #include <unistd.h>
1N/A #include <fcntl.h>
1N/A #include <sys/types.h>
1N/A #include <sys/mman.h>
1N/A #include <sys/stat.h>
1N/A #include <sys/times.h>
1N/A
1N/A #define MAPSIZE (64*1024)
1N/A #define BUFSIZE (MAPSIZE/8)
1N/A #define WRITE (64)
1N/A #define RUN (64)
1N/A
1N/A #define Failed(file) (remove(file),1)
1N/A
1N/A int
1N/A #if _STD_
1N/A main(int argc, char** argv)
1N/A #else
1N/A main(argc,argv)
1N/A int argc;
1N/A char** argv;
1N/A #endif
1N/A {
1N/A caddr_t mm;
1N/A char *t, *f;
1N/A int i, fd, k, run;
1N/A char file[1024], buf[MAPSIZE];
1N/A struct tms stm, etm;
1N/A clock_t rdtm, mmtm;
1N/A
1N/A /* create data file */
1N/A f = argv[0]; t = file;
1N/A while (*t = *f++)
1N/A t++;
1N/A *t++ = '.'; *t++ = 'D'; *t = 0;
1N/A if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
1N/A return 1;
1N/A
1N/A for (i = 0; i < sizeof(buf); ++i)
1N/A buf[i] = '0' + (i%10);
1N/A for (i = 0; i < WRITE; ++i)
1N/A if (write(fd,buf,sizeof(buf)) != sizeof(buf))
1N/A return Failed(file);
1N/A close(fd);
1N/A
1N/A /* read time */
1N/A times(&stm);
1N/A for(run = 0; run < RUN; ++run)
1N/A { if((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A for (i = 0; i < WRITE; ++i)
1N/A { for(k = 0; k < MAPSIZE; k += BUFSIZE)
1N/A if (read(fd,buf,BUFSIZE) != BUFSIZE)
1N/A return Failed(file);
1N/A }
1N/A close(fd);
1N/A }
1N/A times(&etm);
1N/A rdtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
1N/A
1N/A /* mmap time */
1N/A times(&stm);
1N/A for(run = 0; run < RUN; ++run)
1N/A { if ((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A for(i = 0, mm = (caddr_t)0; i < WRITE; ++i)
1N/A { if(mm)
1N/A munmap(mm, MAPSIZE);
1N/A mm = (caddr_t)mmap((caddr_t)0, MAPSIZE,
1N/A (PROT_READ|PROT_WRITE),
1N/A MAP_PRIVATE, fd, i*MAPSIZE );
1N/A if(mm == (caddr_t)(-1) || mm == (caddr_t)0)
1N/A return Failed(file);
1N/A
1N/A /* the memcpy is < BUFSIZE to simulate the
1N/A fact that functions like sfreserve/sfgetr do
1N/A not do buffer copying.
1N/A */
1N/A t = (char*)mm;
1N/A for(k = 0; k < MAPSIZE; k += BUFSIZE, t += BUFSIZE)
1N/A memcpy(buf,t,(3*BUFSIZE)/4);
1N/A }
1N/A close(fd);
1N/A }
1N/A times(&etm);
1N/A mmtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
1N/A
1N/A remove(file);
1N/A
1N/A if(4*mmtm <= 3*rdtm)
1N/A printf("#define _mmap_worthy 2 /* mmap is great */\n");
1N/A else if(4*mmtm <= 5*rdtm)
1N/A printf("#define _mmap_worthy 1 /* mmap is good */\n");
1N/A
1N/A else
1N/A return 1;
1N/A return 0;
1N/A }
1N/A}end
1N/A
1N/Acat{
1N/A
1N/A /* some systems get it wrong but escape concise detection */
1N/A #ifndef _NO_MMAP
1N/A #if __CYGWIN__
1N/A #define _NO_MMAP 1
1N/A #endif
1N/A #endif
1N/A
1N/A #if _NO_MMAP
1N/A #undef _lib_mmap
1N/A #undef _lib_mmap64
1N/A #undef _mmap_anon
1N/A #undef _mmap_devzero
1N/A #undef _mmap_worthy
1N/A #endif
1N/A}end