blob: 0e757e88203a233f7915d8efbf10b129d16b4dca [file] [log] [blame]
Alexandre Julliard902da691995-11-05 14:39:02 +00001/*
2 xmalloc - a safe malloc
3
4 Use this function instead of malloc whenever you don't intend to check
5 the return value yourself, for instance because you don't have a good
6 way to handle a zero return value.
7
Alexandre Julliard0c126c71996-02-18 18:44:41 +00008 Typically, Wine's own memory requests should be handled by this function,
9 while the clients should use malloc directly (and Wine should return an
Alexandre Julliard902da691995-11-05 14:39:02 +000010 error to the client if allocation fails).
11
12 Copyright 1995 by Morten Welinder.
13
14*/
15
Alexandre Julliard02ed4c21996-03-02 19:34:10 +000016#include <stdlib.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000017#include <string.h>
Alexandre Julliard902da691995-11-05 14:39:02 +000018#include "xmalloc.h"
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000019#include "debug.h"
Alexandre Julliard902da691995-11-05 14:39:02 +000020
Alexandre Julliard02ed4c21996-03-02 19:34:10 +000021void *xmalloc( int size )
Alexandre Julliard902da691995-11-05 14:39:02 +000022{
23 void *res;
24
25 res = malloc (size ? size : 1);
26 if (res == NULL)
27 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000028 MSG("Virtual memory exhausted.\n");
Alexandre Julliard902da691995-11-05 14:39:02 +000029 exit (1);
30 }
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000031 memset(res,0,size);
32 return res;
33}
34
35void *xcalloc( int size )
36{
37 void *res;
38
39 res = xmalloc (size);
40 memset(res,0,size);
Alexandre Julliard902da691995-11-05 14:39:02 +000041 return res;
42}
43
44
Alexandre Julliard02ed4c21996-03-02 19:34:10 +000045void *xrealloc( void *ptr, int size )
Alexandre Julliard902da691995-11-05 14:39:02 +000046{
47 void *res = realloc (ptr, size);
Alexandre Julliardc981d0b1996-03-31 16:40:13 +000048 if ((res == NULL) && size)
Alexandre Julliard902da691995-11-05 14:39:02 +000049 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000050 MSG("Virtual memory exhausted.\n");
Alexandre Julliard902da691995-11-05 14:39:02 +000051 exit (1);
52 }
53 return res;
54}
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000055
56
57char *xstrdup( const char *str )
58{
59 char *res = strdup( str );
60 if (!res)
61 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000062 MSG("Virtual memory exhausted.\n");
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000063 exit (1);
64 }
65 return res;
66}