Apache-Mmap

 view release on metacpan or  search on metacpan

Mmap.xs  view on Meta::CPAN

	if (strEQ(name, "PROT_NONE"))
#ifdef PROT_NONE
	    return PROT_NONE;
#else
	    goto not_there;
#endif
	if (strEQ(name, "PROT_READ"))
#ifdef PROT_READ
	    return PROT_READ;
#else
	    goto not_there;
#endif
	if (strEQ(name, "PROT_WRITE"))
#ifdef PROT_WRITE
	    return PROT_WRITE;
#else
	    goto not_there;
#endif
	break;
    default:
	break;	
    }
    errno = EINVAL;
    return 0;

not_there:
    errno = ENOENT;
    return 0;
}

MODULE = Apache::Mmap	PACKAGE = Apache::Mmap


double
constant(name,arg)
	char *		name
	int		arg

Mmap *
TIESCALAR( CLASS, fh, len, prot, flags, off = 0 )
  char *	CLASS
  FILE *	fh
  size_t	len
  int		prot
  int		flags
  off_t		off

 PREINIT:
  int		fd;
  Mmap *        ret;
  SV   *	sv;

 PROTOTYPE: $$$*;$
 CODE:
   fd = fileno( fh );
   if( fd < 0 ) 
     XSRETURN_UNDEF;

   if( !len ) {
     struct stat st;
     if( fstat( fd, &st ) == -1 )
       XSRETURN_UNDEF;
     len = st.st_size;
   }

   Newz( 0, ret, 1, Mmap );

   ret->len = ret->cur = len;
   ret->off = off;
   ret->prot = prot;
   ret->flags = flags;

   ret->addr = mmap(0, len, prot, flags, fd, off);
   if (ret->addr == MAP_FAILED) {
     XSRETURN_UNDEF;
   }

   RETVAL = ret;

 OUTPUT:
 RETVAL

SV *
STORE( self, what ) 
  Mmap *self
  SV *what

  PROTOTYPE: $$

  CODE:
  /* Croak if what isn't available as a string */
  if( !SvPOK( what ) ) {
    croak( "Attepmt to store non-string scalar\n" );
    XSRETURN_UNDEF;
  }

  /* Make sure region was mapped with write privledges */
  if( !( self->prot & PROT_WRITE ) ) {
    croak( "Attempt to store to read only region\n" );
    XSRETURN_UNDEF;
  }

  /* If length of what is less than or equal size of mapped buffer
   * copy it, set self->cur to SvCUR(what), and zero from the end of
   * the string to the end of the buffer.  Otherwise copy only self->len
   * bytes from what.
   */
  if( SvCUR( what ) <= self->len ) {
    memcpy( self->addr, SvPV( what, self->cur ), self->cur );
    memset( self->addr + self->cur, 0, self->len - self->cur );
  } else {
    memcpy( self->addr, SvPV( what, na ), self->len );
    self->cur = self->len;
  }
  
  /* return copy of what we stored */
  RETVAL = newSVpv( self->addr, self->cur );

  OUTPUT:
  RETVAL



( run in 2.774 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )