- better stubs for Get/Set special (Progman/Taskman) window functions
[wine/hacks.git] / ipc / dde_mem.c
blobd6d3693bace03731a7754350ed8e0eacb9df5ab1
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 #ifdef CONFIG_IPC
12 #include <assert.h>
13 #include "debug.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 DECLARE_DEBUG_CHANNEL(dde)
22 DECLARE_DEBUG_CHANNEL(global)
24 #define SEGPTR2HANDLE_INFO(sptr) ( (struct handle_info*)PTR_SEG_TO_LIN(sptr) )
26 #define HINFO2DATAPTR(h_info_ptr) ( (void*) ( (char*)h_info_ptr + \
27 sizeof(struct handle_info) ) )
28 #define DDE_MEM_IDX(handle) ((handle)& 0x7fff)
29 #define DDE_MEM_HANDLE(idx) ((idx) | 0x8000)
30 #define DDE_MEM_INFO(handle) (main_block->handles[ DDE_MEM_IDX(handle) ])
31 /* List of shared handles.
32 * This entry resides on the shared memory, the data comes right
33 * after the `handle_info'.
34 * The entry is on the same block as the actual data.
35 * The `next' field gives relative reference (relative to the start of
36 * the blcok.
38 struct handle_info {
39 WORD lock_count;
40 WORD flags;
41 int size; /* size of the data (net)*/
44 static bit_array free_handles;
45 int debug_last_handle_size= 0; /* for debugging purpose only */
48 /* locate_handle:
49 * locate a shared memory handle.
50 * Application:
51 * The handle is first searched for in attached blocks.
52 * At the beginning, only blocks owned by this process are
53 * attached.
54 * If a handle is not found, new blocks are attached.
55 * Arguments:
56 * h - the handle.
57 * RETURN: pointer to handle info.
59 static struct handle_info *locate_handle(HGLOBAL16 h, struct local_shm_map *map)
61 struct shm_block *block;
63 TRACE(global,"shm: (0x%04x)\n", h);
66 if (SampleBit( &free_handles, DDE_MEM_IDX(h)) == 0) {
67 TRACE(global, "shm: return NULL\n");
68 return NULL; /* free!!! */
71 block= shm_locate_block(DDE_MEM_INFO(h).shmid, map);
72 if (block == NULL) {
73 /* nothing found */
74 TRACE(global, "shm: return NULL\n");
75 return NULL;
78 return (struct handle_info *) REL2PTR(block, DDE_MEM_INFO(h).rel);
82 /* dde_alloc_handle: allocate shared DDE handle */
83 static HGLOBAL16 dde_alloc_handle()
85 int bit_nr;
87 bit_nr= AllocateBit( &free_handles);
89 if (bit_nr != -1)
90 return DDE_MEM_HANDLE(bit_nr);
92 TRACE(global,"dde_alloc_handle: no free DDE handle found\n");
93 return 0;
95 /**********************************************************************
96 * DDE_malloc
98 void *
99 DDE_malloc(unsigned int flags, unsigned long size, SHMDATA *shmdata)
101 int shmid;
102 struct shm_block *block;
103 struct handle_info *h_info;
104 struct local_shm_map *curr;
105 HGLOBAL16 handle;
107 TRACE(global,"DDE_malloc flags %4X, size %ld\n", flags, size);
108 DDE_IPC_init(); /* make sure main shm block allocated */
110 shm_write_wait(main_block->proc[curr_proc_idx].sem);
112 /* Try to find fragment big enough for `size' */
113 /* iterate through all local shm blocks, and try to allocate
114 the fragment */
116 h_info= NULL;
117 for (curr= shm_map ; curr != NULL ; curr= curr->next) {
118 if (curr->proc_idx == curr_proc_idx) {
119 h_info= (struct handle_info *)
120 shm_FragPtrAlloc(curr->ptr, size+sizeof(struct handle_info));
121 if (h_info!=NULL) {
122 shmid= curr->shm_id;
123 break;
128 if (h_info == NULL) {
130 block= shm_create_block(0, size+sizeof(struct handle_info), &shmid);
131 if (block==NULL) {
132 shm_write_signal(main_block->proc[curr_proc_idx].sem);
133 return 0;
135 /* put the new block in the linked list */
136 block->next_shm_id= main_block->proc[curr_proc_idx].shmid;
137 main_block->proc[curr_proc_idx].shmid= shmid;
138 h_info= (struct handle_info *)
139 shm_FragPtrAlloc(block, size+sizeof(struct handle_info));
140 if (h_info==NULL) {
141 ERR(global,"BUG! unallocated fragment\n");
142 shm_write_signal(main_block->proc[curr_proc_idx].sem);
143 return 0;
145 } else {
146 block= curr->ptr;
149 /* Here we have an allocated fragment */
150 h_info->flags= flags;
151 h_info->lock_count= 0;
152 h_info->size= size;
153 handle= dde_alloc_handle();
155 if (handle) {
156 TRACE(global, "returning handle=0x%4x, ptr=0x%08lx\n",
157 (int)handle, (long) HINFO2DATAPTR(h_info));
158 DDE_MEM_INFO(handle).rel= PTR2REL(block, h_info);
159 DDE_MEM_INFO(handle).shmid= shmid;
161 else
162 WARN(global, "failed\n");
164 shm_write_signal(main_block->proc[curr_proc_idx].sem);
166 shmdata->handle= handle;
167 return (char *)HINFO2DATAPTR(h_info);
170 HGLOBAL16 DDE_GlobalFree(HGLOBAL16 h)
172 struct handle_info *h_info;
173 int handle_index= h & 0x7fff;
174 struct local_shm_map map;
176 TRACE(global,"(0x%04x)\n",h);
178 if (h==0)
179 return 0;
181 h_info= locate_handle(h, &map);
182 if (h_info == NULL)
183 return h;
185 shm_write_wait(main_block->proc[map.proc_idx].sem);
187 shm_FragPtrFree(map.ptr, (struct shm_fragment *) h_info);
189 AssignBit( &free_handles, handle_index, 0);
191 /* FIXME: must free the shm block some day. */
192 shm_write_signal(main_block->proc[map.proc_idx].sem);
193 return 0;
196 WORD DDE_SyncHandle(HGLOBAL16 handle, WORD sel)
199 struct handle_info *h_info;
200 void *local_ptr;
201 ldt_entry entry;
203 h_info= locate_handle(handle, NULL);
204 local_ptr= (void *)GET_SEL_BASE(sel);
207 if (h_info == NULL)
208 return 0;
210 if (local_ptr == (void *) HINFO2DATAPTR(h_info))
211 return sel;
213 /* need syncronization ! */
214 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
215 entry.base= (unsigned long) HINFO2DATAPTR(h_info);
216 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
218 return sel;
222 * DDE_AttachHandle:
223 * Attach shm memory (The data must not be already attached).
224 * Parameters:
225 * handle - the memory to attach.
226 * segptr - in not null, return SEGPTR to the same block.
227 * return value:
228 * 32 bit pointer to the memory.
231 void *DDE_AttachHandle(HGLOBAL16 handle, SEGPTR *segptr)
233 struct handle_info *h_info;
234 SHMDATA shmdata;
235 void *ptr;
236 HGLOBAL16 hOwner = GetCurrentPDB16();
238 assert(is_dde_handle(handle));
239 if (segptr != NULL)
240 *segptr=0;
242 TRACE(global,"(%04x)\n",handle);
243 h_info=locate_handle(handle, NULL);
245 if (h_info == NULL)
246 return NULL;
248 if ( !(h_info->flags & GMEM_DDESHARE) ) {
249 ERR(global,"Corrupted memory handle info\n");
250 return NULL;
253 TRACE(global,"h_info=%06lx\n",(long)h_info);
255 shmdata.handle= handle;
256 shmdata.shmid= DDE_MEM_INFO(handle).shmid;
258 ptr= HINFO2DATAPTR(h_info);
259 /* Allocate the selector(s) */
260 if (! GLOBAL_CreateBlock( h_info->flags, ptr, h_info->size, hOwner,
261 FALSE, FALSE, FALSE, &shmdata))
262 return NULL;
264 if (segptr != NULL)
265 *segptr= (SEGPTR)MAKELONG( 0, shmdata.sel);
267 if (TRACE_ON(dde))
268 debug_last_handle_size= h_info->size;
270 TRACE(global,"DDE_AttachHandle returns ptr=0x%08lx\n", (long)ptr);
272 return (LPSTR)ptr;
276 void DDE_mem_init()
278 int nr_of_bits;
280 shm_init();
282 nr_of_bits= BITS_PER_BYTE * sizeof(main_block->free_handles);
283 AssembleArray( &free_handles, main_block->free_handles, nr_of_bits);
286 #endif /* CONFIG_IPC */