_export int
p4_mmap_creat(char* name, void* addr, long size)
{
int fd;
if (! addr || size < 16)
{
P4_warn1 ("[%p], use the function only with args != 0 !!!" , p4TH);
return 0;
}
fd = open (PFE_set.mapfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
if (fd == -1)
{
P4_info3 ("[%p] %s: could not open: %s" ,
p4TH, PFE_set.mapfile, strerror(errno));
return 0;
}else{
register void* done;
if (lseek (fd, size-3, SEEK_SET) != size-3) { close(fd); return 0; }
write (fd, "END" , 3);
done = MAP_FAILED;
if (addr)
{
done = mmap (addr, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (done == MAP_FAILED)
{
P4_fail4 ("[%p] %s: mmap failed for addr %8p : %s" ,
p4TH, name, addr, strerror(errno));
}
}
if (done == MAP_FAILED)
{
done = mmap (0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (done == MAP_FAILED)
{
P4_fail3 ("[%p] %s: mmap failed anyway : %s" ,
p4TH, name, strerror(errno));
}
}
if (done == MAP_FAILED)
{
close (fd);
return 0;
}else{
P4_info3 ("[%p] mapped at %8p len %ld" ,
p4TH, PFE_MEM, size);
return fd;
}
}
} |