1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
7 * Purpose : shared DDE memory functionality for DDE
8 ***************************************************************************
13 #include "debugtools.h"
15 #include "shm_main_blk.h"
16 #include "shm_fragment.h"
17 #include "shm_semaph.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
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 */
49 * locate a shared memory handle.
51 * The handle is first searched for in attached blocks.
52 * At the beginning, only blocks owned by this process are
54 * If a handle is not found, new blocks are attached.
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
);
74 TRACE_(global
)("shm: return NULL\n");
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()
87 bit_nr
= AllocateBit( &free_handles
);
90 return DDE_MEM_HANDLE(bit_nr
);
92 TRACE_(global
)("dde_alloc_handle: no free DDE handle found\n");
95 /**********************************************************************
99 DDE_malloc(unsigned int flags
, unsigned long size
, SHMDATA
*shmdata
)
102 struct shm_block
*block
;
103 struct handle_info
*h_info
;
104 struct local_shm_map
*curr
;
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
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
));
128 if (h_info
== NULL
) {
130 block
= shm_create_block(0, size
+sizeof(struct handle_info
), &shmid
);
132 shm_write_signal(main_block
->proc
[curr_proc_idx
].sem
);
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
));
141 ERR_(global
)("BUG! unallocated fragment\n");
142 shm_write_signal(main_block
->proc
[curr_proc_idx
].sem
);
149 /* Here we have an allocated fragment */
150 h_info
->flags
= flags
;
151 h_info
->lock_count
= 0;
153 handle
= dde_alloc_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
;
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
);
181 h_info
= locate_handle(h
, &map
);
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
);
196 WORD
DDE_SyncHandle(HGLOBAL16 handle
, WORD sel
)
199 struct handle_info
*h_info
;
203 h_info
= locate_handle(handle
, NULL
);
204 local_ptr
= (void *)GET_SEL_BASE(sel
);
210 if (local_ptr
== (void *) HINFO2DATAPTR(h_info
))
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
);
223 * Attach shm memory (The data must not be already attached).
225 * handle - the memory to attach.
226 * segptr - in not null, return SEGPTR to the same block.
228 * 32 bit pointer to the memory.
231 void *DDE_AttachHandle(HGLOBAL16 handle
, SEGPTR
*segptr
)
233 struct handle_info
*h_info
;
236 HGLOBAL16 hOwner
= GetCurrentPDB16();
238 assert(is_dde_handle(handle
));
242 TRACE_(global
)("(%04x)\n",handle
);
243 h_info
=locate_handle(handle
, NULL
);
248 if ( !(h_info
->flags
& GMEM_DDESHARE
) ) {
249 ERR_(global
)("Corrupted memory handle info\n");
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
))
265 *segptr
= (SEGPTR
)MAKELONG( 0, shmdata
.sel
);
268 debug_last_handle_size
= h_info
->size
;
270 TRACE_(global
)("DDE_AttachHandle returns ptr=0x%08lx\n", (long)ptr
);
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 */