Mail Archives: cygwin/2002/09/18/22:21:52
------=_NextPart_000_000C_01C25FD6.BFD01380
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hi,
I am having trouble with mmap using the last 'offset' parameter. I have
attached a small piece of code (and sample file) which demonstrates the
problem. Basically if the offset parameter is passed as a variable to mmap
then the call fails with an ENOMEM error. But if you pass 0 as the parameter
and then access the return address from mmap + offset - it is fine. My
question: is mmap handling the offset parameter correctly?
Usage for the program:
./a.exe <offset> <file> eg: ./a.exe 11200 index.idx
To see the address + offset work, comment out the first mmap call and return
statement and uncomment the lines below each one.
Any help appreciated.
Regards,
Shane
Shane Mann
Software Engineer Phone: +61-7-3259-2223
LeadUp Software Pty Ltd Fax: +61-7-3259-2259
339 Coronation Drive, Email: shane DOT mann AT leadup DOT com DOT au
Milton, QLD, 4064 Web: http://www.leadup.com.au
------=_NextPart_000_000C_01C25FD6.BFD01380
Content-Type: application/octet-stream;
name="mmap_file2.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="mmap_file2.c"
#include <sys/types.h>=0A=
#include <sys/stat.h>=0A=
#include <sys/mman.h>=0A=
#include <unistd.h>=0A=
#include <string.h>=0A=
#include <signal.h>=0A=
#include <stdio.h>=0A=
#include <errno.h>=0A=
#include <fcntl.h>=0A=
=0A=
#define PAGEPROT PROT_READ|PROT_WRITE=0A=
#define MAPFLAGS MAP_PRIVATE|MAP_ANONYMOUS=0A=
=0A=
static int syspagesize =3D 0, pagemask, offmask, error_cnt =3D 0;=0A=
=0A=
void sigsegv(int unused)=0A=
{=0A=
_exit(0);=0A=
}=0A=
=0A=
caddr_t femmap(int fd, off_t foff, size_t syz, int prot, u_char *msg) {=0A=
/* femmap() and femunmap() take care of the need to allocate=0A=
on page boundaries and in multiples of pages. */=0A=
off_t pfoff; /* foff on a page boundary */=0A=
size_t psyz; /* syz rounded up to full pages */=0A=
int chopped;=0A=
caddr_t rslt;=0A=
if (syspagesize =3D=3D 0) {=0A=
syspagesize =3D getpagesize();=0A=
offmask =3D syspagesize - 1;=0A=
pagemask =3D ~offmask;=0A=
if (1) printf("--- Syspagesize %d, pagemask %X, offmask %X\n",=0A=
syspagesize, pagemask, offmask);=0A=
}=0A=
printf("foff is %d\n", foff);=0A=
printf("--- Syspagesize %d, pagemask %X, offmask %X\n",=0A=
syspagesize, pagemask, offmask);=0A=
pfoff =3D foff & pagemask;=0A=
printf("pfoff is %d\n", pfoff);=0A=
chopped =3D foff & offmask;=0A=
psyz =3D (syz + chopped + syspagesize - 1) & pagemask;=0A=
=0A=
printf("**** Vars just before mmap call ****\n");=0A=
printf("psyz =3D %d\nprot =3D %d\nfd =3D %d\npfoff =3D %d\n", psyz, =
prot, fd, (int)pfoff);=0A=
=0A=
if ((rslt =3D mmap(0, psyz, prot, MAP_SHARED, fd, pfoff)) =
=3D=3DMAP_FAILED) {=0A=
/* if ((rslt =3D mmap(0, psyz, prot, MAP_SHARED, fd, 0)) =
=3D=3DMAP_FAILED) {=0A=
*/=0A=
printf("perror reports errno of %d\n", errno);=0A=
printf(" Mapping %d at %X <---> %d (%s\n", psyz, (int)rslt, =
(int)pfoff,=0A=
msg);=0A=
error_cnt++;=0A=
exit(1);=0A=
}=0A=
if (1)=0A=
printf(" Mapping %d at %X <---> %d (%s\n", psyz, (int)rslt, =
(int)pfoff,=0A=
msg);=0A=
return (rslt + chopped);=0A=
/* return (rslt + chopped + pfoff);=0A=
*/=0A=
}=0A=
=0A=
int main(int argc, char **argv)=0A=
{=0A=
int fh;=0A=
off_t foff;=0A=
size_t syz;=0A=
int prot;=0A=
u_char *msg;=0A=
int i;=0A=
off_t size;=0A=
u_char *result =3D NULL;=0A=
=0A=
struct stat finfo;=0A=
=0A=
foff =3D atoi(argv[1]);=0A=
printf("Offset is %d\n", foff);=0A=
=0A=
for (i=3D2; i<argc; i++)=0A=
{=0A=
fh =3D open(argv[i], O_RDONLY);=0A=
=0A=
fstat(fh, &finfo);=0A=
=0A=
size =3D finfo.st_size;=0A=
=0A=
printf("File %s opened on descriptor %d. It is %ld bytes long\n",=0A=
argv[i], fh, (u_long) size);=0A=
=0A=
result =3D (u_char *)femmap(fh, foff, (int) size, PROT_READ, =
"mapping fh");=0A=
=0A=
printf("Result from femmap is %X\n", result);=0A=
printf("Byte is %d\n", *result);=0A=
=0A=
close(fh);=0A=
}=0A=
=0A=
=0A=
return 0;=0A=
} =0A=
------=_NextPart_000_000C_01C25FD6.BFD01380
Content-Type: text/plain; charset=us-ascii
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
------=_NextPart_000_000C_01C25FD6.BFD01380--
- Raw text -