1N/Atst lib_mmap note{ standard mmap interface that works }end execute{
1N/A #define MAPSIZE (64*1024)
1N/A #define BUFSIZE (8*1024)
1N/A #define Failed(file) (remove(file),1)
1N/A main(int argc, char** argv)
1N/A char file[1024], buf[MAPSIZE];
1N/A struct tms stm, etm;
1N/A /* create data file in a local fs if possible */
1N/A if (access(f = "/tmp", 0) == 0 ||
1N/A *t++ = '.'; *t++ = 'D'; *t = 0;
1N/A if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
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 /* see if can overwrite fixed map */
1N/A #ifndef MAP_VARIABLE
1N/A #define MAP_VARIABLE 0
1N/A if ((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
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 if((fd = open(file, O_RDWR)) < 0)
1N/A return Failed(file);
1N/A for (i = 0; i < WRITE; ++i)
1N/A if (read(fd,buf,BUFSIZE) != BUFSIZE)
1N/A return Failed(file);
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 { mm = (caddr_t)mmap(mm, MAPSIZE,
1N/A (PROT_READ|PROT_WRITE),
1N/A (MAP_PRIVATE | (mm ? MAP_FIXED : MAP_VARIABLE)),
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 if(mm == (caddr_t)(-1) || mm == (caddr_t)0)
1N/A return Failed(file);
1N/A return rdtm+60 < mmtm ? 1 : 0;
1N/Atst lib_mmap64 -D_LARGEFILE64_SOURCE note{ mmap64 interface and implementation work }end execute{
1N/A char file[32] = {'/','t','m','p','/','m','m','X','X','X','X','X','X'};
1N/A /* hey, stubs are supposed to fail! */
1N/A if (!mktemp(file) || (fd = open64(file, O_CREAT|O_WRONLY, 0600)) < 0)
1N/A if (lseek64(fd, off, SEEK_SET) != off)
1N/A n = strlen(file) + 1;
1N/A if (write(fd, file, n) != n)
1N/A if (close(fd) < 0 || (fd = open64(file, O_RDWR)) < 0)
1N/A if (!(s = mmap64((caddr_t)0, (size_t)n, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, off)))
1N/A if (strcmp(s, file))
1N/Atst mmap_anon note{ use mmap MAP_ANON to get raw memory }end execute{
1N/A #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
1N/A #define MAP_ANON MAP_ANONYMOUS
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/Atst mmap_devzero note{ use mmap on
/dev/zero to get raw memory }end execute{
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/Atst note{ mmap is worth using }end output{
1N/A #define MAPSIZE (64*1024)
1N/A #define BUFSIZE (MAPSIZE/8)
1N/A #define Failed(file) (remove(file),1)
1N/A main(int argc, char** argv)
1N/A char file[1024], buf[MAPSIZE];
1N/A struct tms stm, etm;
1N/A /* create data file */
1N/A f = argv[0]; t = file;
1N/A *t++ = '.'; *t++ = 'D'; *t = 0;
1N/A if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
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 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 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 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 /* the memcpy is < BUFSIZE to simulate the
1N/A not do buffer copying.
1N/A for(k = 0; k < MAPSIZE; k += BUFSIZE, t += BUFSIZE)
1N/A memcpy(buf,t,(3*BUFSIZE)/4);
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 /* some systems get it wrong but escape concise detection */
1N/A #undef _mmap_devzero