code cleanup by replacing all memory allocation functions with ones that handle exceptions
Costa Tsaousis committed
Aug 11, 2016 at 20:32 UTC
e5080111671c910836a7a6cc2aaf4704398825fc
28 files changed
+366
-556
src/appconfig.c
+13
-25
@@ -121,11 +121,8 @@ static inline struct config *config_section_create(const char *section)
121
{
122
debug(D_CONFIG, "Creating section '%s'.", section);
123
124
- struct config *co = calloc(1, sizeof(struct config));
125
- if(!co) fatal("Cannot allocate config");
126
-
127
- co->name = strdup(section);
128
- if(!co->name) fatal("Cannot allocate config.name");
124
+ struct config *co = callocz(1, sizeof(struct config));
125
+ co->name = strdupz(section);
126
co->hash = simple_hash(co->name);
127
128
avl_init_lock(&co->values_index, config_value_compare);
@@ -152,15 +149,10 @@ static inline struct config_value *config_value_create(struct config *co, const
149
{
150
debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
151
155
- struct config_value *cv = calloc(1, sizeof(struct config_value));
156
- if(!cv) fatal("Cannot allocate config_value");
157
-
158
- cv->name = strdup(name);
159
- if(!cv->name) fatal("Cannot allocate config.name");
152
+ struct config_value *cv = callocz(1, sizeof(struct config_value));
153
+ cv->name = strdupz(name);
154
cv->hash = simple_hash(cv->name);
161
-
162
- cv->value = strdup(value);
163
- if(!cv->value) fatal("Cannot allocate config.value");
155
+ cv->value = strdupz(value);
156
157
config_value_index_add(co, cv);
158
@@ -208,9 +200,8 @@ int config_rename(const char *section, const char *old, const char *new) {
200
201
config_value_index_del(co, cv);
202
211
- free(cv->name);
212
- cv->name = strdup(new);
213
- if(!cv->name) fatal("Cannot allocate memory for config_rename()");
203
+ freez(cv->name);
204
+ cv->name = strdupz(new);
205
206
cv->hash = simple_hash(cv->name);
207
@@ -322,9 +313,8 @@ const char *config_set_default(const char *section, const char *name, const char
313
if(strcmp(cv->value, value) != 0) {
314
cv->flags |= CONFIG_VALUE_CHANGED;
315
325
- free(cv->value);
326
- cv->value = strdup(value);
327
- if(!cv->value) fatal("Cannot allocate config.value");
316
+ freez(cv->value);
317
+ cv->value = strdupz(value);
318
}
319
320
return cv->value;
@@ -346,9 +336,8 @@ const char *config_set(const char *section, const char *name, const char *value)
336
if(strcmp(cv->value, value) != 0) {
337
cv->flags |= CONFIG_VALUE_CHANGED;
338
349
- free(cv->value);
350
- cv->value = strdup(value);
351
- if(!cv->value) fatal("Cannot allocate config.value");
339
+ freez(cv->value);
340
+ cv->value = strdupz(value);
341
}
342
343
return value;
@@ -448,9 +437,8 @@ int load_config(char *filename, int overwrite_used)
437
else {
438
if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
439
debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
451
- free(cv->value);
452
- cv->value = strdup(value);
453
- if(!cv->value) fatal("Cannot allocate config.value");
440
+ freez(cv->value);
441
+ cv->value = strdupz(value);
442
}
443
else
444
debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
src/apps_plugin.c
+29
-73
@@ -183,12 +183,7 @@ struct target *get_users_target(uid_t uid)
183
for(w = users_root_target ; w ; w = w->next)
184
if(w->uid == uid) return w;
185
186
- w = calloc(sizeof(struct target), 1);
187
- if(unlikely(!w)) {
188
- error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
189
- return NULL;
190
- }
191
-
186
+ w = callocz(sizeof(struct target), 1);
187
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
188
w->comparehash = simple_hash(w->compare);
189
w->comparelen = strlen(w->compare);
@@ -221,12 +216,7 @@ struct target *get_groups_target(gid_t gid)
216
for(w = groups_root_target ; w ; w = w->next)
217
if(w->gid == gid) return w;
218
224
- w = calloc(sizeof(struct target), 1);
225
- if(unlikely(!w)) {
226
- error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
227
- return NULL;
228
- }
229
-
219
+ w = callocz(sizeof(struct target), 1);
220
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
221
w->comparehash = simple_hash(w->compare);
222
w->comparelen = strlen(w->compare);
@@ -255,8 +245,7 @@ struct target *get_groups_target(gid_t gid)
245
246
// find or create a new target
247
// there are targets that are just aggregated to other target (the second argument)
258
-struct target *get_apps_groups_target(const char *id, struct target *target)
259
-{
248
+struct target *get_apps_groups_target(const char *id, struct target *target) {
249
int tdebug = 0, thidden = 0, ends_with = 0;
250
const char *nid = id;
251
@@ -276,19 +265,14 @@ struct target *get_apps_groups_target(const char *id, struct target *target)
265
last = w;
266
}
267
279
- w = calloc(sizeof(struct target), 1);
280
- if(unlikely(!w)) {
281
- error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
282
- return NULL;
283
- }
284
-
268
+ w = callocz(sizeof(struct target), 1);
269
strncpyz(w->id, nid, MAX_NAME);
270
w->idhash = simple_hash(w->id);
271
272
strncpyz(w->name, nid, MAX_NAME);
273
274
strncpyz(w->compare, nid, MAX_COMPARE_NAME);
291
- int len = strlen(w->compare);
275
+ size_t len = strlen(w->compare);
276
if(w->compare[len - 1] == '*') {
277
w->compare[len - 1] = '\0';
278
w->starts_with = 1;
@@ -541,16 +525,9 @@ struct pid_stat *get_pid_entry(pid_t pid) {
525
return all_pids[pid];
526
}
527
544
- all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
545
- if(!all_pids[pid]) {
546
- error("Cannot allocate %zu bytes of memory", (size_t)sizeof(struct pid_stat));
547
- return NULL;
548
- }
549
-
550
- all_pids[pid]->fds = calloc(sizeof(int), 100);
551
- if(!all_pids[pid]->fds)
552
- error("Cannot allocate %zu bytes of memory", (size_t)(sizeof(int) * 100));
553
- else all_pids[pid]->fds_size = 100;
528
+ all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
529
+ all_pids[pid]->fds = callocz(sizeof(int), 100);
530
+ all_pids[pid]->fds_size = 100;
531
532
if(root_of_pids) root_of_pids->prev = all_pids[pid];
533
all_pids[pid]->next = root_of_pids;
@@ -577,12 +554,12 @@ void del_pid_entry(pid_t pid) {
554
if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
555
if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
556
580
- if(all_pids[pid]->fds) free(all_pids[pid]->fds);
581
- if(all_pids[pid]->stat_filename) free(all_pids[pid]->stat_filename);
582
- if(all_pids[pid]->statm_filename) free(all_pids[pid]->statm_filename);
583
- if(all_pids[pid]->io_filename) free(all_pids[pid]->io_filename);
584
- if(all_pids[pid]->cmdline_filename) free(all_pids[pid]->cmdline_filename);
585
- free(all_pids[pid]);
557
+ if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
558
+ if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
559
+ if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
560
+ if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
561
+ if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
562
+ freez(all_pids[pid]);
563
564
all_pids[pid] = NULL;
565
all_pids_count--;
@@ -597,14 +574,13 @@ int read_proc_pid_cmdline(struct pid_stat *p) {
574
if(unlikely(!p->cmdline_filename)) {
575
char filename[FILENAME_MAX + 1];
576
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
600
- if(!(p->cmdline_filename = strdup(filename)))
601
- fatal("Cannot allocate memory for filename '%s'", filename);
577
+ p->cmdline_filename = strdupz(filename);
578
}
579
580
int fd = open(p->cmdline_filename, O_RDONLY, 0666);
581
if(unlikely(fd == -1)) goto cleanup;
582
607
- int i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
583
+ ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
584
close(fd);
585
586
if(unlikely(bytes <= 0)) goto cleanup;
@@ -651,8 +627,7 @@ int read_proc_pid_stat(struct pid_stat *p) {
627
if(unlikely(!p->stat_filename)) {
628
char filename[FILENAME_MAX + 1];
629
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
654
- if(!(p->stat_filename = strdup(filename)))
655
- fatal("Cannot allocate memory for filename '%s'", filename);
630
+ p->stat_filename = strdupz(filename);
631
}
632
633
int set_quotes = (!ff)?1:0;
@@ -798,8 +773,7 @@ int read_proc_pid_statm(struct pid_stat *p) {
773
if(unlikely(!p->statm_filename)) {
774
char filename[FILENAME_MAX + 1];
775
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
801
- if(!(p->statm_filename = strdup(filename)))
802
- fatal("Cannot allocate memory for filename '%s'", filename);
776
+ p->statm_filename = strdupz(filename);
777
}
778
779
ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
@@ -837,8 +811,7 @@ int read_proc_pid_io(struct pid_stat *p) {
811
if(unlikely(!p->io_filename)) {
812
char filename[FILENAME_MAX + 1];
813
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
840
- if(!(p->io_filename = strdup(filename)))
841
- fatal("Cannot allocate memory for filename '%s'", filename);
814
+ p->io_filename = strdupz(filename);
815
}
816
817
// open the file
@@ -1107,7 +1080,7 @@ int file_descriptor_find_or_add(const char *name)
1080
if(unlikely(debug))
1081
fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1082
1110
- all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1083
+ all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1084
1085
// if the address changed, we have to rebuild the index
1086
// since all pointers are now invalid
@@ -1159,7 +1132,7 @@ int file_descriptor_find_or_add(const char *name)
1132
if(unlikely(debug))
1133
fprintf(stderr, "apps.plugin: >> %s fd position %d for %s (last name: %s)\n", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
1134
1162
- if(all_files[c].name) free((void *)all_files[c].name);
1135
+ if(all_files[c].name) freez((void *)all_files[c].name);
1136
all_files[c].name = NULL;
1137
last_pos = c;
1138
break;
@@ -1199,7 +1172,7 @@ int file_descriptor_find_or_add(const char *name)
1172
type = FILETYPE_OTHER;
1173
}
1174
1202
- all_files[c].name = strdup(name);
1175
+ all_files[c].name = strdupz(name);
1176
all_files[c].hash = hash;
1177
all_files[c].type = type;
1178
all_files[c].pos = c;
@@ -1242,7 +1215,7 @@ int read_pid_file_descriptors(struct pid_stat *p) {
1215
if(unlikely(debug))
1216
fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1217
1245
- p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1218
+ p->fds = reallocz(p->fds, (fdid + 100) * sizeof(int));
1219
if(!p->fds) {
1220
fatal("Cannot re-allocate fds for %s", p->comm);
1221
break;
@@ -1949,7 +1922,7 @@ long zero_all_targets(struct target *root) {
1922
for (w = root; w ; w = w->next) {
1923
count++;
1924
1952
- if(w->fds) free(w->fds);
1925
+ if(w->fds) freez(w->fds);
1926
w->fds = NULL;
1927
1928
w->minflt = 0;
@@ -1989,11 +1962,8 @@ long zero_all_targets(struct target *root) {
1962
void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1963
(void)o;
1964
1992
- if(unlikely(!w->fds)) {
1993
- w->fds = calloc(sizeof(int), (size_t) all_files_size);
1994
- if(unlikely(!w->fds))
1995
- error("Cannot allocate memory for fds in %s", w->name);
1996
- }
1965
+ if(unlikely(!w->fds))
1966
+ w->fds = callocz(sizeof(int), (size_t) all_files_size);
1967
1968
if(likely(p->updated)) {
1969
w->cutime += p->cutime;
@@ -2104,7 +2074,7 @@ void count_targets_fds(struct target *root) {
2074
}
2075
}
2076
2107
- free(w->fds);
2077
+ freez(w->fds);
2078
w->fds = NULL;
2079
}
2080
}
@@ -2822,24 +2792,10 @@ int main(int argc, char **argv)
2792
2793
parse_args(argc, argv);
2794
2825
- all_pids_sortlist = calloc(sizeof(pid_t), (size_t)pid_max);
2826
- if(!all_pids_sortlist) {
2827
- error("Cannot allocate %zu bytes of memory.", sizeof(pid_t) * pid_max);
2828
- printf("DISABLE\n");
2829
- exit(1);
2830
- }
2831
-
2832
- all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2833
- if(!all_pids) {
2834
- error("Cannot allocate %zu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2835
- printf("DISABLE\n");
2836
- exit(1);
2837
- }
2795
+ all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2796
+ all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
2797
2798
output = buffer_create(1024);
2840
- if(!output)
2841
- fatal("Cannot create BUFFER.");
2842
-
2799
buffer_sprintf(output,
2800
"CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2801
"DIMENSION user '' incremental 1 1000\n"
src/common.c
+43
-1
@@ -5,6 +5,45 @@ int enable_ksm = 1;
5
6
volatile sig_atomic_t netdata_exit = 0;
7
8
+// ----------------------------------------------------------------------------
9
+// memory allocation functions that handle failures
10
+
11
+// although netdata does not use memory allocations too often (netdata tries to
12
+// maintain its memory footprint stable during runtime, i.e. all buffers are
13
+// allocated during initialization and are adapted to current use throughout
14
+// its lifetime), these can be used to override the default system allocation
15
+// routines.
16
+
17
+char *strdupz(const char *s) {
18
+ char *t = strdup(s);
19
+ if(unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
20
+ return t;
21
+}
22
+
23
+void *mallocz(size_t size) {
24
+ void *p = malloc(size);
25
+ if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
26
+ return p;
27
+}
28
+
29
+void *callocz(size_t nmemb, size_t size) {
30
+ void *p = calloc(nmemb, size);
31
+ if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
32
+ return p;
33
+}
34
+
35
+void *reallocz(void *ptr, size_t size) {
36
+ void *p = realloc(ptr, size);
37
+ if(unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
38
+ return p;
39
+}
40
+
41
+void freez(void *ptr) {
42
+ free(ptr);
43
+}
44
+
45
+// ----------------------------------------------------------------------------
46
+
47
// time(NULL) in milliseconds
48
unsigned long long timems(void) {
49
struct timeval now;
@@ -17,7 +56,10 @@ int usecsleep(unsigned long long usec) {
56
#ifndef NETDATA_WITH_USLEEP
57
// we expect microseconds (1.000.000 per second)
58
// but timespec is nanoseconds (1.000.000.000 per second)
20
- struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
59
+ struct timespec rem, req = {
60
+ .tv_sec = (time_t)(usec / 1000000),
61
+ .tv_nsec = (suseconds_t)((usec % 1000000) * 1000)
62
+ };
63
64
while(nanosleep(&req, &rem) == -1) {
65
if(likely(errno == EINTR)) {
src/common.h
+7
@@ -123,6 +123,13 @@ extern char *strncpyz(char *dst, const char *src, size_t n);
123
extern int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
124
extern int snprintfz(char *dst, size_t n, const char *fmt, ...) __attribute__ (( format (printf, 3, 4)));
125
126
+// memory allocation functions that handle failures
127
+extern char *strdupz(const char *s);
128
+extern void *callocz(size_t nmemb, size_t size);
129
+extern void *mallocz(size_t size);
130
+extern void freez(void *ptr);
131
+extern void *reallocz(void *ptr, size_t size);
132
+
133
extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
134
extern int savememory(const char *filename, void *mem, size_t size);
135
src/daemon.c
+8
-11
@@ -46,19 +46,16 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
46
uid_t uid = pw->pw_uid;
47
gid_t gid = pw->pw_gid;
48
49
- int ngroups = sysconf(_SC_NGROUPS_MAX);
49
+ int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
50
gid_t *supplementary_groups = NULL;
51
if(ngroups) {
52
- supplementary_groups = malloc(sizeof(gid_t) * ngroups);
53
- if(supplementary_groups) {
54
- if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
55
- error("Cannot get supplementary groups of user '%s'.", username);
56
- free(supplementary_groups);
57
- supplementary_groups = NULL;
58
- ngroups = 0;
59
- }
52
+ supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
53
+ if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
54
+ error("Cannot get supplementary groups of user '%s'.", username);
55
+ freez(supplementary_groups);
56
+ supplementary_groups = NULL;
57
+ ngroups = 0;
58
}
61
- else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
59
}
60
61
properly_chown_netdata_generated_file(access_fd, uid, gid);
@@ -70,7 +67,7 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
67
if(setgroups(ngroups, supplementary_groups) == -1)
68
error("Cannot set supplementary groups for user '%s'", username);
69
73
- free(supplementary_groups);
70
+ freez(supplementary_groups);
71
supplementary_groups = NULL;
72
ngroups = 0;
73
}
src/dictionary.c
+16
-29
@@ -77,15 +77,12 @@ static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *di
77
static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
78
debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
79
80
- NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
81
- if(unlikely(!nv)) fatal("Cannot allocate name_value of size %zu", sizeof(NAME_VALUE));
80
+ NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
81
82
if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
83
nv->name = (char *)name;
84
else {
86
- nv->name = strdup(name);
87
- if (unlikely(!nv->name))
88
- fatal("Cannot allocate name_value.name of size %zu", strlen(name));
85
+ nv->name = strdupz(name);
86
}
87
88
nv->hash = (hash)?hash:simple_hash(nv->name);
@@ -93,10 +90,7 @@ static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const c
90
if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
91
nv->value = value;
92
else {
96
- nv->value = malloc(value_len);
97
- if (unlikely(!nv->value))
98
- fatal("Cannot allocate name_value.value of size %zu", value_len);
99
-
93
+ nv->value = mallocz(value_len);
94
memcpy(nv->value, value, value_len);
95
}
96
@@ -116,34 +110,30 @@ static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *n
110
111
if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
112
debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
119
- free(nv->value);
113
+ freez(nv->value);
114
}
115
116
if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
117
debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
124
- free(nv->name);
118
+ freez(nv->name);
119
}
120
127
- free(nv);
121
+ freez(nv);
122
}
123
124
// ----------------------------------------------------------------------------
125
// API - basic methods
126
133
-DICTIONARY *dictionary_create(uint32_t flags) {
127
+DICTIONARY *dictionary_create(uint8_t flags) {
128
debug(D_DICTIONARY, "Creating dictionary.");
129
136
- DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
137
- if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
130
+ DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
131
139
- if(flags & DICTIONARY_FLAG_WITH_STATISTICS) {
140
- dict->stats = calloc(1, sizeof(struct dictionary_stats));
141
- if(!dict->stats) fatal("Cannot allocate statistics for DICTIONARY");
142
- }
132
+ if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
133
+ dict->stats = callocz(1, sizeof(struct dictionary_stats));
134
135
if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
145
- dict->rwlock = calloc(1, sizeof(pthread_rwlock_t));
146
- if(!dict->rwlock) fatal("Cannot allocate pthread_rwlock_t for DICTIONARY");
136
+ dict->rwlock = callocz(1, sizeof(pthread_rwlock_t));
137
pthread_rwlock_init(dict->rwlock, NULL);
138
}
139
@@ -164,12 +154,12 @@ void dictionary_destroy(DICTIONARY *dict) {
154
dictionary_unlock(dict);
155
156
if(dict->stats)
167
- free(dict->stats);
157
+ freez(dict->stats);
158
159
if(dict->rwlock)
170
- free(dict->rwlock);
160
+ freez(dict->rwlock);
161
172
- free(dict);
162
+ freez(dict);
163
}
164
165
// ----------------------------------------------------------------------------
@@ -201,17 +191,14 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val
191
192
// copy the new value without breaking
193
// any other thread accessing the same entry
204
- void *new = malloc(value_len),
194
+ void *new = mallocz(value_len),
195
*old = nv->value;
196
207
- if(unlikely(!new))
208
- fatal("Cannot allocate value of size %zu", value_len);
209
-
197
memcpy(new, value, value_len);
198
nv->value = new;
199
200
debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
214
- free(old);
201
+ freez(old);
202
}
203
}
204
src/dictionary.h
+2
-2
@@ -33,12 +33,12 @@ typedef struct dictionary {
33
#define DICTIONARY_FLAG_NAME_LINK_DONT_CLONE 0x00000004
34
#define DICTIONARY_FLAG_WITH_STATISTICS 0x00000008
35
36
-extern DICTIONARY *dictionary_create(uint32_t flags);
36
+extern DICTIONARY *dictionary_create(uint8_t flags);
37
extern void dictionary_destroy(DICTIONARY *dict);
38
extern void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len);
39
extern void *dictionary_get(DICTIONARY *dict, const char *name);
40
extern int dictionary_del(DICTIONARY *dict, const char *name);
41
42
-extern int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data);
42
+extern int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *d), void *data);
43
44
#endif /* NETDATA_DICTIONARY_H */
src/eval.c
+8
-11
@@ -579,8 +579,7 @@ static inline unsigned char parse_operator(const char **string, int *precedence)
579
static inline EVAL_NODE *eval_node_alloc(int count) {
580
static int id = 1;
581
582
- EVAL_NODE *op = calloc(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
583
- if(!op) fatal("Cannot allocate memory for OPERAND with %d slots", count);
582
+ EVAL_NODE *op = callocz(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
583
584
op->id = id++;
585
op->operator = EVAL_OPERATOR_NOP;
@@ -606,7 +605,7 @@ static inline void eval_node_set_value_to_constant(EVAL_NODE *op, int pos, calcu
605
}
606
607
static inline void eval_variable_free(EVAL_VARIABLE *v) {
609
- free(v);
608
+ freez(v);
609
}
610
611
static inline void eval_value_free(EVAL_VALUE *v) {
@@ -631,7 +630,7 @@ static inline void eval_node_free(EVAL_NODE *op) {
630
eval_value_free(&op->ops[i]);
631
}
632
634
- free(op);
633
+ freez(op);
634
}
635
636
// ----------------------------------------------------------------------------
@@ -820,11 +819,9 @@ EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, in
819
return NULL;
820
}
821
823
- EVAL_EXPRESSION *exp = calloc(1, sizeof(EVAL_EXPRESSION));
824
- if(!exp) fatal("Cannot allocate EVAL_EXPRESSION");
822
+ EVAL_EXPRESSION *exp = callocz(1, sizeof(EVAL_EXPRESSION));
823
826
- exp->parsed_as = strdup(buffer_tostring(out));
827
- if(!exp->parsed_as) fatal("Cannot allocate memory for parsed-as string");
824
+ exp->parsed_as = strdupz(buffer_tostring(out));
825
buffer_free(out);
826
827
exp->error_msg = buffer_create(100);
@@ -837,10 +834,10 @@ void expression_free(EVAL_EXPRESSION *exp) {
834
if(!exp) return;
835
836
if(exp->nodes) eval_node_free((EVAL_NODE *)exp->nodes);
840
- free((void *)exp->source);
841
- free((void *)exp->parsed_as);
837
+ freez((void *)exp->source);
838
+ freez((void *)exp->parsed_as);
839
buffer_free(exp->error_msg);
843
- free(exp);
840
+ freez(exp);
841
}
842
843
const char *expression_strerror(int error) {
src/health.c
+75
-58
@@ -34,8 +34,7 @@ static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, u
34
}
35
36
static inline RRDVAR *rrdvar_create(const char *name, uint32_t hash, int type, calculated_number *value) {
37
- RRDVAR *rv = calloc(1, sizeof(RRDVAR));
38
- if(!rv) fatal("Cannot allocate memory for RRDVAR");
37
+ RRDVAR *rv = callocz(1, sizeof(RRDVAR));
38
39
rv->name = (char *)name;
40
rv->hash = (hash)?hash:simple_hash((rv->name));
@@ -54,7 +53,7 @@ static inline void rrdvar_free(RRDHOST *host, RRDVAR *rv) {
53
if (rf->rrdvar == rv) rf->rrdvar = NULL;
54
}
55
57
- free(rv);
56
+ freez(rv);
57
}
58
59
static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, uint32_t hash, int type, calculated_number *value) {
@@ -101,22 +100,18 @@ static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *
100
101
RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
102
debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
104
- RRDSETVAR *rs = (RRDSETVAR *)calloc(1, sizeof(RRDSETVAR));
105
- if(!rs) fatal("Cannot allocate memory for RRDSETVAR");
103
+ RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
104
105
char buffer[RRDSETVAR_ID_MAX + 1];
106
snprintfz(buffer, RRDSETVAR_ID_MAX, "%s.%s", st->id, variable);
109
- rs->fullid = strdup(buffer);
110
- if(!rs->fullid) fatal("Cannot allocate memory for RRDVASET id");
107
+ rs->fullid = strdupz(buffer);
108
rs->hash_fullid = simple_hash(rs->fullid);
109
110
snprintfz(buffer, RRDSETVAR_ID_MAX, "%s.%s", st->name, variable);
114
- rs->fullname = strdup(buffer);
115
- if(!rs->fullname) fatal("Cannot allocate memory for RRDVASET name");
111
+ rs->fullname = strdupz(buffer);
112
rs->hash_fullname = simple_hash(rs->fullname);
113
118
- rs->variable = strdup(variable);
119
- if(!rs->variable) fatal("Cannot allocate memory for RRDVASET variable name");
114
+ rs->variable = strdupz(variable);
115
rs->hash_variable = simple_hash(rs->variable);
116
117
rs->type = type;
@@ -162,9 +157,8 @@ void rrdsetvar_rename_all(RRDSET *st) {
157
rrdvar_free(st->rrdhost, rs->host_name);
158
}
159
165
- free(rs->fullname);
166
- rs->fullname = strdup(st->name);
167
- if(!rs->fullname) fatal("Cannot allocate memory for RRDSETVAR name");
160
+ freez(rs->fullname);
161
+ rs->fullname = strdupz(st->name);
162
rs->hash_fullname = simple_hash(rs->fullname);
163
rs->context_name = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->fullname, rs->hash_fullname, rs->type, rs->value);
164
rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->hash_fullname, rs->type, rs->value);
@@ -213,10 +207,10 @@ void rrdsetvar_free(RRDSETVAR *rs) {
207
else t->next = rs->next;
208
}
209
216
- free(rs->fullid);
217
- free(rs->fullname);
218
- free(rs->variable);
219
- free(rs);
210
+ freez(rs->fullid);
211
+ freez(rs->fullname);
212
+ freez(rs->variable);
213
+ freez(rs);
214
}
215
216
// ----------------------------------------------------------------------------
@@ -233,43 +227,33 @@ RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char
227
if(!suffix) suffix = "";
228
229
char buffer[RRDDIMVAR_ID_MAX + 1];
236
- RRDDIMVAR *rs = (RRDDIMVAR *)calloc(1, sizeof(RRDDIMVAR));
237
- if(!rs) fatal("Cannot allocate memory for RRDDIMVAR");
230
+ RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
231
239
- rs->prefix = strdup(prefix);
240
- if(!rs->prefix) fatal("Cannot allocate memory for RRDDIMVAR prefix");
241
-
242
- rs->suffix = strdup(suffix);
243
- if(!rs->suffix) fatal("Cannot allocate memory for RRDDIMVAR suffix");
232
+ rs->prefix = strdupz(prefix);
233
+ rs->suffix = strdupz(suffix);
234
235
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
246
- rs->id = strdup(buffer);
247
- if(!rs->id) fatal("Cannot allocate memory for RRDIM id");
236
+ rs->id = strdupz(buffer);
237
rs->hash = simple_hash(rs->id);
238
239
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
251
- rs->name = strdup(buffer);
252
- if(!rs->name) fatal("Cannot allocate memory for RRDIM name");
240
+ rs->name = strdupz(buffer);
241
rs->hash_name = simple_hash(rs->name);
242
243
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
256
- rs->fullidid = strdup(buffer);
257
- if(!rs->fullidid) fatal("Cannot allocate memory for RRDDIMVAR fullidid");
244
+ rs->fullidid = strdupz(buffer);
245
rs->hash_fullidid = simple_hash(rs->fullidid);
246
247
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
261
- rs->fullidname = strdup(buffer);
262
- if(!rs->fullidname) fatal("Cannot allocate memory for RRDDIMVAR fullidname");
248
+ rs->fullidname = strdupz(buffer);
249
rs->hash_fullidname = simple_hash(rs->fullidname);
250
251
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
266
- rs->fullnameid = strdup(buffer);
267
- if(!rs->fullnameid) fatal("Cannot allocate memory for RRDDIMVAR fullnameid");
252
+ rs->fullnameid = strdupz(buffer);
253
rs->hash_fullnameid = simple_hash(rs->fullnameid);
254
255
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
271
- rs->fullnamename = strdup(buffer);
272
- if(!rs->fullnamename) fatal("Cannot allocate memory for RRDDIMVAR fullnamename");
256
+ rs->fullnamename = strdupz(buffer);
257
rs->hash_fullnamename = simple_hash(rs->fullnamename);
258
259
rs->type = type;
@@ -313,10 +297,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
297
rrdvar_index_del(&st->variables_root_index, rs->local_name);
298
rrdvar_free(st->rrdhost, rs->local_name);
299
}
316
- free(rs->name);
300
+ freez(rs->name);
301
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
318
- rs->name = strdup(buffer);
319
- if(!rs->name) fatal("Cannot allocate memory for RRDDIMVAR name");
302
+ rs->name = strdupz(buffer);
303
rs->hash_name = simple_hash(rs->name);
304
rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->hash_name, rs->type, rs->value);
305
@@ -329,10 +312,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
312
rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullidname);
313
rrdvar_free(st->rrdhost, rs->host_fullidname);
314
}
332
- free(rs->fullidname);
315
+ freez(rs->fullidname);
316
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
334
- rs->fullidname = strdup(buffer);
335
- if(!rs->fullidname) fatal("Cannot allocate memory for RRDDIMVAR fullidname");
317
+ rs->fullidname = strdupz(buffer);
318
rs->hash_fullidname = simple_hash(rs->fullidname);
319
rs->context_fullidname = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
320
rs->fullidname, rs->hash_fullidname, rs->type, rs->value);
@@ -348,10 +330,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
330
rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullnameid);
331
rrdvar_free(st->rrdhost, rs->host_fullnameid);
332
}
351
- free(rs->fullnameid);
333
+ freez(rs->fullnameid);
334
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
353
- rs->fullnameid = strdup(buffer);
354
- if(!rs->fullnameid) fatal("Cannot allocate memory for RRDDIMVAR fullnameid");
335
+ rs->fullnameid = strdupz(buffer);
336
rs->hash_fullnameid = simple_hash(rs->fullnameid);
337
rs->context_fullnameid = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
338
rs->fullnameid, rs->hash_fullnameid, rs->type, rs->value);
@@ -367,10 +348,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
348
rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullnamename);
349
rrdvar_free(st->rrdhost, rs->host_fullnamename);
350
}
370
- free(rs->fullnamename);
351
+ freez(rs->fullnamename);
352
snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
372
- rs->fullnamename = strdup(buffer);
373
- if(!rs->fullnamename) fatal("Cannot allocate memory for RRDDIMVAR fullnamename");
353
+ rs->fullnamename = strdupz(buffer);
354
rs->hash_fullnamename = simple_hash(rs->fullnamename);
355
rs->context_fullnamename = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
356
rs->fullnamename, rs->hash_fullnamename, rs->type, rs->value);
@@ -440,15 +420,15 @@ void rrddimvar_free(RRDDIMVAR *rs) {
420
else t->next = rs->next;
421
}
422
443
- free(rs->prefix);
444
- free(rs->suffix);
445
- free(rs->id);
446
- free(rs->name);
447
- free(rs->fullidid);
448
- free(rs->fullidname);
449
- free(rs->fullnameid);
450
- free(rs->fullnamename);
451
- free(rs);
423
+ freez(rs->prefix);
424
+ freez(rs->suffix);
425
+ freez(rs->id);
426
+ freez(rs->name);
427
+ freez(rs->fullidid);
428
+ freez(rs->fullidname);
429
+ freez(rs->fullnameid);
430
+ freez(rs->fullnamename);
431
+ freez(rs);
432
}
433
434
// ----------------------------------------------------------------------------
@@ -588,3 +568,40 @@ void rrdsetcalc_unlink(RRDCALC *rc) {
568
569
rrdhostcalc_unlinked(host, rc);
570
}
571
+
572
+RRDCALC *rrdcalc_create(RRDHOST *host, const char *name, const char *chart, const char *dimensions, int group_method, uint32_t after, uint32_t before, int update_every, uint32_t options) {
573
+ uint32_t hash = simple_hash(name);
574
+
575
+ RRDCALC *rc;
576
+
577
+ // make sure it does not already exist
578
+ for(rc = host->calculations; rc ; rc = rc->next) {
579
+ if (rc->hash == hash && !strcmp(name, rc->name)) {
580
+ error("Attempted to create RRDCAL '%s' in host '%s', but it already exists. Ignoring it.", name, host->hostname);
581
+ return NULL;
582
+ }
583
+ }
584
+
585
+ rc = callocz(1, sizeof(RRDCALC));
586
+
587
+ rc->name = strdupz(name);
588
+ rc->hash = simple_hash(rc->name);
589
+
590
+ rc->chart = strdupz(chart);
591
+ rc->hash_chart = simple_hash(rc->chart);
592
+
593
+ if(dimensions) {
594
+ rc->dimensions = strdupz(dimensions);
595
+ rc->hash_chart = simple_hash(rc->chart);
596
+ }
597
+
598
+ return NULL;
599
+}
600
+
601
+void rrdcalc_free(RRDCALC *rc) {
602
+ if(!rc) return;
603
+
604
+ if(rc->rrdset) rrdsetcalc_unlink(rc);
605
+
606
+ freez(rc);
607
+}
\ No newline at end of file
src/health.h
+2
@@ -155,6 +155,8 @@ typedef struct rrdcalc {
155
time_t last_updated;
156
time_t next_update;
157
158
+ EVAL_EXPRESSION *expression;
159
+
160
calculated_number value;
161
162
RRDVAR *local;
src/main.c
+1
-3
@@ -667,9 +667,7 @@ int main(int argc, char **argv)
667
struct netdata_static_thread *st = &static_threads[i];
668
669
if(st->enabled) {
670
- st->thread = malloc(sizeof(pthread_t));
671
- if(!st->thread)
672
- fatal("Cannot allocate pthread_t memory");
670
+ st->thread = mallocz(sizeof(pthread_t));
671
672
info("Starting thread %s.", st->name);
673
src/plugin_tc.c
+24
-47
@@ -140,12 +140,11 @@ static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
140
141
tc_class_index_del(n, c);
142
143
- if(c->id) free(c->id);
144
- if(c->name) free(c->name);
145
- if(c->leafid) free(c->leafid);
146
- if(c->parentid) free(c->parentid);
147
-
148
- free(c);
143
+ freez(c->id);
144
+ freez(c->name);
145
+ freez(c->leafid);
146
+ freez(c->parentid);
147
+ freez(c);
148
}
149
150
static inline void tc_device_classes_cleanup(struct tc_device *d) {
@@ -429,45 +428,36 @@ static inline void tc_device_set_class_name(struct tc_device *d, char *id, char
428
{
429
struct tc_class *c = tc_class_index_find(d, id, 0);
430
if(likely(c)) {
432
- if(likely(c->name))
433
- free(c->name);
434
-
431
+ freez(c->name);
432
c->name = NULL;
433
434
if(likely(name && *name && strcmp(c->id, name) != 0)) {
435
debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
439
- c->name = strdup(name);
440
- if(likely(c->name))
441
- c->name_updated = 1;
436
+ c->name = strdupz(name);
437
+ c->name_updated = 1;
438
}
439
}
440
}
441
442
static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
447
- if(likely(d->name))
448
- free(d->name);
449
-
443
+ freez(d->name);
444
d->name = NULL;
445
446
if(likely(name && *name && strcmp(d->id, name) != 0)) {
447
debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
454
- d->name = strdup(name);
455
- if(likely(d->name))
456
- d->name_updated = 1;
448
+ d->name = strdupz(name);
449
+ d->name_updated = 1;
450
}
451
}
452
453
static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
461
- if(likely(d->family))
462
- free(d->family);
463
-
454
+ freez(d->family);
455
d->family = NULL;
456
457
if(likely(family && *family && strcmp(d->id, family) != 0)) {
458
debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
468
- d->family = strdup(family);
469
- if(likely(d->family))
470
- d->family_updated = 1;
459
+ d->family = strdupz(family);
460
+ d->family_updated = 1;
461
}
462
// no need for null termination - it is already null
463
}
@@ -479,13 +469,9 @@ static inline struct tc_device *tc_device_create(char *id)
469
if(!d) {
470
debug(D_TC_LOOP, "TC: Creating device '%s'", id);
471
482
- d = calloc(1, sizeof(struct tc_device));
483
- if(!d) {
484
- fatal("Cannot allocate memory for tc_device %s", id);
485
- return NULL;
486
- }
472
+ d = callocz(1, sizeof(struct tc_device));
473
488
- d->id = strdup(id);
474
+ d->id = strdupz(id);
475
d->hash = simple_hash(d->id);
476
d->enabled = -1;
477
@@ -512,30 +498,22 @@ static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char
498
if(!c) {
499
debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
500
515
- c = calloc(1, sizeof(struct tc_class));
516
- if(!c) {
517
- fatal("Cannot allocate memory for tc class");
518
- return NULL;
519
- }
501
+ c = callocz(1, sizeof(struct tc_class));
502
503
if(n->classes) n->classes->prev = c;
504
c->next = n->classes;
505
n->classes = c;
506
525
- c->id = strdup(id);
526
- if(!c->id) {
527
- free(c);
528
- return NULL;
529
- }
507
+ c->id = strdupz(id);
508
c->hash = simple_hash(c->id);
509
510
if(parentid && *parentid) {
533
- c->parentid = strdup(parentid);
511
+ c->parentid = strdupz(parentid);
512
c->parent_hash = simple_hash(c->parentid);
513
}
514
515
if(leafid && *leafid) {
538
- c->leafid = strdup(leafid);
516
+ c->leafid = strdupz(leafid);
517
c->leaf_hash = simple_hash(c->leafid);
518
}
519
@@ -560,11 +538,10 @@ static inline void tc_device_free(struct tc_device *n)
538
539
while(n->classes) tc_class_free(n, n->classes);
540
563
- if(n->id) free(n->id);
564
- if(n->name) free(n->name);
565
- if(n->family) free(n->family);
566
-
567
- free(n);
541
+ freez(n->id);
542
+ freez(n->name);
543
+ freez(n->family);
544
+ freez(n);
545
}
546
547
static inline void tc_device_free_all()
src/plugins_d.c
+1
-2
@@ -508,8 +508,7 @@ void *pluginsd_main(void *ptr)
508
// it is not running
509
// allocate a new one, or use the obsolete one
510
if(unlikely(!cd)) {
511
- cd = calloc(sizeof(struct plugind), 1);
512
- if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
511
+ cd = callocz(sizeof(struct plugind), 1);
512
513
snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
514
src/proc_diskstats.c
+3
-4
@@ -51,10 +51,9 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
51
52
// not found
53
// create a new disk structure
54
- d = (struct disk *)malloc(sizeof(struct disk));
55
- if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats.");
54
+ d = (struct disk *)mallocz(sizeof(struct disk));
55
57
- d->disk = strdup(disk);
56
+ d->disk = strdupz(disk);
57
d->major = major;
58
d->minor = minor;
59
d->type = DISK_TYPE_PHYSICAL; // Default type. Changed later if not correct.
@@ -127,7 +126,7 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
126
}
127
128
if(mi)
130
- d->mount_point = strdup(mi->mount_point);
129
+ d->mount_point = strdupz(mi->mount_point);
130
// no need to check for NULL
131
else
132
d->mount_point = NULL;
src/proc_interrupts.c
+1
-4
@@ -23,10 +23,7 @@ static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
23
24
if(lines < allocated) return irrs;
25
else {
26
- irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
27
- if(!irrs)
28
- fatal("Cannot allocate memory for %d interrupts", lines);
29
-
26
+ irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
27
allocated = lines;
28
}
29
src/proc_loadavg.c
+4
-1
@@ -1,5 +1,8 @@
1
#include "common.h"
2
3
+// linux calculates this once every 5 seconds
4
+#define MIN_LOADAVG_UPDATE_EVERY 5
5
+
6
int do_proc_loadavg(int update_every, unsigned long long dt) {
7
static procfile *ff = NULL;
8
static int do_loadavg = -1, do_all_processes = -1;
@@ -44,7 +47,7 @@ int do_proc_loadavg(int update_every, unsigned long long dt) {
47
if(do_loadavg) {
48
st = rrdset_find_byname("system.load");
49
if(!st) {
47
- st = rrdset_create("system", "load", NULL, "load", NULL, "System Load Average", "load", 100, update_every, RRDSET_TYPE_LINE);
50
+ st = rrdset_create("system", "load", NULL, "load", NULL, "System Load Average", "load", 100, (update_every < MIN_LOADAVG_UPDATE_EVERY)?MIN_LOADAVG_UPDATE_EVERY:update_every, RRDSET_TYPE_LINE);
51
52
rrddim_add(st, "load1", NULL, 1, 1000, RRDDIM_ABSOLUTE);
53
rrddim_add(st, "load5", NULL, 1, 1000, RRDDIM_ABSOLUTE);
src/proc_self_mountinfo.c
+16
-24
@@ -69,9 +69,9 @@ void mountinfo_free(struct mountinfo *mi) {
69
if(likely(mi->next))
70
mountinfo_free(mi->next);
71
72
- if(mi->root) free(mi->root);
73
- if(mi->mount_point) free(mi->mount_point);
74
- if(mi->mount_options) free(mi->mount_options);
72
+ freez(mi->root);
73
+ freez(mi->mount_point);
74
+ freez(mi->mount_options);
75
76
/*
77
if(mi->optional_fields_count) {
@@ -81,15 +81,14 @@ void mountinfo_free(struct mountinfo *mi) {
81
}
82
free(mi->optional_fields);
83
*/
84
- free(mi->filesystem);
85
- free(mi->mount_source);
86
- free(mi->super_options);
87
- free(mi);
84
+ freez(mi->filesystem);
85
+ freez(mi->mount_source);
86
+ freez(mi->super_options);
87
+ freez(mi);
88
}
89
90
-static char *strdup_decoding_octal(const char *string) {
91
- char *buffer = strdup(string);
92
- if(!buffer) fatal("Cannot allocate memory.");
90
+static char *strdupz_decoding_octal(const char *string) {
91
+ char *buffer = strdupz(string);
92
93
char *d = buffer;
94
const char *s = string;
@@ -137,8 +136,7 @@ struct mountinfo *mountinfo_read() {
136
if(procfile_linewords(ff, l) < 5)
137
continue;
138
140
- mi = malloc(sizeof(struct mountinfo));
141
- if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
139
+ mi = mallocz(sizeof(struct mountinfo));
140
141
if(unlikely(!root))
142
root = last = mi;
@@ -160,16 +158,13 @@ struct mountinfo *mountinfo_read() {
158
mi->major = strtoul(major, NULL, 10);
159
mi->minor = strtoul(minor, NULL, 10);
160
163
- mi->root = strdup(procfile_lineword(ff, l, w)); w++;
164
- if(unlikely(!mi->root)) fatal("Cannot allocate memory");
161
+ mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
162
mi->root_hash = simple_hash(mi->root);
163
167
- mi->mount_point = strdup_decoding_octal(procfile_lineword(ff, l, w)); w++;
168
- if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
164
+ mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
165
mi->mount_point_hash = simple_hash(mi->mount_point);
166
171
- mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
172
- if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
167
+ mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
168
169
// count the optional fields
170
/*
@@ -206,16 +201,13 @@ struct mountinfo *mountinfo_read() {
201
if(likely(*s == '-')) {
202
w++;
203
209
- mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
210
- if(!mi->filesystem) fatal("Cannot allocate memory");
204
+ mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
205
mi->filesystem_hash = simple_hash(mi->filesystem);
206
213
- mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
214
- if(!mi->mount_source) fatal("Cannot allocate memory");
207
+ mi->mount_source = strdupz(procfile_lineword(ff, l, w)); w++;
208
mi->mount_source_hash = simple_hash(mi->mount_source);
209
217
- mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
218
- if(!mi->super_options) fatal("Cannot allocate memory");
210
+ mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
211
}
212
else {
213
mi->filesystem = NULL;
src/proc_softirqs.c
+1
-4
@@ -23,10 +23,7 @@ static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
23
24
if(lines < allocated) return irrs;
25
else {
26
- irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
27
- if(!irrs)
28
- fatal("Cannot allocate memory for %d interrupts", lines);
29
-
26
+ irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
27
allocated = lines;
28
}
29
src/procfile.c
+9
-37
@@ -24,13 +24,7 @@ pfwords *pfwords_add(pfwords *fw, char *str) {
24
if(unlikely(fw->len == fw->size)) {
25
// debug(D_PROCFILE, PF_PREFIX ": expanding words");
26
27
- pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
28
- if(unlikely(!new)) {
29
- error(PF_PREFIX ": failed to expand words");
30
- free(fw);
31
- return NULL;
32
- }
33
- fw = new;
27
+ fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
28
fw->size += PFWORDS_INCREASE_STEP;
29
}
30
@@ -44,9 +38,7 @@ pfwords *pfwords_new(void) {
38
39
uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
40
47
- pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
48
- if(unlikely(!new)) return NULL;
49
-
41
+ pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
42
new->len = 0;
43
new->size = size;
44
return new;
@@ -60,7 +52,7 @@ void pfwords_reset(pfwords *fw) {
52
void pfwords_free(pfwords *fw) {
53
// debug(D_PROCFILE, PF_PREFIX ": freeing words");
54
63
- free(fw);
55
+ freez(fw);
56
}
57
58
@@ -73,13 +65,7 @@ pflines *pflines_add(pflines *fl, uint32_t first_word) {
65
if(unlikely(fl->len == fl->size)) {
66
// debug(D_PROCFILE, PF_PREFIX ": expanding lines");
67
76
- pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
77
- if(unlikely(!new)) {
78
- error(PF_PREFIX ": failed to expand lines");
79
- free(fl);
80
- return NULL;
81
- }
82
- fl = new;
68
+ fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
69
fl->size += PFLINES_INCREASE_STEP;
70
}
71
@@ -94,9 +80,7 @@ pflines *pflines_new(void) {
80
81
uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
82
97
- pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
98
- if(unlikely(!new)) return NULL;
99
-
83
+ pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
84
new->len = 0;
85
new->size = size;
86
return new;
@@ -111,7 +95,7 @@ void pflines_reset(pflines *fl) {
95
void pflines_free(pflines *fl) {
96
// debug(D_PROCFILE, PF_PREFIX ": freeing lines");
97
114
- free(fl);
98
+ freez(fl);
99
}
100
101
@@ -132,7 +116,7 @@ void procfile_close(procfile *ff) {
116
if(likely(ff->words)) pfwords_free(ff->words);
117
118
if(likely(ff->fd != -1)) close(ff->fd);
135
- free(ff);
119
+ freez(ff);
120
}
121
122
procfile *procfile_parser(procfile *ff) {
@@ -293,13 +277,7 @@ procfile *procfile_readall(procfile *ff) {
277
if(!x) {
278
debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
279
296
- procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
297
- if(unlikely(!new)) {
298
- error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
299
- procfile_close(ff);
300
- return NULL;
301
- }
302
- ff = new;
280
+ ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
281
ff->size += PROCFILE_INCREMENT_BUFFER;
282
}
283
@@ -416,13 +394,7 @@ procfile *procfile_open(const char *filename, const char *separators, uint32_t f
394
}
395
396
size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
419
- procfile *ff = malloc(sizeof(procfile) + size);
420
- if(unlikely(!ff)) {
421
- error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
422
- close(fd);
423
- return NULL;
424
- }
425
-
397
+ procfile *ff = mallocz(sizeof(procfile) + size);
398
strncpyz(ff->filename, filename, FILENAME_MAX);
399
400
ff->fd = fd;
src/registry.c
+16
-22
@@ -312,14 +312,12 @@ static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen)
312
urllen = registry.max_url_length;
313
314
debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
315
- URL *u = malloc(sizeof(URL) + urllen);
316
- if(!u) fatal("Cannot allocate %zu bytes for URL '%s'", sizeof(URL) + urllen, url);
315
+ URL *u = mallocz(sizeof(URL) + urllen);
316
317
// a simple strcpy() should do the job
318
// but I prefer to be safe, since the caller specified urllen
320
- strncpyz(u->url, url, urllen);
321
-
322
- u->len = urllen;
319
+ u->len = (uint16_t)urllen;
320
+ strncpyz(u->url, url, u->len);
321
u->links = 0;
322
323
registry.urls_memory += sizeof(URL) + urllen;
@@ -356,7 +354,7 @@ static inline void registry_url_unlink_nolock(URL *u) {
354
if(!u->links) {
355
debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
356
dictionary_del(registry.urls, u->url);
359
- free(u);
357
+ freez(u);
358
}
359
else
360
debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): URL has %u links left", u->url, u->links);
@@ -374,13 +372,12 @@ static inline MACHINE *registry_machine_find(const char *machine_guid) {
372
static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
373
debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
374
377
- MACHINE_URL *mu = malloc(sizeof(MACHINE_URL));
378
- if(!mu) fatal("registry_machine_link_to_url('%s', '%s'): cannot allocate %zu bytes.", m->guid, u->url, sizeof(MACHINE_URL));
375
+ MACHINE_URL *mu = mallocz(sizeof(MACHINE_URL));
376
377
// mu->persons = dictionary_create(DICTIONARY_FLAGS);
378
// dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
379
383
- mu->first_t = mu->last_t = when;
380
+ mu->first_t = mu->last_t = (uint32_t)when;
381
mu->usages = 1;
382
mu->url = u;
383
mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
@@ -397,15 +394,14 @@ static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, tim
394
static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
395
debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
396
400
- MACHINE *m = malloc(sizeof(MACHINE));
401
- if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
397
+ MACHINE *m = mallocz(sizeof(MACHINE));
398
399
strncpyz(m->guid, machine_guid, 36);
400
401
debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
402
m->urls = dictionary_create(DICTIONARY_FLAGS);
403
408
- m->first_t = m->last_t = when;
404
+ m->first_t = m->last_t = (uint32_t)when;
405
m->usages = 0;
406
407
registry.machines_memory += sizeof(MACHINE);
@@ -458,8 +454,7 @@ static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, UR
454
debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
455
sizeof(PERSON_URL) + namelen);
456
461
- PERSON_URL *pu = malloc(sizeof(PERSON_URL) + namelen);
462
- if(!pu) fatal("registry_person_url_allocate('%s', '%s', '%s'): cannot allocate %zu bytes.", p->guid, m->guid, u->url, sizeof(PERSON_URL) + namelen);
457
+ PERSON_URL *pu = mallocz(sizeof(PERSON_URL) + namelen);
458
459
// a simple strcpy() should do the job
460
// but I prefer to be safe, since the caller specified urllen
@@ -498,7 +493,7 @@ static inline PERSON_URL *registry_person_url_reallocate(PERSON *p, MACHINE *m,
493
registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
494
registry_url_unlink_nolock(u);
495
501
- free(pu);
496
+ freez(pu);
497
498
return tpu;
499
}
@@ -508,8 +503,7 @@ static inline PERSON *registry_person_allocate(const char *person_guid, time_t w
503
504
debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
505
511
- p = malloc(sizeof(PERSON));
512
- if(!p) fatal("Registry: cannot allocate memory for new person.");
506
+ p = mallocz(sizeof(PERSON));
507
508
if(!person_guid) {
509
for (; ;) {
@@ -914,7 +908,7 @@ PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url
908
909
dictionary_del(p->urls, dpu->url->url);
910
registry_url_unlink_nolock(dpu->url);
917
- free(dpu);
911
+ freez(dpu);
912
913
registry_person_urls_unlock(p);
914
return p;
@@ -1720,7 +1714,7 @@ void registry_free(void) {
1714
registry_url_unlink_nolock(pu->url);
1715
1716
debug(D_REGISTRY, "Registry: freeing person url");
1723
- free(pu);
1717
+ freez(pu);
1718
}
1719
1720
debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
@@ -1730,7 +1724,7 @@ void registry_free(void) {
1724
dictionary_destroy(p->urls);
1725
1726
debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1733
- free(p);
1727
+ freez(p);
1728
}
1729
1730
while(registry.machines->values_index.root) {
@@ -1753,7 +1747,7 @@ void registry_free(void) {
1747
registry_url_unlink_nolock(mu->url);
1748
1749
debug(D_REGISTRY, "Registry: freeing machine url");
1756
- free(mu);
1750
+ freez(mu);
1751
}
1752
1753
debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
@@ -1763,7 +1757,7 @@ void registry_free(void) {
1757
dictionary_destroy(m->urls);
1758
1759
debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1766
- free(m);
1760
+ freez(m);
1761
}
1762
1763
// and free the memory of remaining dictionary structures
src/rrd.c
+8
-20
@@ -63,12 +63,9 @@ static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t
63
RRDCONTEXT *rrdcontext_create(const char *id) {
64
RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
65
if(!rc) {
66
- rc = calloc(1, sizeof(RRDCONTEXT));
67
- if(!rc) fatal("Cannot allocate RRDCONTEXT memory");
68
-
69
- rc->id = strdup(id);
70
- if(!rc->id) fatal("Cannot allocate RRDCONTEXT.id memory");
66
+ rc = callocz(1, sizeof(RRDCONTEXT));
67
68
+ rc->id = strdupz(id);
69
rc->hash = simple_hash(rc->id);
70
71
// initialize the variables index
@@ -93,8 +90,8 @@ void rrdcontext_free(RRDCONTEXT *rc) {
90
if(rc->variables_root_index.avl_tree.root != NULL)
91
fatal("INTERNAL ERROR: Variables index of RRDCONTEXT '%s' that is freed, is not empty.", rc->id);
92
96
- free((void *)rc->id);
97
- free(rc);
93
+ freez((void *)rc->id);
94
+ freez(rc);
95
}
96
}
97
@@ -465,11 +462,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
462
st->variables = NULL;
463
}
464
else {
468
- st = calloc(1, size);
469
- if(!st) {
470
- fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
471
- return NULL;
472
- }
465
+ st = callocz(1, size);
466
st->mapped = RRD_MEMORY_MODE_RAM;
467
}
468
st->memsize = size;
@@ -611,12 +604,7 @@ RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier
604
else {
605
// if we didn't manage to get a mmap'd dimension, just create one
606
614
- rd = calloc(1, size);
615
- if(!rd) {
616
- fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
617
- return NULL;
618
- }
619
-
607
+ rd = callocz(1, size);
608
rd->mapped = RRD_MEMORY_MODE_RAM;
609
}
610
rd->memsize = size;
@@ -723,7 +711,7 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
711
}
712
else {
713
debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
726
- free(rd);
714
+ freez(rd);
715
}
716
}
717
@@ -768,7 +756,7 @@ void rrdset_free_all(void)
756
munmap(st, st->memsize);
757
}
758
else
771
- free(st);
759
+ freez(st);
760
761
st = next;
762
}
src/rrd2json.c
+10
-25
@@ -1150,11 +1150,11 @@ inline static void rrdr_free(RRDR *r)
1150
}
1151
1152
rrdr_unlock_rrdset(r);
1153
- if(likely(r->t)) free(r->t);
1154
- if(likely(r->v)) free(r->v);
1155
- if(likely(r->o)) free(r->o);
1156
- if(likely(r->od)) free(r->od);
1157
- free(r);
1153
+ freez(r->t);
1154
+ freez(r->v);
1155
+ freez(r->o);
1156
+ freez(r->od);
1157
+ freez(r);
1158
}
1159
1160
inline void rrdr_done(RRDR *r)
@@ -1170,9 +1170,7 @@ static RRDR *rrdr_create(RRDSET *st, long n)
1170
return NULL;
1171
}
1172
1173
- RRDR *r = calloc(1, sizeof(RRDR));
1174
- if(unlikely(!r)) goto cleanup;
1175
-
1173
+ RRDR *r = callocz(1, sizeof(RRDR));
1174
r->st = st;
1175
1176
rrdr_lock_rrdset(r);
@@ -1182,17 +1180,10 @@ static RRDR *rrdr_create(RRDSET *st, long n)
1180
1181
r->n = n;
1182
1185
- r->t = malloc(n * sizeof(time_t));
1186
- if(unlikely(!r->t)) goto cleanup;
1187
-
1188
- r->v = malloc(n * r->d * sizeof(calculated_number));
1189
- if(unlikely(!r->v)) goto cleanup;
1190
-
1191
- r->o = malloc(n * r->d * sizeof(uint8_t));
1192
- if(unlikely(!r->o)) goto cleanup;
1193
-
1194
- r->od = malloc(r->d * sizeof(uint8_t));
1195
- if(unlikely(!r->od)) goto cleanup;
1183
+ r->t = mallocz(n * sizeof(time_t));
1184
+ r->v = mallocz(n * r->d * sizeof(calculated_number));
1185
+ r->o = mallocz(n * r->d * sizeof(uint8_t));
1186
+ r->od = mallocz(r->d * sizeof(uint8_t));
1187
1188
// set the hidden flag on hidden dimensions
1189
int c;
@@ -1202,16 +1193,10 @@ static RRDR *rrdr_create(RRDSET *st, long n)
1193
}
1194
1195
r->c = -1;
1205
-
1196
r->group = 1;
1197
r->update_every = 1;
1198
1199
return r;
1210
-
1211
-cleanup:
1212
- error("Cannot allocate RRDR memory for %ld entries", n);
1213
- if(likely(r)) rrdr_free(r);
1214
- return NULL;
1200
}
1201
1202
RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
src/sys_fs_cgroup.c
+40
-58
@@ -261,13 +261,9 @@ void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
261
}
262
263
if(i != ca->cpus) {
264
- free(ca->cpu_percpu);
265
-
266
- ca->cpu_percpu = malloc(sizeof(unsigned long long) * i);
267
- if(!ca->cpu_percpu)
268
- fatal("Cannot allocate memory (%zu bytes)", sizeof(unsigned long long) * i);
269
-
270
- ca->cpus = i;
264
+ freez(ca->cpu_percpu);
265
+ ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
266
+ ca->cpus = (unsigned int)i;
267
}
268
269
for(i = 0; i < ca->cpus ;i++) {
@@ -612,17 +608,12 @@ void cgroup_get_chart_id(struct cgroup *cg) {
608
609
trim(s);
610
615
- free(cg->chart_title);
616
- cg->chart_title = strdup(s);
617
- if(!cg->chart_title)
618
- fatal("CGROUP: Cannot allocate memory for chart name of cgroup '%s' chart name: '%s'", cg->id, s);
619
-
611
+ freez(cg->chart_title);
612
+ cg->chart_title = strdupz(s);
613
netdata_fix_chart_name(cg->chart_title);
614
622
- free(cg->chart_id);
623
- cg->chart_id = strdup(s);
624
- if(!cg->chart_id)
625
- fatal("CGROUP: Cannot allocate memory for chart id of cgroup '%s' chart id: '%s'", cg->id, s);
615
+ freez(cg->chart_id);
616
+ cg->chart_id = strdupz(s);
617
618
netdata_fix_chart_id(cg->chart_id);
619
@@ -683,21 +674,15 @@ struct cgroup *cgroup_add(const char *id) {
674
}
675
}
676
686
- struct cgroup *cg = calloc(1, sizeof(struct cgroup));
687
- if(!cg) fatal("Cannot allocate memory for cgroup '%s'", id);
677
+ struct cgroup *cg = callocz(1, sizeof(struct cgroup));
678
679
debug(D_CGROUP, "adding cgroup '%s'", id);
680
691
- cg->id = strdup(id);
692
- if(!cg->id) fatal("Cannot allocate memory for cgroup '%s'", id);
693
-
681
+ cg->id = strdupz(id);
682
cg->hash = simple_hash(cg->id);
683
696
- cg->chart_id = strdup(chart_id);
697
- if(!cg->chart_id) fatal("Cannot allocate memory for cgroup '%s'", id);
698
-
699
- cg->chart_title = strdup(chart_id);
700
- if(!cg->chart_title) fatal("Cannot allocate memory for cgroup '%s'", id);
684
+ cg->chart_id = strdupz(chart_id);
685
+ cg->chart_title = strdupz(chart_id);
686
687
if(!cgroup_root)
688
cgroup_root = cg;
@@ -725,22 +710,22 @@ struct cgroup *cgroup_add(const char *id) {
710
void cgroup_free(struct cgroup *cg) {
711
debug(D_CGROUP, "Removing cgroup '%s' with chart id '%s' (was %s and %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (cg->available)?"available":"not available");
712
728
- free(cg->cpuacct_usage.cpu_percpu);
713
+ freez(cg->cpuacct_usage.cpu_percpu);
714
730
- free(cg->cpuacct_stat.filename);
731
- free(cg->cpuacct_usage.filename);
732
- free(cg->memory.filename);
733
- free(cg->io_service_bytes.filename);
734
- free(cg->io_serviced.filename);
735
- free(cg->throttle_io_service_bytes.filename);
736
- free(cg->throttle_io_serviced.filename);
737
- free(cg->io_merged.filename);
738
- free(cg->io_queued.filename);
715
+ freez(cg->cpuacct_stat.filename);
716
+ freez(cg->cpuacct_usage.filename);
717
+ freez(cg->memory.filename);
718
+ freez(cg->io_service_bytes.filename);
719
+ freez(cg->io_serviced.filename);
720
+ freez(cg->throttle_io_service_bytes.filename);
721
+ freez(cg->throttle_io_serviced.filename);
722
+ freez(cg->io_merged.filename);
723
+ freez(cg->io_queued.filename);
724
740
- free(cg->id);
741
- free(cg->chart_id);
742
- free(cg->chart_title);
743
- free(cg);
725
+ freez(cg->id);
726
+ freez(cg->chart_id);
727
+ freez(cg->chart_title);
728
+ freez(cg);
729
730
cgroup_root_count--;
731
}
@@ -833,15 +818,12 @@ void find_dir_in_subdirs(const char *base, const char *this, void (*callback)(co
818
}
819
820
if(enabled) {
836
- char *s = malloc(dirlen + strlen(de->d_name) + 2);
837
- if(s) {
838
- strcpy(s, this);
839
- strcat(s, "/");
840
- strcat(s, de->d_name);
841
- find_dir_in_subdirs(base, s, callback);
842
- free(s);
843
- }
844
- else error("Cannot allocate memory.");
821
+ char *s = mallocz(dirlen + strlen(de->d_name) + 2);
822
+ strcpy(s, this);
823
+ strcat(s, "/");
824
+ strcat(s, de->d_name);
825
+ find_dir_in_subdirs(base, s, callback);
826
+ freez(s);
827
}
828
}
829
}
@@ -920,7 +902,7 @@ void find_all_cgroups() {
902
if(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename) {
903
snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
904
if(stat(filename, &buf) != -1) {
923
- cg->cpuacct_stat.filename = strdup(filename);
905
+ cg->cpuacct_stat.filename = strdupz(filename);
906
debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
907
}
908
else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -928,7 +910,7 @@ void find_all_cgroups() {
910
if(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename) {
911
snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
912
if(stat(filename, &buf) != -1) {
931
- cg->cpuacct_usage.filename = strdup(filename);
913
+ cg->cpuacct_usage.filename = strdupz(filename);
914
debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
915
}
916
else debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -936,7 +918,7 @@ void find_all_cgroups() {
918
if(cgroup_enable_memory && !cg->memory.filename) {
919
snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
920
if(stat(filename, &buf) != -1) {
939
- cg->memory.filename = strdup(filename);
921
+ cg->memory.filename = strdupz(filename);
922
debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
923
}
924
else debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -945,7 +927,7 @@ void find_all_cgroups() {
927
if(!cg->io_service_bytes.filename) {
928
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
929
if(stat(filename, &buf) != -1) {
948
- cg->io_service_bytes.filename = strdup(filename);
930
+ cg->io_service_bytes.filename = strdupz(filename);
931
debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
932
}
933
else debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -953,7 +935,7 @@ void find_all_cgroups() {
935
if(!cg->io_serviced.filename) {
936
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
937
if(stat(filename, &buf) != -1) {
956
- cg->io_serviced.filename = strdup(filename);
938
+ cg->io_serviced.filename = strdupz(filename);
939
debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
940
}
941
else debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -961,7 +943,7 @@ void find_all_cgroups() {
943
if(!cg->throttle_io_service_bytes.filename) {
944
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
945
if(stat(filename, &buf) != -1) {
964
- cg->throttle_io_service_bytes.filename = strdup(filename);
946
+ cg->throttle_io_service_bytes.filename = strdupz(filename);
947
debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
948
}
949
else debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -969,7 +951,7 @@ void find_all_cgroups() {
951
if(!cg->throttle_io_serviced.filename) {
952
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
953
if(stat(filename, &buf) != -1) {
972
- cg->throttle_io_serviced.filename = strdup(filename);
954
+ cg->throttle_io_serviced.filename = strdupz(filename);
955
debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
956
}
957
else debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -977,7 +959,7 @@ void find_all_cgroups() {
959
if(!cg->io_merged.filename) {
960
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
961
if(stat(filename, &buf) != -1) {
980
- cg->io_merged.filename = strdup(filename);
962
+ cg->io_merged.filename = strdupz(filename);
963
debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
964
}
965
else debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
@@ -985,7 +967,7 @@ void find_all_cgroups() {
967
if(!cg->io_queued.filename) {
968
snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
969
if(stat(filename, &buf) != -1) {
988
- cg->io_queued.filename = strdup(filename);
970
+ cg->io_queued.filename = strdupz(filename);
971
debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
972
}
973
else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
src/url.c
+5
-18
@@ -20,10 +20,7 @@ char to_hex(char code) {
20
char *url_encode(char *str) {
21
char *buf, *pbuf;
22
23
- pbuf = buf = malloc(strlen(str) * 3 + 1);
24
-
25
- if(!buf)
26
- fatal("Cannot allocate memory.");
23
+ pbuf = buf = mallocz(strlen(str) * 3 + 1);
24
25
while (*str) {
26
if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
@@ -39,16 +36,9 @@ char *url_encode(char *str) {
36
}
37
*pbuf = '\0';
38
42
- // FIX: I think this is prudent. URLs can be as long as 2 KiB or more.
43
- // We allocated 3 times more space to accomodate %NN encoding of
44
- // non ASCII chars. If URL has none of these kind of chars we will
45
- // end up with a big unused buffer.
46
- //
47
- // Try to shrink the buffer...
48
- if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1)))
49
- buf = pbuf;
50
-
51
- return buf;
39
+ pbuf = strdupz(buf);
40
+ freez(buf);
41
+ return pbuf;
42
}
43
44
/* Returns a url-decoded version of str */
@@ -56,10 +46,7 @@ char *url_encode(char *str) {
46
char *url_decode(char *str) {
47
size_t size = strlen(str) + 1;
48
59
- char *buf = malloc(size);
60
- if(!buf)
61
- fatal("Cannot allocate %zu bytes of memory.", size);
62
-
49
+ char *buf = mallocz(size);
50
return url_decode_r(buf, str, size);
51
}
52
src/web_buffer.c
+5
-20
@@ -323,18 +323,8 @@ BUFFER *buffer_create(size_t size)
323
324
debug(D_WEB_BUFFER, "Creating new web buffer of size %zu.", size);
325
326
- b = calloc(1, sizeof(BUFFER));
327
- if(!b) {
328
- error("Cannot allocate a web_buffer.");
329
- return NULL;
330
- }
331
-
332
- b->buffer = malloc(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
333
- if(!b->buffer) {
334
- error("Cannot allocate a buffer of size %zu.", size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
335
- free(b);
336
- return NULL;
337
- }
326
+ b = callocz(1, sizeof(BUFFER));
327
+ b->buffer = mallocz(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
328
b->buffer[0] = '\0';
329
b->size = size;
330
b->contenttype = CT_TEXT_PLAIN;
@@ -350,8 +340,8 @@ void buffer_free(BUFFER *b)
340
341
debug(D_WEB_BUFFER, "Freeing web buffer of size %zu.", b->size);
342
353
- free(b->buffer);
354
- free(b);
343
+ freez(b->buffer);
344
+ freez(b);
345
}
346
347
void buffer_increase(BUFFER *b, size_t free_size_required)
@@ -367,12 +357,7 @@ void buffer_increase(BUFFER *b, size_t free_size_required)
357
358
debug(D_WEB_BUFFER, "Increasing data buffer from size %zu to %zu.", b->size, b->size + increase);
359
370
- b->buffer = realloc(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
371
- if(!b->buffer)
372
- fatal("Failed to increase data buffer from size %zu to %zu.",
373
- b->size + sizeof(BUFFER_OVERFLOW_EOF) + 2,
374
- b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
375
-
360
+ b->buffer = reallocz(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
361
b->size += increase;
362
363
buffer_overflow_init(b);
src/web_client.c
+7
-40
@@ -48,12 +48,7 @@ struct web_client *web_client_create(int listener)
48
{
49
struct web_client *w;
50
51
- w = calloc(1, sizeof(struct web_client));
52
- if(!w) {
53
- error("Cannot allocate new web_client memory.");
54
- return NULL;
55
- }
56
-
51
+ w = callocz(1, sizeof(struct web_client));
52
w->id = ++web_clients_count;
53
w->mode = WEB_CLIENT_MODE_NORMAL;
54
@@ -67,7 +62,7 @@ struct web_client *web_client_create(int listener)
62
w->ifd = accept4(listener, sadr, &addrlen, SOCK_NONBLOCK);
63
if (w->ifd == -1) {
64
error("%llu: Cannot accept new incoming connection.", w->id);
70
- free(w);
65
+ freez(w);
66
return NULL;
67
}
68
w->ofd = w->ifd;
@@ -109,32 +104,8 @@ struct web_client *web_client_create(int listener)
104
}
105
106
w->response.data = buffer_create(INITIAL_WEB_DATA_LENGTH);
112
- if(unlikely(!w->response.data)) {
113
- // no need for error log - web_buffer_create already logged the error
114
- close(w->ifd);
115
- free(w);
116
- return NULL;
117
- }
118
-
107
w->response.header = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
120
- if(unlikely(!w->response.header)) {
121
- // no need for error log - web_buffer_create already logged the error
122
- buffer_free(w->response.data);
123
- close(w->ifd);
124
- free(w);
125
- return NULL;
126
- }
127
-
108
w->response.header_output = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
129
- if(unlikely(!w->response.header_output)) {
130
- // no need for error log - web_buffer_create already logged the error
131
- buffer_free(w->response.header);
132
- buffer_free(w->response.data);
133
- close(w->ifd);
134
- free(w);
135
- return NULL;
136
- }
137
-
109
w->origin[0] = '*';
110
w->wait_receive = 1;
111
@@ -263,7 +234,7 @@ struct web_client *web_client_free(struct web_client *w) {
234
if(w->response.data) buffer_free(w->response.data);
235
if(w->ifd != -1) close(w->ifd);
236
if(w->ofd != -1 && w->ofd != w->ifd) close(w->ofd);
266
- free(w);
237
+ freez(w);
238
239
global_statistics.connected_clients--;
240
@@ -700,10 +671,8 @@ int web_client_api_v1_badge(struct web_client *w, char *url) {
671
if(!dimensions)
672
dimensions = buffer_create(100);
673
703
- if(dimensions) {
704
- buffer_strcat(dimensions, "|");
705
- buffer_strcat(dimensions, value);
706
- }
674
+ buffer_strcat(dimensions, "|");
675
+ buffer_strcat(dimensions, value);
676
}
677
else if(!strcmp(name, "after")) after_str = value;
678
else if(!strcmp(name, "before")) before_str = value;
@@ -861,10 +830,8 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
830
if(!strcmp(name, "chart")) chart = value;
831
else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
832
if(!dimensions) dimensions = buffer_create(100);
864
- if(dimensions) {
865
- buffer_strcat(dimensions, "|");
866
- buffer_strcat(dimensions, value);
867
- }
833
+ buffer_strcat(dimensions, "|");
834
+ buffer_strcat(dimensions, value);
835
}
836
else if(!strcmp(name, "after")) after_str = value;
837
else if(!strcmp(name, "before")) before_str = value;
src/web_server.c
+11
-16
@@ -1,7 +1,7 @@
1
#include "common.h"
2
3
int listen_backlog = LISTEN_BACKLOG;
4
-int listen_fds_count = 0;
4
+size_t listen_fds_count = 0;
5
int listen_fds[MAX_LISTEN_FDS] = { [0 ... 99] = -1 };
6
char *listen_fds_names[MAX_LISTEN_FDS] = { [0 ... 99] = NULL };
7
int listen_port = LISTEN_PORT;
@@ -165,14 +165,14 @@ static inline int add_listen_socket(int fd, const char *ip, int port) {
165
166
char buffer[100 + 1];
167
snprintfz(buffer, 100, "[%s]:%d", ip, port);
168
- listen_fds_names[listen_fds_count] = strdup(buffer);
168
+ listen_fds_names[listen_fds_count] = strdupz(buffer);
169
170
listen_fds_count++;
171
return 0;
172
}
173
174
int is_listen_socket(int fd) {
175
- int i;
175
+ size_t i;
176
for(i = 0; i < listen_fds_count ;i++)
177
if(listen_fds[i] == fd) return 1;
178
@@ -180,12 +180,12 @@ int is_listen_socket(int fd) {
180
}
181
182
static inline void close_listen_sockets(void) {
183
- int i;
183
+ size_t i;
184
for(i = 0; i < listen_fds_count ;i++) {
185
close(listen_fds[i]);
186
listen_fds[i] = -1;
187
188
- if(listen_fds_names[i]) free(listen_fds_names[i]);
188
+ freez(listen_fds_names[i]);
189
listen_fds_names[i] = NULL;
190
}
191
@@ -319,7 +319,7 @@ int create_listen_sockets(void) {
319
if(!listen_fds_count)
320
fatal("Cannot listen on any socket. Exiting...");
321
322
- return listen_fds_count;
322
+ return (int)listen_fds_count;
323
}
324
325
// --------------------------------------------------------------------------------------
@@ -367,11 +367,9 @@ void *socket_listen_main_multi_threaded(void *ptr) {
367
if(!listen_fds_count)
368
fatal("LISTENER: No sockets to listen to.");
369
370
- struct pollfd *fds = calloc(sizeof(struct pollfd), listen_fds_count);
371
- if(!fds)
372
- fatal("LISTENER: Cannot allocate memory for poll fds.");
370
+ struct pollfd *fds = callocz(sizeof(struct pollfd), listen_fds_count);
371
374
- int i;
372
+ size_t i;
373
for(i = 0; i < listen_fds_count ;i++) {
374
fds[i].fd = listen_fds[i];
375
fds[i].events = POLLIN;
@@ -489,20 +487,17 @@ void *socket_listen_main_single_threaded(void *ptr) {
487
struct web_client *w;
488
int retval;
489
492
- if(ptr) { ; }
493
-
490
if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
491
error("Cannot set pthread cancel type to DEFERRED.");
492
493
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
494
error("Cannot set pthread cancel state to ENABLE.");
495
500
- int i;
501
-
496
if(!listen_fds_count)
497
fatal("LISTENER: no listen sockets available.");
498
505
- for(i = 0; i < FD_SETSIZE ; i++)
499
+ size_t i;
500
+ for(i = 0; i < FD_SETSIZE ; i++)
501
single_threaded_clients[i] = NULL;
502
503
fd_set ifds, ofds, efds, rifds, rofds, refds;
@@ -549,7 +544,7 @@ void *socket_listen_main_single_threaded(void *ptr) {
544
}
545
}
546
552
- for(i = 0 ; i <= fdmax ; i++) {
547
+ for(i = 0 ; i <= (size_t)fdmax ; i++) {
548
if(likely(!FD_ISSET(i, &rifds) && !FD_ISSET(i, &rofds) && !FD_ISSET(i, &refds)))
549
continue;
550
web/index.html
+1
-1
@@ -1431,7 +1431,7 @@ var chartData = {
1431
},
1432
1433
'system.load': {
1434
- info: 'Current system load read from <code>/proc/loadavg</code>.',
1434
+ info: 'Current system load, a measurement of the work the system is performing. A completely idle computer has a load average of 0. Each process either using or waiting for system resources (e.g. CPU, disk) adds 1 to the load average. So, if your system has a load of 5, five processes are either using or waiting for system resources. Linux calculates this once every 5 seconds. Netdata reads it from <code>/proc/loadavg</code>. For more information see: <a href="http://www.howtogeek.com/194642/understanding-the-load-average-on-linux-and-other-unix-like-systems/" target="_blank">this article</a>',
1435
height: 0.7
1436
},
1437