File: xrealloc.c

package info (click to toggle)
icmake 6.22-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,148 kB
  • ctags: 1,042
  • sloc: ansic: 9,241; makefile: 1,134; sh: 235; asm: 126
file content (79 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download | duplicates (4)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
\funcref{xrealloc}{VOIDP xrealloc (\params)}
    {
        {VOIDP} {ptr} {pointer to previously allocated memory, or NULL}
        {int} {size} {new requested size, or 0}
    }
    {pointer to reallocated memory}
    {error()}
    {xstrdup()}
    {xrealloc.c}
    {
        {\em xrealloc()} attempts to reallocate the memory pointed to by {\em
        ptr}. If {\em ptr} is NULL, {\em xrealloc()} simply behaves like {\em
        malloc()}. When allocation indicates failure, {\em error()} is called
        to terminate the program with an appropriate message.

        The new requested size may be zero. In this case, {\em xrealloc()}
        frees the memory associated with {\em ptr}.
    }
*/


#include <malloc.h>
#include "icrssdef.h"

#ifdef MSDOS

void *xrealloc (void *ptr, int size)
{
    register void
        *newptr;

    if (! size)
    {
        if (ptr)
            free (ptr);
        return (NULL);
    }

    newptr = malloc (size);

    if (! newptr)
        error ("out of memory");

    if (ptr)
    {
        memcpy (newptr, ptr, size);
        free (ptr);
    }

    return (newptr);
}

#else

void *xrealloc (void *ptr, int size)
{
    register void
        *newptr;

    if (! size)
    {
        if (ptr)
            free (ptr);
        return (NULL);
    }

    if (ptr)
        newptr = realloc (ptr, size);
    else
        newptr = malloc (size);

    if (! newptr)
        error ("out of memory");

    return (newptr);
}

#endif