summaryrefslogtreecommitdiffstats
path: root/hw4/l4basic/memwrap.c
blob: a1547d7c58494aa4fb7c262aee2d639e20904ffc (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "memwrap.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define RETRY_SEC     0
#define RETRY_NSEC    250000000
#define MSG_BUF_LEN   256

static void show_errmsg (void) {
    int errno_backup = errno;

    const char fail_msg[] = "Fail to allocate memory: ";
    const size_t fail_len = LBS_STR_STATIC_STRLEN (fail_msg);
    write (STDERR_FILENO, fail_msg, fail_len);

    char err_msg[MSG_BUF_LEN];
    size_t err_len;
    if (strerror_r (errno_backup, err_msg, MSG_BUF_LEN)) {
        const char unknown_msg[] = "Unknown error code";
        const size_t unknown_len = LBS_STR_STATIC_STRLEN (unknown_msg);
        write (STDERR_FILENO, unknown_msg, unknown_len);
    } else {
        err_len = strlen (err_msg);
        write (STDERR_FILENO, err_msg, err_len);
    }

    const char retry_msg[] = ". Retry ...\n";
    const size_t retry_len = LBS_STR_STATIC_STRLEN (retry_msg);
    write (STDERR_FILENO, retry_msg, retry_len);

    errno = errno_backup;
}

void* xmalloc (size_t size) {
    void* memptr;
    while ((memptr = malloc (size)) == NULL) {
        show_errmsg ();
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
    }
    return memptr;
}

void* xrealloc (void* ptr, size_t size) {
    void* newptr;
    while ((newptr = realloc (ptr, size)) == NULL) {
        show_errmsg ();
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
    }
    return newptr;
}

char* xstrdup (const char* str) {
    char* newstr;
    while ((newstr = strdup (str)) == NULL) {
        show_errmsg ();
        nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
    }
    return newstr;
}