Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 8 | 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 Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 10 | error to the client if allocation fails). |
| 11 | |
| 12 | Copyright 1995 by Morten Welinder. |
| 13 | |
| 14 | */ |
| 15 | |
Alexandre Julliard | 02ed4c2 | 1996-03-02 19:34:10 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 17 | #include <string.h> |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 18 | #include "xmalloc.h" |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 19 | #include "debug.h" |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 20 | |
Alexandre Julliard | 02ed4c2 | 1996-03-02 19:34:10 +0000 | [diff] [blame] | 21 | void *xmalloc( int size ) |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 22 | { |
| 23 | void *res; |
| 24 | |
| 25 | res = malloc (size ? size : 1); |
| 26 | if (res == NULL) |
| 27 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 28 | MSG("Virtual memory exhausted.\n"); |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 29 | exit (1); |
| 30 | } |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 31 | memset(res,0,size); |
| 32 | return res; |
| 33 | } |
| 34 | |
| 35 | void *xcalloc( int size ) |
| 36 | { |
| 37 | void *res; |
| 38 | |
| 39 | res = xmalloc (size); |
| 40 | memset(res,0,size); |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 41 | return res; |
| 42 | } |
| 43 | |
| 44 | |
Alexandre Julliard | 02ed4c2 | 1996-03-02 19:34:10 +0000 | [diff] [blame] | 45 | void *xrealloc( void *ptr, int size ) |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 46 | { |
| 47 | void *res = realloc (ptr, size); |
Alexandre Julliard | c981d0b | 1996-03-31 16:40:13 +0000 | [diff] [blame] | 48 | if ((res == NULL) && size) |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 49 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 50 | MSG("Virtual memory exhausted.\n"); |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 51 | exit (1); |
| 52 | } |
| 53 | return res; |
| 54 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | char *xstrdup( const char *str ) |
| 58 | { |
| 59 | char *res = strdup( str ); |
| 60 | if (!res) |
| 61 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 62 | MSG("Virtual memory exhausted.\n"); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 63 | exit (1); |
| 64 | } |
| 65 | return res; |
| 66 | } |