upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / shmem / beos / 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 <stdio.h>
23 #include <stdlib.h>
24 #include <kernel/OS.h>
25 #include "apr_portable.h"
26
27 struct apr_shm_t {
28     apr_pool_t *pool;
29     void *memblock;
30     void *ptr;
31     apr_size_t reqsize;
32     apr_size_t avail;
33     area_id aid;
34 };
35
36 APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m, 
37                                          apr_size_t reqsize, 
38                                          const char *file, 
39                                          apr_pool_t *p)
40 {
41     apr_size_t pagesize;
42     area_id newid;
43     char *addr;
44     
45     (*m) = (apr_shm_t *)apr_pcalloc(p, sizeof(apr_shm_t));
46     /* we MUST allocate in pages, so calculate how big an area we need... */
47     pagesize = ((reqsize + B_PAGE_SIZE - 1) / B_PAGE_SIZE) * B_PAGE_SIZE;
48      
49     newid = create_area("apr_shmem", (void*)&addr, B_ANY_ADDRESS,
50                         pagesize, B_CONTIGUOUS, B_READ_AREA|B_WRITE_AREA);
51
52     if (newid < 0)
53         return errno;
54
55     (*m)->pool = p;
56     (*m)->aid = newid;
57     (*m)->memblock = addr;
58     (*m)->ptr = (void*)addr;
59     (*m)->avail = pagesize; /* record how big an area we actually created... */
60     (*m)->reqsize = reqsize;
61
62     return APR_SUCCESS;
63 }
64
65 APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
66 {
67     delete_area(m->aid);
68     m->avail = 0;
69     m->memblock = NULL;
70     return APR_SUCCESS;
71 }
72
73
74 APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
75                                          const char *filename,
76                                          apr_pool_t *pool)
77 {
78     area_info ai;
79     thread_info ti;
80     area_id deleteme;
81     apr_shm_t *new_m;
82     
83     deleteme = find_area(filename);
84     if (deleteme == B_NAME_NOT_FOUND)
85         return APR_EINVAL;
86
87     new_m = (apr_shm_t*)apr_palloc(pool, sizeof(apr_shm_t*));
88     if (new_m == NULL)
89         return APR_ENOMEM;
90     new_m->pool = pool;
91
92     get_area_info(deleteme, &ai);
93     get_thread_info(find_thread(NULL), &ti);
94
95     if (ti.team != ai.team) {
96         area_id narea;
97         
98         narea = clone_area(ai.name, &(ai.address), B_CLONE_ADDRESS,
99                            B_READ_AREA|B_WRITE_AREA, ai.area);
100
101         if (narea < B_OK)
102             return narea;
103             
104         get_area_info(narea, &ai);
105         new_m->aid = narea;
106         new_m->memblock = ai.address;
107         new_m->ptr = (void*)ai.address;
108         new_m->avail = ai.size;
109         new_m->reqsize = ai.size;
110     }
111
112     (*m) = new_m;
113     
114     return APR_SUCCESS;
115 }
116
117 APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
118 {
119     delete_area(m->aid);
120     return APR_SUCCESS;
121 }
122
123 APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
124 {
125     return m->memblock;
126 }
127
128 APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
129 {
130     return m->reqsize;
131 }
132
133 APR_POOL_IMPLEMENT_ACCESSOR(shm)
134
135 APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
136                                          apr_shm_t *shm)
137 {
138     return APR_ENOTIMPL;
139 }
140
141 APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
142                                          apr_os_shm_t *osshm,
143                                          apr_pool_t *pool)
144 {
145     return APR_ENOTIMPL;
146 }    
147