ドキュメントの修正
[YACASL2.git] / src / cmem.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <stdbool.h>
6 #include "cmem.h"
7
8 void *malloc_chk(size_t size, char *tag)
9 {
10     void *p;
11
12     if((p = malloc(size)) == NULL) {
13         fprintf(stderr, "%s: cannot allocate memory\n", tag);
14         exit(1);
15     }
16     return memset(p, 0, size);
17 }
18
19 void *calloc_chk(size_t nmemb, size_t size, char *tag)
20 {
21     void *p;
22
23     if((p = calloc(nmemb, size)) == NULL) {
24         fprintf(stderr, "%s: cannot allocate memory\n", tag);
25         exit(1);
26     }
27     return p;
28 }
29
30 char *strdup_chk(const char *s, char *tag)
31 {
32     assert(s != NULL);
33     char *t;
34
35     t = malloc_chk(strlen(s) + 1, tag);
36     strcpy(t, s);
37     return t;
38 }