wine/win32/memory.c
Alexandre Julliard 2c69f6d8e9 Release 960928
Fri Sep 27 14:18:42 1996  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [controls/button.c]
	Fixed focus rectangle size and clipping.

	* [controls/scroll.c]
	Converted to Win32 and added support for scroll page.
	Completed SetScrollInfo() and implemented other Win32 functions.

	* [files/file.c]
	Removed FILE_Read() (use _lread32 instead).

	* [objects/dce.c] [include/dce.h]
	Allocate DCE on the Win32 heap, and use pointers instead of
	handles.
	Implemented Win32 version of DC functions.

	* [windows/painting.c]
	Attempt to make CS_PARENTDC style work again.

Wed Sep 25 23:40:52 1996 Alex Korobka <alex@trantor.pharm.sunysb.edu>

	* [windows/dce.c] [windows/winpos.c]
	Override SaveUnder attribute when painting took place
	in a window below. Force X to raise activated window 
	in seamless mode.

	* [misc/clipboard.c] [windows/event.c]
	Translation between DOS and Unix text formats and several
	other fixes for the sudden selection loss.

	* [windows/message.c]
	Apply "first" and "last" when checking for WM_QUIT in 
        MSG_PeekMessage().

	* [windows/win.c]
	Rearranged DestroyWindow() to fit "Windows Internals"
	description.

	* [windows/win.c] [windows/winpos.c] [windows/nonclient.c]
	Misc. fixes to CBT hook calls.

	* [controls/menu.c] [misc/user.c]
	Fixup resident popup menu window so that it doesn't get
	destroyed by USER_AppExit().

	* [loader/module.c] [loader/task.c] [windows/event.c]
	Process "unsafe" X events outside the scheduler to prevent
	deadlocks.

	* [windows/message.c] [windows/queue.c] [windows/winpos.c]
	Lots of fixes for better Win16 multitasking.

Wed Sep 25 20:36:30 1996  Marcus Meissner <msmeissn@cip.informatik.uni-erlangen.de>

	* [include/windows.h]
	Added some missing HOOK defines.

	* [misc/shell.c][if1632/shell32.spec][include/shell.h]
	SHGetFileInfoA stub added (win95 mplayer.exe /play bla.avi).

	* [win32/console.c][include/wincon.h]
	GetConsoleScreenBufferInfo, GetLargestConsoleWindowSize added.

	* [misc/registry.c]
	Some null ptr fixes.

	* [loader/pe_image.c]
	Fixed exported function lookup. (msvcrt20.dll)
	Add debugsyms for entrypoint, exported functions and sections.

	* [multimedia/mmsystem.c]
	MCIOpen: support for element opens (mplayer.exe /play bla.avi).

	* [several]
	Added several missing things/stubs/simple thunks from win32
	to win16 code.

Sat Sep 21 17:27:44 1996  O.Flebbe  <flebbe@science-computing.uni-tuebingen.de>

	* [windows/property.c]
	Fixed debugging of 16 Bit RemoveProp().

	* [debugger/memory.c]
	Added DEBUG_checkmap_bad() for linux.

Thu Sep 19 20:48:31 1996  Albrecht Kleine  <kleine@ak.sax.de>

	* [windows/event.c] [windows/hook.c]
	Use EnableHardwareInput() for JournalPlayback hook.

	* [controls/listbox.c]
	Changed handling of LB_GETITEMRECT in empty listboxes.

Thu Sep 19 13:34:35 1996  Slaven Rezic  <eserte@cs.tu-berlin.de>

	* [misc/main.c]
	Fixes to X resources handling.	

Wed Sep 18 00:31:15 1996  Huw D. M. Davies <h.davies1@physics.oxford.ac.uk>

	* [objects/metafile.c] [include/gdi.h] [objects/dc.c]
	Individual handle table created for each metafile. Fixed
 	GlobalReAlloc() bug in MF_AddHandleDC() (was MF_AddHandleInternal).

	* [windows/graphics.c] [objects/dc.c]
	Rectangle() changed to work better with wide pens and PS_NULL.
	Use JoinMiter.

	* [windows/winpos.c]
	Make the whole (non X) window invalid on resize if CS_[VH]REDRAW
 	is set.

	* [windows/nonclient.c]
	AdjustWindowRectEx() should perform calculations even if the
 	window is minimized.

	* [windows/mdi.c]
	Better handling of system button painting. Maximized windows can
 	contain scroll bars. Icons now maximize properly.

	* [windows/defwnd.c] [windows/nonclient.c] [controls/menu.c]
	Improved greying of items in system menu. WM_INITMEMUPOPUP no
 	longer caught in DefWndProc, DEFWND_InitSysMenuPopup moved to
 	menu.c.

Mon Sep 16 21:30:00 1996  Uwe Bonnes <bon@elektron.ikp.physik.th-darmstadt.de>

	* [several files]
	Fix missing includes and wrong printing arguments.

	* [controls/listbox.c]
	Don't sort drives in ListBoxDirectory().
	
Sat Sep 14 09:05:47 1996  Petri Tuomola <ptuomola@xs4all.nl>

	* [windows/dialog.c]
	Fixed handling of Shift-TAB in dialogs.

Thu Sep 12 18:31:00 1996  Thomas Sandford <t.d.g.sandford@prds-grn.demon.co.uk>

	* [if1632/gdi32.spec]
	Added SelectClipRgn - call win16 version.

	* [if1632/user32.spec]
	Added GetAsyncKeyState, GetMenuItemID and GetMenuStringA.

	* [include/wincon.h]
	Added COORD and SMALL_RECT typedefs, moved CONSOLE_SCREEN_BUFFER_INFO
	out of #if 0 protected portion of file.

	* [loader/pe_image.c]
	PE_InitTEB() - Tidy up, bug fix to stack pointer value (Borland
	programs now work better)

	* [win32/console.c]
	Added stub functions for GetConsoleScreenBufferInfo and 
	GetLargestConsoleWindowSize

	* [win32/findfile.c]
	FindFirstFile32A() - removed erroneous strcpy

	* [windows/keyboard.c]
	GetAsyncKeyState() - bug fix - now returns value as per Microsoft
	specification. NB - I still have doubts about some other functions
	in this file.
1996-09-28 18:11:01 +00:00

343 lines
8.4 KiB
C

/*
* Win32 kernel functions
*
* Copyright 1995 Martin von Loewis and Cameron Heide
*/
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include "windows.h"
#include "winerror.h"
#include "winbase.h"
#include "handle32.h"
#include "stddebug.h"
#include "debug.h"
#ifndef PROT_NONE /* FreeBSD doesn't define PROT_NONE */
#define PROT_NONE 0
#endif
typedef struct {
caddr_t ptr;
long size;
} virtual_mem_t;
virtual_mem_t *mem = 0;
int mem_count = 0;
int mem_used = 0;
/*******************************************************************
* VRANGE
* A VRANGE denotes a contiguous part of the address space. It is used
* for house keeping, and will be obtained by higher-level memory allocation
* functions (VirtualAlloc, MapViewOfFile)
* There can be at most one VRANGE object covering any address at any time.
* Currently, all VRANGE objects are stored in a sorted list. Wine does not
* attempt to give a complete list of in-use address ranges, only those
* allocated via Win32.
* An exception is IsVrangeFree, which should test the OS specific
* mappings, too. As a default, an range not known to be allocated is
* considered free.
*******************************************************************/
VRANGE_OBJECT *MEMORY_ranges=0;
VRANGE_OBJECT *MEMORY_FindVrange(DWORD start)
{
VRANGE_OBJECT *range;
for(range=MEMORY_ranges;range && range->start<start;range=range->next)
{
if(range->start<start && start<range->start+range->size)
return range;
}
return 0;
}
static int MEMORY_IsVrangeFree(DWORD start,DWORD size)
{
DWORD end;
VRANGE_OBJECT *range;
if(!size)
return 1;
/* First, check our lists*/
end=start+size;
for(range=MEMORY_ranges;range && range->start<start;range=range->next)
{
if((range->start<start && start<range->start+range->size) ||
(range->start<end && end<range->start+range->size))
return 0;
}
/* Now, check the maps that are not under our control */
#ifdef linux
{
FILE *f=fopen("/proc/self/maps","r");
char line[80];
int found=0;
while(1)
{
char *it;
int lower,upper;
if(!fgets(line,sizeof(line),f))
break;
it=line;
lower=strtoul(it,&it,16);
if(*it++!='-')
fprintf(stderr,"Format of /proc/self/maps changed\n");
upper=strtoul(it,&it,16);
if((lower<start && start<upper) || (lower<start+size && start+size<upper))
{
found=1;
break;
}
}
fclose(f);
return !found;
}
#else
{
static int warned=0;
if(!warned)
{
fprintf(stdnimp, "Don't know how to perform MEMORY_IsVrangeFree on "
"this system.\n Please fix\n");
warned=0;
}
return 1;
}
#endif
}
/* FIXME: might need to consolidate ranges */
void MEMORY_InsertVrange(VRANGE_OBJECT *r)
{
VRANGE_OBJECT *it,*last;
if(!MEMORY_ranges || r->start<MEMORY_ranges->start)
{
r->next=MEMORY_ranges;
MEMORY_ranges=r;
}
for(it=MEMORY_ranges,last=0;it && it->start<r->start;it=it->next)
last=it;
r->next=last->next;
last->next=r;
}
VRANGE_OBJECT *MEMORY_AllocVrange(int start,int size)
{
VRANGE_OBJECT *ret=CreateKernelObject(sizeof(VRANGE_OBJECT));
ret->common.magic=KERNEL_OBJECT_VRANGE;
MEMORY_InsertVrange(ret);
return ret;
}
void MEMORY_ReleaseVrange(VRANGE_OBJECT *r)
{
VRANGE_OBJECT *it;
if(MEMORY_ranges==r)
{
MEMORY_ranges=r->next;
ReleaseKernelObject(r);
return;
}
for(it=MEMORY_ranges;it;it=it->next)
if(it->next==r)break;
if(!it)
{
fprintf(stderr,"VRANGE not found\n");
return;
}
it->next=r->next;
ReleaseKernelObject(r);
}
/***********************************************************************
* VirtualAlloc (KERNEL32.548)
*/
int TranslateProtectionFlags(DWORD);
LPVOID VirtualAlloc(LPVOID lpvAddress, DWORD cbSize,
DWORD fdwAllocationType, DWORD fdwProtect)
{
caddr_t ptr;
int i;
virtual_mem_t *tmp_mem;
int prot;
static int fdzero = -1;
if (fdzero == -1)
{
if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
{
perror( "/dev/zero: open" );
return (LPVOID)NULL;
}
}
dprintf_win32(stddeb, "VirtualAlloc: size = %ld, address=%p\n", cbSize, lpvAddress);
if (fdwAllocationType & MEM_RESERVE || !lpvAddress) {
ptr = mmap((void *)((((unsigned long)lpvAddress-1) & 0xFFFF0000L)
+ 0x00010000L),
cbSize, PROT_NONE, MAP_PRIVATE, fdzero, 0 );
if (ptr == (caddr_t) -1) {
dprintf_win32(stddeb, "VirtualAlloc: returning NULL");
return (LPVOID) NULL;
}
if (lpvAddress && ((unsigned long)ptr & 0xFFFF0000L)) {
munmap(ptr, cbSize);
cbSize += 65535;
ptr = mmap(lpvAddress, cbSize,
PROT_NONE, MAP_PRIVATE, fdzero, 0 );
if (ptr == (caddr_t) -1) {
dprintf_win32(stddeb, "VirtualAlloc: returning NULL");
return (LPVOID) NULL;
}
ptr = (void *)((((unsigned long)ptr-1) & 0xFFFF0000L)+0x00010000L);
}
/* remember the size for VirtualFree since it's going to be handed
a zero len */
if (ptr) {
if (mem_count == mem_used) {
tmp_mem = realloc(mem,(mem_count+10)*sizeof(virtual_mem_t));
if (!tmp_mem) return 0;
mem = tmp_mem;
memset(mem+mem_count, 0, 10*sizeof(virtual_mem_t));
mem_count += 10;
}
for (i=0; i<mem_count; i++) {
if (!(mem+i)->ptr) {
(mem+i)->ptr = ptr;
(mem+i)->size = cbSize;
mem_used++;
break;
}
}
}
} else {
ptr = lpvAddress;
}
if (fdwAllocationType & MEM_COMMIT) {
prot = TranslateProtectionFlags(fdwProtect &
~(PAGE_GUARD | PAGE_NOCACHE));
mprotect(ptr, cbSize, prot);
}
#if 0
/* kludge for gnu-win32 */
if (fdwAllocationType & MEM_RESERVE) return sbrk(0);
ptr = malloc(cbSize + 65536);
if(ptr)
{
/* Round it up to the next 64K boundary and zero it.
*/
ptr = (void *)(((unsigned long)ptr & 0xFFFF0000L) + 0x00010000L);
memset(ptr, 0, cbSize);
}
#endif
dprintf_win32(stddeb, "VirtualAlloc: got pointer %p\n", ptr);
return ptr;
}
/***********************************************************************
* VirtualFree (KERNEL32.550)
*/
BOOL32 VirtualFree(LPVOID lpvAddress, DWORD cbSize, DWORD fdwFreeType)
{
int i;
if (fdwFreeType & MEM_RELEASE) {
for (i=0; i<mem_count; i++) {
if ((mem+i)->ptr == lpvAddress) {
munmap(lpvAddress, (mem+i)->size);
(mem+i)->ptr = 0;
mem_used--;
break;
}
}
} else {
mprotect(lpvAddress, cbSize, PROT_NONE);
}
#if 0
if(lpvAddress)
free(lpvAddress);
#endif
return 1;
}
/***********************************************************************
* VirtualQuery (KERNEL32.554)
*/
BOOL32 VirtualQuery(LPCVOID address,LPMEMORY_BASIC_INFORMATION buf,DWORD len)
{
/* FIXME: fill out structure ... */
return TRUE;
}
int TranslateProtectionFlags(DWORD protection_flags)
{
int prot;
switch(protection_flags) {
case PAGE_READONLY:
prot=PROT_READ;
break;
case PAGE_READWRITE:
prot=PROT_READ|PROT_WRITE;
break;
case PAGE_WRITECOPY:
prot=PROT_WRITE;
break;
case PAGE_EXECUTE:
prot=PROT_EXEC;
break;
case PAGE_EXECUTE_READ:
prot=PROT_EXEC|PROT_READ;
break;
case PAGE_EXECUTE_READWRITE:
prot=PROT_EXEC|PROT_READ|PROT_WRITE;
break;
case PAGE_EXECUTE_WRITECOPY:
prot=PROT_EXEC|PROT_WRITE;
break;
case PAGE_NOACCESS:
default:
prot=PROT_NONE;
break;
}
return prot;
}
/******************************************************************
* IsBadReadPtr
*/
BOOL WIN32_IsBadReadPtr(void* ptr, unsigned int bytes)
{
dprintf_global(stddeb,"IsBadReadPtr(%x,%x)\n",(int)ptr,bytes);
/* FIXME: Should make check based on actual mappings, here */
return FALSE;
}
/******************************************************************
* IsBadWritePtr
*/
BOOL WIN32_IsBadWritePtr(void* ptr, unsigned int bytes)
{
dprintf_global(stddeb,"IsBadWritePtr(%x,%x)\n",(int)ptr,bytes);
/* FIXME: Should make check based on actual mappings, here */
return FALSE;
}
/******************************************************************
* IsBadWritePtr
*/
BOOL WIN32_IsBadCodePtr(void* ptr, unsigned int bytes)
{
dprintf_global(stddeb,"IsBadCodePtr(%x,%x)\n",(int)ptr,bytes);
/* FIXME: Should make check based on actual mappings, here */
return FALSE;
}