summaryrefslogtreecommitdiff
path: root/shmem
diff options
context:
space:
mode:
authorLorry <lorry@roadtrain.codethink.co.uk>2012-08-28 17:19:35 +0100
committerLorry <lorry@roadtrain.codethink.co.uk>2012-08-28 17:19:35 +0100
commitd3c52eb4b4402f0afbebc3f36b9ef8c0994a6c1a (patch)
treeb5ed37757fd9e0f821d103c26692e35ddf2976cb /shmem
downloadlibapr-tarball-d3c52eb4b4402f0afbebc3f36b9ef8c0994a6c1a.tar.gz
Tarball conversion
Diffstat (limited to 'shmem')
-rw-r--r--shmem/beos/shm.c166
-rw-r--r--shmem/os2/shm.c144
-rw-r--r--shmem/unix/shm.c598
-rw-r--r--shmem/win32/shm.c317
4 files changed, 1225 insertions, 0 deletions
diff --git a/shmem/beos/shm.c b/shmem/beos/shm.c
new file mode 100644
index 0000000..1f06f78
--- /dev/null
+++ b/shmem/beos/shm.c
@@ -0,0 +1,166 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_shm.h"
+#include "apr_errno.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <kernel/OS.h>
+#include "apr_portable.h"
+
+struct apr_shm_t {
+ apr_pool_t *pool;
+ void *memblock;
+ void *ptr;
+ apr_size_t reqsize;
+ apr_size_t avail;
+ area_id aid;
+};
+
+APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *p)
+{
+ apr_size_t pagesize;
+ area_id newid;
+ char *addr;
+ char shname[B_OS_NAME_LENGTH];
+
+ (*m) = (apr_shm_t *)apr_pcalloc(p, sizeof(apr_shm_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;
+
+ if (!filename) {
+ int num = 0;
+ snprintf(shname, B_OS_NAME_LENGTH, "apr_shmem_%ld", find_thread(NULL));
+ while (find_area(shname) >= 0)
+ snprintf(shname, B_OS_NAME_LENGTH, "apr_shmem_%ld_%d",
+ find_thread(NULL), num++);
+ }
+ newid = create_area(filename ? filename : shname,
+ (void*)&addr, B_ANY_ADDRESS,
+ pagesize, B_LAZY_LOCK, B_READ_AREA|B_WRITE_AREA);
+
+ if (newid < 0)
+ return errno;
+
+ (*m)->pool = p;
+ (*m)->aid = newid;
+ (*m)->memblock = addr;
+ (*m)->ptr = (void*)addr;
+ (*m)->avail = pagesize; /* record how big an area we actually created... */
+ (*m)->reqsize = reqsize;
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
+{
+ delete_area(m->aid);
+ m->avail = 0;
+ m->memblock = NULL;
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
+ apr_pool_t *pool)
+{
+ area_id deleteme = find_area(filename);
+
+ if (deleteme == B_NAME_NOT_FOUND)
+ return APR_EINVAL;
+
+ delete_area(deleteme);
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool)
+{
+ area_info ai;
+ thread_info ti;
+ apr_shm_t *new_m;
+ area_id deleteme = find_area(filename);
+
+ if (deleteme == B_NAME_NOT_FOUND)
+ return APR_EINVAL;
+
+ new_m = (apr_shm_t*)apr_palloc(pool, sizeof(apr_shm_t*));
+ if (new_m == NULL)
+ return APR_ENOMEM;
+ new_m->pool = pool;
+
+ get_area_info(deleteme, &ai);
+ get_thread_info(find_thread(NULL), &ti);
+
+ if (ti.team != ai.team) {
+ area_id narea;
+
+ narea = clone_area(ai.name, &(ai.address), B_CLONE_ADDRESS,
+ B_READ_AREA|B_WRITE_AREA, ai.area);
+
+ if (narea < B_OK)
+ return narea;
+
+ get_area_info(narea, &ai);
+ new_m->aid = narea;
+ new_m->memblock = ai.address;
+ new_m->ptr = (void*)ai.address;
+ new_m->avail = ai.size;
+ new_m->reqsize = ai.size;
+ }
+
+ (*m) = new_m;
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
+{
+ delete_area(m->aid);
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
+{
+ return m->memblock;
+}
+
+APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
+{
+ return m->reqsize;
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(shm)
+
+APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
+ apr_shm_t *shm)
+{
+ return APR_ENOTIMPL;
+}
+
+APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
+ apr_os_shm_t *osshm,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
diff --git a/shmem/os2/shm.c b/shmem/os2/shm.c
new file mode 100644
index 0000000..340cae4
--- /dev/null
+++ b/shmem/os2/shm.c
@@ -0,0 +1,144 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_shm.h"
+#include "apr_errno.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_portable.h"
+
+struct apr_shm_t {
+ apr_pool_t *pool;
+ void *memblock;
+};
+
+APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *pool)
+{
+ int rc;
+ apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
+ char *name = NULL;
+ ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
+
+ newm->pool = pool;
+
+ if (filename) {
+ name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
+ }
+
+ if (name == NULL) {
+ flags |= OBJ_GETTABLE;
+ }
+
+ rc = DosAllocSharedMem(&(newm->memblock), name, reqsize, flags);
+
+ if (rc) {
+ return APR_OS2_STATUS(rc);
+ }
+
+ *m = newm;
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
+{
+ DosFreeMem(m->memblock);
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool)
+{
+ int rc;
+ apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
+ char *name = NULL;
+ ULONG flags = PAG_READ|PAG_WRITE;
+
+ newm->pool = pool;
+ name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
+
+ rc = DosGetNamedSharedMem(&(newm->memblock), name, flags);
+
+ if (rc) {
+ return APR_FROM_OS_ERROR(rc);
+ }
+
+ *m = newm;
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
+{
+ int rc = 0;
+
+ if (m->memblock) {
+ rc = DosFreeMem(m->memblock);
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
+{
+ return m->memblock;
+}
+
+APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
+{
+ ULONG flags, size = 0x1000000;
+ DosQueryMem(m->memblock, &size, &flags);
+ return size;
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(shm)
+
+APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
+ apr_shm_t *shm)
+{
+ *osshm = shm->memblock;
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
+ apr_os_shm_t *osshm,
+ apr_pool_t *pool)
+{
+ int rc;
+ apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
+ ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
+
+ newm->pool = pool;
+
+ rc = DosGetSharedMem(&(newm->memblock), flags);
+
+ if (rc) {
+ return APR_FROM_OS_ERROR(rc);
+ }
+
+ *m = newm;
+ return APR_SUCCESS;
+}
+
diff --git a/shmem/unix/shm.c b/shmem/unix/shm.c
new file mode 100644
index 0000000..80d5689
--- /dev/null
+++ b/shmem/unix/shm.c
@@ -0,0 +1,598 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_arch_shm.h"
+
+#include "apr_general.h"
+#include "apr_errno.h"
+#include "apr_user.h"
+#include "apr_strings.h"
+
+static apr_status_t shm_cleanup_owner(void *m_)
+{
+ apr_shm_t *m = (apr_shm_t *)m_;
+
+ /* anonymous shared memory */
+ if (m->filename == NULL) {
+#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
+ if (munmap(m->base, m->realsize) == -1) {
+ return errno;
+ }
+ return APR_SUCCESS;
+#elif APR_USE_SHMEM_SHMGET_ANON
+ if (shmdt(m->base) == -1) {
+ return errno;
+ }
+ /* This segment will automatically remove itself after all
+ * references have detached. */
+ return APR_SUCCESS;
+#endif
+ }
+
+ /* name-based shared memory */
+ else {
+#if APR_USE_SHMEM_MMAP_TMP
+ if (munmap(m->base, m->realsize) == -1) {
+ return errno;
+ }
+ if (access(m->filename, F_OK)) {
+ return APR_SUCCESS;
+ }
+ else {
+ return apr_file_remove(m->filename, m->pool);
+ }
+#elif APR_USE_SHMEM_MMAP_SHM
+ if (munmap(m->base, m->realsize) == -1) {
+ return errno;
+ }
+ if (shm_unlink(m->filename) == -1) {
+ return errno;
+ }
+ return APR_SUCCESS;
+#elif APR_USE_SHMEM_SHMGET
+ /* Indicate that the segment is to be destroyed as soon
+ * as all processes have detached. This also disallows any
+ * new attachments to the segment. */
+ if (shmctl(m->shmid, IPC_RMID, NULL) == -1 && errno != EINVAL) {
+ return errno;
+ }
+ if (shmdt(m->base) == -1) {
+ return errno;
+ }
+ if (access(m->filename, F_OK)) {
+ return APR_SUCCESS;
+ }
+ else {
+ return apr_file_remove(m->filename, m->pool);
+ }
+#else
+ return APR_ENOTIMPL;
+#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 || APR_USE_SHMEM_SHMGET_ANON
+ 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_size_t nbytes;
+ key_t shmkey;
+#endif
+#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \
+ APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
+ apr_file_t *file; /* file where metadata is stored */
+#endif
+
+ /* 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));
+ new_m->pool = pool;
+ new_m->reqsize = reqsize;
+ new_m->realsize = reqsize +
+ APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
+ new_m->filename = NULL;
+
+#if APR_USE_SHMEM_MMAP_ZERO
+ status = apr_file_open(&file, "/dev/zero", APR_READ | APR_WRITE,
+ APR_OS_DEFAULT, pool);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+ status = apr_os_file_get(&tmpfd, 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 == (void *)MAP_FAILED) {
+ return errno;
+ }
+
+ status = apr_file_close(file);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ /* store the real size in the metadata */
+ *(apr_size_t*)(new_m->base) = new_m->realsize;
+ /* metadata isn't usable */
+ new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#elif APR_USE_SHMEM_MMAP_ANON
+ new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_SHARED, -1, 0);
+ if (new_m->base == (void *)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 = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#endif /* APR_USE_SHMEM_MMAP_ZERO */
+#elif APR_USE_SHMEM_SHMGET_ANON
+ new_m = apr_palloc(pool, sizeof(apr_shm_t));
+ new_m->pool = pool;
+ new_m->reqsize = reqsize;
+ new_m->realsize = reqsize;
+ new_m->filename = NULL;
+
+ if ((new_m->shmid = shmget(IPC_PRIVATE, new_m->realsize,
+ SHM_R | SHM_W | IPC_CREAT)) < 0) {
+ return errno;
+ }
+
+ if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
+ return errno;
+ }
+ new_m->usable = new_m->base;
+
+ if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
+ return errno;
+ }
+ apr_uid_current(&uid, &gid, pool);
+ shmbuf.shm_perm.uid = uid;
+ shmbuf.shm_perm.gid = gid;
+ if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
+ return errno;
+ }
+
+ /* Remove the segment once use count hits zero.
+ * We will not attach to this segment again, since it is
+ * anonymous memory, so it is ok to mark it for deletion.
+ */
+ if (shmctl(new_m->shmid, IPC_RMID, NULL) == -1) {
+ return errno;
+ }
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+#else
+ /* 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 */
+#endif
+ }
+
+ /* Name-based shared memory */
+ else {
+ new_m = apr_palloc(pool, sizeof(apr_shm_t));
+ 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 +
+ APR_ALIGN_DEFAULT(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(&file, filename,
+ APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
+ APR_OS_DEFAULT, pool);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ status = apr_os_file_get(&tmpfd, file);
+ if (status != APR_SUCCESS) {
+ apr_file_close(file); /* ignore errors, we're failing */
+ apr_file_remove(new_m->filename, new_m->pool);
+ return status;
+ }
+
+ status = apr_file_trunc(file, new_m->realsize);
+ if (status != APR_SUCCESS) {
+ apr_file_close(file); /* ignore errors, we're failing */
+ apr_file_remove(new_m->filename, new_m->pool);
+ return status;
+ }
+
+ new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
+ MAP_SHARED, tmpfd, 0);
+ /* FIXME: check for errors */
+
+ status = apr_file_close(file);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+#endif /* APR_USE_SHMEM_MMAP_TMP */
+#if APR_USE_SHMEM_MMAP_SHM
+ tmpfd = shm_open(filename, O_RDWR | O_CREAT | O_EXCL, 0644);
+ if (tmpfd == -1) {
+ return errno;
+ }
+
+ status = apr_os_file_put(&file, &tmpfd,
+ APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
+ pool);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ status = apr_file_trunc(file, new_m->realsize);
+ if (status != APR_SUCCESS) {
+ shm_unlink(filename); /* we're failing, remove the object */
+ return status;
+ }
+ new_m->base = mmap(NULL, reqsize, PROT_READ | PROT_WRITE,
+ MAP_SHARED, tmpfd, 0);
+
+ /* FIXME: check for errors */
+
+ status = apr_file_close(file);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+#endif /* APR_USE_SHMEM_MMAP_SHM */
+
+ /* store the real size in the metadata */
+ *(apr_size_t*)(new_m->base) = new_m->realsize;
+ /* metadata isn't usable */
+ new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#elif APR_USE_SHMEM_SHMGET
+ new_m->realsize = reqsize;
+
+ /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */
+ status = apr_file_open(&file, filename,
+ APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
+ APR_OS_DEFAULT, pool);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ /* ftok() (on solaris at least) requires that the file actually
+ * exist before calling ftok(). */
+ shmkey = ftok(filename, 1);
+ if (shmkey == (key_t)-1) {
+ return errno;
+ }
+
+ if ((new_m->shmid = shmget(shmkey, new_m->realsize,
+ SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) {
+ return errno;
+ }
+
+ if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
+ return errno;
+ }
+ new_m->usable = new_m->base;
+
+ if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
+ return errno;
+ }
+ apr_uid_current(&uid, &gid, pool);
+ shmbuf.shm_perm.uid = uid;
+ shmbuf.shm_perm.gid = gid;
+ if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
+ return errno;
+ }
+
+ 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;
+ }
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#else
+ return APR_ENOTIMPL;
+#endif
+ }
+}
+
+APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
+ apr_pool_t *pool)
+{
+#if APR_USE_SHMEM_SHMGET
+ apr_status_t status;
+ apr_file_t *file;
+ key_t shmkey;
+ int shmid;
+#endif
+
+#if APR_USE_SHMEM_MMAP_TMP
+ return apr_file_remove(filename, pool);
+#elif APR_USE_SHMEM_MMAP_SHM
+ if (shm_unlink(filename) == -1) {
+ return errno;
+ }
+ return APR_SUCCESS;
+#elif APR_USE_SHMEM_SHMGET
+ /* Presume that the file already exists; just open for writing */
+ status = apr_file_open(&file, filename, APR_FOPEN_WRITE,
+ APR_OS_DEFAULT, pool);
+ if (status) {
+ return status;
+ }
+
+ /* ftok() (on solaris at least) requires that the file actually
+ * exist before calling ftok(). */
+ shmkey = ftok(filename, 1);
+ if (shmkey == (key_t)-1) {
+ goto shm_remove_failed;
+ }
+
+ apr_file_close(file);
+
+ if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) < 0) {
+ goto shm_remove_failed;
+ }
+
+ /* Indicate that the segment is to be destroyed as soon
+ * as all processes have detached. This also disallows any
+ * new attachments to the segment. */
+ if (shmctl(shmid, IPC_RMID, NULL) == -1) {
+ goto shm_remove_failed;
+ }
+ return apr_file_remove(filename, pool);
+
+shm_remove_failed:
+ status = errno;
+ /* ensure the file has been removed anyway. */
+ apr_file_remove(filename, pool);
+ return status;
+#else
+
+ /* No support for anonymous shm */
+ return APR_ENOTIMPL;
+#endif
+}
+
+APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
+{
+ return apr_pool_cleanup_run(m->pool, m, shm_cleanup_owner);
+}
+
+static apr_status_t shm_cleanup_attach(void *m_)
+{
+ apr_shm_t *m = (apr_shm_t *)m_;
+
+ if (m->filename == NULL) {
+ /* It doesn't make sense to detach from an anonymous memory segment. */
+ return APR_EINVAL;
+ }
+ else {
+#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
+ if (munmap(m->base, m->realsize) == -1) {
+ return errno;
+ }
+ return APR_SUCCESS;
+#elif APR_USE_SHMEM_SHMGET
+ if (shmdt(m->base) == -1) {
+ return errno;
+ }
+ return APR_SUCCESS;
+#else
+ return APR_ENOTIMPL;
+#endif
+ }
+}
+
+APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool)
+{
+ if (filename == NULL) {
+ /* It doesn't make sense to attach to a segment if you don't know
+ * the filename. */
+ return APR_EINVAL;
+ }
+ else {
+#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
+ apr_shm_t *new_m;
+ apr_status_t status;
+ int tmpfd;
+ apr_file_t *file; /* file where metadata is stored */
+ apr_size_t nbytes;
+
+ new_m = apr_palloc(pool, sizeof(apr_shm_t));
+ new_m->pool = pool;
+ new_m->filename = apr_pstrdup(pool, filename);
+
+ status = apr_file_open(&file, filename,
+ APR_READ | APR_WRITE,
+ APR_OS_DEFAULT, pool);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+ status = apr_os_file_get(&tmpfd, file);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ nbytes = sizeof(new_m->realsize);
+ status = apr_file_read(file, (void *)&(new_m->realsize),
+ &nbytes);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ status = apr_os_file_get(&tmpfd, file);
+ if (status != APR_SUCCESS) {
+ apr_file_close(file); /* ignore errors, we're failing */
+ apr_file_remove(new_m->filename, new_m->pool);
+ return status;
+ }
+
+ new_m->reqsize = new_m->realsize - sizeof(apr_size_t);
+
+ new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
+ MAP_SHARED, tmpfd, 0);
+ /* FIXME: check for errors */
+
+ status = apr_file_close(file);
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ /* metadata isn't part of the usable segment */
+ new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#elif APR_USE_SHMEM_SHMGET
+ apr_shm_t *new_m;
+ apr_status_t status;
+ apr_file_t *file; /* file where metadata is stored */
+ apr_size_t nbytes;
+ key_t shmkey;
+
+ new_m = apr_palloc(pool, sizeof(apr_shm_t));
+
+ status = apr_file_open(&file, filename,
+ APR_FOPEN_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->filename = apr_pstrdup(pool, filename);
+ new_m->pool = pool;
+ shmkey = ftok(filename, 1);
+ if (shmkey == (key_t)-1) {
+ return errno;
+ }
+ if ((new_m->shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
+ return errno;
+ }
+ if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
+ return errno;
+ }
+ new_m->usable = new_m->base;
+ new_m->realsize = new_m->reqsize;
+
+ apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
+ apr_pool_cleanup_null);
+ *m = new_m;
+ return APR_SUCCESS;
+
+#else
+ return APR_ENOTIMPL;
+#endif
+ }
+}
+
+APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
+{
+ apr_status_t rv = shm_cleanup_attach(m);
+ apr_pool_cleanup_kill(m->pool, m, shm_cleanup_attach);
+ return rv;
+}
+
+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)
+
+APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
+ apr_shm_t *shm)
+{
+ return APR_ENOTIMPL;
+}
+
+APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
+ apr_os_shm_t *osshm,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
diff --git a/shmem/win32/shm.c b/shmem/win32/shm.c
new file mode 100644
index 0000000..da736aa
--- /dev/null
+++ b/shmem/win32/shm.c
@@ -0,0 +1,317 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_errno.h"
+#include "apr_file_io.h"
+#include "apr_shm.h"
+#include "apr_strings.h"
+#include "apr_arch_file_io.h"
+#include "limits.h"
+
+typedef struct memblock_t {
+ apr_size_t size;
+ apr_size_t length;
+} memblock_t;
+
+struct apr_shm_t {
+ apr_pool_t *pool;
+ memblock_t *memblk;
+ void *usrmem;
+ apr_size_t size;
+ apr_size_t length;
+ HANDLE hMap;
+ const char *filename;
+};
+
+static apr_status_t shm_cleanup(void* shm)
+{
+ apr_status_t rv = APR_SUCCESS;
+ apr_shm_t *m = shm;
+
+ if (!UnmapViewOfFile(m->memblk)) {
+ rv = apr_get_os_error();
+ }
+ if (!CloseHandle(m->hMap)) {
+ rv = rv != APR_SUCCESS ? rv : apr_get_os_error();
+ }
+ if (m->filename) {
+ /* Remove file if file backed */
+ apr_status_t rc = apr_file_remove(m->filename, m->pool);
+ rv = rv != APR_SUCCESS ? rv : rc;
+ }
+ return rv;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *file,
+ apr_pool_t *pool)
+{
+ static apr_size_t memblock = 0;
+ HANDLE hMap, hFile;
+ apr_status_t rv;
+ apr_size_t size;
+ apr_file_t *f;
+ void *base;
+ void *mapkey;
+ DWORD err, sizelo, sizehi;
+
+ reqsize += sizeof(memblock_t);
+
+ if (!memblock)
+ {
+ SYSTEM_INFO si;
+ GetSystemInfo(&si);
+ memblock = si.dwAllocationGranularity;
+ }
+
+ /* Compute the granualar multiple of the pagesize */
+ size = memblock * (1 + (reqsize - 1) / memblock);
+ sizelo = (DWORD)size;
+#ifdef WIN64
+ sizehi = (DWORD)(size >> 32);
+#else
+ sizehi = 0;
+#endif
+
+ if (!file) {
+ /* Do Anonymous, which must be passed as a duplicated handle */
+#ifndef _WIN32_WCE
+ hFile = INVALID_HANDLE_VALUE;
+#endif
+ mapkey = NULL;
+ }
+ else {
+ /* Do file backed, which is not an inherited handle
+ * While we could open APR_EXCL, it doesn't seem that Unix
+ * ever did. Ignore that error here, but fail later when
+ * we discover we aren't the creator of the file map object.
+ */
+ rv = apr_file_open(&f, file,
+ APR_READ | APR_WRITE | APR_BINARY | APR_CREATE,
+ APR_UREAD | APR_UWRITE, pool);
+ if ((rv != APR_SUCCESS)
+ || ((rv = apr_os_file_get(&hFile, f)) != APR_SUCCESS)) {
+ return rv;
+ }
+ rv = apr_file_trunc(f, size);
+
+ /* res_name_from_filename turns file into a pseudo-name
+ * without slashes or backslashes, and prepends the \global
+ * prefix on Win2K and later
+ */
+ mapkey = res_name_from_filename(file, 1, pool);
+ }
+
+#if APR_HAS_UNICODE_FS
+ IF_WIN_OS_IS_UNICODE
+ {
+ hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE,
+ sizehi, sizelo, mapkey);
+ }
+#endif
+#if APR_HAS_ANSI_FS
+ ELSE_WIN_OS_IS_ANSI
+ {
+ hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE,
+ sizehi, sizelo, mapkey);
+ }
+#endif
+ err = apr_get_os_error();
+
+ if (file) {
+ apr_file_close(f);
+ }
+
+ if (hMap && APR_STATUS_IS_EEXIST(err)) {
+ CloseHandle(hMap);
+ return APR_EEXIST;
+ }
+ if (!hMap) {
+ return err;
+ }
+
+ base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE,
+ 0, 0, size);
+ if (!base) {
+ CloseHandle(hMap);
+ return apr_get_os_error();
+ }
+
+ *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
+ (*m)->pool = pool;
+ (*m)->hMap = hMap;
+ (*m)->memblk = base;
+ (*m)->size = size;
+
+ (*m)->usrmem = (char*)base + sizeof(memblock_t);
+ (*m)->length = reqsize - sizeof(memblock_t);;
+
+ (*m)->memblk->length = (*m)->length;
+ (*m)->memblk->size = (*m)->size;
+ (*m)->filename = file ? apr_pstrdup(pool, file) : NULL;
+
+ apr_pool_cleanup_register((*m)->pool, *m,
+ shm_cleanup, apr_pool_cleanup_null);
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
+{
+ apr_status_t rv = shm_cleanup(m);
+ apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
+ return rv;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
+ apr_pool_t *pool)
+{
+ return apr_file_remove(filename, pool);
+}
+
+APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
+ const char *file,
+ apr_pool_t *pool)
+{
+ HANDLE hMap;
+ void *mapkey;
+ void *base;
+
+ if (!file) {
+ return APR_EINVAL;
+ }
+ else {
+ /* res_name_from_filename turns file into a pseudo-name
+ * without slashes or backslashes, and prepends the \global
+ * prefix on Win2K and later
+ */
+ mapkey = res_name_from_filename(file, 1, pool);
+ }
+
+#if APR_HAS_UNICODE_FS
+ IF_WIN_OS_IS_UNICODE
+ {
+#ifndef _WIN32_WCE
+ hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
+#else
+ /* The WCE 3.0 lacks OpenFileMapping. So we emulate one with
+ * opening the existing shmem and reading its size from the header
+ */
+ hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
+ PAGE_READWRITE, 0, sizeof(apr_shm_t), mapkey);
+#endif
+ }
+#endif
+#if APR_HAS_ANSI_FS
+ ELSE_WIN_OS_IS_ANSI
+ {
+ hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
+ }
+#endif
+
+ if (!hMap) {
+ return apr_get_os_error();
+ }
+
+ base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
+ if (!base) {
+ CloseHandle(hMap);
+ return apr_get_os_error();
+ }
+
+ *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
+ (*m)->pool = pool;
+ (*m)->memblk = base;
+ /* Real (*m)->mem->size could be recovered with VirtualQuery */
+ (*m)->size = (*m)->memblk->size;
+#if _WIN32_WCE
+ /* Reopen with real size */
+ UnmapViewOfFile(base);
+ CloseHandle(hMap);
+
+ hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
+ PAGE_READWRITE, 0, (*m)->size, mapkey);
+ if (!hMap) {
+ return apr_get_os_error();
+ }
+ base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
+ if (!base) {
+ CloseHandle(hMap);
+ return apr_get_os_error();
+ }
+#endif
+ (*m)->hMap = hMap;
+ (*m)->length = (*m)->memblk->length;
+ (*m)->usrmem = (char*)base + sizeof(memblock_t);
+ (*m)->filename = NULL;
+
+ apr_pool_cleanup_register((*m)->pool, *m,
+ shm_cleanup, apr_pool_cleanup_null);
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
+{
+ apr_status_t rv = shm_cleanup(m);
+ apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
+ return rv;
+}
+
+APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
+{
+ return m->usrmem;
+}
+
+APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
+{
+ return m->length;
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(shm)
+
+APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
+ apr_shm_t *shm)
+{
+ *osshm = shm->hMap;
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
+ apr_os_shm_t *osshm,
+ apr_pool_t *pool)
+{
+ void* base;
+ base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
+ if (!base) {
+ return apr_get_os_error();
+ }
+
+ *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
+ (*m)->pool = pool;
+ (*m)->hMap = *osshm;
+ (*m)->memblk = base;
+ (*m)->usrmem = (char*)base + sizeof(memblock_t);
+ /* Real (*m)->mem->size could be recovered with VirtualQuery */
+ (*m)->size = (*m)->memblk->size;
+ (*m)->length = (*m)->memblk->length;
+ (*m)->filename = NULL;
+
+ apr_pool_cleanup_register((*m)->pool, *m,
+ shm_cleanup, apr_pool_cleanup_null);
+ return APR_SUCCESS;
+}
+