File: xmalloc.c

package info (click to toggle)
wine 0.0.980315-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 10,136 kB
  • ctags: 26,112
  • sloc: ansic: 156,310; makefile: 1,160; yacc: 807; perl: 655; lex: 555; sh: 304
file content (56 lines) | stat: -rw-r--r-- 1,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
   xmalloc - a safe malloc

   Use this function instead of malloc whenever you don't intend to check
   the return value yourself, for instance because you don't have a good
   way to handle a zero return value.

   Typically, Wine's own memory requests should be handled by this function,
   while the clients should use malloc directly (and Wine should return an
   error to the client if allocation fails).

   Copyright 1995 by Morten Welinder.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xmalloc.h"

void *xmalloc( int size )
{
    void *res;

    res = malloc (size ? size : 1);
    if (res == NULL)
    {
        fprintf (stderr, "Virtual memory exhausted.\n");
        exit (1);
    }
    return res;
}


void *xrealloc( void *ptr, int size )
{
    void *res = realloc (ptr, size);
    if ((res == NULL) && size)
    {
        fprintf (stderr, "Virtual memory exhausted.\n");
        exit (1);
    }
    return res;
}


char *xstrdup( const char *str )
{
    char *res = strdup( str );
    if (!res)
    {
        fprintf (stderr, "Virtual memory exhausted.\n");
        exit (1);
    }
    return res;
}