wine/memory/heap.c
Alexandre Julliard 36ca1368c9 Release 940602
Sat May 28 12:03:23 1994  Bob Amstadt  (bob@pooh)

	* miscemu/int21.c (OpenExistingFile): 
	OpenExistingFile needed to return handle in AX register instead
	of the BX register.

	* miscemu/int21.c (ioctlGetDeviceInfo): 
	Added a little code to give a fake result for normal files.

Wed May 25 21:55:38 1994  Bob Amstadt  (bob@pooh)

	* [memory/global.c]
	return value from GlobalSize was completely wrong.

	* [miscemu/int21.h]
	fixed bug in FindFirst.  Directory pointer (dp) was not placed in
	dta correctly.

	* [tools/build.c]
	fixed creation of pop.h to guarantee that flags are restored correctly.

	* [misc/comm.c]
	changed all occurance of strncmp() to strncasecmp().
	BuildCommDCB() should not require that OpenComm() be called first.

	* [loader/selector.c]
	Heap initialized to size of full segment less stack size and 
	automatic data size.

Sat May 28 09:14:33 1994  Rick Sladkey  (jrs@world.std.com)

	* [controls/listbox.c]
	Correct typos in ListBoxResetContent where lpls variable is
	used where lsls2 variable is meant.  Don't call USER_HEAP_FREE
	twice on the same handle if hData and hMem are the same.

	* [debugger/opcodes/i386-dis.c]
	Add new name array names_rmw for table driven decoding of the
	16-bit mod/rm field.  Omit large case statement in OP_E and
	replace with array reference to match existing coding style.
	Add new static variable machine with value 286 or 386 to
	correctly decode mod/rm field in either 16 or 32 bit modes.
	Set it in print_insn_i{2,3}86.  In OP_E use it to decide how
	to decode mod/rm.  While the code was correct for 16 bit code,
	it was improperly decoding mod/rm fields on word prefixed
	32 bit instructions.

	* [debugger/debug.l]
	Recognize new token ABORT.   Recognize single letters 'p'
	and 'q' as tokens.

	* [debugger/dbg.y]
	Add new token ABORT.  Allow print command to be invoked by
	'p' and quit command by 'q', ala GDB.  Change lots of '};'
	to just '}'.  Add static dummy_regs to wine_debug so that
	wine_debug(0, NULL) doesn't core dump with qmagic.

	* [debugger/info.c]
	Correct syntax of break command in helptext and omit former
	comment about probable bugginess of the disassembly since it
	is now correct.  Change fprintf of first backtrace stack
	frame to match that of the second and subsequent frames.

	* [loader/selector.c]
	Change construction of command line in CreatePSP from creating
	a string that looks like "arg1 arg2 \r" to "arg1 arg2".  A DOS
	PSP command line looks like " arg1 arg2\r" with the length not
	including the trailing "\r" but that is not how Windows does it.

	* [loader/library.c]
	Change uses of %s to print strings in GetModuleHandle to %x so
	that string IDs don't cause a core dump with qmagic.  Handle
	converting a string id to a literal module handle.  For
	example, GetModuleHandle((LPSTR) 0x57) now returns 0x57 if
	it is a real module handle.

	* [misc/message.c]
	In MessageBox, translate a NULL title argument to the string "Error".

	* [misc/profile.c]
	In GetSetProfile translate a NULL Default argument to "".  Any
	caller whose Default argument is NULL is buggy, but CHARMAP does it
	anyway.

	* [objects/font.c]
	Add NULL pointer checks in EnumFontFamilies to prevent core dumps.

Sat May 28 20:01:51 1994 Jon Tombs (jon@gtex02.us.es) 

	* New options/resourses nosaveunders and nobackingstore.  By 
	default backingstore and saveunders are now enabled, these use 
	more memory but avoids those slow (sometimes multiple) redraws 
	caused be exposure events.

May 30, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [misc/driver.c] New file
	Skeleton for 'Installable Wine Drivers' functions. :-)
	* [misc/audio.c] New file
	Skeleton for basic 'Audio Driver' functions.
	* [misc/network.c] New file
	Stubs for few networking functions.

	* [misc/mmsystem.c]
	More coding ... a dust in a galaxy ...
	* [misc/shell.c]
	Some coding for 'RegXXX' functions ... a dust in the wind ...

	* [misc/profile.c]
	Bug fix in GetSetProfile(), bad enumeration if KeyName == NULL.

	* [objects/gdi.c]
	New function CreateDiscardableBitmap(), it just calling 
	CreateCompatibleBitmap() for now. It's get 'clock.exe' running ! :-)

	* [controls/listbox.c]
	* [controls/combo.c]
	New font member assigned to SYSTEM_FONT as default.
	Added processing for WM_SETFONT message;


Tue May 31 20:34:25 EDT 1994  John Richardson <jrichard@cs.uml.edu>

        * [windows/event.c]
        Added AsyncMouseButtonsStates array for GetAsyncKeyState.

        * [windows/keyboard.c]
        Implemented beginning of GetAsyncKeyState.

Wed May 25 23:35:03 1994  David Metcalfe <david@prism.demon.co.uk>

        * [objects/metafile.c] [include/metafile.h]
          [windows/mapping.c] [objects/clipping.c] [objects/bitblt.c]
          [windows/dc.c]
        Further metafile support.
1994-06-02 22:38:20 +00:00

568 lines
12 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

static char RCSId[] = "$Id: heap.c,v 1.3 1993/07/04 04:04:21 root Exp root $";
static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prototypes.h"
#include "segmem.h"
#include "heap.h"
#include "regfunc.h"
/* #define DEBUG_HEAP /* */
LHEAP *LocalHeaps = NULL;
/**********************************************************************
* HEAP_Init
*/
void
HEAP_Init(MDESC **free_list, void *start, int length)
{
if (length < 2 * sizeof(MDESC))
return;
*free_list = (MDESC *) start;
(*free_list)->prev = NULL;
(*free_list)->next = NULL;
(*free_list)->length = length - sizeof(MDESC);
}
/**********************************************************************
* HEAP_Alloc
*/
void *
HEAP_Alloc(MDESC **free_list, int flags, int bytes)
{
MDESC *m, *m_new;
#ifdef DEBUG_HEAP
printf("HeapAlloc: free_list %08x, flags %x, bytes %d\n",
free_list, flags, bytes);
#endif
/*
* Find free block big enough.
*/
for (m = *free_list; m != NULL; m = m->next)
{
if (m->length >= bytes && m->length < bytes + 4 * sizeof(MDESC))
{
break;
}
else if (m->length > bytes)
{
m_new = m + (bytes / sizeof(MDESC)) + 2;
if (m->prev == NULL)
*free_list = m_new;
else
m->prev->next = m_new;
if (m->next != NULL)
m->next->prev = m_new;
m_new->next = m->next;
m_new->prev = m->prev;
m_new->length = m->length - ((int) m_new - (int) m);
m->length -= (m_new->length + sizeof(MDESC));
m->prev = m;
m->next = m;
m->lock = 0;
m->flags = 0;
if (flags & GLOBAL_FLAGS_ZEROINIT)
memset(m + 1, 0, bytes);
#ifdef DEBUG_HEAP
printf("HeapAlloc: returning %08x\n", (m + 1));
#endif
return (void *) (m + 1);
}
}
if (m != NULL)
{
if (m->prev == NULL)
*free_list = m->next;
else
m->prev->next = m->next;
if (m->next != NULL)
m->next->prev = m->prev;
m->prev = m;
m->next = m;
m->lock = 0;
m->flags = 0;
if (flags & GLOBAL_FLAGS_ZEROINIT)
memset(m + 1, 0, bytes);
#ifdef DEBUG_HEAP
printf("HeapAlloc: returning %08x\n", (m + 1));
#endif
return (void *) (m + 1);
}
#ifdef DEBUG_HEAP
printf("HeapAlloc: returning %08x\n", 0);
#endif
return 0;
}
/**********************************************************************
* HEAP_ReAlloc
*/
void *
HEAP_ReAlloc(MDESC **free_list, void *old_block,
int new_size, unsigned int flags)
{
MDESC *m_free;
MDESC *m;
if (!old_block)
return HEAP_Alloc(free_list, flags, new_size);
/*
* Check validity of block
*/
m = (MDESC *) old_block - 1;
#ifdef DEBUG_HEAP
printf("HEAP_ReAlloc new_size=%d !\n", new_size);
printf("HEAP_ReAlloc old_block=%08X !\n", old_block);
printf("HEAP_ReAlloc m=%08X free_list=%08X !\n", m, free_list);
printf("HEAP_ReAlloc m->prev=%08X !\n", m->prev);
printf("HEAP_ReAlloc m->next=%08X !\n", m->next);
printf("HEAP_ReAlloc *free_list=%08X !\n", *free_list);
#endif
if (m->prev != m || m->next != m ||
((int) m & 0xffff0000) != ((int) *free_list & 0xffff0000))
{
#ifdef DEBUG_HEAP
printf("Attempt to resize bad pointer, m = %08x, *free_list = %08x\n",
m, free_list);
#endif
return NULL;
}
/*
* Check for grow block
*/
#ifdef DEBUG_HEAP
printf("HEAP_ReAlloc Check for grow block !\n");
#endif
if (new_size > m->length)
{
m_free = m + 1 + m->length / sizeof(MDESC);
if (m_free->next == m_free ||
m_free->prev == m_free ||
m_free->length + sizeof(MDESC) < new_size)
{
void *new_p = HEAP_Alloc(free_list, flags, new_size);
if (new_p ==NULL)
return NULL;
memcpy(new_p, old_block, m->length);
HEAP_Free(free_list, old_block);
return new_p;
}
if (m_free->prev == NULL)
*free_list = m_free->next;
else
m_free->prev->next = m_free->next;
if (m_free->next != NULL)
m_free->next->prev = m_free->prev;
m->length += sizeof(MDESC) + m_free->length;
#ifdef DEBUG_HEAP
printf("HEAP_ReAlloc before GLOBAL_FLAGS_ZEROINIT !\n");
#endif
if (flags & GLOBAL_FLAGS_ZEROINIT)
memset(m_free, '\0', sizeof(MDESC) + m_free->length);
}
/*
* Check for shrink block.
*/
#ifdef DEBUG_HEAP
printf("HEAP_ReAlloc Check for shrink block !\n");
#endif
if (new_size < m->length - 4 * sizeof(MDESC))
{
m_free = m + new_size / sizeof(MDESC) + 2;
m_free->next = m_free;
m_free->prev = m_free;
m_free->length = m->length - (int) m_free - (int) m;
m->length = (int) m_free - (int) (m + 1);
HEAP_Free(free_list, m_free + 1);
}
return old_block;
}
/**********************************************************************
* HEAP_Free
*/
int
HEAP_Free(MDESC **free_list, void *block)
{
MDESC *m_free;
MDESC *m;
MDESC *m_prev;
/*
* Validate pointer.
*/
m_free = (MDESC *) block - 1;
if (m_free->prev != m_free || m_free->next != m_free ||
((int) m_free & 0xffff0000) != ((int) *free_list & 0xffff0000))
{
#ifdef DEBUG_HEAP
printf("Attempt to free bad pointer,"
"m_free = %08x, *free_list = %08x\n",
m_free, free_list);
#endif
return -1;
}
/*
* Find location in free list.
*/
m_prev = NULL;
for (m = *free_list; m != NULL && m < m_free; m = m->next)
m_prev = m;
if (m_prev != NULL && (int) m_prev + m_prev->length > (int) m_free)
{
#ifdef DEBUG_HEAP
printf("Attempt to free bad pointer,"
"m_free = %08x, m_prev = %08x (length %x)\n",
m_free, m_prev, m_prev->length);
#endif
return -1;
}
if ((m != NULL && (int) m_free + m_free->length > (int) m) ||
(int) m_free + m_free->length > ((int) m_free | 0xffff))
{
#ifdef DEBUG_HEAP
printf("Attempt to free bad pointer,"
"m_free = %08x (length %x), m = %08x\n",
m_free, m_free->length, m);
#endif
return -1;
}
/*
* Put block back in free list.
* Does it merge with the previos block?
*/
if (m_prev != NULL)
{
if ((int) m_prev + m_prev->length == (int) m_free)
{
m_prev->length += sizeof(MDESC) + m_free->length;
m_free = m_prev;
}
else
{
m_prev->next = m_free;
m_free->prev = m_prev;
}
}
else
{
*free_list = m_free;
m_free->prev = NULL;
}
/*
* Does it merge with the next block?
*/
if (m != NULL)
{
if ((int) m_free + m_free->length == (int) m)
{
m_free->length += sizeof(MDESC) + m->length;
m_free->next = m->next;
}
else
{
m->prev = m_free;
m_free->next = m;
}
}
else
{
m_free->next = NULL;
}
return 0;
}
/**********************************************************************
* HEAP_LocalFindHeap
*/
LHEAP *
HEAP_LocalFindHeap(unsigned short owner)
{
LHEAP *lh;
#ifdef DEBUG_HEAP
printf("HEAP_LocalFindHeap: owner %04x\n", owner);
#endif
for (lh = LocalHeaps; lh != NULL; lh = lh->next)
{
if (lh->selector == owner)
return lh;
}
return NULL;
}
/**********************************************************************
* HEAP_LocalInit
*/
void
HEAP_LocalInit(unsigned short owner, void *start, int length)
{
LHEAP *lh;
#ifdef DEBUG_HEAP
printf("HEAP_LocalInit: owner %04x, start %08x, length %04x\n",
owner, start, length);
#endif
if (length < 2 * sizeof(MDESC))
return;
lh = (LHEAP *) malloc(sizeof(*lh));
if (lh == NULL)
return;
lh->next = LocalHeaps;
lh->selector = owner;
lh->local_table = NULL;
HEAP_Init(&lh->free_list, start, length);
LocalHeaps = lh;
}
/**********************************************************************
* WIN16_LocalAlloc
*/
void *
WIN16_LocalAlloc(int flags, int bytes)
{
void *m;
#ifdef DEBUG_HEAP
printf("WIN16_LocalAlloc: flags %x, bytes %d\n", flags, bytes);
printf(" called from segment %04x\n", Stack16Frame[11]);
#endif
m = HEAP_Alloc(LOCALHEAP(), flags, bytes);
#ifdef DEBUG_HEAP
printf("WIN16_LocalAlloc: returning %x\n", (int) m);
#endif
return m;
}
/**********************************************************************
* WIN16_LocalCompact
*/
int
WIN16_LocalCompact(int min_free)
{
MDESC *m;
int max_block;
max_block = 0;
for (m = *LOCALHEAP(); m != NULL; m = m->next)
if (m->length > max_block)
max_block = m->length;
return max_block;
}
/**********************************************************************
* WIN16_LocalFlags
*/
unsigned int
WIN16_LocalFlags(unsigned int handle)
{
MDESC *m;
m = (MDESC *) (((int) *LOCALHEAP() & 0xffff0000) |
(handle & 0xffff)) - 1;
if (m->next != m || m->prev != m)
return 0;
return m->lock;
}
/**********************************************************************
* WIN16_LocalFree
*/
unsigned int
WIN16_LocalFree(unsigned int handle)
{
unsigned int addr;
addr = ((int) *LOCALHEAP() & 0xffff0000) | (handle & 0xffff);
if (HEAP_Free(LOCALHEAP(), (void *) addr) < 0)
return handle;
else
return 0;
}
/**********************************************************************
* WIN16_LocalInit
*/
unsigned int
WIN16_LocalInit(unsigned int segment, unsigned int start, unsigned int end)
{
unsigned short owner = HEAP_OWNER;
LHEAP *lh = HEAP_LocalFindHeap(owner);
if (segment == 0)
{
/* Get current DS */
segment = Stack16Frame[6];
}
if (lh == NULL)
{
HEAP_LocalInit(owner,
(void *) ((segment << 16) | start), end - start + 1);
}
else
{
HEAP_Init(&lh->free_list,
(void *) ((segment << 16) | start), end - start + 1);
}
printf("WIN16_LocalInit // return segment=%04X !\n", segment);
return segment;
}
/**********************************************************************
* WIN16_LocalLock
*/
void *
WIN16_LocalLock(unsigned int handle)
{
MDESC *m;
m = (MDESC *) (((int) *LOCALHEAP() & 0xffff0000) |
(handle & 0xffff)) - 1;
if (m->next != m || m->prev != m)
return 0;
m->lock++;
return (void *) (m + 1);
}
/**********************************************************************
* WIN16_LocalReAlloc
*/
void *
WIN16_LocalReAlloc(unsigned int handle, int bytes, int flags)
{
void *m;
#ifdef DEBUG_HEAP
printf("WIN16_LocalReAlloc(%04X, %d, %04X); !\n", handle, bytes, flags);
printf("WIN16_LocalReAlloc // LOCALHEAP()=%08X !\n", LOCALHEAP());
printf("WIN16_LocalReAlloc // *LOCALHEAP()=%08X !\n", *LOCALHEAP());
#endif
m = HEAP_ReAlloc(LOCALHEAP(), (void *)
(((int) *LOCALHEAP() & 0xffff0000) | (handle & 0xffff)),
bytes, flags);
return m;
}
/**********************************************************************
* WIN16_LocalSize
*/
unsigned int
WIN16_LocalSize(unsigned int handle)
{
MDESC *m;
m = (MDESC *) (((int) *LOCALHEAP() & 0xffff0000) |
(handle & 0xffff)) - 1;
if (m->next != m || m->prev != m)
return 0;
return m->length;
}
/**********************************************************************
* WIN16_LocalUnlock
*/
unsigned int
WIN16_LocalUnlock(unsigned int handle)
{
MDESC *m;
m = (MDESC *) (((int) *LOCALHEAP() & 0xffff0000) |
(handle & 0xffff)) - 1;
if (m->next != m || m->prev != m)
return 1;
if (m->lock > 0)
m->lock--;
return 0;
}
/**********************************************************************
* GetFreeSystemResources (user.284)
*/
#define USERRESOURCES 2
#define GDIRESOURCES 1
#define SYSTEMRESOURCES 0
#include <user.h>
#include <gdi.h>
WORD GetFreeSystemResources(WORD SystemResourceType)
{
unsigned int GdiFree=0,GdiResult=0;
unsigned int UserFree=0,UserResult=0;
unsigned int result=0;
MDESC *m;
printf("GetFreeSystemResources(%u)\n",SystemResourceType);
switch(SystemResourceType) {
case(USERRESOURCES):
for (m = USER_Heap; m != NULL; m = m->next) /* add up free area in heap */
UserFree += m->length;
result=(UserFree*100)/65516; /* 65516 == 64K */
break;
case(GDIRESOURCES):
for (m = GDI_Heap; m != NULL; m = m->next)
GdiFree += m->length;
result=(GdiFree*100)/65516;
break;
case(SYSTEMRESOURCES):
for (m = USER_Heap; m != NULL; m = m->next)
UserFree += m->length;
UserResult=(UserFree*100)/65516;
for (m = GDI_Heap; m != NULL; m = m->next)
GdiFree += m->length;
GdiResult=(GdiFree*100)/65516;
result=(UserResult < GdiResult) ? UserResult:GdiResult;
break;
default:
result=0;
break;
}
return(result);
}