Release 980628
[wine/multimedia.git] / memory / selector.c
blobf6290d871b04d5953aba9b15f001969238d1c671
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <string.h>
8 #include "windows.h"
9 #include "ldt.h"
10 #include "miscemu.h"
11 #include "selectors.h"
12 #include "stackframe.h"
13 #include "debug.h"
16 /***********************************************************************
17 * AllocSelectorArray (KERNEL.206)
19 WORD WINAPI AllocSelectorArray( WORD count )
21 WORD i, sel, size = 0;
22 ldt_entry entry;
24 if (!count) return 0;
25 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
27 if (!IS_LDT_ENTRY_FREE(i)) size = 0;
28 else if (++size >= count) break;
30 if (i == LDT_SIZE) return 0;
31 sel = i - size + 1;
33 entry.base = 0;
34 entry.type = SEGMENT_DATA;
35 entry.seg_32bit = FALSE;
36 entry.read_only = FALSE;
37 entry.limit_in_pages = FALSE;
38 entry.limit = 1; /* avoid 0 base and limit */
40 for (i = 0; i < count; i++)
42 /* Mark selector as allocated */
43 ldt_flags_copy[sel + i] |= LDT_FLAGS_ALLOCATED;
44 LDT_SetEntry( sel + i, &entry );
46 return ENTRY_TO_SELECTOR( sel );
50 /***********************************************************************
51 * AllocSelector (KERNEL.175)
53 WORD WINAPI AllocSelector( WORD sel )
55 WORD newsel, count, i;
57 count = sel ? ((GET_SEL_LIMIT(sel) >> 16) + 1) : 1;
58 newsel = AllocSelectorArray( count );
59 TRACE(selector, "(%04x): returning %04x\n",
60 sel, newsel );
61 if (!newsel) return 0;
62 if (!sel) return newsel; /* nothing to copy */
63 for (i = 0; i < count; i++)
65 ldt_entry entry;
66 LDT_GetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
67 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel) + i, &entry );
69 return newsel;
73 /***********************************************************************
74 * FreeSelector (KERNEL.176)
76 WORD WINAPI FreeSelector( WORD sel )
78 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
79 SELECTOR_FreeBlock( sel, 1 );
80 return 0;
84 /***********************************************************************
85 * SELECTOR_SetEntries
87 * Set the LDT entries for an array of selectors.
89 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size,
90 enum seg_type type, BOOL32 is32bit,
91 BOOL32 readonly )
93 ldt_entry entry;
94 WORD i, count;
96 /* The limit for the first selector is the whole */
97 /* block. The next selectors get a 64k limit. */
98 entry.base = (unsigned long)base;
99 entry.type = type;
100 entry.seg_32bit = is32bit;
101 entry.read_only = readonly;
102 entry.limit_in_pages = (size > 0x100000);
103 if (entry.limit_in_pages) entry.limit = ((size + 0xfff) >> 12) - 1;
104 else entry.limit = size - 1;
105 /* Make sure base and limit are not 0 together if the size is not 0 */
106 if (!base && !entry.limit && size) entry.limit = 1;
107 count = (size + 0xffff) / 0x10000;
108 for (i = 0; i < count; i++)
110 LDT_SetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
111 entry.base += 0x10000;
112 /* Apparently the next selectors should *not* get a 64k limit. */
113 /* Can't remember where I read they should... --AJ */
114 entry.limit -= entry.limit_in_pages ? 0x10 : 0x10000;
119 /***********************************************************************
120 * SELECTOR_AllocBlock
122 * Allocate selectors for a block of linear memory.
124 WORD SELECTOR_AllocBlock( const void *base, DWORD size, enum seg_type type,
125 BOOL32 is32bit, BOOL32 readonly )
127 WORD sel, count;
129 if (!size) return 0;
130 count = (size + 0xffff) / 0x10000;
131 sel = AllocSelectorArray( count );
132 if (sel) SELECTOR_SetEntries( sel, base, size, type, is32bit, readonly );
133 return sel;
137 /***********************************************************************
138 * SELECTOR_MoveBlock
140 * Move a block of selectors in linear memory.
142 void SELECTOR_MoveBlock( WORD sel, const void *new_base )
144 WORD i, count = (GET_SEL_LIMIT(sel) >> 16) + 1;
146 for (i = 0; i < count; i++)
148 ldt_entry entry;
149 LDT_GetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
150 entry.base = (unsigned long)new_base;
151 LDT_SetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
156 /***********************************************************************
157 * SELECTOR_FreeBlock
159 * Free a block of selectors.
161 void SELECTOR_FreeBlock( WORD sel, WORD count )
163 WORD i, nextsel;
164 ldt_entry entry;
165 STACK16FRAME *frame;
167 TRACE(selector, "(%04x,%d)\n", sel, count );
168 sel &= ~(__AHINCR - 1); /* clear bottom bits of selector */
169 nextsel = sel + (count << __AHSHIFT);
171 #ifdef __i386__
173 /* Check if we are freeing current %fs or %gs selector */
175 WORD fs, gs;
176 GET_FS(fs);
177 if ((fs >= sel) && (fs < nextsel))
179 WARN(selector, "Freeing %%fs selector (%04x), not good.\n", fs );
180 SET_FS( 0 );
182 GET_GS(gs);
183 if ((gs >= sel) && (gs < nextsel)) SET_GS( 0 );
185 #endif /* __i386__ */
187 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
188 for (i = SELECTOR_TO_ENTRY(sel); count; i++, count--)
190 LDT_SetEntry( i, &entry );
191 ldt_flags_copy[i] &= ~LDT_FLAGS_ALLOCATED;
194 /* Clear the saved 16-bit selector */
195 frame = CURRENT_STACK16;
196 while (frame && frame->frame32)
198 if ((frame->ds >= sel) && (frame->ds < nextsel)) frame->ds = 0;
199 if ((frame->es >= sel) && (frame->es < nextsel)) frame->es = 0;
200 frame = PTR_SEG_TO_LIN( frame->frame32->frame16 );
205 /***********************************************************************
206 * SELECTOR_ReallocBlock
208 * Change the size of a block of selectors.
210 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size,
211 enum seg_type type, BOOL32 is32bit, BOOL32 readonly)
213 WORD i, oldcount, newcount;
215 if (!size) size = 1;
216 oldcount = (GET_SEL_LIMIT(sel) >> 16) + 1;
217 newcount = (size + 0xffff) >> 16;
219 if (oldcount < newcount) /* We need to add selectors */
221 /* Check if the next selectors are free */
222 if (SELECTOR_TO_ENTRY(sel) + newcount > LDT_SIZE) i = oldcount;
223 else
224 for (i = oldcount; i < newcount; i++)
225 if (!IS_LDT_ENTRY_FREE(SELECTOR_TO_ENTRY(sel)+i)) break;
227 if (i < newcount) /* they are not free */
229 SELECTOR_FreeBlock( sel, oldcount );
230 sel = AllocSelectorArray( newcount );
232 else /* mark the selectors as allocated */
234 for (i = oldcount; i < newcount; i++)
235 ldt_flags_copy[SELECTOR_TO_ENTRY(sel)+i] |=LDT_FLAGS_ALLOCATED;
238 else if (oldcount > newcount) /* We need to remove selectors */
240 SELECTOR_FreeBlock( ENTRY_TO_SELECTOR(SELECTOR_TO_ENTRY(sel)+newcount),
241 oldcount - newcount );
243 if (sel) SELECTOR_SetEntries( sel, base, size, type, is32bit, readonly );
244 return sel;
248 /***********************************************************************
249 * PrestoChangoSelector (KERNEL.177)
251 WORD WINAPI PrestoChangoSelector( WORD selSrc, WORD selDst )
253 ldt_entry entry;
254 LDT_GetEntry( SELECTOR_TO_ENTRY( selSrc ), &entry );
255 entry.type ^= SEGMENT_CODE; /* toggle the executable bit */
256 LDT_SetEntry( SELECTOR_TO_ENTRY( selDst ), &entry );
257 return selDst;
261 /***********************************************************************
262 * AllocCStoDSAlias (KERNEL.170)
264 WORD WINAPI AllocCStoDSAlias( WORD sel )
266 WORD newsel;
267 ldt_entry entry;
269 newsel = AllocSelectorArray( 1 );
270 TRACE(selector, "(%04x): returning %04x\n",
271 sel, newsel );
272 if (!newsel) return 0;
273 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
274 entry.type = SEGMENT_DATA;
275 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
276 return newsel;
280 /***********************************************************************
281 * AllocDStoCSAlias (KERNEL.171)
283 WORD WINAPI AllocDStoCSAlias( WORD sel )
285 WORD newsel;
286 ldt_entry entry;
288 newsel = AllocSelectorArray( 1 );
289 TRACE(selector, "(%04x): returning %04x\n",
290 sel, newsel );
291 if (!newsel) return 0;
292 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
293 entry.type = SEGMENT_CODE;
294 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
295 return newsel;
299 /***********************************************************************
300 * LongPtrAdd (KERNEL.180)
302 void WINAPI LongPtrAdd( DWORD ptr, DWORD add )
304 ldt_entry entry;
305 LDT_GetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
306 entry.base += add;
307 LDT_SetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
311 /***********************************************************************
312 * GetSelectorBase (KERNEL.186)
314 DWORD WINAPI GetSelectorBase( WORD sel )
316 DWORD base = GET_SEL_BASE(sel);
318 /* if base points into DOSMEM, assume we have to
319 * return pointer into physical lower 1MB */
321 return DOSMEM_MapLinearToDos( (LPVOID)base );
325 /***********************************************************************
326 * SetSelectorBase (KERNEL.187)
328 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
330 ldt_entry entry;
332 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
334 entry.base = (DWORD)DOSMEM_MapDosToLinear(base);
336 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
337 return sel;
341 /***********************************************************************
342 * GetSelectorLimit (KERNEL.188)
344 DWORD WINAPI GetSelectorLimit( WORD sel )
346 return GET_SEL_LIMIT(sel);
350 /***********************************************************************
351 * SetSelectorLimit (KERNEL.189)
353 WORD WINAPI SetSelectorLimit( WORD sel, DWORD limit )
355 ldt_entry entry;
356 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
357 entry.limit_in_pages = (limit >= 0x100000);
358 if (entry.limit_in_pages) entry.limit = limit >> 12;
359 else entry.limit = limit;
360 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
361 return sel;
365 /***********************************************************************
366 * SelectorAccessRights (KERNEL.196)
368 WORD WINAPI SelectorAccessRights( WORD sel, WORD op, WORD val )
370 ldt_entry entry;
371 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
372 if (op == 0) /* get */
374 return 0x01 | /* accessed */
375 0x10 | /* not system */
376 0x60 | /* DPL 3 */
377 0x80 | /* present */
378 ((entry.read_only == 0) << 1) |
379 (entry.type << 2) |
380 (entry.seg_32bit << 14) |
381 (entry.limit_in_pages << 15);
383 else /* set */
385 entry.read_only = ((val & 2) == 0);
386 entry.type = (val >> 2) & 3;
387 entry.seg_32bit = val & 0x4000;
388 entry.limit_in_pages = val & 0x8000;
389 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
390 return 0;
395 /***********************************************************************
396 * IsBadCodePtr16 (KERNEL.336)
398 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
400 WORD sel;
401 ldt_entry entry;
403 sel = SELECTOROF(lpfn);
404 if (!sel) return TRUE;
405 if (IS_SELECTOR_FREE(sel)) return TRUE;
406 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
407 if (entry.type != SEGMENT_CODE) return TRUE;
408 if (OFFSETOF(lpfn) > entry.limit) return TRUE;
409 return FALSE;
413 /***********************************************************************
414 * IsBadStringPtr16 (KERNEL.337)
416 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
418 WORD sel;
419 ldt_entry entry;
421 sel = SELECTOROF(ptr);
422 if (!sel) return TRUE;
423 if (IS_SELECTOR_FREE(sel)) return TRUE;
424 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
425 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
426 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr));
427 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
428 return FALSE;
432 /***********************************************************************
433 * IsBadHugeReadPtr16 (KERNEL.346)
435 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
437 WORD sel;
438 ldt_entry entry;
440 sel = SELECTOROF(ptr);
441 if (!sel) return TRUE;
442 if (IS_SELECTOR_FREE(sel)) return TRUE;
443 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
444 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
445 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
446 return FALSE;
450 /***********************************************************************
451 * IsBadHugeWritePtr16 (KERNEL.347)
453 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
455 WORD sel;
456 ldt_entry entry;
458 sel = SELECTOROF(ptr);
459 if (!sel) return TRUE;
460 if (IS_SELECTOR_FREE(sel)) return TRUE;
461 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
462 if ((entry.type == SEGMENT_CODE) || entry.read_only) return TRUE;
463 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
464 return FALSE;
467 /***********************************************************************
468 * IsBadReadPtr16 (KERNEL.334)
470 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
472 return IsBadHugeReadPtr16( ptr, size );
476 /***********************************************************************
477 * IsBadWritePtr16 (KERNEL.335)
479 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
481 return IsBadHugeWritePtr16( ptr, size );
485 /***********************************************************************
486 * MemoryRead (TOOLHELP.78)
488 DWORD WINAPI MemoryRead( WORD sel, DWORD offset, void *buffer, DWORD count )
490 if (IS_SELECTOR_FREE(sel)) return 0;
491 if (offset > GET_SEL_LIMIT(sel)) return 0;
492 if (offset + count > GET_SEL_LIMIT(sel) + 1)
493 count = GET_SEL_LIMIT(sel) + 1 - offset;
494 memcpy( buffer, ((char *)GET_SEL_BASE(sel)) + offset, count );
495 return count;
499 /***********************************************************************
500 * MemoryWrite (TOOLHELP.79)
502 DWORD WINAPI MemoryWrite( WORD sel, DWORD offset, void *buffer, DWORD count )
504 if (IS_SELECTOR_FREE(sel)) return 0;
505 if (offset > GET_SEL_LIMIT(sel)) return 0;
506 if (offset + count > GET_SEL_LIMIT(sel) + 1)
507 count = GET_SEL_LIMIT(sel) + 1 - offset;
508 memcpy( ((char *)GET_SEL_BASE(sel)) + offset, buffer, count );
509 return count;
512 /************************************* Win95 pointer mapping functions *
516 /***********************************************************************
517 * MapSL (KERNEL32.523)
519 * Maps fixed segmented pointer to linear.
521 LPVOID WINAPI MapSL( SEGPTR sptr )
523 return (LPVOID)PTR_SEG_TO_LIN(sptr);
526 /***********************************************************************
527 * MapSLFix (KERNEL32.524)
529 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
530 * unexpected linear address change when GlobalCompact() shuffles
531 * moveable blocks.
534 LPVOID WINAPI MapSLFix( SEGPTR sptr )
536 return (LPVOID)PTR_SEG_TO_LIN(sptr);
539 /***********************************************************************
540 * UnMapSLFixArray (KERNEL32.701)
543 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT32 length )
547 /***********************************************************************
548 * MapLS (KERNEL32.522)
550 * Maps linear pointer to segmented.
552 SEGPTR WINAPI MapLS( LPVOID ptr )
554 if (!HIWORD(ptr))
555 return (SEGPTR)ptr;
556 else
558 WORD sel = SELECTOR_AllocBlock( ptr, 0x10000, SEGMENT_DATA, FALSE, FALSE );
559 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
564 /***********************************************************************
565 * UnMapLS (KERNEL32.700)
567 * Free mapped selector.
569 void WINAPI UnMapLS( SEGPTR sptr )
571 if (SELECTOROF(sptr))
572 SELECTOR_FreeBlock( SELECTOROF(sptr), 1 );
575 /***********************************************************************
576 * GetThreadSelectorEntry (KERNEL32)
577 * FIXME: add #ifdef i386 for non x86
579 BOOL32 WINAPI GetThreadSelectorEntry( HANDLE32 hthread, DWORD sel,
580 LPLDT_ENTRY ldtent)
582 ldt_entry ldtentry;
584 LDT_GetEntry(SELECTOR_TO_ENTRY(sel),&ldtentry);
585 ldtent->BaseLow = ldtentry.base & 0x0000ffff;
586 ldtent->HighWord.Bits.BaseMid = (ldtentry.base & 0x00ff0000) >> 16;
587 ldtent->HighWord.Bits.BaseHi = (ldtentry.base & 0xff000000) >> 24;
588 ldtent->LimitLow = ldtentry.limit & 0x0000ffff;
589 ldtent->HighWord.Bits.LimitHi = (ldtentry.limit & 0x00ff0000) >> 16;
590 ldtent->HighWord.Bits.Dpl = 3;
591 ldtent->HighWord.Bits.Sys = 0;
592 ldtent->HighWord.Bits.Pres = 1;
593 ldtent->HighWord.Bits.Type = 0x10|(ldtentry.type << 2);
594 if (ldtentry.read_only)
595 ldtent->HighWord.Bits.Type|=0x2;
596 ldtent->HighWord.Bits.Granularity = ldtentry.limit_in_pages;
597 ldtent->HighWord.Bits.Default_Big = ldtentry.seg_32bit;
598 return TRUE;
602 /**********************************************************************
603 * SMapLS* (KERNEL32)
604 * These functions map linear pointers at [EBP+xxx] to segmented pointers
605 * and return them.
606 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
607 * unravel them at SUnMapLS. We just store the segmented pointer there.
609 static void
610 x_SMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
611 DWORD val,ptr;
613 val =*(DWORD*)(EBP_reg(context)+argoff);
614 if (val<0x10000) {
615 ptr=val;
616 *(DWORD*)(EBP_reg(context)+argoff) = 0;
617 } else {
618 ptr = MapLS((LPVOID)val);
619 *(DWORD*)(EBP_reg(context)+argoff) = ptr;
621 EAX_reg(context) = ptr;
624 REGS_ENTRYPOINT(SMapLS_IP_EBP_8) {x_SMapLS_IP_EBP_x(context,8);}
625 REGS_ENTRYPOINT(SMapLS_IP_EBP_12) {x_SMapLS_IP_EBP_x(context,12);}
626 REGS_ENTRYPOINT(SMapLS_IP_EBP_16) {x_SMapLS_IP_EBP_x(context,16);}
627 REGS_ENTRYPOINT(SMapLS_IP_EBP_20) {x_SMapLS_IP_EBP_x(context,20);}
628 REGS_ENTRYPOINT(SMapLS_IP_EBP_24) {x_SMapLS_IP_EBP_x(context,24);}
629 REGS_ENTRYPOINT(SMapLS_IP_EBP_28) {x_SMapLS_IP_EBP_x(context,28);}
630 REGS_ENTRYPOINT(SMapLS_IP_EBP_32) {x_SMapLS_IP_EBP_x(context,32);}
631 REGS_ENTRYPOINT(SMapLS_IP_EBP_36) {x_SMapLS_IP_EBP_x(context,36);}
632 REGS_ENTRYPOINT(SMapLS_IP_EBP_40) {x_SMapLS_IP_EBP_x(context,40);}
634 REGS_ENTRYPOINT(SMapLS)
636 if (EAX_reg(context)>=0x10000) {
637 EAX_reg(context) = MapLS((LPVOID)EAX_reg(context));
638 EDX_reg(context) = EAX_reg(context);
639 } else {
640 EDX_reg(context) = 0;
644 REGS_ENTRYPOINT(SUnMapLS)
646 if (EAX_reg(context)>=0x10000)
647 UnMapLS((SEGPTR)EAX_reg(context));
650 static void
651 x_SUnMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
652 if (*(DWORD*)(EBP_reg(context)+argoff))
653 UnMapLS(*(DWORD*)(EBP_reg(context)+argoff));
654 *(DWORD*)(EBP_reg(context)+argoff)=0;
656 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_8) { x_SUnMapLS_IP_EBP_x(context,12); }
657 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_12) { x_SUnMapLS_IP_EBP_x(context,12); }
658 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_16) { x_SUnMapLS_IP_EBP_x(context,16); }
659 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_20) { x_SUnMapLS_IP_EBP_x(context,20); }
660 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_24) { x_SUnMapLS_IP_EBP_x(context,24); }
661 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_28) { x_SUnMapLS_IP_EBP_x(context,28); }
662 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_32) { x_SUnMapLS_IP_EBP_x(context,32); }
663 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_36) { x_SUnMapLS_IP_EBP_x(context,36); }
664 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_40) { x_SUnMapLS_IP_EBP_x(context,40); }
666 /**********************************************************************
667 * AllocMappedBuffer (KERNEL32.38)
669 * This is a undocumented KERNEL32 function that
670 * SMapLS's a GlobalAlloc'ed buffer.
672 * Input: EDI register: size of buffer to allocate
673 * Output: EDI register: pointer to buffer
675 * Note: The buffer is preceeded by 8 bytes:
676 * ...
677 * edi+0 buffer
678 * edi-4 SEGPTR to buffer
679 * edi-8 some magic Win95 needs for SUnMapLS
680 * (we use it for the memory handle)
682 * The SEGPTR is used by the caller!
685 REGS_ENTRYPOINT(AllocMappedBuffer)
687 HGLOBAL32 handle = GlobalAlloc32(0, EDI_reg(context) + 8);
688 DWORD *buffer = (DWORD *)GlobalLock32(handle);
689 SEGPTR ptr = 0;
691 if (buffer)
692 if (!(ptr = MapLS(buffer + 2)))
694 GlobalUnlock32(handle);
695 GlobalFree32(handle);
698 if (!ptr)
699 EAX_reg(context) = EDI_reg(context) = 0;
700 else
702 buffer[0] = handle;
703 buffer[1] = ptr;
705 EAX_reg(context) = (DWORD) ptr;
706 EDI_reg(context) = (DWORD)(buffer + 2);
710 /**********************************************************************
711 * FreeMappedBuffer (KERNEL32.39)
713 * Free a buffer allocated by AllocMappedBuffer
715 * Input: EDI register: pointer to buffer
718 REGS_ENTRYPOINT(FreeMappedBuffer)
720 if (EDI_reg(context))
722 DWORD *buffer = (DWORD *)EDI_reg(context) - 2;
724 UnMapLS(buffer[1]);
726 GlobalUnlock32(buffer[0]);
727 GlobalFree32(buffer[0]);
731 /**********************************************************************
732 * WOWGetVDMPointer (KERNEL32.55)
733 * Get linear from segmented pointer. (MSDN lib)
735 LPVOID WINAPI WOWGetVDMPointer(DWORD vp,DWORD nrofbytes,BOOL32 protected)
737 /* FIXME: add size check too */
738 if (protected)
739 return PTR_SEG_TO_LIN(vp);
740 else
741 return DOSMEM_MapRealToLinear(vp);
744 /**********************************************************************
745 * GetVDMPointer32W (KERNEL.516)
747 LPVOID WINAPI GetVDMPointer32W(DWORD vp,DWORD mode)
749 return WOWGetVDMPointer(vp,0,mode);
752 /**********************************************************************
753 * WOWGetVDMPointerFix (KERNEL32.55)
754 * Dito, but fix heapsegment (MSDN lib)
756 LPVOID WINAPI WOWGetVDMPointerFix(DWORD vp,DWORD nrofbytes,BOOL32 protected)
758 /* FIXME: fix heapsegment */
759 return WOWGetVDMPointer(vp,nrofbytes,protected);
762 /**********************************************************************
763 * WOWGetVDMPointerUnFix (KERNEL32.56)
765 void WINAPI WOWGetVDMPointerUnfix(DWORD vp)
767 /* FIXME: unfix heapsegment */
770 /***********************************************************************
771 * UTSelectorOffsetToLinear (WIN32S16.48)
773 * rough guesswork, but seems to work (I had no "reasonable" docu)
775 LPVOID WINAPI UTSelectorOffsetToLinear(SEGPTR sptr)
777 return PTR_SEG_TO_LIN(sptr);
780 /***********************************************************************
781 * UTLinearToSelectorOffset (WIN32S16.49)
783 * FIXME: I don't know if that's the right way to do linear -> segmented
785 SEGPTR WINAPI UTLinearToSelectorOffset(LPVOID lptr)
787 return (SEGPTR)lptr;