xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/shmem/os2/shm.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
apr_shm_create(apr_shm_t ** m,apr_size_t reqsize,const char * filename,apr_pool_t * pool)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 
apr_shm_create_ex(apr_shm_t ** m,apr_size_t reqsize,const char * filename,apr_pool_t * p,apr_int32_t flags)59 APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
60                                             apr_size_t reqsize,
61                                             const char *filename,
62                                             apr_pool_t *p,
63                                             apr_int32_t flags)
64 {
65     return apr_shm_create(m, reqsize, filename, p);
66 }
67 
apr_shm_destroy(apr_shm_t * m)68 APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
69 {
70     DosFreeMem(m->memblock);
71     return APR_SUCCESS;
72 }
73 
apr_shm_remove(const char * filename,apr_pool_t * pool)74 APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
75                                          apr_pool_t *pool)
76 {
77     return APR_ENOTIMPL;
78 }
79 
apr_shm_attach(apr_shm_t ** m,const char * filename,apr_pool_t * pool)80 APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
81                                          const char *filename,
82                                          apr_pool_t *pool)
83 {
84     int rc;
85     apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
86     char *name = NULL;
87     ULONG flags = PAG_READ|PAG_WRITE;
88 
89     newm->pool = pool;
90     name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
91 
92     rc = DosGetNamedSharedMem(&(newm->memblock), name, flags);
93 
94     if (rc) {
95         return APR_FROM_OS_ERROR(rc);
96     }
97 
98     *m = newm;
99     return APR_SUCCESS;
100 }
101 
apr_shm_attach_ex(apr_shm_t ** m,const char * filename,apr_pool_t * pool,apr_int32_t flags)102 APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
103                                             const char *filename,
104                                             apr_pool_t *pool,
105                                             apr_int32_t flags)
106 {
107     return apr_shm_attach(m, filename, pool);
108 }
109 
apr_shm_detach(apr_shm_t * m)110 APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
111 {
112     int rc = 0;
113 
114     if (m->memblock) {
115         rc = DosFreeMem(m->memblock);
116     }
117 
118     return APR_FROM_OS_ERROR(rc);
119 }
120 
apr_shm_baseaddr_get(const apr_shm_t * m)121 APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
122 {
123     return m->memblock;
124 }
125 
apr_shm_size_get(const apr_shm_t * m)126 APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
127 {
128     ULONG flags, size = 0x1000000;
129     DosQueryMem(m->memblock, &size, &flags);
130     return size;
131 }
132 
133 APR_POOL_IMPLEMENT_ACCESSOR(shm)
134 
APR_DECLARE(apr_status_t)135 APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
136                                          apr_shm_t *shm)
137 {
138     *osshm = shm->memblock;
139     return APR_SUCCESS;
140 }
141 
apr_os_shm_put(apr_shm_t ** m,apr_os_shm_t * osshm,apr_pool_t * pool)142 APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
143                                          apr_os_shm_t *osshm,
144                                          apr_pool_t *pool)
145 {
146     int rc;
147     apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
148     ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
149 
150     newm->pool = pool;
151 
152     rc = DosGetSharedMem(&(newm->memblock), flags);
153 
154     if (rc) {
155         return APR_FROM_OS_ERROR(rc);
156     }
157 
158     *m = newm;
159     return APR_SUCCESS;
160 }
161 
162