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 #include "nedit_malloc.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34
35
36
37
38
39
40
41 void *NEditMalloc(
size_t size)
42 {
43 void *ptr = malloc(size);
44 if(!ptr) {
45 fprintf(stderr,
"NEditMalloc(%lu) failed\n", (
unsigned long)size);
46 exit(
1);
47 }
48 return ptr;
49 }
50
51
52 void *NEditCalloc(
size_t nmemb,
size_t size)
53 {
54 void *ptr = NEditMalloc(nmemb*size);
55 memset(ptr,
0,nmemb*size);
56 return ptr;
57 }
58
59
60 void *NEditRealloc(
void *ptr,
size_t new_size)
61 {
62 void *new_ptr = realloc(ptr,new_size);
63 if(!new_ptr && new_size) {
64 fprintf(stderr,
"NEditRealloc(%lu) failed\n", (
unsigned long)new_size);
65 exit(
1);
66 }
67 return new_ptr;
68 }
69
70
71 void NEditFree(
void *ptr)
72 {
73 free(ptr);
74 }
75
76
77 char *NEditStrdup(
const char *str)
78 {
79 size_t len;
80 if(!str)
81 return NULL;
82 len = strlen(str);
83 char *new_str= (
char*)malloc(len+
1);
84 if(!new_str) {
85 fprintf(stderr,
"NEditStrdup(%lu) failed\n", (
unsigned long)len);
86 exit(
1);
87 }
88 memcpy(new_str,str,len+
1);
89 return new_str;
90 }
91
92 char *NEditStrndup(
const char *str,
size_t len)
93 {
94 char *newstr = malloc(len+
1);
95 newstr[len] =
0;
96 memcpy(newstr, str, len);
97 return newstr;
98 }
99