You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by dr...@hyperreal.org on 1999/10/21 23:13:58 UTC

cvs commit: apache-2.0/src/lib/apr/test testmmap.c

dreid       99/10/21 14:13:50

  Modified:    src/lib/apr configure.in
               src/lib/apr/mmap/beos Makefile.in mmap.c
               src/lib/apr/mmap/unix Makefile.in mmap.c
               src/lib/apr/test testmmap.c
  Log:
  This set of changes adds the function ap_mmap_open_create which allows
  an ap_file_t to be mmap'd.  This was requested and so here it is.  Test
  program has been updated to test this.  Also mmap has now been added to
  the module list in configure.in so it will now be built.
  
  Submitted by: david reid
  
  Revision  Changes    Path
  1.22      +1 -1      apache-2.0/src/lib/apr/configure.in
  
  Index: configure.in
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/configure.in,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- configure.in	1999/10/19 19:21:01	1.21
  +++ configure.in	1999/10/21 21:13:15	1.22
  @@ -4,7 +4,7 @@
   
   # These added to allow default directories to be used...
   DEFAULT_OSDIR="unix"
  -MODULES="file_io network_io threadproc misc signal locks time" 
  +MODULES="file_io network_io threadproc misc signal locks time mmap" 
   
   echo "Configuring APR library"
   echo "Platform: ${OS}"
  
  
  
  1.2       +1 -1      apache-2.0/src/lib/apr/mmap/beos/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/mmap/beos/Makefile.in,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Makefile.in	1999/10/20 20:22:02	1.1
  +++ Makefile.in	1999/10/21 21:13:24	1.2
  @@ -10,7 +10,7 @@
   LIBS=@LIBS@
   LDFLAGS=@LDFLAGS@ $(LIBS)
   INCDIR=../../inc
  -INCDIR1=../../include
  +INCDIR1=../../include -I../../file_io/unix
   INCLUDES=-I$(INCDIR) -I$(INCDIR1) -I.
   
   LIB=libmmap.a
  
  
  
  1.2       +66 -20    apache-2.0/src/lib/apr/mmap/beos/mmap.c
  
  Index: mmap.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/mmap/beos/mmap.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- mmap.c	1999/10/20 20:22:02	1.1
  +++ mmap.c	1999/10/21 21:13:26	1.2
  @@ -58,6 +58,7 @@
   #include "apr_general.h"
   #include "apr_portable.h"
   #include "apr_lib.h"
  +#include "fileio.h"
   #include <kernel/OS.h>
   #include <errno.h>
   #include <string.h>
  @@ -82,17 +83,11 @@
        ap_context_t *cont)
   {
       struct stat st;
  -    int fd;
  +    int fd = -1;
       void *mm;
  -    struct mmap_t *next;
  -    area_id aid;
  -    char *areaname;
  -    uint32 size;
  -    int len;               
  -
  -    /* We really should check to see that we haven't already mmap'd
  -     * this file before.  Cycling the linked list will allow this.
  -     */
  +    area_id aid = -1;
  +    char *areaname = "apr_mmap\0";
  +    uint32 size = 0;
   
       (*new) = (struct mmap_t *)ap_palloc(cont, sizeof(struct mmap_t));
       
  @@ -109,16 +104,10 @@
       if ((fd = open(fname, O_RDONLY, 0)) == -1) {
           return APR_EBADF;
       }
  -
  -    /* generate a unique name for this area */
  -    len = strlen(fname) > 22 ? 22 : strlen(fname);
  -    areaname = malloc(sizeof(char) * 32);
  -    strncpy(areaname, "beos_mmap:\0", 11);
  -    strncat(areaname, fname + (strlen(fname)-len), len);
  -    
  -    aid = create_area(areaname, mm, B_ANY_ADDRESS, size * B_PAGE_SIZE, 
  +  
  +    aid = create_area(areaname, &mm , B_ANY_ADDRESS, size * B_PAGE_SIZE, 
           B_FULL_LOCK, B_READ_AREA|B_WRITE_AREA);
  -    free(areaname);
  +
       if (aid >= B_NO_ERROR)
           read(fd, mm, st.st_size);    
       
  @@ -142,10 +131,67 @@
       return APR_SUCCESS;
   }
   
  +ap_status_t ap_mmap_open_create(struct mmap_t **new, ap_file_t *file, 
  +               ap_context_t *cont)
  +{
  +    void *mm;
  +    area_id aid = -1;
  +    char *areaname = "apr_mmap\0";
  +    uint32 size;           
  +    
  +    if (file->buffered)
  +        /* we don't yet mmap buffered files... */
  +        return APR_EBADF;
  +    if (file->filedes == -1)
  +        /* there isn't a file handle so how can we mmap?? */
  +        return APR_EBADF;
  +    (*new) = (struct mmap_t*)ap_palloc(file->cntxt, sizeof(struct mmap_t));
  +    
  +    if (!file->stated) {
  +        /* hmmmm... we need to stat the file now */
  +        struct stat st;
  +        if (stat(file->fname, &st) == -1) {
  +            /* hmm, is this fatal?? */
  +            return APR_EBADF;
  +        }
  +        file->stated = 1;
  +        file->size = st.st_size;
  +        file->atime = st.st_atime;
  +        file->mtime = st.st_mtime;
  +        file->ctime = st.st_ctime;
  +        (*new)->sinfo = st;
  +    }
  +    
  +    size = ((file->size -1) / B_PAGE_SIZE) + 1;
  +
  +    aid = create_area(areaname, &mm, B_ANY_ADDRESS, size * B_PAGE_SIZE, 
  +        B_LAZY_LOCK, B_READ_AREA|B_WRITE_AREA);
  +    free(areaname);
  +    
  +    if (aid < B_OK) {
  +        /* we failed to get an mmap'd file... */
  +        return APR_ENOMEM;
  +    }  
  +    if (aid >= B_OK)
  +        read(file->filedes, mm, file->size);    
  +
  +    (*new)->filename = ap_pstrdup(cont, file->fname);
  +    (*new)->mm = mm;
  +    (*new)->size = file->size;
  +    (*new)->area = aid;
  +    (*new)->cntxt = cont;
  +           
  +    /* register the cleanup... */ 
  +    ap_register_cleanup((*new)->cntxt, (void*)(*new), mmap_cleanup,
  +             ap_null_cleanup);
  +
  +    return APR_SUCCESS;
  +}
  +
   ap_status_t ap_mmap_delete(struct mmap_t *mmap)
   {
       ap_status_t rv;
  -    if (mm->area == -1)
  +    if (mmap->area == -1)
           return APR_ENOENT;
            
       if ((rv = mmap_cleanup(mmap)) == APR_SUCCESS) {
  
  
  
  1.2       +1 -1      apache-2.0/src/lib/apr/mmap/unix/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/mmap/unix/Makefile.in,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Makefile.in	1999/10/20 20:22:04	1.1
  +++ Makefile.in	1999/10/21 21:13:32	1.2
  @@ -10,7 +10,7 @@
   LIBS=@LIBS@
   LDFLAGS=@LDFLAGS@ $(LIBS)
   INCDIR=../../inc
  -INCDIR1=../../include
  +INCDIR1=../../include -I../../file_io/@OSDIR@
   INCLUDES=-I$(INCDIR) -I$(INCDIR1) -I.
   
   LIB=libmmap.a
  
  
  
  1.2       +48 -7     apache-2.0/src/lib/apr/mmap/unix/mmap.c
  
  Index: mmap.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/mmap/unix/mmap.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- mmap.c	1999/10/20 20:22:04	1.1
  +++ mmap.c	1999/10/21 21:13:34	1.2
  @@ -54,6 +54,7 @@
    */
   
   #include "mmap_h.h"
  +#include "fileio.h"
   #include "apr_mmap.h"
   #include "apr_general.h"
   #include "apr_portable.h"
  @@ -83,13 +84,7 @@
       struct stat st;
       int fd;
       caddr_t mm;
  -    ap_mmap_t next;
  -    
  -    /* before we go blindly in and create an mmap, we should probably check
  -     * to make sure we haven't already mmap'd the file.
  -     * I'll add this once I've got the basics working.
  -     */
  -    
  +   
       (*new) = (struct mmap_t *)ap_palloc(cont, sizeof(struct mmap_t));
       
       if (stat(fname, &st) == -1) {
  @@ -120,6 +115,52 @@
                ap_null_cleanup);
       return APR_SUCCESS;
   }
  +
  +ap_status_t ap_mmap_open_create(struct mmap_t **new, ap_file_t *file, 
  +               ap_context_t *cont)
  +{
  +    caddr_t mm;
  +
  +    if (file->buffered)
  +        /* we don't yet mmap buffered files... */
  +        return APR_EBADF;
  +    if (file->filedes == -1)
  +        /* there isn't a file handle so how can we mmap?? */
  +        return APR_EBADF;
  +    (*new) = (struct mmap_t*)ap_palloc(file->cntxt, sizeof(struct mmap_t));
  +    
  +    if (!file->stated) {
  +        /* hmmmm... we need to stat the file now */
  +        struct stat st;
  +        if (stat(file->fname, &st) == -1) {
  +            /* hmm, is this fatal?? */
  +            return APR_EBADF;
  +        }
  +        file->stated = 1;
  +        file->size = st.st_size;
  +        file->atime = st.st_atime;
  +        file->mtime = st.st_mtime;
  +        file->ctime = st.st_ctime;
  +        (*new)->sinfo = st;
  +    }
  +
  +    mm = mmap(NULL, file->size, PROT_READ, MAP_SHARED, file->filedes ,0);
  +    if (mm == (caddr_t)-1) {
  +        /* we failed to get an mmap'd file... */
  +        return APR_ENOMEM;
  +    }
  +
  +    (*new)->filename = ap_pstrdup(cont, file->fname);
  +    (*new)->mm = mm;
  +    (*new)->size = file->size;
  +    (*new)->cntxt = cont;
  +           
  +    /* register the cleanup... */
  +    ap_register_cleanup((*new)->cntxt, (void*)(*new), mmap_cleanup,
  +             ap_null_cleanup);
  +    return APR_SUCCESS;
  +}
  +
   
   ap_status_t ap_mmap_delete(struct mmap_t *mmap)
   {
  
  
  
  1.2       +35 -8     apache-2.0/src/lib/apr/test/testmmap.c
  
  Index: testmmap.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/test/testmmap.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- testmmap.c	1999/10/20 20:24:07	1.1
  +++ testmmap.c	1999/10/21 21:13:38	1.2
  @@ -58,6 +58,7 @@
   #include "apr_errno.h"
   #include "apr_general.h"
   #include "apr_lib.h"
  +#include "apr_file_io.h"
   
   /* hmmm, what is a truly portable define for the max path
    * length on a platform?
  @@ -69,8 +70,10 @@
       ap_context_t *context;
       ap_mmap_t *themmap = NULL;
       ap_status_t status = 0;
  -    char *file;
  -
  +    ap_file_t *thefile;
  +    ap_int32_t flag = APR_READ;
  +    char *file1;
  +    
       fprintf (stdout,"APR MMAP Test\n*************\n\n");
       
       fprintf(stdout,"Creating context....................");    
  @@ -79,25 +82,49 @@
           exit(-1);
       }
       fprintf(stdout,"OK\n");
  -    
  -    file = (char*) malloc(sizeof(char) * PATH_LEN);
  -    getcwd(file, PATH_LEN);
  -    strncat(file,"/testmmap.c",11);  
       
  +    file1 = (char*) ap_palloc(context, sizeof(char) * PATH_LEN);
  +    getcwd(file1, PATH_LEN);
  +    strncat(file1,"/testmmap.c",11);  
  +
       fprintf(stdout,"Trying to mmap file.................");
  -    if (ap_mmap_create(&themmap, file, context) != APR_SUCCESS) {
  +    if (ap_mmap_create(&themmap, file1, context) != APR_SUCCESS) {
           fprintf(stderr,"Failed.\n");
           exit (-1);
       }
       fprintf(stdout,"OK\n");
  -
  +    
       fprintf(stdout,"Trying to delete the mmap file......");
       if (ap_mmap_delete(themmap) != APR_SUCCESS) {
           fprintf(stderr,"Failed!\n");
           exit (-1);
       }
  +    fprintf(stdout,"OK\n\n");
  +
  +    fprintf(stdout, "Opening file........................");
  +    if (ap_open(&thefile, file1, flag, APR_UREAD | APR_GREAD, context) != APR_SUCCESS) {
  +        perror("Didn't open file");
  +        exit(-1);
  +    }
  +    else {
  +        fprintf(stdout, "OK\n");
  +    }
  +
  +    fprintf(stdout,"Trying to mmap the open file........");
  +    if (ap_mmap_open_create(&themmap, thefile, context) != APR_SUCCESS) {
  +        fprintf(stderr,"Failed!\n");
  +        exit(-1);
  +    }
       fprintf(stdout,"OK\n");
   
  +    fprintf(stdout,"Trying to delete the mmap file......");
  +    if (ap_mmap_delete(themmap) != APR_SUCCESS) {
  +        fprintf(stderr,"Failed!\n");
  +        exit (-1);
  +    }
  +    fprintf(stdout,"OK\n");
  +    
  +    
       fprintf (stdout,"\nTest Complete\n");
       
       return 1;