Release 950727
[wine/multimedia.git] / ipc / dde_mem.c
blobf19b013afdf492d377897e22f41702f9455d2f2b
1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
6 * File: dde_mem.c
7 * Purpose : shared DDE memory functionality for DDE
8 ***************************************************************************
9 */
10 #include <stdio.h>
11 #include <stddebug.h>
12 #include <debug.h>
13 #include <assert.h>
14 #include "ldt.h"
15 #include "shm_main_blk.h"
16 #include "shm_fragment.h"
17 #include "shm_semaph.h"
18 #include "dde_mem.h"
19 #include "bit_array.h"
21 #define SEGPTR2HANDLE_INFO(sptr) ( (struct handle_info*)PTR_SEG_TO_LIN(sptr) )
23 #define HINFO2DATAPTR(h_info_ptr) ( (void*) ( (char*)h_info_ptr + \
24 sizeof(struct handle_info) ) )
25 #define DDE_MEM_IDX(handle) ((handle)& 0x7fff)
26 #define DDE_MEM_HANDLE(idx) ((idx) | 0x8000)
27 #define DDE_MEM_INFO(handle) (main_block->handles[ DDE_MEM_IDX(handle) ])
28 /* List of shared handles.
29 * This entry resides on the shared memory, the data comes right
30 * after the `handle_info'.
31 * The entry is on the same block as the actual data.
32 * The `next' field gives relative reference (relative to the start of
33 * the blcok.
35 struct handle_info {
36 WORD lock_count;
37 WORD flags;
38 int size; /* size of the data (net)*/
41 static bit_array free_handles;
42 int debug_last_handle_size= 0; /* for debugging purpose only */
45 /* locate_handle:
46 * locate a shared memory handle.
47 * Application:
48 * The handle is first searched for in attached blocks.
49 * At the beginning, only blocks owned by this process are
50 * attached.
51 * If a handle is not found, new blocks are attached.
52 * Arguments:
53 * h - the handle.
54 * RETURN: pointer to handle info.
56 static struct handle_info *locate_handle(HGLOBAL h, struct local_shm_map *map)
58 struct shm_block *block;
60 dprintf_global(stddeb,"shm:locate_handle(0x%04x)\n", h);
63 if (SampleBit( &free_handles, DDE_MEM_IDX(h)) == 0) {
64 dprintf_global(stddeb, "shm:locate_handle: return NULL\n");
65 return NULL; /* free!!! */
68 block= shm_locate_block(DDE_MEM_INFO(h).shmid, map);
69 if (block == NULL) {
70 /* nothing found */
71 dprintf_global(stddeb, "shm:locate_handle: return NULL\n");
72 return NULL;
75 return (struct handle_info *) REL2PTR(block, DDE_MEM_INFO(h).rel);
79 /* dde_alloc_handle: allocate shared DDE handle */
80 static HGLOBAL dde_alloc_handle()
82 int bit_nr;
84 bit_nr= AllocateBit( &free_handles);
86 if (bit_nr != -1)
87 return DDE_MEM_HANDLE(bit_nr);
89 dprintf_global(stddeb,"dde_alloc_handle: no free DDE handle found\n");
90 return 0;
92 /**********************************************************************
93 * DDE_malloc
95 void *
96 DDE_malloc(unsigned int flags, unsigned long size, SHMDATA *shmdata)
98 int shmid;
99 struct shm_block *block;
100 struct handle_info *h_info;
101 struct local_shm_map *curr;
102 HGLOBAL handle;
104 dprintf_global(stddeb,"DDE_malloc flags %4X, size %ld\n", flags, size);
105 DDE_IPC_init(); /* make sure main shm block allocated */
107 shm_write_wait(main_block->proc[curr_proc_idx].sem);
109 /* Try to find fragment big enough for `size' */
110 /* iterate through all local shm blocks, and try to allocate
111 the fragment */
113 h_info= NULL;
114 for (curr= shm_map ; curr != NULL ; curr= curr->next) {
115 if (curr->proc_idx == curr_proc_idx) {
116 h_info= (struct handle_info *)
117 shm_FragPtrAlloc(curr->ptr, size+sizeof(struct handle_info));
118 if (h_info!=NULL) {
119 shmid= curr->shm_id;
120 break;
125 if (h_info == NULL) {
127 block= shm_create_block(0, size+sizeof(struct handle_info), &shmid);
128 if (block==NULL) {
129 shm_write_signal(main_block->proc[curr_proc_idx].sem);
130 return 0;
132 /* put the new block in the linked list */
133 block->next_shm_id= main_block->proc[curr_proc_idx].shmid;
134 main_block->proc[curr_proc_idx].shmid= shmid;
135 h_info= (struct handle_info *)
136 shm_FragPtrAlloc(block, size+sizeof(struct handle_info));
137 if (h_info==NULL) {
138 fprintf(stderr,"DDE_malloc: BUG! unallocated fragment\n");
139 shm_write_signal(main_block->proc[curr_proc_idx].sem);
140 return 0;
142 } else {
143 block= curr->ptr;
146 /* Here we have an allocated fragment */
147 h_info->flags= flags;
148 h_info->lock_count= 0;
149 h_info->size= size;
150 handle= dde_alloc_handle();
152 if (handle) {
153 dprintf_global(stddeb,
154 "DDE_malloc returning handle=0x%4x, ptr=0x%08lx\n",
155 (int)handle, (long) HINFO2DATAPTR(h_info));
156 DDE_MEM_INFO(handle).rel= PTR2REL(block, h_info);
157 DDE_MEM_INFO(handle).shmid= shmid;
159 else
160 dprintf_global(stddeb,"DDE_malloc failed\n");
162 shm_write_signal(main_block->proc[curr_proc_idx].sem);
164 shmdata->handle= handle;
165 return (char *)HINFO2DATAPTR(h_info);
168 HGLOBAL DDE_GlobalFree(HGLOBAL h)
170 struct handle_info *h_info;
171 int handle_index= h & 0x7fff;
172 struct local_shm_map map;
174 dprintf_global(stddeb,"DDE_GlobalFree(0x%04x)\n",h);
176 if (h==0)
177 return 0;
179 h_info= locate_handle(h, &map);
180 if (h_info == NULL)
181 return h;
183 shm_write_wait(main_block->proc[map.proc_idx].sem);
185 shm_FragPtrFree(map.ptr, (struct shm_fragment *) h_info);
187 AssignBit( &free_handles, handle_index, 0);
189 /* FIXME: must free the shm block some day. */
190 shm_write_signal(main_block->proc[map.proc_idx].sem);
191 return 0;
194 WORD DDE_SyncHandle(HGLOBAL handle, WORD sel)
197 struct handle_info *h_info;
198 void *local_ptr;
199 ldt_entry entry;
201 h_info= locate_handle(handle, NULL);
202 local_ptr= (void *)GET_SEL_BASE(sel);
205 if (h_info == NULL)
206 return 0;
208 if (local_ptr == (void *) HINFO2DATAPTR(h_info))
209 return sel;
211 /* need syncronization ! */
212 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
213 entry.base= (unsigned long) HINFO2DATAPTR(h_info);
214 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
216 return sel;
220 * DDE_AttachHandle:
221 * Attach shm memory (The data must not be already attached).
222 * Parameters:
223 * handle - the memory to attach.
224 * segptr - in not null, return SEGPTR to the same block.
225 * return value:
226 * 32 bit pointer to the memory.
229 void *DDE_AttachHandle(HGLOBAL handle, SEGPTR *segptr)
231 struct handle_info *h_info;
232 SHMDATA shmdata;
233 void *ptr;
234 HGLOBAL hOwner = GetCurrentPDB();
236 assert(is_dde_handle(handle));
237 if (segptr != NULL)
238 *segptr=0;
240 dprintf_global(stddeb,"DDE_AttachHandle(%04x)\n",handle);
241 h_info=locate_handle(handle, NULL);
243 if (h_info == NULL)
244 return NULL;
246 if ( !(h_info->flags & GMEM_DDESHARE) ) {
247 fprintf(stderr,"DDE_AttachHandle: Corrupted memory handle info\n");
248 return NULL;
251 dprintf_global(stddeb,"DDE_AttachHandle: h_info=%06lx\n",(long)h_info);
253 shmdata.handle= handle;
254 shmdata.shmid= DDE_MEM_INFO(handle).shmid;
256 ptr= HINFO2DATAPTR(h_info);
257 /* Allocate the selector(s) */
258 if (! GLOBAL_CreateBlock( h_info->flags, ptr, h_info->size, hOwner,
259 FALSE, FALSE, FALSE, &shmdata))
260 return NULL;
262 if (segptr != NULL)
263 *segptr= (SEGPTR)MAKELONG( 0, shmdata.sel);
265 if (debugging_dde)
266 debug_last_handle_size= h_info->size;
268 dprintf_global(stddeb,"DDE_AttachHandle returns ptr=0x%08lx\n", (long)ptr);
270 return (LPSTR)ptr;
274 void DDE_mem_init()
276 int nr_of_bits;
278 shm_init();
280 nr_of_bits= BITS_PER_BYTE * sizeof(main_block->free_handles);
281 AssembleArray( &free_handles, main_block->free_handles, nr_of_bits);