apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aa...@apache.org
Subject cvs commit: apr/shmem/os2 shm.c shmem.c
Date Thu, 10 Jan 2002 00:20:06 GMT
aaron       02/01/09 16:20:06

  Modified:    include  apr.h.in
               shmem/unix Makefile.in
               test     Makefile.in
  Added:       include  apr_shm.h
               shmem/unix shm.c
               shmem/beos shm.c
               shmem/os2 shm.c
  Removed:     include  apr_shmem.h
               shmem/unix shmem.c
               shmem/beos shmem.c
               shmem/os2 shmem.c
  Log:
  This is the new apr_shm_t API. It completely replaces the old shared
  memory API, which was unusable on Win32 and other platforms that do
  not have inherited shared memory segments since it only supported
  anonymous memory. We now support both anonymous and name-based
  shared memory.
  
  Anonymous shmem tested to work on the following platforms:
  Linux 2.2, Linux 2.4, Solaris 8, FreeBSD 5.0-CURRENT
  
  Name-based shmem is _not_ fully functional on UNIX, but this API
  replaces all of the preexisting apr_shmem_t functionality.
  
  Stubs were provided for Beos and OS/2, and as much relevant code
  as possible was preserved, but no guarantees of correctness.
  
  Reviewed by:	Justin Erenkrantz, Will Rowe
  
  Revision  Changes    Path
  1.98      +15 -21    apr/include/apr.h.in
  
  Index: apr.h.in
  ===================================================================
  RCS file: /home/cvs/apr/include/apr.h.in,v
  retrieving revision 1.97
  retrieving revision 1.98
  diff -u -r1.97 -r1.98
  --- apr.h.in	8 Jan 2002 06:22:26 -0000	1.97
  +++ apr.h.in	10 Jan 2002 00:20:06 -0000	1.98
  @@ -63,12 +63,21 @@
   #define APR_HAVE_SYS_WAIT_H      @sys_waith@
   #define APR_HAVE_UNISTD_H        @unistdh@
   
  -#define APR_USE_SHMEM_MMAP_TMP   @usemmaptmp@
  -#define APR_USE_SHMEM_MMAP_SHM   @usemmapshm@
  -#define APR_USE_SHMEM_MMAP_ZERO  @usemmapzero@
  -#define APR_USE_SHMEM_SHMGET     @useshmget@
  -#define APR_USE_SHMEM_MMAP_ANON  @usemmapanon@
  -#define APR_USE_SHMEM_BEOS       @usebeosarea@
  +#define APR_HAVE_SHMEM_MMAP_TMP     @havemmaptmp@
  +#define APR_HAVE_SHMEM_MMAP_SHM     @havemmapshm@
  +#define APR_HAVE_SHMEM_MMAP_ZERO    @havemmapzero@
  +#define APR_HAVE_SHMEM_SHMGET_ANON  @haveshmgetanon@
  +#define APR_HAVE_SHMEM_SHMGET       @haveshmget@
  +#define APR_HAVE_SHMEM_MMAP_ANON    @havemmapanon@
  +#define APR_HAVE_SHMEM_BEOS         @havebeosarea@
  +
  +#define APR_USE_SHMEM_MMAP_TMP     @usemmaptmp@
  +#define APR_USE_SHMEM_MMAP_SHM     @usemmapshm@
  +#define APR_USE_SHMEM_MMAP_ZERO    @usemmapzero@
  +#define APR_USE_SHMEM_SHMGET_ANON  @useshmgetanon@
  +#define APR_USE_SHMEM_SHMGET       @useshmget@
  +#define APR_USE_SHMEM_MMAP_ANON    @usemmapanon@
  +#define APR_USE_SHMEM_BEOS         @usebeosarea@
   
   #define APR_USE_FLOCK_SERIALIZE           @flockser@ 
   #define APR_USE_SYSVSEM_SERIALIZE         @sysvser@
  @@ -83,21 +92,6 @@
   #define APR_HAS_RWLOCK_SERIALIZE          @hasrwlockser@
   
   #define APR_PROCESS_LOCK_IS_GLOBAL        @proclockglobal@
  -
  -#define APR_USES_ANONYMOUS_SHM            @anonymous_shm@
  -#define APR_USES_FILEBASED_SHM            @filebased_shm@
  -#define APR_USES_KEYBASED_SHM             @keybased_shm@
  -
  -/* These look VERY similar to the macro's above.  They aren't.  The
  - * difference is in implementation.  The above macros describe how to
  - * access the shared memory, either anonymously, through a key or through
  - * a file.  The macros defined below describe actually how the shared
  - * memory is actually implemented.  Is it actually a file that has been
  - * opened by multiple processes, or it is stored in memory somehow.  This
  - * is important for some optimizations in Apache.
  - */ 
  -#define APR_FILE_BASED_SHM      @file_based@
  -#define APR_MEM_BASED_SHM       @mem_based@
   
   #define APR_HAVE_CORKABLE_TCP   @have_corkable_tcp@ 
   #define APR_HAVE_GETRLIMIT      @have_getrlimit@
  
  
  
  1.1                  apr/include/apr_shm.h
  
  Index: apr_shm.h
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  #ifndef APR_SHM_H
  #define APR_SHM_H
  
  #include "apr.h"
  #include "apr_pools.h"
  #include "apr_errno.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif /* __cplusplus */
  
  /**
   * @file apr_shm.h
   * @brief APR Shared Memory Routines
   */
  
  /**
   * @defgroup APR_SHM Shared Memory Routines
   * @ingroup APR
   * @{
   */
  
  /**
   * Private, platform-specific data struture representing a shared memory
   * segment.
   */
  typedef struct apr_shm_t apr_shm_t;
  
  /**
   * Create and make accessable a shared memory segment.
   * @param m The shared memory structure to create.
   * @param reqsize The desired size of the segment.
   * @param file The file to use for shared memory on platforms that
   *        require it.
   * @param pool the pool from which to allocate the shared memory
   *        structure.
   * @remark A note about Anonymous vs. Named shared memory segments:
   *         Not all plaforms support anonymous shared memory segments, but in
   *         some cases it is prefered over other types of shared memory
   *         implementations. Passing a NULL 'file' parameter to this function
   *         will cause the subsystem to use anonymous shared memory segments.
   *         If such a system is not available, APR_ENOTIMPL is returned.
   * @remark A note about allocation sizes:
   *         On some platforms it is necessary to store some metainformation
   *         about the segment within the actual segment. In order to supply
   *         the caller with the requested size it may be necessary for the
   *         implementation to request a slightly greater segment length
   *         from the subsystem. In all cases, the apr_shm_baseaddr_get()
   *         function will return the first usable byte of memory.
   * 
   */
  APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
                                           apr_size_t reqsize,
                                           const char *filename,
                                           apr_pool_t *pool);
  
  /**
   * Destroy a shared memory segment and associated memory.
   * @param m The shared memory segment structure to destroy.
   */
  APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m);
  
  /**
   * Attach to a shared memory segment that was created
   * by another process.
   * @param m The shared memory structure to create.
   * @param file The file used to create the original segment.
   *        (This MUST match the original filename.)
   * @param pool the pool from which to allocate the shared memory
   *        structure for this process.
   */
  APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
                                           const char *filename,
                                           apr_pool_t *pool);
  
  /**
   * Detach from a shared memory segment without destroying it.
   * @param m The shared memory structure representing the segment
   *        to detach from.
   */
  APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m);
  
  /**
   * Retrieve the base address of the shared memory segment.
   * NOTE: This address is only usable within the callers address
   * space, since this API does not guarantee that other attaching
   * processes will maintain the same address mapping.
   * @param m The shared memory segment from which to retrieve
   *        the base address.
   */
  APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m);
  
  /**
   * Retrieve the length of a shared memory segment in bytes.
   * @param m The shared memory segment from which to retrieve
   *        the segment length.
   */
  APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m);
  
  /**
   * Get the pool used by this shared memory segment.
   */
  APR_POOL_DECLARE_ACCESSOR(shm);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif  /* APR_SHM_T */
  
  
  
  1.18      +4 -2      apr/shmem/unix/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apr/shmem/unix/Makefile.in,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Makefile.in	13 Aug 2001 21:49:08 -0000	1.17
  +++ Makefile.in	10 Jan 2002 00:20:06 -0000	1.18
  @@ -1,10 +1,12 @@
   
  -TARGETS = shmem.lo
  +TARGETS = shm.lo
   
   # bring in rules.mk for standard functionality
   @INCLUDE_RULES@
   
   INCDIR=../../include
  -INCLUDES=-I$(INCDIR)
  +INCDIR2=$(INCDIR)/arch
  +INCDIR3=$(INCDIR)/arch/unix
  +INCLUDES=-I$(INCDIR) -I$(INCDIR2) -I$(INCDIR3)
   
   # DO NOT REMOVE
  
  
  
  1.1                  apr/shmem/unix/shm.c
  
  Index: shm.c
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  #include "shm.h"
  
  #include "apr_general.h"
  #include "apr_errno.h"
  #include "apr_user.h"
  #include "apr_strings.h"
  
  /*
  #define APR_WANT_MEMFUNC
  #include "apr_want.h"
  */
  
  /*
  #include "apr_portable.h"
  */
  
  #if 0
  #if APR_HAVE_SHMEM_SHMGET
  /* The metadata that is stored in the file that we use to rendevous
   * with the segment in unrelated processes. */
  struct apr_shm_shmget_metadata {
      apr_size_t reqsize;   /* requested size of the segment */
  };
  #endif /* APR_HAVE_SHMEM_SHMGET */
  #endif
  
  APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
                                           apr_size_t reqsize, 
                                           const char *filename,
                                           apr_pool_t *pool)
  {
      apr_shm_t *new_m;
      apr_status_t status;
  #if APR_USE_SHMEM_SHMGET
      struct shmid_ds shmbuf;
      apr_uid_t uid;
      apr_gid_t gid;
  #endif
  #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \
      APR_USE_SHMEM_MMAP_ZERO
      int tmpfd;
  #endif
  #if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
      apr_size_t nbytes;
      apr_file_t *file;   /* file where metadata is stored */
      int shmid;
  #endif
  
      /* FIXME: associate this thing with a pool and set up a destructor
       * to call detach. */
  
      /* Check if they want anonymous or name-based shared memory */
      if (filename == NULL) {
  #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
          new_m = apr_palloc(pool, sizeof(apr_shm_t));
          if (!new_m) {
              return APR_ENOMEM;
          }
          new_m->pool = pool;
          new_m->reqsize = reqsize;
          new_m->realsize = reqsize + sizeof(apr_size_t); /* room for metadata */
          new_m->filename = NULL;
      
  #if APR_USE_SHMEM_MMAP_ZERO
          status = apr_file_open(&new_m->file, "/dev/zero", APR_READ | APR_WRITE, 
                                 APR_OS_DEFAULT, pool);
          if (status != APR_SUCCESS) {
              return status;
          }
          status = apr_os_file_get(&tmpfd, new_m->file);
          if (status != APR_SUCCESS) {
              return status;
          }
  
          new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
                             MAP_SHARED, tmpfd, 0);
          if (new_m->base == MAP_FAILED) {
              return errno;
          }
  
          /* No need to keep the file open after we map it. */
          close(tmpfd);
  
          /* store the real size in the metadata */
          *(apr_size_t*)(new_m->base) = new_m->realsize;
          /* metadata isn't usable */
          new_m->usable = new_m->base + sizeof(apr_size_t);
  
          *m = new_m;
          return APR_SUCCESS;
  
  #elif APR_USE_SHMEM_MMAP_ANON
          new_m->base = mmap(NULL, reqsize, PROT_READ|PROT_WRITE,
                             MAP_ANON|MAP_SHARED, -1, 0);
          if (new_m->base == MAP_FAILED) {
              return errno;
          }
  
          /* store the real size in the metadata */
          *(apr_size_t*)(new_m->base) = new_m->realsize;
          /* metadata isn't usable */
          new_m->usable = new_m->base + sizeof(apr_size_t);
  
          *m = new_m;
          return APR_SUCCESS;
  
  #endif /* APR_USE_SHMEM_MMAP_ZERO */
  #endif /* APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON */
  #if APR_USE_SHMEM_SHMGET_ANON
  
          new_m = apr_palloc(pool, sizeof(apr_shm_t));
          if (!new_m) {
              return APR_ENOMEM;
          }
          new_m->pool = pool;
          new_m->reqsize = reqsize;
          new_m->realsize = reqsize;
          new_m->filename = NULL;
  
          if ((shmid = shmget(IPC_PRIVATE, new_m->realsize,
                              SHM_R | SHM_W | IPC_CREAT)) < 0) {
              return errno;
          }
  
          if ((new_m->base = shmat(shmid, NULL, 0)) < 0) {
              return errno;
          }
  
          new_m->usable = new_m->base;
  
          if (shmctl(shmid, IPC_STAT, &shmbuf) == -1) {
              return errno;
          }
          apr_current_userid(&uid, &gid, pool);
          shmbuf.shm_perm.uid = uid;
          shmbuf.shm_perm.gid = gid;
          if (shmctl(shmid, IPC_SET, &shmbuf) == -1) {
              return errno;
          }
  
          new_m->shmid = shmid;
  
          *m = new_m;
          return APR_SUCCESS;
  #endif /* APR_USE_SHMEM_SHMGET_ANON */
          /* It is an error if they want anonymous memory but we don't have it. */
          return APR_ENOTIMPL; /* requested anonymous but we don't have it */
      }
  
      /* Name-based shared memory */
      else {
          new_m = apr_palloc(pool, sizeof(apr_shm_t));
          if (!new_m) {
              return APR_ENOMEM;
          }
          new_m->pool = pool;
          new_m->reqsize = reqsize;
          new_m->filename = apr_pstrdup(pool, filename);
  
  #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
          new_m->realsize = reqsize + sizeof(apr_size_t); /* room for metadata */
          /* FIXME: Ignore error for now. *
           * status = apr_file_remove(file, pool);*/
          status = APR_SUCCESS;
      
  #if APR_USE_SHMEM_MMAP_TMP
          /* FIXME: Is APR_OS_DEFAULT sufficient? */
          status = apr_file_open(&new_m->file, filename, 
                                 APR_READ | APR_WRITE | APR_CREATE,
                                 APR_OS_DEFAULT, pool);
          if (status != APR_SUCCESS) {
              return status;
          }
          if ((status = apr_os_file_get(&tmpfd, new_m->file)) != APR_SUCCESS) {
              return status;
          }
          status = apr_file_trunc(new_m->file, new_m->realsize);
          if (status != APR_SUCCESS) {
              /* FIXME: should we unlink the file here? */
              /* FIXME: should we close the file here? */
              return status;
          }
  #elif APR_USE_SHMEM_MMAP_SHM
          /* FIXME: Is APR_OS_DEFAULT sufficient? */
          tmpfd = shm_open(filename, O_RDWR | O_CREAT, APR_OS_DEFAULT);
          if (tmpfd == -1) {
              return errno;
          }
  
          apr_os_file_put(&new_m->file, &tmpfd, pool); 
          /* FIXME: check for errors */
  
          status = apr_file_trunc(new_m->file, new_m->realsize);
          if (status != APR_SUCCESS) {
              shm_unlink(filename); /* we're failing, remove the object */
              return status;
          }
  #endif /* APR_USE_SHMEM_MMAP_SHM */
          new_m->base = mmap(NULL, reqsize, PROT_READ|PROT_WRITE,
                             MAP_SHARED, tmpfd, 0);
  
          /* FIXME: check for error */
  
          /* FIXME: close the file (can we close the file if we're using
           *        shm_open? */
  
          /* store the real size in the metadata */
          *(apr_size_t*)(new_m->base) = new_m->realsize;
          /* metadata isn't usable */
          new_m->usable = new_m->base + sizeof(apr_size_t);
  
          *m = new_m;
          return APR_SUCCESS;
  
  #endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
  
  #if APR_USE_SHMEM_SHMGET
          new_m->realsize = reqsize;
  
          if ((shmid = shmget(ftok(filename, 1), new_m->realsize,
                              SHM_R | SHM_W | IPC_CREAT)) < 0) {
              return errno;
          }
  
          new_m->base = shmat(shmid, NULL, 0);
          /* FIXME: Handle errors. */
          new_m->usable = new_m->base;
  
          if (shmctl(shmid, IPC_STAT, &shmbuf) == -1) {
              return errno;
          }
          apr_current_userid(&uid, &gid, pool);
          shmbuf.shm_perm.uid = uid;
          shmbuf.shm_perm.gid = gid;
          if (shmctl(shmid, IPC_SET, &shmbuf) == -1) {
              return errno;
          }
  
          new_m->shmid = shmid;
  
          /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */
          status = apr_file_open(&file, filename, 
                                 APR_WRITE | APR_CREATE,
                                 APR_OS_DEFAULT, pool);
          if (status != APR_SUCCESS) {
              return status;
          }
  
          nbytes = sizeof(reqsize);
          status = apr_file_write(file, (const void *)&reqsize,
                                  &nbytes);
          if (status != APR_SUCCESS) {
              return status;
          }
          status = apr_file_close(file);
          if (status != APR_SUCCESS) {
              return status;
          }
  
          /* Remove the segment once use count hits zero. */
          if (shmctl(shmid, IPC_RMID, NULL) == -1) {
              return errno;
          }
  
          *m = new_m; 
          return APR_SUCCESS;
  
  #endif /* APR_USE_SHMEM_SHMGET */
      }
  
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
  {
      /* FIXME: do cleanups based on what was allocated, not what was
       *        defined at runtime. */
  #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || APR_USE_SHMEM_MMAP_ZERO
      munmap(m->base, m->realsize);
      apr_file_close(m->file);
  #elif APR_USE_SHMEM_MMAP_ANON
      munmap(m->base, m->realsize);
  #elif APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
      shmdt(m->base);
  #endif
  
      return APR_SUCCESS;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
                                           const char *filename,
                                           apr_pool_t *pool)
  {
      apr_status_t status;
  
      if (filename == NULL) {
  #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
          /* If they want anonymous memory they shouldn't call attach. */
          return APR_EGENERAL;
  #else
          return APR_ENOTIMPL;
  #endif
      }
      else {
  #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
          int tmpfd;
          struct stat buf;
          apr_shm_t *new_m;
  
          new_m = apr_palloc(pool, sizeof(apr_shm_t));
          if (!new_m) {
              return APR_ENOMEM;
          }
          new_m->pool = pool;
          new_m->reqsize = reqsize;
          new_m->realsize = reqsize + sizeof(apr_size_t); /* room for metadata */
          new_m->filename = apr_pstrdup(pool, filename);
  
          /* FIXME: open the file, read the length, mmap the segment,
           *        close the file, reconstruct the apr_shm_t. */
          status = apr_file_open(&new_m->file, filename, 
                                 APR_READ | APR_WRITE | APR_CREATE,
                                 APR_OS_DEFAULT, pool);
          if (status != APR_SUCCESS) {
              return status;
          }
          if ((status = apr_os_file_get(&tmpfd, new_m->file)) != APR_SUCCESS) {
              return status;
          }
          
  
          return APR_ENOTIMPL;
  
  #elif APR_USE_SHMEM_SHMGET
          apr_shm_t *new_m;
          apr_file_t *file;   /* file where metadata is stored */
          apr_size_t nbytes;
  
          new_m = apr_palloc(pool, sizeof(apr_shm_t));
          if (!new_m) {
              return APR_ENOMEM;
          }
  
          /* FIXME: does APR_OS_DEFAULT matter for reading? */
          status = apr_file_open(&file, filename, 
                                 APR_READ, APR_OS_DEFAULT, pool);
          if (status != APR_SUCCESS) {
              return status;
          }
  
          nbytes = sizeof(new_m->reqsize);
          status = apr_file_read(file, (void *)&(new_m->reqsize),
                                 &nbytes);
          if (status != APR_SUCCESS) {
              return status;
          }
          status = apr_file_close(file);
          if (status != APR_SUCCESS) {
              return status;
          }
  
          new_m->pool = pool;
          if ((new_m->shmid = shmget(ftok(filename, 1), 0,
                                     SHM_R | SHM_W)) < 0) {
              return errno;
          }
          new_m->base = shmat(new_m->shmid, NULL, 0);
          /* FIXME: handle errors */
          new_m->usable = new_m->base;
          new_m->realsize = new_m->reqsize;
  
          *m = new_m;
          return APR_SUCCESS;
  
  #else
          return APR_ENOTIMPL;
  #endif
      }
  }
  
  APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
  {
  #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
      /* FIXME: munmap the segment. */
      return APR_ENOTIMPL;
  #elif APR_USE_SHMEM_SHMGET
      /* FIXME: shmdt. */
      return APR_ENOTIMPL;
  #else
      return APR_ENOTIMPL;
  #endif
  }
  
  APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
  {
      return m->usable;
  }
  
  APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
  {
      return m->reqsize;
  }
  
  APR_POOL_IMPLEMENT_ACCESSOR(shm)
  
  
  
  
  1.72      +12 -0     apr/test/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apr/test/Makefile.in,v
  retrieving revision 1.71
  retrieving revision 1.72
  diff -u -r1.71 -r1.72
  --- Makefile.in	5 Jan 2002 00:41:21 -0000	1.71
  +++ Makefile.in	10 Jan 2002 00:20:06 -0000	1.72
  @@ -18,6 +18,9 @@
   	testud@EXEEXT@ \
   	testmmap@EXEEXT@ \
   	testshmem@EXEEXT@ \
  +	testshm@EXEEXT@ \
  +	testshmproducer@EXEEXT@ \
  +	testshmconsumer@EXEEXT@ \
   	testpipe@EXEEXT@ \
   	testoc@EXEEXT@ \
   	testuuid@EXEEXT@ \
  @@ -115,6 +118,15 @@
   
   testshmem@EXEEXT@: testshmem.lo $(LOCAL_LIBS)
   	$(LINK) testshmem.lo $(LOCAL_LIBS) $(ALL_LIBS)
  +
  +testshm@EXEEXT@: testshm.lo $(LOCAL_LIBS) testshmproducer@EXEEXT@ testshmconsumer@EXEEXT@
  +	$(LINK) testshm.lo $(LOCAL_LIBS) $(ALL_LIBS)
  +
  +testshmproducer@EXEEXT@: testshmproducer.lo $(LOCAL_LIBS)
  +	$(LINK) testshmproducer.lo $(LOCAL_LIBS) $(ALL_LIBS)
  +
  +testshmconsumer@EXEEXT@: testshmconsumer.lo $(LOCAL_LIBS)
  +	$(LINK) testshmconsumer.lo $(LOCAL_LIBS) $(ALL_LIBS)
   
   testpipe@EXEEXT@: testpipe.lo $(LOCAL_LIBS)
   	$(LINK) testpipe.lo $(LOCAL_LIBS) $(ALL_LIBS)
  
  
  
  1.1                  apr/shmem/beos/shm.c
  
  Index: shm.c
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  #include "apr_general.h"
  #include "apr_shmem.h"
  #include "apr_errno.h"
  #include "apr_lib.h"
  #include "apr_strings.h"
  #include <stdio.h>
  #include <stdlib.h>
  #include <kernel/OS.h>
  
  struct apr_shm_t {
      apr_pool_t *p;
      void *memblock;
      void *ptr;
      apr_size_t avail;
      area_id aid;
  };
  
  APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, const char *file,

                                         apr_pool_t *p)
  {
      apr_size_t pagesize;
      area_id newid;
      char *addr;
  
      (*m) = (apr_shmem_t *)apr_pcalloc(p, sizeof(apr_shmem_t));
      /* we MUST allocate in pages, so calculate how big an area we need... */
      pagesize = ((reqsize + B_PAGE_SIZE - 1) / B_PAGE_SIZE) * B_PAGE_SIZE;
      
      newid = create_area("apr_shm", (void*)&addr, B_ANY_ADDRESS,
                          pagesize, B_CONTIGUOUS, B_READ_AREA|B_WRITE_AREA);
  
      if (newid < 0)
          return errno;
  
      (*m)->p = p;
      (*m)->aid = newid;
      (*m)->memblock = addr;
      (*m)->ptr = (void*)addr;
      (*m)->avail = pagesize; /* record how big an area we actually created... */
  
      return APR_SUCCESS;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m)
  {
      delete_area(m->aid);
      m->avail = 0;
      m->memblock = NULL;
      return APR_SUCCESS;
  }
  
  
  APR_DECLARE(apr_status_t) apr_shm_attach(apr_shmem_t **m,
                                           const char *filename,
                                           apr_pool_t *pool)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_detach(apr_shmem_t *m)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
  {
      return APR_ENOTIMPL;
  }
  
  
  
  
  1.1                  apr/shmem/os2/shm.c
  
  Index: shm.c
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  #include "apr_general.h"
  #include "apr_shmem.h"
  #include "apr_errno.h"
  #include "apr_lib.h"
  #include "apr_strings.h"
  #include <umalloc.h>
  #include <stdlib.h>
  
  struct apr_shmem_t {
      void *memblock;
      Heap_t heap;
  };
  
  APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, const char *filename,
apr_pool_t *cont)
  {
      int rc;
      apr_shmem_t *newm = (apr_shmem_t *)apr_palloc(cont, sizeof(apr_shmem_t));
      char *name = NULL;
      ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
  
      if (file)
          name = apr_pstrcat(cont, "\\SHAREMEM\\", file, NULL);
  
      if (name == NULL)
          flags |= OBJ_GETTABLE;
  
      reqsize += 1024; /* Allow some overhead for heap structures */
      rc = DosAllocSharedMem(&(newm->memblock), name, reqsize, flags);
  
      if (rc)
          return APR_OS2_STATUS(rc);
  
      newm->heap = _ucreate(newm->memblock, reqsize, !_BLOCK_CLEAN, _HEAP_REGULAR|_HEAP_SHARED,
NULL, NULL);
      _uopen(newm->heap);
      *m = newm;
      return APR_SUCCESS;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m)
  {
      _uclose(m->heap);
      _udestroy(m->heap, _FORCE);
      DosFreeMem(m->memblock);
      return APR_SUCCESS;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_attach(apr_shmem_t **m,
                                           const char *filename,
                                           apr_pool_t *pool)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(apr_status_t) apr_shm_detach(apr_shmem_t *m)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
  {
      return APR_ENOTIMPL;
  }
  
  APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
  {
      return APR_ENOTIMPL;
  
  
  
  

Mime
View raw message