diff options
author | aaron <aaron@13f79535-47bb-0310-9956-ffa450edef68> | 2002-01-10 00:20:06 +0000 |
---|---|---|
committer | aaron <aaron@13f79535-47bb-0310-9956-ffa450edef68> | 2002-01-10 00:20:06 +0000 |
commit | 13a2f15109de2463a6cb661eaac7930734b0b451 (patch) | |
tree | 87f1f5d12c47ba1dfc46993a40ab8fecfa733b30 | |
parent | abce3b319a68d71ef6c2b183987d59ea334a255f (diff) | |
download | libapr-13a2f15109de2463a6cb661eaac7930734b0b451.tar.gz |
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
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@62735 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | include/apr.h.in | 36 | ||||
-rw-r--r-- | include/apr_shm.h | 163 | ||||
-rw-r--r-- | include/apr_shmem.h | 173 | ||||
-rw-r--r-- | shmem/beos/shm.c | 128 | ||||
-rw-r--r-- | shmem/beos/shmem.c | 321 | ||||
-rw-r--r-- | shmem/os2/shm.c (renamed from shmem/os2/shmem.c) | 64 | ||||
-rw-r--r-- | shmem/unix/Makefile.in | 6 | ||||
-rw-r--r-- | shmem/unix/shm.c | 459 | ||||
-rw-r--r-- | shmem/unix/shmem.c | 331 | ||||
-rw-r--r-- | test/Makefile.in | 12 |
10 files changed, 792 insertions, 901 deletions
diff --git a/include/apr.h.in b/include/apr.h.in index f37477577..ade01bd45 100644 --- a/include/apr.h.in +++ b/include/apr.h.in @@ -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@ @@ -84,21 +93,6 @@ #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@ #define APR_HAVE_IN_ADDR @have_in_addr@ diff --git a/include/apr_shm.h b/include/apr_shm.h new file mode 100644 index 000000000..c48ea3913 --- /dev/null +++ b/include/apr_shm.h @@ -0,0 +1,163 @@ +/* ==================================================================== + * 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 */ diff --git a/include/apr_shmem.h b/include/apr_shmem.h deleted file mode 100644 index 696c2d09c..000000000 --- a/include/apr_shmem.h +++ /dev/null @@ -1,173 +0,0 @@ -/* ==================================================================== - * 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_SHMEM_H -#define APR_SHMEM_H -/** - * @file apr_shmem.h - * @brief APR Shared Memory Routines - */ -/** - * @defgroup APR_shmem Shared Memory Routines - * @ingroup APR - * @{ - */ - -#include "apr.h" -#include "apr_pools.h" -#include "apr_errno.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#if APR_USES_FILEBASED_SHM -typedef char * apr_shm_name_t; -#elif APR_USES_KEYBASED_SHM -typedef key_t apr_shm_name_t; -#else -/* If APR_USES_ANONYMOUS_SHM or any other case... - * we can't leave apr_shm_name_t entirely undefined. - */ -typedef void apr_shm_name_t; -#endif - -typedef struct apr_shmem_t apr_shmem_t; - -/** - * Create a pool of shared memory for use later. - * @param m The shared memory block. - * @param reqsize The size of the shared memory pool. - * @param file The file to use for the shared memory on platforms - * that require it. - * @param cont The pool to use - */ -APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, - const char *file, apr_pool_t *cont); - -/** - * Destroy the shared memory block. - * @param m The shared memory block to destroy. - */ -APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m); - -/** - * allocate memory from the block of shared memory. - * @param c The shared memory block to destroy. - * @param reqsize How much memory to allocate - */ -APR_DECLARE(void *) apr_shm_malloc(apr_shmem_t *c, apr_size_t reqsize); - -/** - * allocate memory from the block of shared memory and initialize it to zero. - * @param shared The shared memory block to destroy. - * @param size How much memory to allocate - */ -APR_DECLARE(void *) apr_shm_calloc(apr_shmem_t *shared, apr_size_t size); - -/** - * free shared memory previously allocated. - * @param shared The shared memory block to destroy. - * @param entity The actual data to free. - */ -APR_DECLARE(apr_status_t) apr_shm_free(apr_shmem_t *shared, void *entity); - -/** - * Get the name of the shared memory segment if not using anonymous - * shared memory. - * @param c The shared memory block to destroy. - * @param name The name of the shared memory block, NULL if anonymous - * shared memory. - * @return APR_USES_ANONYMOUS_SHM if we are using anonymous shared - * memory. APR_USES_FILEBASED_SHM if our shared memory is - * based on file access. APR_USES_KEYBASED_SHM if shared - * memory is based on a key value such as shmctl. If the - * shared memory is anonymous, the name is NULL. - */ -APR_DECLARE(apr_status_t) apr_shm_name_get(apr_shmem_t *c, - apr_shm_name_t **name); - -/** - * Set the name of the shared memory segment if not using anonymous - * shared memory. This is to allow processes to open shared memory - * created by another process. - * @param c The shared memory block to destroy. - * @param name The name of the shared memory block, NULL if anonymous - * shared memory. - * @return APR_USES_ANONYMOUS_SHM if we are using anonymous shared - * memory. APR_SUCCESS if we are using named shared memory - * and we were able to assign the name correctly. - */ -APR_DECLARE(apr_status_t) apr_shm_name_set(apr_shmem_t *c, - apr_shm_name_t *name); - -/** - * Open the shared memory block in a child process. - * @param The shared memory block to open in the child. - */ -APR_DECLARE(apr_status_t) apr_shm_open(apr_shmem_t *c); - -/** - * Determine how much memory is available in the specified shared memory block - * @param c The shared memory block to open in the child. - * @param avail The amount of space available in the shared memory block. - */ -APR_DECLARE(apr_status_t) apr_shm_avail(apr_shmem_t *c, apr_size_t *avail); - -#ifdef __cplusplus -} -#endif -/** @} */ -#endif /* ! APR_SHMEM_H */ - diff --git a/shmem/beos/shm.c b/shmem/beos/shm.c new file mode 100644 index 000000000..a73d36206 --- /dev/null +++ b/shmem/beos/shm.c @@ -0,0 +1,128 @@ +/* ==================================================================== + * 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; +} + diff --git a/shmem/beos/shmem.c b/shmem/beos/shmem.c deleted file mode 100644 index faa8f07b9..000000000 --- a/shmem/beos/shmem.c +++ /dev/null @@ -1,321 +0,0 @@ -/* ==================================================================== - * 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 block_t { - apr_pool_t *p; - void *addr; - apr_size_t size; - void *nxt; - void *prev; -}; - -struct apr_shmem_t { - apr_pool_t *p; - void *memblock; - void *ptr; - apr_size_t avail; - area_id aid; - struct block_t *uselist; - struct block_t *freelist; -}; - -#define MIN_BLK_SIZE 128 - - -void add_block(struct block_t **list, struct block_t *blk); -void split_block(struct block_t **list, struct block_t *blk, apr_size_t size); - -static struct block_t * find_block_by_addr(struct block_t *list, void *addr) -{ - struct block_t *b = list; - do { - if (b->addr == addr) - return b; - } - while ((b = b->nxt) != NULL); - - return NULL; -} - -static struct block_t *find_block_of_size(struct block_t *list, apr_size_t size) -{ - struct block_t *b = list, *rv = NULL; - apr_ssize_t diff = -1; - - if (!list) - return NULL; - - do { - if (b->size == size) - return b; - if (b->size > size){ - if (diff == -1) - diff = b->size; - if ((b->size - size) < diff){ - diff = b->size - size; - rv = b; - } - } - } - while ((b = b->nxt) != list); - - if (diff > MIN_BLK_SIZE) { - split_block(&list, rv, size); - } - - if (rv) - return rv; - - return NULL; -} - -void add_block(struct block_t **list, struct block_t *blk) -{ - if ((*list) == NULL) - *list = blk; - - if (blk == (*list)){ - blk->prev = blk; - blk->nxt = blk; - } else { - ((struct block_t*)(*list)->prev)->nxt = blk; - blk->prev = ((struct block_t *)(*list))->prev; - blk->nxt = (*list); - (*list)->prev = blk; - } -} - -void split_block(struct block_t **list, struct block_t *blk, apr_size_t size) -{ - apr_size_t nsz = blk->size - size; - struct block_t *b = (struct block_t*)apr_pcalloc(blk->p, sizeof(struct block_t)); - b->p = blk->p; - b->size = nsz; - blk->size = size; - b->addr = blk->addr + size; - add_block(list, b); -} - -static void remove_block(struct block_t **list, struct block_t *blk) -{ - if (((*list) == blk) && (blk->nxt == blk) && (blk == blk->prev)){ - *list = NULL; - blk->nxt = NULL; - blk->prev = NULL; - } else { - ((struct block_t*)(blk->nxt))->prev = blk->prev; - ((struct block_t*)(blk->prev))->nxt = blk->nxt; - if (*list == blk) - *list = blk->nxt; - blk->nxt = NULL; - blk->prev = NULL; - } -} - -/* puts a used block onto the free list for it to be reused... */ -static void free_block(apr_shmem_t *m, void *entity) -{ - struct block_t *b; - if ((b = find_block_by_addr(m->uselist, entity)) != NULL){ - remove_block(&(m->uselist), b); - add_block(&(m->freelist), b); - m->avail += b->size; - } -} - -/* assigns a block of our memory and puts an entry on the uselist */ -static struct block_t *alloc_block(apr_shmem_t *m, apr_size_t size) -{ - struct block_t *b = NULL; - if (m->avail < size) - return NULL; - - if ((b = find_block_of_size(m->freelist, size)) != NULL){ - remove_block(&(m->freelist), b); - } else { - b = (struct block_t*)apr_pcalloc(m->p, sizeof(struct block_t)); - b->p = m->p; - b->addr = m->ptr; - b->size = size; - m->ptr += size; - } - m->avail -= b->size; /* actual size may be different if we're reusing a block */ - add_block(&(m->uselist), b); - - return b; -} - -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... */ - (*m)->uselist = NULL; - (*m)->freelist = NULL; - return APR_SUCCESS; -} - -APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m) -{ - delete_area(m->aid); - m->avail = 0; - m->freelist = NULL; - m->uselist = NULL; - m->memblock = NULL; - return APR_SUCCESS; -} - -APR_DECLARE(void *) apr_shm_malloc(apr_shmem_t *m, apr_size_t reqsize) -{ - struct block_t *b; - if ((b = alloc_block(m, reqsize)) != NULL) - return b->addr; - return NULL; -} - -APR_DECLARE(void *) apr_shm_calloc(apr_shmem_t *m, apr_size_t reqsize) -{ - struct block_t *b; - if ((b = alloc_block(m, reqsize)) != NULL){ - memset(b->addr, 0, reqsize); - return b->addr; - } - return NULL; -} - -APR_DECLARE(apr_status_t) apr_shm_free(apr_shmem_t *m, void *entity) -{ - free_block(m, entity); - return APR_SUCCESS; -} - -APR_DECLARE(apr_status_t) apr_shm_name_get(apr_shmem_t *c, apr_shm_name_t **name) -{ - *name = NULL; - return APR_ANONYMOUS; -} - -APR_DECLARE(apr_status_t) apr_shm_name_set(apr_shmem_t *c, apr_shm_name_t *name) -{ - return APR_ANONYMOUS; -} - -APR_DECLARE(apr_status_t) apr_shm_open(apr_shmem_t *m) -{ - /* If we've forked we need a clone of the original area or we - * will only have access to a one time copy of the data made when - * the fork occurred. This strange bit of code fixes that problem! - */ - thread_info ti; - area_info ai; - area_id deleteme = area_for(m->memblock); - - /* we need to check which team we're in, so we need to get - * the appropriate info structures for the current thread and - * the area we're using. - */ - get_area_info(m->aid, &ai); - get_thread_info(find_thread(NULL), &ti); - - if (ti.team != ai.team){ - area_id nai; - /* if we are in a child then we need to delete the system - * created area as it's a one time copy and won't be a clone - * which is not good. - */ - delete_area(deleteme); - /* now we make our own clone and use that from now on! */ - nai = clone_area(ai.name, &(ai.address), B_CLONE_ADDRESS, - B_READ_AREA | B_WRITE_AREA, ai.area); - get_area_info(nai, &ai); - m->memblock = ai.address; - } - - return APR_SUCCESS; -} - -APR_DECLARE(apr_status_t) apr_shm_avail(apr_shmem_t *m, apr_size_t *size) -{ - *size = m->avail; - if (m->avail == 0) - return APR_ENOSHMAVAIL; - return APR_SUCCESS; - -} diff --git a/shmem/os2/shmem.c b/shmem/os2/shm.c index 009b5a968..9adf40358 100644 --- a/shmem/os2/shmem.c +++ b/shmem/os2/shm.c @@ -65,9 +65,7 @@ struct apr_shmem_t { Heap_t heap; }; - - -APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, const char *file, apr_pool_t *cont) +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)); @@ -92,8 +90,6 @@ APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, cons return APR_SUCCESS; } - - APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m) { _uclose(m->heap); @@ -102,62 +98,24 @@ APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m) return APR_SUCCESS; } - - -APR_DECLARE(void *) apr_shm_malloc(apr_shmem_t *m, apr_size_t reqsize) -{ - return _umalloc(m->heap, reqsize); -} - - - -APR_DECLARE(void *) apr_shm_calloc(apr_shmem_t *m, apr_size_t size) +APR_DECLARE(apr_status_t) apr_shm_attach(apr_shmem_t **m, + const char *filename, + apr_pool_t *pool) { - return _ucalloc(m->heap, size, 1); -} - - - -APR_DECLARE(apr_status_t) apr_shm_free(apr_shmem_t *m, void *entity) -{ - free(entity); - return APR_SUCCESS; -} - - - -APR_DECLARE(apr_status_t) apr_shm_name_get(apr_shmem_t *c, apr_shm_name_t **name) -{ - *name = NULL; - return APR_ANONYMOUS; + return APR_ENOTIMPL; } - - -APR_DECLARE(apr_status_t) apr_shm_name_set(apr_shmem_t *c, apr_shm_name_t *name) +APR_DECLARE(apr_status_t) apr_shm_detach(apr_shmem_t *m) { - return APR_ANONYMOUS; + return APR_ENOTIMPL; } - - -APR_DECLARE(apr_status_t) apr_shm_open(apr_shmem_t *m) +APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m) { - int rc; - - rc = DosGetSharedMem(m->memblock, PAG_READ|PAG_WRITE); - - if (rc) - return APR_OS2_STATUS(rc); - - _uopen(m->heap); - return APR_SUCCESS; + return APR_ENOTIMPL; } - - -APR_DECLARE(apr_status_t) apr_shm_avail(apr_shmem_t *c, apr_size_t *size) +APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m) { - return APR_ENOTIMPL; -} + diff --git a/shmem/unix/Makefile.in b/shmem/unix/Makefile.in index 7b8e96223..fdf5956c7 100644 --- a/shmem/unix/Makefile.in +++ b/shmem/unix/Makefile.in @@ -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 diff --git a/shmem/unix/shm.c b/shmem/unix/shm.c new file mode 100644 index 000000000..0b21449d8 --- /dev/null +++ b/shmem/unix/shm.c @@ -0,0 +1,459 @@ +/* ==================================================================== + * 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) + diff --git a/shmem/unix/shmem.c b/shmem/unix/shmem.c deleted file mode 100644 index a4b09c82f..000000000 --- a/shmem/unix/shmem.c +++ /dev/null @@ -1,331 +0,0 @@ -/* ==================================================================== - * 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_lock.h" -#include "apr_portable.h" -#include "apr_errno.h" -#define APR_WANT_MEMFUNC -#include "apr_want.h" - -/* - * This is the Unix implementation of shared memory. - * - * Currently, this code supports the following shared memory techniques: - * - * - mmap on a temporary file - * - mmap/shm_open on a temporary file (POSIX.1) - * - mmap with MAP_ANON (4.4BSD) - * - mmap /dev/zero (SVR4) - * - shmget (SysV) - * - create_area (BeOS) - */ - -#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON -#include <sys/mman.h> -#elif APR_USE_SHMEM_SHMGET -#include <sys/ipc.h> -#include <sys/shm.h> -#if !defined(SHM_R) -#define SHM_R 0400 -#endif -#if !defined(SHM_W) -#define SHM_W 0200 -#endif -#include <sys/file.h> -#elif APR_USE_SHMEM_BEOS -#include <kernel/OS.h> -#endif - -struct apr_shmem_t { - void *mem; - void *curmem; - apr_size_t length; - apr_lock_t *lock; - char *filename; -#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || APR_USE_SHMEM_MMAP_ZERO - apr_file_t *file; -#elif APR_USE_SHMEM_MMAP_ANON - /* Nothing else. */ -#elif APR_USE_SHMEM_SHMGET - apr_os_file_t file; -#elif APR_USE_SHMEM_BEOS - area_id areaid; -#endif -}; - -APR_DECLARE(apr_status_t) apr_shm_init(apr_shmem_t **m, apr_size_t reqsize, - const char *filename, apr_pool_t *pool) -{ - apr_shmem_t *new_m; - void *mem; -#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 - apr_status_t status; -#endif -#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \ - APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET - int tmpfd; -#endif - - new_m = apr_palloc(pool, sizeof(apr_shmem_t)); - if (!new_m) - return APR_ENOMEM; - -/* These implementations are very similar except for opening the file. */ -#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || APR_USE_SHMEM_MMAP_ZERO - /* FIXME: Ignore error for now. * - * status = apr_file_remove(filename, 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 APR_EGENERAL; - - status = apr_os_file_get(&tmpfd, new_m->file); - status = apr_file_trunc(new_m->file, reqsize); - if (status != APR_SUCCESS) - return APR_EGENERAL; - -#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, O_READ | O_WRITE | O_CREATE, pool); - status = apr_file_trunc(new_m->file, reqsize); - if (status != APR_SUCCESS) - { - shm_unlink(filename); - return APR_EGENERAL; - } -#elif 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 APR_EGENERAL; - status = apr_os_file_get(&tmpfd, new_m->file); -#endif - - mem = mmap(NULL, reqsize, PROT_READ|PROT_WRITE, MAP_SHARED, tmpfd, 0); - -#elif APR_USE_SHMEM_MMAP_ANON - mem = mmap(NULL, reqsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0); -#elif APR_USE_SHMEM_SHMGET - tmpfd = shmget(IPC_PRIVATE, reqsize, (SHM_R|SHM_W|IPC_CREAT)); - if (tmpfd == -1) - return errno; - - new_m->file = tmpfd; - - mem = shmat(new_m->file, NULL, 0); - - /* FIXME: Handle errors. */ - if (shmctl(new_m->file, IPC_STAT, &shmbuf) == -1) - return errno; - - apr_current_userid(&uid, &gid, pool); - shmbuf.shm_perm.uid = uid; - shmbuf.shm_perm.gid = gid; - - if (shmctl(new_m->file, IPC_SET, &shmbuf) == -1) - return errno; - - /* remove in future (once use count hits zero) */ - if (shmctl(new_m->file, IPC_RMID, NULL) == -1) - return errno; - -#elif APR_USE_SHMEM_BEOS - new_m->area_id = create_area("mm", (void*)&mem, B_ANY_ADDRESS, reqsize, - B_LAZY_LOCK, B_READ_AREA|B_WRITE_AREA); - /* FIXME: error code? */ - if (new_m->area_id < 0) - return APR_EGENERAL; - -#endif - - new_m->mem = mem; - new_m->curmem = mem; - new_m->length = reqsize; - - apr_lock_create(&new_m->lock, APR_MUTEX, APR_CROSS_PROCESS, - APR_LOCK_DEFAULT, NULL, pool); - if (!new_m->lock) - return APR_EGENERAL; - - *m = new_m; - return APR_SUCCESS; -} - -APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shmem_t *m) -{ -#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || APR_USE_SHMEM_MMAP_ZERO - munmap(m->mem, m->length); - apr_file_close(m->file); -#elif APR_USE_SHMEM_MMAP_ANON - munmap(m->mem, m->length); -#elif APR_USE_SHMEM_SHMGET - shmdt(m->mem); -#elif APR_USE_SHMEM_BEOS - delete_area(new_m->area_id); -#endif - - return APR_SUCCESS; -} - -APR_DECLARE(void *) apr_shm_malloc(apr_shmem_t *m, apr_size_t reqsize) -{ - void *new; - new = NULL; - - apr_lock_acquire(m->lock); - /* Do we have enough space? */ - if (((char *)m->curmem - (char *)m->mem + reqsize) <= m->length) - { - new = m->curmem; - m->curmem = (char *)m->curmem + reqsize; - } - apr_lock_release(m->lock); - return new; -} - -APR_DECLARE(void *) apr_shm_calloc(apr_shmem_t *m, apr_size_t reqsize) -{ - void *new = apr_shm_malloc(m, reqsize); - if (new) - memset(new, '\0', reqsize); - return new; -} - -APR_DECLARE(apr_status_t) apr_shm_free(apr_shmem_t *shared, void *entity) -{ - /* Without a memory management scheme within our shared memory, it - * is impossible to implement free. */ - return APR_SUCCESS; -} - -APR_DECLARE(apr_status_t) apr_shm_name_get(apr_shmem_t *c, - apr_shm_name_t **name) -{ -#if APR_USES_ANONYMOUS_SHM - *name = NULL; - return APR_ANONYMOUS; -/* Currently, we are not supporting name based shared memory on Unix - * systems. This may change in the future however, so I will leave - * this in here for now. Plus, this gives other platforms a good idea - * of how to proceed. - */ -#elif APR_USES_FILEBASED_SHM -#elif APR_USES_KEYBASED_SHM -#endif -} - -APR_DECLARE(apr_status_t) apr_shm_name_set(apr_shmem_t *c, - apr_shm_name_t *name) -{ -#if APR_USES_ANONYMOUS_SHM - return APR_ANONYMOUS; -/* Currently, we are not supporting name based shared memory on Unix - * systems. This may change in the future however, so I will leave - * this in here for now. Plus, this gives other platforms a good idea - * of how to proceed. - */ -#elif APR_USES_FILEBASED_SHM -#elif APR_USES_KEYBASED_SHM -#endif -} - -APR_DECLARE(apr_status_t) apr_shm_open(apr_shmem_t *c) -{ -#if APR_USES_ANONYMOUS_SHM - -/* we don't need to open shared memory segments in child segments, so - * just return immediately. - */ - return APR_SUCCESS; -/* Currently, we are not supporting name based shared memory on Unix - * systems. This may change in the future however, so I will leave - * this in here for now. Plus, this gives other platforms a good idea - * of how to proceed. - */ -#elif APR_USES_FILEBASED_SHM -#elif APR_USES_KEYBASED_SHM -#endif -} - -APR_DECLARE(apr_status_t) apr_shm_avail(apr_shmem_t *m, apr_size_t *size) -{ - apr_status_t status; - - status = APR_ENOSHMAVAIL; - - apr_lock_acquire(m->lock); - - *size = m->length - ((char *)m->curmem - (char *)m->mem); - if (*size) - status = APR_SUCCESS; - - apr_lock_release(m->lock); - return status; -} diff --git a/test/Makefile.in b/test/Makefile.in index 38bfb7368..9f4d9fa68 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -18,6 +18,9 @@ PROGRAMS = \ testud@EXEEXT@ \ testmmap@EXEEXT@ \ testshmem@EXEEXT@ \ + testshm@EXEEXT@ \ + testshmproducer@EXEEXT@ \ + testshmconsumer@EXEEXT@ \ testpipe@EXEEXT@ \ testoc@EXEEXT@ \ testuuid@EXEEXT@ \ @@ -116,6 +119,15 @@ testmmap@EXEEXT@: testmmap.lo $(LOCAL_LIBS) 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) |