summaryrefslogtreecommitdiff
path: root/proc/alloc.c
blob: 0bc71a5224c056487e60c96cfcb9b713018eee17 (plain)
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
80
// Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
// Copyright 2002 Albert Cahalan
//
// This file is placed under the conditions of the GNU Library
// General Public License, version 2, or any later version.
// See file COPYING for information on distribution conditions.

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "alloc.h"


static void xdefault_error(const char *restrict fmts, ...) __attribute__((format(printf,1,2)));
static void xdefault_error(const char *restrict fmts, ...) {
    va_list va;

    va_start(va, fmts);
    fprintf(stderr, fmts, va);
    va_end(va);
}

message_fn xalloc_err_handler = xdefault_error;


void *xcalloc(unsigned int size) {
    void * p;

    if (size == 0)
        ++size;
    p = calloc(1, size);
    if (!p) {
        xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
        exit(EXIT_FAILURE);
    }
    return p;
}

void *xmalloc(unsigned int size) {
    void *p;

    if (size == 0)
        ++size;
    p = malloc(size);
    if (!p) {
        xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
        exit(EXIT_FAILURE);
    }
    return(p);
}

void *xrealloc(void *oldp, unsigned int size) {
    void *p;

    if (size == 0)
        ++size;
    p = realloc(oldp, size);
    if (!p) {
        xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
        exit(EXIT_FAILURE);
    }
    return(p);
}

char *xstrdup(const char *str) {
    char *p = NULL;

    if (str) {
        unsigned int size = strlen(str) + 1;
        p = malloc(size);
        if (!p) {
            xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
            exit(EXIT_FAILURE);
        }
        strcpy(p, str);
    }
    return(p);
}