Removed most calls to xmalloc/xrealloc.

diff --git a/graphics/win16drv/init.c b/graphics/win16drv/init.c
index e089035..62310c0 100644
--- a/graphics/win16drv/init.c
+++ b/graphics/win16drv/init.c
@@ -13,7 +13,6 @@
 #include "heap.h"
 #include "font.h"
 #include "options.h"
-#include "xmalloc.h"
 #include "debugtools.h"
 #include "dc.h"
 
@@ -225,8 +224,12 @@
 
     /* Now Get the device capabilities from the printer driver */
     
-    printerDevCaps = (DeviceCaps *) xmalloc(sizeof(DeviceCaps));
-    memset(printerDevCaps, 0, sizeof(DeviceCaps));
+    printerDevCaps = (DeviceCaps *) calloc(1, sizeof(DeviceCaps));
+    if(printerDevCaps == NULL) {
+        ERR("No memory to read the device capabilities!");
+        HeapFree( GetProcessHeap(), 0, physDev );
+        return FALSE;
+    }
 
     if(!output) output = "LPT1:";
     /* Get GDIINFO which is the same as a DeviceCaps structure */
diff --git a/graphics/x11drv/bitblt.c b/graphics/x11drv/bitblt.c
index 6a42042..a27ff0d 100644
--- a/graphics/x11drv/bitblt.c
+++ b/graphics/x11drv/bitblt.c
@@ -22,7 +22,6 @@
 #include "options.h"
 #include "x11drv.h"
 #include "debugtools.h"
-#include "xmalloc.h" /* for XCREATEIMAGE macro */
 
 DEFAULT_DEBUG_CHANNEL(bitblt)
 
diff --git a/graphics/x11drv/brush.c b/graphics/x11drv/brush.c
index c22aa91..abaecd0 100644
--- a/graphics/x11drv/brush.c
+++ b/graphics/x11drv/brush.c
@@ -16,7 +16,6 @@
 #include "color.h"
 #include "x11drv.h"
 #include "debugtools.h"
-#include "xmalloc.h" /* for XCREATEIMAGE macro */
 #include "monitor.h"
 #include "local.h"
 
diff --git a/graphics/x11drv/dib.c b/graphics/x11drv/dib.c
index eabe051..bb1cc05 100644
--- a/graphics/x11drv/dib.c
+++ b/graphics/x11drv/dib.c
@@ -21,6 +21,7 @@
 # endif
 #endif /* defined(HAVE_LIBXXSHM) */
 
+#include <stdlib.h>
 #include "windef.h"
 #include "bitmap.h"
 #include "x11drv.h"
@@ -30,7 +31,6 @@
 #include "callback.h"
 #include "selectors.h"
 #include "global.h"
-#include "xmalloc.h" /* for XCREATEIMAGE macro */
 
 DEFAULT_DEBUG_CHANNEL(bitmap)
 DECLARE_DEBUG_CHANNEL(x11drv)
@@ -2510,7 +2510,12 @@
 				 DefaultVisualOfScreen(X11DRV_GetXScreen()),
 				 descr->depth, ZPixmap, 0, NULL,
 				 descr->infoWidth, lines, 32, 0 );
-	bmpImage->data = xcalloc( bmpImage->bytes_per_line * lines );
+	bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
+        if(bmpImage->data == NULL) {
+            ERR("Out of memory!");
+            XDestroyImage( bmpImage );
+            return lines;
+        }
     }
 
       /* Transfer the pixels */
@@ -2610,8 +2615,12 @@
 				 DefaultVisualOfScreen(X11DRV_GetXScreen()),
 				 descr->depth, ZPixmap, 0, NULL,
 				 descr->infoWidth, lines, 32, 0 );
-	bmpImage->data = xcalloc( bmpImage->bytes_per_line * lines );
-    }
+	bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
+        if(bmpImage->data == NULL) {
+            ERR("Out of memory!");
+            XDestroyImage( bmpImage );
+            return lines;
+        }                                                                           }
 
     XGetSubImage( display, descr->drawable, descr->xDest, descr->yDest,
                   descr->width, descr->height, AllPlanes, ZPixmap,
diff --git a/graphics/x11drv/graphics.c b/graphics/x11drv/graphics.c
index 55bfd5b..9d05d3d 100644
--- a/graphics/x11drv/graphics.c
+++ b/graphics/x11drv/graphics.c
@@ -40,7 +40,6 @@
 #include "region.h"
 #include "struct32.h"
 #include "debugtools.h"
-#include "xmalloc.h"
 
 DEFAULT_DEBUG_CHANNEL(graphics)
 
@@ -977,11 +976,15 @@
 
     if((oldwidth = physDev->pen.width) == 0) physDev->pen.width = 1;
 
-    points = (XPoint *) xmalloc (sizeof (XPoint) * (count));
+    if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
+    {
+        WARN("No memory to convert POINTs to XPoints!\n");
+        return FALSE;
+    }
     for (i = 0; i < count; i++)
     {
-    points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
-    points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
+        points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
+        points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
     }
 
     if (X11DRV_SetupGCForPen ( dc ))
@@ -996,7 +999,7 @@
     	X11DRV_DIB_UpdateDIBSection(dc, TRUE);
     }
 
-    free( points );
+    HeapFree( GetProcessHeap(), 0, points );
     physDev->pen.width = oldwidth;
     return TRUE;
 }
@@ -1013,7 +1016,11 @@
     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
     BOOL update = FALSE;
 
-    points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
+    if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
+    {
+        WARN("No memory to convert POINTs to XPoints!\n");
+        return FALSE;
+    }
     for (i = 0; i < count; i++)
     {
 	points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
@@ -1040,7 +1047,7 @@
     /* Update the DIBSection from the pixmap */
     if (update) X11DRV_DIB_UpdateDIBSection(dc, TRUE);
 
-    free( points );
+    HeapFree( GetProcessHeap(), 0, points );
     return TRUE;
 }
 
@@ -1072,8 +1079,11 @@
 	X11DRV_DIB_UpdateDIBSection(dc, FALSE);
  
 	for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
-	points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
-
+        if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
+        {
+            WARN("No memory to convert POINTs to XPoints!\n");
+            return FALSE;
+        }
 	for (i = 0; i < polygons; i++)
 	{
 	    for (j = 0; j < counts[i]; j++)
@@ -1090,7 +1100,7 @@
 	/* Update the DIBSection of the dc's bitmap */
 	X11DRV_DIB_UpdateDIBSection(dc, TRUE);
 
-	free( points );
+	HeapFree( GetProcessHeap(), 0, points );
     }
     return TRUE;
 }
@@ -1113,8 +1123,11 @@
     	X11DRV_DIB_UpdateDIBSection(dc, FALSE);
  
         for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
-        points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
-
+        if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
+        {
+            WARN("No memory to convert POINTs to XPoints!\n");
+            return FALSE;
+        }
         for (i = 0; i < polylines; i++)
         {
             for (j = 0; j < counts[i]; j++)
@@ -1131,7 +1144,7 @@
 	/* Update the DIBSection of the dc's bitmap */
     	X11DRV_DIB_UpdateDIBSection(dc, TRUE);
 
-        free( points );
+	HeapFree( GetProcessHeap(), 0, points );
     }
     return TRUE;
 }
diff --git a/graphics/x11drv/palette.c b/graphics/x11drv/palette.c
index 53a0329..55612a6 100644
--- a/graphics/x11drv/palette.c
+++ b/graphics/x11drv/palette.c
@@ -24,7 +24,7 @@
 #include "xmalloc.h"
 #include "x11drv.h"
 
-DEFAULT_DEBUG_CHANNEL(palette)
+DEFAULT_DEBUG_CHANNEL(palette);
 
 /* Palette indexed mode:
  *	logical palette -> mapping -> pixel
@@ -463,21 +463,32 @@
 		      (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) ) 
 		     ? NB_RESERVED_COLORS/2 : -1;
 
-   COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*256);
+   COLOR_sysPal = (PALETTEENTRY*)malloc(sizeof(PALETTEENTRY)*256);
+   if(COLOR_sysPal == NULL) {
+       ERR("Can not allocate system palette!\n");
+       return FALSE;
+   }
 
    /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
 
    if( MONITOR_GetDepth(&MONITOR_PrimaryMonitor) <= 8 )
-     {
-       X11DRV_PALETTE_XPixelToPalette = (int*)xmalloc(sizeof(int)*256);
-       memset( X11DRV_PALETTE_XPixelToPalette, 0, 256*sizeof(int) );
-     }
+   {
+       X11DRV_PALETTE_XPixelToPalette = (int*)calloc(256, sizeof(int));
+       if(X11DRV_PALETTE_XPixelToPalette == NULL) {
+           ERR("Out of memory: XPixelToPalette!\n");
+           return FALSE;
+       }
+   }
 
    /* for hicolor visuals PaletteToPixel mapping is used to skip
     * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical(). 
     */
 
-   X11DRV_PALETTE_PaletteToXPixel = (int*)xmalloc(sizeof(int)*256);
+   X11DRV_PALETTE_PaletteToXPixel = (int*)malloc(sizeof(int)*256);
+   if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
+       ERR("Out of memory: PaletteToXPixel!\n");
+       return FALSE;
+   }
 
    for( i = j = 0; i < 256; i++ )
    {
@@ -851,6 +862,7 @@
     char flag;
     int  prevMapping = (palPtr->mapping) ? 1 : 0;
     int  index, iRemapped = 0;
+    int* mapping;
 
     /* reset dynamic system palette entries */
 
@@ -859,8 +871,13 @@
 
     /* initialize palette mapping table */
  
-    palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
-                                     palPtr->logpalette.palNumEntries);
+    mapping = (int*)realloc(palPtr->mapping, sizeof(int)*
+                            palPtr->logpalette.palNumEntries);
+    if(mapping == NULL) {
+        ERR("Can not allocate new mapping -- memory exausted!");
+        return 0;
+    }
+    palPtr->mapping = mapping;
 
     for( uNum += uStart; uStart < uNum; uStart++ )
     {
diff --git a/include/x11drv.h b/include/x11drv.h
index 5a8aa03..cba80c9 100644
--- a/include/x11drv.h
+++ b/include/x11drv.h
@@ -191,7 +191,7 @@
 { \
     int width_bytes = X11DRV_DIB_GetXImageWidthBytes( (width), (bpp) ); \
     (image) = TSXCreateImage(display, DefaultVisualOfScreen(X11DRV_GetXScreen()), \
-                           (bpp), ZPixmap, 0, xcalloc( (height)*width_bytes ),\
+                           (bpp), ZPixmap, 0, calloc( (height), width_bytes ),\
                            (width), (height), 32, width_bytes ); \
 }
 
@@ -250,8 +250,6 @@
 
 } X11DRV_DIB_IMAGEBITS_DESCR;
 
-extern int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr );
-extern int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr );
 extern int *X11DRV_DIB_BuildColorMap( struct tagDC *dc, WORD coloruse,
 				      WORD depth, const BITMAPINFO *info,
 				      int *nColors );
diff --git a/library/winestub.c b/library/winestub.c
index 1c0317b..d818872 100644
--- a/library/winestub.c
+++ b/library/winestub.c
@@ -5,7 +5,6 @@
 #include "winbase.h"
 #include "wingdi.h"
 #include "winuser.h"
-#include "xmalloc.h"
 
 extern int PASCAL WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
 
@@ -33,7 +32,11 @@
 
   /* Alloc szCmdParam */
   for (i = 1; i < argc; i++) len += strlen(argv[i]) + 1;
-  lpszCmdParam = (LPSTR) xmalloc(len + 1);
+  lpszCmdParam = (LPSTR) malloc(len + 1);
+  if(lpszCmdParam == NULL) {
+    MESSAGE("Not enough memory to store command parameters!");
+    return 1;
+  }
   /* Concatenate arguments */
   if (argc > 1) strcpy(lpszCmdParam, argv[1]);
   else lpszCmdParam[0] = '\0';
diff --git a/loader/ne/segment.c b/loader/ne/segment.c
index 87e9b7e..3c209c2 100644
--- a/loader/ne/segment.c
+++ b/loader/ne/segment.c
@@ -27,7 +27,6 @@
 #include "stackframe.h"
 #include "builtin16.h"
 #include "debugtools.h"
-#include "xmalloc.h"
 #include "toolhelp.h"
 
 DECLARE_DEBUG_CHANNEL(dll)
@@ -138,8 +137,14 @@
 	 but may be missing something. If you have any doc please either send
 	 it to me or fix the code yourself. gfm@werple.mira.net.au
       */
-      char* buff = xmalloc(size);
+      char* buff = HeapAlloc(GetProcessHeap(), 0, size);
       char* curr = buff;
+
+      if(buff == NULL) {
+          WARN_(dll)("Memory exausted!");
+          return FALSE;
+      }
+
       ReadFile(hf, buff, size, &res, NULL);
       while(curr < buff + size) {
 	unsigned int rept = *((short*) curr)++;
@@ -152,7 +157,7 @@
 	}
 	curr += len;
       }
-      free(buff);
+      HeapFree(GetProcessHeap(), 0, buff);
     }
 
     pSeg->flags |= NE_SEGFLAGS_LOADED;
@@ -175,7 +180,11 @@
                    (char *)pModule + pModule->name_table + 1,
                    segnum, pSeg->hSeg );
 
-    reloc_entries = (struct relocation_entry_s *)xmalloc(count * sizeof(struct relocation_entry_s));
+    reloc_entries = (struct relocation_entry_s *)HeapAlloc(GetProcessHeap(), 0, count * sizeof(struct relocation_entry_s));
+    if(reloc_entries == NULL) {
+        WARN_(fixup)("Not enough memory for relocation entries!");
+        return FALSE;
+    }
     if (!ReadFile( hf, reloc_entries, count * sizeof(struct relocation_entry_s), &res, NULL) ||
         (res != count * sizeof(struct relocation_entry_s)))
     {
@@ -362,7 +371,7 @@
         }
     }
 
-    free(reloc_entries);
+    HeapFree(GetProcessHeap(), 0, reloc_entries);
     return TRUE;
 
 unknown:
@@ -370,7 +379,7 @@
          "TYPE %d,  OFFSET %04x,  TARGET %04x %04x\n",
          i + 1, rep->address_type, rep->relocation_type, 
          rep->offset, rep->target1, rep->target2);
-    free(reloc_entries);
+    HeapFree(GetProcessHeap(), 0, reloc_entries);
     return FALSE;
 }
 
diff --git a/memory/virtual.c b/memory/virtual.c
index e373ff4..79f0d62 100644
--- a/memory/virtual.c
+++ b/memory/virtual.c
@@ -24,7 +24,6 @@
 #include "winerror.h"
 #include "file.h"
 #include "process.h"
-#include "xmalloc.h"
 #include "global.h"
 #include "server.h"
 #include "debugtools.h"
diff --git a/misc/main.c b/misc/main.c
index a23838b..e61cd93 100644
--- a/misc/main.c
+++ b/misc/main.c
@@ -32,7 +32,6 @@
 #include "builtin32.h"
 #include "debugtools.h"
 #include "debugdefs.h"
-#include "xmalloc.h"
 #include "module.h"
 #include "version.h"
 #include "winnls.h"
diff --git a/misc/printdrv.c b/misc/printdrv.c
index 9f853fb..8b19090 100644
--- a/misc/printdrv.c
+++ b/misc/printdrv.c
@@ -23,7 +23,6 @@
 #include "gdi.h"
 #include "dc.h"
 #include "callback.h"
-#include "xmalloc.h"
 #include "options.h"
 #include "heap.h"
 
@@ -345,7 +344,7 @@
             prev->next = queue->next;
         else
             hpqueue = queue->next;
-        free(queue);
+        HeapFree(GetProcessHeap(), 0, queue);
     }
     
     TRACE("%x got tag %d key %d\n", hPQ, tag, key); 
@@ -359,7 +358,11 @@
  */
 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key) 
 {
-    struct hpq *queueItem = xmalloc(sizeof(struct hpq));
+    struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
+    if(queueItem == NULL) {
+        ERR("Memory exausted!");
+        return FALSE;
+    }
     queueItem->next = hpqueue;
     hpqueue = queueItem;
     queueItem->key = key;
@@ -487,10 +490,10 @@
     if (pPrintJob != NULL)
     {
 	gPrintJobsTable[pPrintJob->nIndex] = NULL;
-	free(pPrintJob->pszOutput);
-	free(pPrintJob->pszTitle);
+	HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
+	HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
 	if (pPrintJob->fd >= 0) close(pPrintJob->fd);
-	free(pPrintJob);
+	HeapFree(GetProcessHeap(), 0, pPrintJob);
 	nRet = SP_OK;
     }
     return nRet;
@@ -516,14 +519,17 @@
 	fd = CreateSpoolFile(lpOutput);
 	if (fd >= 0)
 	{
-	    hHandle = 1;
+	    pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
+            if(pPrintJob == NULL) {
+                WARN("Memory exausted!");
+                return hHandle;
+            }
+            
+            hHandle = 1;
 
-	    pPrintJob = xmalloc(sizeof(PRINTJOB));
-	    memset(pPrintJob, 0, sizeof(PRINTJOB));
-
-	    pPrintJob->pszOutput = strdup(lpOutput);
+	    pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
 	    if(lpTitle)
-	        pPrintJob->pszTitle = strdup(lpTitle);
+	        pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
 	    pPrintJob->hDC = hDC;
 	    pPrintJob->fd = fd;
 	    pPrintJob->nIndex = 0;
diff --git a/misc/registry.c b/misc/registry.c
index 9330668..9b0556a 100644
--- a/misc/registry.c
+++ b/misc/registry.c
@@ -44,7 +44,6 @@
 #include "file.h"
 #include "heap.h"
 #include "debugtools.h"
-#include "xmalloc.h"
 #include "options.h"
 #include "winreg.h"
 #include "server.h"
@@ -73,6 +72,17 @@
 #define UNICONVMASK	((1<<REG_SZ)|(1<<REG_MULTI_SZ)|(1<<REG_EXPAND_SZ))
 
 
+static void *xmalloc( size_t size )
+{
+    void *res;
+ 
+    res = malloc (size ? size : 1);
+    if (res == NULL) {
+        WARN("Virtual memory exhausted.\n");
+        exit (1);
+    }
+    return res;
+}                                                                              
 
 /*
  * QUESTION
@@ -404,8 +414,13 @@
 			if (NULL==(s=strchr(curread,'\n'))) {
 				/* buffer wasn't large enough */
 				curoff	= strlen(*buf);
-				*buf	= xrealloc(*buf,*len*2);
-				curread	= *buf + curoff;
+				curread	= realloc(*buf,*len*2);
+                                if(curread == NULL) {
+                                    WARN("Out of memory");
+                                    return 0;
+                                }
+                                *buf	= curread;
+				curread+= curoff;
 				mylen	= *len;	/* we filled up the buffer and 
 						 * got new '*len' bytes to fill
 						 */
diff --git a/misc/xmalloc.c b/misc/xmalloc.c
index 10e69c9..eed0cc3 100644
--- a/misc/xmalloc.c
+++ b/misc/xmalloc.c
@@ -31,36 +31,3 @@
     memset(res,0,size);
     return res;
 }
-
-void *xcalloc( size_t size )
-{
-    void *res;
-
-    res = xmalloc (size);
-    memset(res,0,size);
-    return res;
-}
-
-
-void *xrealloc( void *ptr, size_t size )
-{
-    void *res = realloc (ptr, size);
-    if ((res == NULL) && size)
-    {
-        MESSAGE("Virtual memory exhausted.\n");
-        exit (1);
-    }
-    return res;
-}
-
-
-char *xstrdup( const char *str )
-{
-    char *res = strdup( str );
-    if (!res)
-    {
-        MESSAGE("Virtual memory exhausted.\n");
-        exit (1);
-    }
-    return res;
-}
diff --git a/objects/palette.c b/objects/palette.c
index 1340446..576df4a 100644
--- a/objects/palette.c
+++ b/objects/palette.c
@@ -18,7 +18,6 @@
 #include "gdi.h"
 #include "color.h"
 #include "palette.h"
-#include "xmalloc.h"
 #include "debugtools.h"
 #include "winerror.h"
 
@@ -64,15 +63,14 @@
         palPtr->palPalEntry[i].peFlags = 0;
     }
     hpalette = CreatePalette16( palPtr );
+    HeapFree( GetProcessHeap(), 0, palPtr );
 
     palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
     if (palObj)
     {
-        palObj->mapping = xmalloc( sizeof(int) * 20 );
-
+        if (!(palObj->mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int) * 20 )))
+            ERR("Can not create palette mapping -- out of memory!");
         GDI_HEAP_UNLOCK( hpalette );
-
-        HeapFree( GetProcessHeap(), 0, palPtr );
     }
     	
     return hpalette;
@@ -335,8 +333,19 @@
     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
     if( !palPtr ) return FALSE;
 
-    if( mapping )
-        palPtr->mapping = (int*) xrealloc( mapping, cEntries * sizeof(int) );
+    if( mapping ) 
+    {
+        int *newMap = (int*) HeapReAlloc(GetProcessHeap(), 0, 
+                                    mapping, cEntries * sizeof(int) );
+	if(newMap == NULL) 
+        {
+            ERR("Can not resize mapping -- out of memory!");
+            GDI_HEAP_UNLOCK( hPal );
+            return FALSE;
+        }
+        palPtr->mapping = newMap;
+    }
+
     if( cEntries > cPrevEnt ) 
     {
 	if( mapping )
diff --git a/scheduler/client.c b/scheduler/client.c
index 6767416..f148f53 100644
--- a/scheduler/client.c
+++ b/scheduler/client.c
@@ -30,7 +30,6 @@
 #include "server.h"
 #include "winerror.h"
 #include "options.h"
-#include "xmalloc.h"
 
 /* Some versions of glibc don't define this */
 #ifndef SCM_RIGHTS
@@ -305,7 +304,8 @@
             execl( BINDIR "/wineserver", "wineserver", NULL );
             if (oldcwd) chdir( oldcwd );
             /* now try the dir we were launched from */
-            path = xmalloc( strlen(argv0) + 20 );
+            if (!(path = malloc( strlen(argv0) + 20 )))
+                fatal_error( "out of memory\n" );
             if ((p = strrchr( strcpy( path, argv0 ), '/' )))
             {
                 strcpy( p, "/wineserver" );
@@ -396,7 +396,7 @@
     /* retrieve the current directory */
     for (size = 512; ; size *= 2)
     {
-        oldcwd = xmalloc( size );
+        if (!(oldcwd = malloc( size ))) break;
         if (getcwd( oldcwd, size )) break;
         free( oldcwd );
         if (errno == ERANGE) continue;
@@ -407,7 +407,8 @@
     /* get the server directory name */
     if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
     configdir = PROFILE_GetConfigDir();
-    serverdir = xmalloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
+    serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
+    if (!serverdir) fatal_error( "out of memory\n" );
     strcpy( serverdir, configdir );
     strcat( serverdir, SERVERDIR );
     strcat( serverdir, hostname );
diff --git a/scheduler/critsection.c b/scheduler/critsection.c
index c5560fa..3c8c587 100644
--- a/scheduler/critsection.c
+++ b/scheduler/critsection.c
@@ -115,20 +115,21 @@
  */
 BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
 {
-    if (InterlockedIncrement( &crit->LockCount ))
+    BOOL ret = FALSE;
+    if (InterlockedCompareExchange( (PVOID *)&crit->LockCount,
+                                    (PVOID)0L, (PVOID)-1L ) == (PVOID)-1L)
     {
-        if (crit->OwningThread == GetCurrentThreadId())
-        {
-            crit->RecursionCount++;
-            return TRUE;
-        }
-        /* FIXME: this doesn't work */
-        InterlockedDecrement( &crit->LockCount );
-        return FALSE;
+        crit->OwningThread   = GetCurrentThreadId();
+        crit->RecursionCount = 1;
+        ret = TRUE;
     }
-    crit->OwningThread   = GetCurrentThreadId();
-    crit->RecursionCount = 1;
-    return TRUE;
+    else if (crit->OwningThread == GetCurrentThreadId())
+    {
+	InterlockedIncrement( &crit->LockCount );
+	crit->RecursionCount++;
+	ret = TRUE;
+    }
+    return ret;
 }
 
 
diff --git a/windows/clipboard.c b/windows/clipboard.c
index 91540af..c1a77ad 100644
--- a/windows/clipboard.c
+++ b/windows/clipboard.c
@@ -33,7 +33,6 @@
 #include "task.h"
 #include "queue.h"
 #include "clipboard.h"
-#include "xmalloc.h"
 #include "debugtools.h"
 
 DEFAULT_DEBUG_CHANNEL(clipboard)
@@ -1042,13 +1041,21 @@
 
     /* allocate storage for new format entry */
 
-    lpNewFormat = (LPWINE_CLIPFORMAT)xmalloc(sizeof(WINE_CLIPFORMAT));
+    lpNewFormat = (LPWINE_CLIPFORMAT)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
+    if(lpNewFormat == NULL) {
+        WARN("No more memory for a new format!");
+        return 0;
+    }
     lpFormat->NextFormat = lpNewFormat;
     lpNewFormat->wFormatID = LastRegFormat;
     lpNewFormat->wRefCount = 1;
 
-    lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
-    strcpy(lpNewFormat->Name, FormatName);
+    lpNewFormat->Name = (LPSTR)HEAP_strdupA(GetProcessHeap(), 0, FormatName);
+    if(lpNewFormat->Name == NULL) {
+        WARN("No more memory for the new format name!");
+        HeapFree(GetProcessHeap(), 0, lpNewFormat);
+        return 0;
+    }
 
     lpNewFormat->wDataPresent = 0;
     lpNewFormat->hData16 = 0;
diff --git a/windows/ttydrv/clipboard.c b/windows/ttydrv/clipboard.c
index d836d80..c9ef3aa 100644
--- a/windows/ttydrv/clipboard.c
+++ b/windows/ttydrv/clipboard.c
@@ -66,7 +66,7 @@
 }
 
 /**************************************************************************
- *		X11DRV_CLIPBOARD_IsSelectionowner
+ *		TTYDRV_CLIPBOARD_IsSelectionowner
  *
  * Returns: TRUE - We(WINE) own the selection, FALSE - Selection not owned by us
  */
diff --git a/windows/ttydrv/keyboard.c b/windows/ttydrv/keyboard.c
index d374ca0..f59975a 100644
--- a/windows/ttydrv/keyboard.c
+++ b/windows/ttydrv/keyboard.c
@@ -77,7 +77,7 @@
 }
 
 /***********************************************************************
- *		X11DRV_KEYBOARD_GetDIState
+ *		TTYDRV_KEYBOARD_GetDIState
  */
 BOOL TTYDRV_KEYBOARD_GetDIState(DWORD len, LPVOID ptr)
 {