upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / shmem / os2 / shm.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_general.h"
18 #include "apr_shm.h"
19 #include "apr_errno.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "apr_portable.h"
23
24 struct apr_shm_t {
25     apr_pool_t *pool;
26     void *memblock;
27 };
28
29 APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
30                                          apr_size_t reqsize,
31                                          const char *filename,
32                                          apr_pool_t *pool)
33 {
34     int rc;
35     apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
36     char *name = NULL;
37     ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
38
39     newm->pool = pool;
40
41     if (filename) {
42         name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
43     }
44
45     if (name == NULL) {
46         flags |= OBJ_GETTABLE;
47     }
48
49     rc = DosAllocSharedMem(&(newm->memblock), name, reqsize, flags);
50
51     if (rc) {
52         return APR_OS2_STATUS(rc);
53     }
54
55     *m = newm;
56     return APR_SUCCESS;
57 }
58
59 APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
60 {
61     DosFreeMem(m->memblock);
62     return APR_SUCCESS;
63 }
64
65 APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
66                                          const char *filename,
67                                          apr_pool_t *pool)
68 {
69     int rc;
70     apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
71     char *name = NULL;
72     ULONG flags = PAG_READ|PAG_WRITE;
73
74     newm->pool = pool;
75     name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
76
77     rc = DosGetNamedSharedMem(&(newm->memblock), name, flags);
78
79     if (rc) {
80         return APR_FROM_OS_ERROR(rc);
81     }
82
83     *m = newm;
84     return APR_SUCCESS;
85 }
86
87 APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
88 {
89     int rc = 0;
90
91     if (m->memblock) {
92         rc = DosFreeMem(m->memblock);
93     }
94
95     return APR_FROM_OS_ERROR(rc);
96 }
97
98 APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
99 {
100     return m->memblock;
101 }
102
103 APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
104 {
105     ULONG flags, size = 0x1000000;
106     DosQueryMem(m->memblock, &size, &flags);
107     return size;
108 }
109
110 APR_POOL_IMPLEMENT_ACCESSOR(shm)
111
112 APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
113                                          apr_shm_t *shm)
114 {
115     *osshm = shm->memblock;
116     return APR_SUCCESS;
117 }
118
119 APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
120                                          apr_os_shm_t *osshm,
121                                          apr_pool_t *pool)
122 {
123     int rc;
124     apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
125     ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
126
127     newm->pool = pool;
128
129     rc = DosGetSharedMem(&(newm->memblock), flags);
130
131     if (rc) {
132         return APR_FROM_OS_ERROR(rc);
133     }
134
135     *m = newm;
136     return APR_SUCCESS;
137 }    
138