]> arthur.barton.de Git - netdata.git/commitdiff
code cleanup by replacing all memory allocation functions with ones that handle excep...
authorCosta Tsaousis <costa@tsaousis.gr>
Thu, 11 Aug 2016 17:32:59 +0000 (20:32 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Thu, 11 Aug 2016 17:32:59 +0000 (20:32 +0300)
28 files changed:
src/appconfig.c
src/apps_plugin.c
src/common.c
src/common.h
src/daemon.c
src/dictionary.c
src/dictionary.h
src/eval.c
src/health.c
src/health.h
src/main.c
src/plugin_tc.c
src/plugins_d.c
src/proc_diskstats.c
src/proc_interrupts.c
src/proc_loadavg.c
src/proc_self_mountinfo.c
src/proc_softirqs.c
src/procfile.c
src/registry.c
src/rrd.c
src/rrd2json.c
src/sys_fs_cgroup.c
src/url.c
src/web_buffer.c
src/web_client.c
src/web_server.c
web/index.html

index dd90aa87b6027c1bfa810d4bf13ced4e02f16bdc..c43f4cd731c433ee4380fb9d921232a6a17147b6 100644 (file)
@@ -121,11 +121,8 @@ static inline struct config *config_section_create(const char *section)
 {
        debug(D_CONFIG, "Creating section '%s'.", section);
 
-       struct config *co = calloc(1, sizeof(struct config));
-       if(!co) fatal("Cannot allocate config");
-
-       co->name = strdup(section);
-       if(!co->name) fatal("Cannot allocate config.name");
+       struct config *co = callocz(1, sizeof(struct config));
+       co->name = strdupz(section);
        co->hash = simple_hash(co->name);
 
        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
 {
        debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
 
-       struct config_value *cv = calloc(1, sizeof(struct config_value));
-       if(!cv) fatal("Cannot allocate config_value");
-
-       cv->name = strdup(name);
-       if(!cv->name) fatal("Cannot allocate config.name");
+       struct config_value *cv = callocz(1, sizeof(struct config_value));
+       cv->name = strdupz(name);
        cv->hash = simple_hash(cv->name);
-
-       cv->value = strdup(value);
-       if(!cv->value) fatal("Cannot allocate config.value");
+       cv->value = strdupz(value);
 
        config_value_index_add(co, cv);
 
@@ -208,9 +200,8 @@ int config_rename(const char *section, const char *old, const char *new) {
 
        config_value_index_del(co, cv);
 
-       free(cv->name);
-       cv->name = strdup(new);
-       if(!cv->name) fatal("Cannot allocate memory for config_rename()");
+       freez(cv->name);
+       cv->name = strdupz(new);
 
        cv->hash = simple_hash(cv->name);
 
@@ -322,9 +313,8 @@ const char *config_set_default(const char *section, const char *name, const char
        if(strcmp(cv->value, value) != 0) {
                cv->flags |= CONFIG_VALUE_CHANGED;
 
-               free(cv->value);
-               cv->value = strdup(value);
-               if(!cv->value) fatal("Cannot allocate config.value");
+               freez(cv->value);
+               cv->value = strdupz(value);
        }
 
        return cv->value;
@@ -346,9 +336,8 @@ const char *config_set(const char *section, const char *name, const char *value)
        if(strcmp(cv->value, value) != 0) {
                cv->flags |= CONFIG_VALUE_CHANGED;
 
-               free(cv->value);
-               cv->value = strdup(value);
-               if(!cv->value) fatal("Cannot allocate config.value");
+               freez(cv->value);
+               cv->value = strdupz(value);
        }
 
        return value;
@@ -448,9 +437,8 @@ int load_config(char *filename, int overwrite_used)
                else {
                        if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
                                debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
-                               free(cv->value);
-                               cv->value = strdup(value);
-                               if(!cv->value) fatal("Cannot allocate config.value");
+                               freez(cv->value);
+                               cv->value = strdupz(value);
                        }
                        else
                                debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
index 188d5a9546057fbe8ec20fdb11c65459b346ac1f..210b51038602810c1389df2c75529325df5a2f72 100644 (file)
@@ -183,12 +183,7 @@ struct target *get_users_target(uid_t uid)
        for(w = users_root_target ; w ; w = w->next)
                if(w->uid == uid) return w;
 
-       w = calloc(sizeof(struct target), 1);
-       if(unlikely(!w)) {
-               error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
-               return NULL;
-       }
-
+       w = callocz(sizeof(struct target), 1);
        snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
        w->comparehash = simple_hash(w->compare);
        w->comparelen = strlen(w->compare);
@@ -221,12 +216,7 @@ struct target *get_groups_target(gid_t gid)
        for(w = groups_root_target ; w ; w = w->next)
                if(w->gid == gid) return w;
 
-       w = calloc(sizeof(struct target), 1);
-       if(unlikely(!w)) {
-               error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
-               return NULL;
-       }
-
+       w = callocz(sizeof(struct target), 1);
        snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
        w->comparehash = simple_hash(w->compare);
        w->comparelen = strlen(w->compare);
@@ -255,8 +245,7 @@ struct target *get_groups_target(gid_t gid)
 
 // find or create a new target
 // there are targets that are just aggregated to other target (the second argument)
-struct target *get_apps_groups_target(const char *id, struct target *target)
-{
+struct target *get_apps_groups_target(const char *id, struct target *target) {
        int tdebug = 0, thidden = 0, ends_with = 0;
        const char *nid = id;
 
@@ -276,19 +265,14 @@ struct target *get_apps_groups_target(const char *id, struct target *target)
                last = w;
        }
 
-       w = calloc(sizeof(struct target), 1);
-       if(unlikely(!w)) {
-               error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
-               return NULL;
-       }
-
+       w = callocz(sizeof(struct target), 1);
        strncpyz(w->id, nid, MAX_NAME);
        w->idhash = simple_hash(w->id);
 
        strncpyz(w->name, nid, MAX_NAME);
 
        strncpyz(w->compare, nid, MAX_COMPARE_NAME);
-       int len = strlen(w->compare);
+       size_t len = strlen(w->compare);
        if(w->compare[len - 1] == '*') {
                w->compare[len - 1] = '\0';
                w->starts_with = 1;
@@ -541,16 +525,9 @@ struct pid_stat *get_pid_entry(pid_t pid) {
                return all_pids[pid];
        }
 
-       all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
-       if(!all_pids[pid]) {
-               error("Cannot allocate %zu bytes of memory", (size_t)sizeof(struct pid_stat));
-               return NULL;
-       }
-
-       all_pids[pid]->fds = calloc(sizeof(int), 100);
-       if(!all_pids[pid]->fds)
-               error("Cannot allocate %zu bytes of memory", (size_t)(sizeof(int) * 100));
-       else all_pids[pid]->fds_size = 100;
+       all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
+       all_pids[pid]->fds = callocz(sizeof(int), 100);
+       all_pids[pid]->fds_size = 100;
 
        if(root_of_pids) root_of_pids->prev = all_pids[pid];
        all_pids[pid]->next = root_of_pids;
@@ -577,12 +554,12 @@ void del_pid_entry(pid_t pid) {
        if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
        if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
 
-       if(all_pids[pid]->fds) free(all_pids[pid]->fds);
-       if(all_pids[pid]->stat_filename) free(all_pids[pid]->stat_filename);
-       if(all_pids[pid]->statm_filename) free(all_pids[pid]->statm_filename);
-       if(all_pids[pid]->io_filename) free(all_pids[pid]->io_filename);
-       if(all_pids[pid]->cmdline_filename) free(all_pids[pid]->cmdline_filename);
-       free(all_pids[pid]);
+       if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
+       if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
+       if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
+       if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
+       if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
+       freez(all_pids[pid]);
 
        all_pids[pid] = NULL;
        all_pids_count--;
@@ -597,14 +574,13 @@ int read_proc_pid_cmdline(struct pid_stat *p) {
        if(unlikely(!p->cmdline_filename)) {
                char filename[FILENAME_MAX + 1];
                snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
-               if(!(p->cmdline_filename = strdup(filename)))
-                       fatal("Cannot allocate memory for filename '%s'", filename);
+               p->cmdline_filename = strdupz(filename);
        }
 
        int fd = open(p->cmdline_filename, O_RDONLY, 0666);
        if(unlikely(fd == -1)) goto cleanup;
 
-       int i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
+       ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
        close(fd);
 
        if(unlikely(bytes <= 0)) goto cleanup;
@@ -651,8 +627,7 @@ int read_proc_pid_stat(struct pid_stat *p) {
        if(unlikely(!p->stat_filename)) {
                char filename[FILENAME_MAX + 1];
                snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
-               if(!(p->stat_filename = strdup(filename)))
-                       fatal("Cannot allocate memory for filename '%s'", filename);
+               p->stat_filename = strdupz(filename);
        }
 
        int set_quotes = (!ff)?1:0;
@@ -798,8 +773,7 @@ int read_proc_pid_statm(struct pid_stat *p) {
        if(unlikely(!p->statm_filename)) {
                char filename[FILENAME_MAX + 1];
                snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
-               if(!(p->statm_filename = strdup(filename)))
-                       fatal("Cannot allocate memory for filename '%s'", filename);
+               p->statm_filename = strdupz(filename);
        }
 
        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) {
        if(unlikely(!p->io_filename)) {
                char filename[FILENAME_MAX + 1];
                snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
-               if(!(p->io_filename = strdup(filename)))
-                       fatal("Cannot allocate memory for filename '%s'", filename);
+               p->io_filename = strdupz(filename);
        }
 
        // open the file
@@ -1107,7 +1080,7 @@ int file_descriptor_find_or_add(const char *name)
                if(unlikely(debug))
                        fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
 
-               all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
+               all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
 
                // if the address changed, we have to rebuild the index
                // since all pointers are now invalid
@@ -1159,7 +1132,7 @@ int file_descriptor_find_or_add(const char *name)
                        if(unlikely(debug))
                                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);
 
-                       if(all_files[c].name) free((void *)all_files[c].name);
+                       if(all_files[c].name) freez((void *)all_files[c].name);
                        all_files[c].name = NULL;
                        last_pos = c;
                        break;
@@ -1199,7 +1172,7 @@ int file_descriptor_find_or_add(const char *name)
                type = FILETYPE_OTHER;
        }
 
-       all_files[c].name = strdup(name);
+       all_files[c].name = strdupz(name);
        all_files[c].hash = hash;
        all_files[c].type = type;
        all_files[c].pos  = c;
@@ -1242,7 +1215,7 @@ int read_pid_file_descriptors(struct pid_stat *p) {
                                if(unlikely(debug))
                                        fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
 
-                               p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
+                               p->fds = reallocz(p->fds, (fdid + 100) * sizeof(int));
                                if(!p->fds) {
                                        fatal("Cannot re-allocate fds for %s", p->comm);
                                        break;
@@ -1949,7 +1922,7 @@ long zero_all_targets(struct target *root) {
        for (w = root; w ; w = w->next) {
                count++;
 
-               if(w->fds) free(w->fds);
+               if(w->fds) freez(w->fds);
                w->fds = NULL;
 
                w->minflt = 0;
@@ -1989,11 +1962,8 @@ long zero_all_targets(struct target *root) {
 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
        (void)o;
 
-       if(unlikely(!w->fds)) {
-               w->fds = calloc(sizeof(int), (size_t) all_files_size);
-               if(unlikely(!w->fds))
-                       error("Cannot allocate memory for fds in %s", w->name);
-       }
+       if(unlikely(!w->fds))
+               w->fds = callocz(sizeof(int), (size_t) all_files_size);
 
        if(likely(p->updated)) {
                w->cutime  += p->cutime;
@@ -2104,7 +2074,7 @@ void count_targets_fds(struct target *root) {
                        }
                }
 
-               free(w->fds);
+               freez(w->fds);
                w->fds = NULL;
        }
 }
@@ -2822,24 +2792,10 @@ int main(int argc, char **argv)
 
        parse_args(argc, argv);
 
-       all_pids_sortlist = calloc(sizeof(pid_t), (size_t)pid_max);
-       if(!all_pids_sortlist) {
-               error("Cannot allocate %zu bytes of memory.", sizeof(pid_t) * pid_max);
-               printf("DISABLE\n");
-               exit(1);
-       }
-
-       all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
-       if(!all_pids) {
-               error("Cannot allocate %zu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
-               printf("DISABLE\n");
-               exit(1);
-       }
+       all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
+       all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
 
        output = buffer_create(1024);
-       if(!output)
-               fatal("Cannot create BUFFER.");
-
        buffer_sprintf(output,
                "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
                "DIMENSION user '' incremental 1 1000\n"
index 237f9732fbb21ff6b93b21d2d6bf2e65c324b984..8d2c3aa8a40d1b7858ceeae32613bfbce86433f9 100644 (file)
@@ -5,6 +5,45 @@ int enable_ksm = 1;
 
 volatile sig_atomic_t netdata_exit = 0;
 
+// ----------------------------------------------------------------------------
+// memory allocation functions that handle failures
+
+// although netdata does not use memory allocations too often (netdata tries to
+// maintain its memory footprint stable during runtime, i.e. all buffers are
+// allocated during initialization and are adapted to current use throughout
+// its lifetime), these can be used to override the default system allocation
+// routines.
+
+char *strdupz(const char *s) {
+    char *t = strdup(s);
+    if(unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
+    return t;
+}
+
+void *mallocz(size_t size) {
+    void *p = malloc(size);
+    if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
+    return p;
+}
+
+void *callocz(size_t nmemb, size_t size) {
+    void *p = calloc(nmemb, size);
+    if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
+    return p;
+}
+
+void *reallocz(void *ptr, size_t size) {
+    void *p = realloc(ptr, size);
+    if(unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
+    return p;
+}
+
+void freez(void *ptr) {
+    free(ptr);
+}
+
+// ----------------------------------------------------------------------------
+
 // time(NULL) in milliseconds
 unsigned long long timems(void) {
        struct timeval now;
@@ -17,7 +56,10 @@ int usecsleep(unsigned long long usec) {
 #ifndef NETDATA_WITH_USLEEP
        // we expect microseconds (1.000.000 per second)
        // but timespec is nanoseconds (1.000.000.000 per second)
-       struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
+       struct timespec rem, req = {
+            .tv_sec = (time_t)(usec / 1000000),
+            .tv_nsec = (suseconds_t)((usec % 1000000) * 1000)
+    };
 
        while(nanosleep(&req, &rem) == -1) {
                if(likely(errno == EINTR)) {
index 6344a69dbbb77da68cca8c585699ba82a3769350..9793adf66f0d9bddb0d45658357b15028ac59cb4 100644 (file)
@@ -123,6 +123,13 @@ extern char *strncpyz(char *dst, const char *src, size_t n);
 extern int  vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
 extern int  snprintfz(char *dst, size_t n, const char *fmt, ...) __attribute__ (( format (printf, 3, 4)));
 
+// memory allocation functions that handle failures
+extern char *strdupz(const char *s);
+extern void *callocz(size_t nmemb, size_t size);
+extern void *mallocz(size_t size);
+extern void freez(void *ptr);
+extern void *reallocz(void *ptr, size_t size);
+
 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
 extern int savememory(const char *filename, void *mem, size_t size);
 
index eb4b8d458ae7b2db55dfea3c115f9320d9bb4d2e..9d4d5b32b4c3822f93dd4c846ba9cfc1d059d39d 100644 (file)
@@ -46,19 +46,16 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
        uid_t uid = pw->pw_uid;
        gid_t gid = pw->pw_gid;
 
-       int ngroups =  sysconf(_SC_NGROUPS_MAX);
+       int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
        gid_t *supplementary_groups = NULL;
        if(ngroups) {
-               supplementary_groups = malloc(sizeof(gid_t) * ngroups);
-               if(supplementary_groups) {
-                       if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
-                               error("Cannot get supplementary groups of user '%s'.", username);
-                               free(supplementary_groups);
-                               supplementary_groups = NULL;
-                               ngroups = 0;
-                       }
+               supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
+               if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
+                       error("Cannot get supplementary groups of user '%s'.", username);
+                       freez(supplementary_groups);
+                       supplementary_groups = NULL;
+                       ngroups = 0;
                }
-               else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
        }
 
        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
                if(setgroups(ngroups, supplementary_groups) == -1)
                        error("Cannot set supplementary groups for user '%s'", username);
 
-               free(supplementary_groups);
+               freez(supplementary_groups);
                supplementary_groups = NULL;
                ngroups = 0;
        }
index a360803b2b65c37bf2d2e0a99e755477c9c761c8..6f2a5902de1ea241b87f92c4b365d5c92b74992b 100644 (file)
@@ -77,15 +77,12 @@ static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *di
 static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
        debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
 
-       NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
-       if(unlikely(!nv)) fatal("Cannot allocate name_value of size %zu", sizeof(NAME_VALUE));
+       NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
 
        if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
                nv->name = (char *)name;
        else {
-               nv->name = strdup(name);
-               if (unlikely(!nv->name))
-                       fatal("Cannot allocate name_value.name of size %zu", strlen(name));
+               nv->name = strdupz(name);
        }
 
        nv->hash = (hash)?hash:simple_hash(nv->name);
@@ -93,10 +90,7 @@ static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const c
        if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
                nv->value = value;
        else {
-               nv->value = malloc(value_len);
-               if (unlikely(!nv->value))
-                       fatal("Cannot allocate name_value.value of size %zu", value_len);
-
+               nv->value = mallocz(value_len);
                memcpy(nv->value, value, value_len);
        }
 
@@ -116,34 +110,30 @@ static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *n
 
        if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
                debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
-               free(nv->value);
+               freez(nv->value);
        }
 
        if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
                debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
-               free(nv->name);
+               freez(nv->name);
        }
 
-       free(nv);
+       freez(nv);
 }
 
 // ----------------------------------------------------------------------------
 // API - basic methods
 
-DICTIONARY *dictionary_create(uint32_t flags) {
+DICTIONARY *dictionary_create(uint8_t flags) {
        debug(D_DICTIONARY, "Creating dictionary.");
 
-       DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
-       if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
+       DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
 
-       if(flags & DICTIONARY_FLAG_WITH_STATISTICS) {
-               dict->stats = calloc(1, sizeof(struct dictionary_stats));
-               if(!dict->stats) fatal("Cannot allocate statistics for DICTIONARY");
-       }
+       if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
+               dict->stats = callocz(1, sizeof(struct dictionary_stats));
 
        if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
-               dict->rwlock = calloc(1, sizeof(pthread_rwlock_t));
-               if(!dict->rwlock) fatal("Cannot allocate pthread_rwlock_t for DICTIONARY");
+               dict->rwlock = callocz(1, sizeof(pthread_rwlock_t));
                pthread_rwlock_init(dict->rwlock, NULL);
        }
 
@@ -164,12 +154,12 @@ void dictionary_destroy(DICTIONARY *dict) {
        dictionary_unlock(dict);
 
        if(dict->stats)
-               free(dict->stats);
+               freez(dict->stats);
 
        if(dict->rwlock)
-               free(dict->rwlock);
+               freez(dict->rwlock);
 
-       free(dict);
+       freez(dict);
 }
 
 // ----------------------------------------------------------------------------
@@ -201,17 +191,14 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val
 
                        // copy the new value without breaking
                        // any other thread accessing the same entry
-                       void *new = malloc(value_len),
+                       void *new = mallocz(value_len),
                                        *old = nv->value;
 
-                       if(unlikely(!new))
-                               fatal("Cannot allocate value of size %zu", value_len);
-
                        memcpy(new, value, value_len);
                        nv->value = new;
 
                        debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
-                       free(old);
+                       freez(old);
                }
        }
 
index 7613e2853303ef80a710f175dbdd59d428f0b614..4164b6dc7b28e9bd8b45ce89b628a65c67011aa2 100644 (file)
@@ -33,12 +33,12 @@ typedef struct dictionary {
 #define DICTIONARY_FLAG_NAME_LINK_DONT_CLONE   0x00000004
 #define DICTIONARY_FLAG_WITH_STATISTICS                        0x00000008
 
-extern DICTIONARY *dictionary_create(uint32_t flags);
+extern DICTIONARY *dictionary_create(uint8_t flags);
 extern void dictionary_destroy(DICTIONARY *dict);
 extern void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len);
 extern void *dictionary_get(DICTIONARY *dict, const char *name);
 extern int dictionary_del(DICTIONARY *dict, const char *name);
 
-extern int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data);
+extern int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *d), void *data);
 
 #endif /* NETDATA_DICTIONARY_H */
index ac5e7d5595e0248cdbe0fd1253eec7466a6f8cb4..e57a12c851c5a58374c1c40fb3d098dbe77973a2 100644 (file)
@@ -579,8 +579,7 @@ static inline unsigned char parse_operator(const char **string, int *precedence)
 static inline EVAL_NODE *eval_node_alloc(int count) {
     static int id = 1;
 
-    EVAL_NODE *op = calloc(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
-    if(!op) fatal("Cannot allocate memory for OPERAND with %d slots", count);
+    EVAL_NODE *op = callocz(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
 
     op->id = id++;
     op->operator = EVAL_OPERATOR_NOP;
@@ -606,7 +605,7 @@ static inline void eval_node_set_value_to_constant(EVAL_NODE *op, int pos, calcu
 }
 
 static inline void eval_variable_free(EVAL_VARIABLE *v) {
-    free(v);
+    freez(v);
 }
 
 static inline void eval_value_free(EVAL_VALUE *v) {
@@ -631,7 +630,7 @@ static inline void eval_node_free(EVAL_NODE *op) {
             eval_value_free(&op->ops[i]);
     }
 
-    free(op);
+    freez(op);
 }
 
 // ----------------------------------------------------------------------------
@@ -820,11 +819,9 @@ EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, in
         return NULL;
     }
 
-    EVAL_EXPRESSION *exp = calloc(1, sizeof(EVAL_EXPRESSION));
-    if(!exp) fatal("Cannot allocate EVAL_EXPRESSION");
+    EVAL_EXPRESSION *exp = callocz(1, sizeof(EVAL_EXPRESSION));
 
-    exp->parsed_as = strdup(buffer_tostring(out));
-    if(!exp->parsed_as) fatal("Cannot allocate memory for parsed-as string");
+    exp->parsed_as = strdupz(buffer_tostring(out));
     buffer_free(out);
 
     exp->error_msg = buffer_create(100);
@@ -837,10 +834,10 @@ void expression_free(EVAL_EXPRESSION *exp) {
     if(!exp) return;
 
     if(exp->nodes) eval_node_free((EVAL_NODE *)exp->nodes);
-    free((void *)exp->source);
-    free((void *)exp->parsed_as);
+    freez((void *)exp->source);
+    freez((void *)exp->parsed_as);
     buffer_free(exp->error_msg);
-    free(exp);
+    freez(exp);
 }
 
 const char *expression_strerror(int error) {
index 28bc464e673a92b0a30945213580c8be50005f4b..3173781c13c94d82f918882827c781a10a1adc76 100644 (file)
@@ -34,8 +34,7 @@ static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, u
 }
 
 static inline RRDVAR *rrdvar_create(const char *name, uint32_t hash, int type, calculated_number *value) {
-    RRDVAR *rv = calloc(1, sizeof(RRDVAR));
-    if(!rv) fatal("Cannot allocate memory for RRDVAR");
+    RRDVAR *rv = callocz(1, sizeof(RRDVAR));
 
     rv->name = (char *)name;
     rv->hash = (hash)?hash:simple_hash((rv->name));
@@ -54,7 +53,7 @@ static inline void rrdvar_free(RRDHOST *host, RRDVAR *rv) {
             if (rf->rrdvar == rv) rf->rrdvar = NULL;
     }
 
-    free(rv);
+    freez(rv);
 }
 
 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 *
 
 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
     debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
-    RRDSETVAR *rs = (RRDSETVAR *)calloc(1, sizeof(RRDSETVAR));
-    if(!rs) fatal("Cannot allocate memory for RRDSETVAR");
+    RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
 
     char buffer[RRDSETVAR_ID_MAX + 1];
     snprintfz(buffer, RRDSETVAR_ID_MAX, "%s.%s", st->id, variable);
-    rs->fullid = strdup(buffer);
-    if(!rs->fullid) fatal("Cannot allocate memory for RRDVASET id");
+    rs->fullid = strdupz(buffer);
     rs->hash_fullid = simple_hash(rs->fullid);
 
     snprintfz(buffer, RRDSETVAR_ID_MAX, "%s.%s", st->name, variable);
-    rs->fullname = strdup(buffer);
-    if(!rs->fullname) fatal("Cannot allocate memory for RRDVASET name");
+    rs->fullname = strdupz(buffer);
     rs->hash_fullname = simple_hash(rs->fullname);
 
-    rs->variable = strdup(variable);
-    if(!rs->variable) fatal("Cannot allocate memory for RRDVASET variable name");
+    rs->variable = strdupz(variable);
     rs->hash_variable = simple_hash(rs->variable);
 
     rs->type = type;
@@ -162,9 +157,8 @@ void rrdsetvar_rename_all(RRDSET *st) {
                 rrdvar_free(st->rrdhost, rs->host_name);
             }
 
-            free(rs->fullname);
-            rs->fullname = strdup(st->name);
-            if(!rs->fullname) fatal("Cannot allocate memory for RRDSETVAR name");
+            freez(rs->fullname);
+            rs->fullname = strdupz(st->name);
             rs->hash_fullname = simple_hash(rs->fullname);
             rs->context_name = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->fullname, rs->hash_fullname, rs->type, rs->value);
             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) {
         else t->next = rs->next;
     }
 
-    free(rs->fullid);
-    free(rs->fullname);
-    free(rs->variable);
-    free(rs);
+    freez(rs->fullid);
+    freez(rs->fullname);
+    freez(rs->variable);
+    freez(rs);
 }
 
 // ----------------------------------------------------------------------------
@@ -233,43 +227,33 @@ RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char
     if(!suffix) suffix = "";
 
     char buffer[RRDDIMVAR_ID_MAX + 1];
-    RRDDIMVAR *rs = (RRDDIMVAR *)calloc(1, sizeof(RRDDIMVAR));
-    if(!rs) fatal("Cannot allocate memory for RRDDIMVAR");
+    RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
 
-    rs->prefix = strdup(prefix);
-    if(!rs->prefix) fatal("Cannot allocate memory for RRDDIMVAR prefix");
-
-    rs->suffix = strdup(suffix);
-    if(!rs->suffix) fatal("Cannot allocate memory for RRDDIMVAR suffix");
+    rs->prefix = strdupz(prefix);
+    rs->suffix = strdupz(suffix);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
-    rs->id = strdup(buffer);
-    if(!rs->id) fatal("Cannot allocate memory for RRDIM id");
+    rs->id = strdupz(buffer);
     rs->hash = simple_hash(rs->id);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
-    rs->name = strdup(buffer);
-    if(!rs->name) fatal("Cannot allocate memory for RRDIM name");
+    rs->name = strdupz(buffer);
     rs->hash_name = simple_hash(rs->name);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
-    rs->fullidid = strdup(buffer);
-    if(!rs->fullidid) fatal("Cannot allocate memory for RRDDIMVAR fullidid");
+    rs->fullidid = strdupz(buffer);
     rs->hash_fullidid = simple_hash(rs->fullidid);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
-    rs->fullidname = strdup(buffer);
-    if(!rs->fullidname) fatal("Cannot allocate memory for RRDDIMVAR fullidname");
+    rs->fullidname = strdupz(buffer);
     rs->hash_fullidname = simple_hash(rs->fullidname);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
-    rs->fullnameid = strdup(buffer);
-    if(!rs->fullnameid) fatal("Cannot allocate memory for RRDDIMVAR fullnameid");
+    rs->fullnameid = strdupz(buffer);
     rs->hash_fullnameid = simple_hash(rs->fullnameid);
 
     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
-    rs->fullnamename = strdup(buffer);
-    if(!rs->fullnamename) fatal("Cannot allocate memory for RRDDIMVAR fullnamename");
+    rs->fullnamename = strdupz(buffer);
     rs->hash_fullnamename = simple_hash(rs->fullnamename);
 
     rs->type = type;
@@ -313,10 +297,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
                 rrdvar_index_del(&st->variables_root_index, rs->local_name);
                 rrdvar_free(st->rrdhost, rs->local_name);
             }
-            free(rs->name);
+            freez(rs->name);
             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
-            rs->name = strdup(buffer);
-            if(!rs->name) fatal("Cannot allocate memory for RRDDIMVAR name");
+            rs->name = strdupz(buffer);
             rs->hash_name = simple_hash(rs->name);
             rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->hash_name, rs->type, rs->value);
 
@@ -329,10 +312,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
                 rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullidname);
                 rrdvar_free(st->rrdhost, rs->host_fullidname);
             }
-            free(rs->fullidname);
+            freez(rs->fullidname);
             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
-            rs->fullidname = strdup(buffer);
-            if(!rs->fullidname) fatal("Cannot allocate memory for RRDDIMVAR fullidname");
+            rs->fullidname = strdupz(buffer);
             rs->hash_fullidname = simple_hash(rs->fullidname);
             rs->context_fullidname = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
                                                              rs->fullidname, rs->hash_fullidname, rs->type, rs->value);
@@ -348,10 +330,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
                 rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullnameid);
                 rrdvar_free(st->rrdhost, rs->host_fullnameid);
             }
-            free(rs->fullnameid);
+            freez(rs->fullnameid);
             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
-            rs->fullnameid = strdup(buffer);
-            if(!rs->fullnameid) fatal("Cannot allocate memory for RRDDIMVAR fullnameid");
+            rs->fullnameid = strdupz(buffer);
             rs->hash_fullnameid = simple_hash(rs->fullnameid);
             rs->context_fullnameid = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
                                                              rs->fullnameid, rs->hash_fullnameid, rs->type, rs->value);
@@ -367,10 +348,9 @@ void rrddimvar_rename_all(RRDDIM *rd) {
                 rrdvar_index_del(&st->rrdhost->variables_root_index, rs->context_fullnamename);
                 rrdvar_free(st->rrdhost, rs->host_fullnamename);
             }
-            free(rs->fullnamename);
+            freez(rs->fullnamename);
             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
-            rs->fullnamename = strdup(buffer);
-            if(!rs->fullnamename) fatal("Cannot allocate memory for RRDDIMVAR fullnamename");
+            rs->fullnamename = strdupz(buffer);
             rs->hash_fullnamename = simple_hash(rs->fullnamename);
             rs->context_fullnamename = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index,
                                                              rs->fullnamename, rs->hash_fullnamename, rs->type, rs->value);
@@ -440,15 +420,15 @@ void rrddimvar_free(RRDDIMVAR *rs) {
         else t->next = rs->next;
     }
 
-    free(rs->prefix);
-    free(rs->suffix);
-    free(rs->id);
-    free(rs->name);
-    free(rs->fullidid);
-    free(rs->fullidname);
-    free(rs->fullnameid);
-    free(rs->fullnamename);
-    free(rs);
+    freez(rs->prefix);
+    freez(rs->suffix);
+    freez(rs->id);
+    freez(rs->name);
+    freez(rs->fullidid);
+    freez(rs->fullidname);
+    freez(rs->fullnameid);
+    freez(rs->fullnamename);
+    freez(rs);
 }
 
 // ----------------------------------------------------------------------------
@@ -588,3 +568,40 @@ void rrdsetcalc_unlink(RRDCALC *rc) {
 
     rrdhostcalc_unlinked(host, rc);
 }
+
+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) {
+    uint32_t hash = simple_hash(name);
+
+    RRDCALC *rc;
+
+    // make sure it does not already exist
+    for(rc = host->calculations; rc ; rc = rc->next) {
+        if (rc->hash == hash && !strcmp(name, rc->name)) {
+            error("Attempted to create RRDCAL '%s' in host '%s', but it already exists. Ignoring it.", name, host->hostname);
+            return NULL;
+        }
+    }
+
+    rc = callocz(1, sizeof(RRDCALC));
+
+    rc->name = strdupz(name);
+    rc->hash = simple_hash(rc->name);
+
+    rc->chart = strdupz(chart);
+    rc->hash_chart = simple_hash(rc->chart);
+
+    if(dimensions) {
+        rc->dimensions = strdupz(dimensions);
+        rc->hash_chart = simple_hash(rc->chart);
+    }
+
+    return NULL;
+}
+
+void rrdcalc_free(RRDCALC *rc) {
+    if(!rc) return;
+
+    if(rc->rrdset) rrdsetcalc_unlink(rc);
+
+    freez(rc);
+}
\ No newline at end of file
index 760cee4fe10527751b38ada2b01184f7d311104f..f30e432f56090647ccf14726a4046a18fae9dc1f 100644 (file)
@@ -155,6 +155,8 @@ typedef struct rrdcalc {
     time_t last_updated;
     time_t next_update;
 
+    EVAL_EXPRESSION *expression;
+
     calculated_number value;
 
     RRDVAR *local;
index ea84f976ea1e67c9102ac54dda6053ae887ab248..f93fef7b300a1e065bf03bcea78f3b211209b54e 100644 (file)
@@ -667,9 +667,7 @@ int main(int argc, char **argv)
                struct netdata_static_thread *st = &static_threads[i];
 
                if(st->enabled) {
-                       st->thread = malloc(sizeof(pthread_t));
-                       if(!st->thread)
-                               fatal("Cannot allocate pthread_t memory");
+                       st->thread = mallocz(sizeof(pthread_t));
 
                        info("Starting thread %s.", st->name);
 
index 201cfcad570d61478c0249bf45d3c4bbd6d2a0ed..b7ccfba2574af54a5045e080966e0e41e50e31d0 100644 (file)
@@ -140,12 +140,11 @@ static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
 
        tc_class_index_del(n, c);
 
-       if(c->id) free(c->id);
-       if(c->name) free(c->name);
-       if(c->leafid) free(c->leafid);
-       if(c->parentid) free(c->parentid);
-
-       free(c);
+       freez(c->id);
+       freez(c->name);
+       freez(c->leafid);
+       freez(c->parentid);
+       freez(c);
 }
 
 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
 {
        struct tc_class *c = tc_class_index_find(d, id, 0);
        if(likely(c)) {
-               if(likely(c->name))
-                       free(c->name);
-
+               freez(c->name);
                c->name = NULL;
 
                if(likely(name && *name && strcmp(c->id, name) != 0)) {
                        debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
-                       c->name = strdup(name);
-                       if(likely(c->name))
-                               c->name_updated = 1;
+                       c->name = strdupz(name);
+                       c->name_updated = 1;
                }
        }
 }
 
 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
-       if(likely(d->name))
-               free(d->name);
-
+       freez(d->name);
        d->name = NULL;
 
        if(likely(name && *name && strcmp(d->id, name) != 0)) {
                debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
-               d->name = strdup(name);
-               if(likely(d->name))
-                       d->name_updated = 1;
+               d->name = strdupz(name);
+               d->name_updated = 1;
        }
 }
 
 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
-       if(likely(d->family))
-               free(d->family);
-
+       freez(d->family);
        d->family = NULL;
 
        if(likely(family && *family && strcmp(d->id, family) != 0)) {
                debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
-               d->family = strdup(family);
-               if(likely(d->family))
-                       d->family_updated = 1;
+               d->family = strdupz(family);
+               d->family_updated = 1;
        }
        // no need for null termination - it is already null
 }
@@ -479,13 +469,9 @@ static inline struct tc_device *tc_device_create(char *id)
        if(!d) {
                debug(D_TC_LOOP, "TC: Creating device '%s'", id);
 
-               d = calloc(1, sizeof(struct tc_device));
-               if(!d) {
-                       fatal("Cannot allocate memory for tc_device %s", id);
-                       return NULL;
-               }
+               d = callocz(1, sizeof(struct tc_device));
 
-               d->id = strdup(id);
+               d->id = strdupz(id);
                d->hash = simple_hash(d->id);
                d->enabled = -1;
 
@@ -512,30 +498,22 @@ static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char
        if(!c) {
                debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
 
-               c = calloc(1, sizeof(struct tc_class));
-               if(!c) {
-                       fatal("Cannot allocate memory for tc class");
-                       return NULL;
-               }
+               c = callocz(1, sizeof(struct tc_class));
 
                if(n->classes) n->classes->prev = c;
                c->next = n->classes;
                n->classes = c;
 
-               c->id = strdup(id);
-               if(!c->id) {
-                       free(c);
-                       return NULL;
-               }
+               c->id = strdupz(id);
                c->hash = simple_hash(c->id);
 
                if(parentid && *parentid) {
-                       c->parentid = strdup(parentid);
+                       c->parentid = strdupz(parentid);
                        c->parent_hash = simple_hash(c->parentid);
                }
 
                if(leafid && *leafid) {
-                       c->leafid = strdup(leafid);
+                       c->leafid = strdupz(leafid);
                        c->leaf_hash = simple_hash(c->leafid);
                }
 
@@ -560,11 +538,10 @@ static inline void tc_device_free(struct tc_device *n)
 
        while(n->classes) tc_class_free(n, n->classes);
 
-       if(n->id) free(n->id);
-       if(n->name) free(n->name);
-       if(n->family) free(n->family);
-
-       free(n);
+       freez(n->id);
+       freez(n->name);
+       freez(n->family);
+       freez(n);
 }
 
 static inline void tc_device_free_all()
index aa233cfb410fc321f26b1ff608fd04d677a7ba7b..7e106bf81942b60a167a801efb08bc00580887f0 100644 (file)
@@ -508,8 +508,7 @@ void *pluginsd_main(void *ptr)
                        // it is not running
                        // allocate a new one, or use the obsolete one
                        if(unlikely(!cd)) {
-                               cd = calloc(sizeof(struct plugind), 1);
-                               if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
+                               cd = callocz(sizeof(struct plugind), 1);
 
                                snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
 
index 87140adaff529069207a684b36427eed5b3fde12..f47cd4469243d04cf55d6d010eb7e38a7a3dcc18 100644 (file)
@@ -51,10 +51,9 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
 
        // not found
        // create a new disk structure
-       d = (struct disk *)malloc(sizeof(struct disk));
-       if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats.");
+       d = (struct disk *)mallocz(sizeof(struct disk));
 
-       d->disk = strdup(disk);
+       d->disk = strdupz(disk);
        d->major = major;
        d->minor = minor;
        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
        }
 
        if(mi)
-               d->mount_point = strdup(mi->mount_point);
+               d->mount_point = strdupz(mi->mount_point);
                // no need to check for NULL
        else
                d->mount_point = NULL;
index b4a62f0429740bfe01b65649abd0cefeaed264c5..6f703434878eb7201a1d72889e2038401127ba63 100644 (file)
@@ -23,10 +23,7 @@ static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
 
        if(lines < allocated) return irrs;
        else {
-               irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
-               if(!irrs)
-                       fatal("Cannot allocate memory for %d interrupts", lines);
-
+               irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
                allocated = lines;
        }
 
index 54c08c2b0260f64ae798008658cc934c51c2e486..ec04ec250f5ccfa2798751ee0310617d60d97fa3 100644 (file)
@@ -1,5 +1,8 @@
 #include "common.h"
 
+// linux calculates this once every 5 seconds
+#define MIN_LOADAVG_UPDATE_EVERY 5
+
 int do_proc_loadavg(int update_every, unsigned long long dt) {
        static procfile *ff = NULL;
        static int do_loadavg = -1, do_all_processes = -1;
@@ -44,7 +47,7 @@ int do_proc_loadavg(int update_every, unsigned long long dt) {
        if(do_loadavg) {
                st = rrdset_find_byname("system.load");
                if(!st) {
-                       st = rrdset_create("system", "load", NULL, "load", NULL, "System Load Average", "load", 100, update_every, RRDSET_TYPE_LINE);
+                       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);
 
                        rrddim_add(st, "load1", NULL, 1, 1000, RRDDIM_ABSOLUTE);
                        rrddim_add(st, "load5", NULL, 1, 1000, RRDDIM_ABSOLUTE);
index b4b2f684dfa20fee2c77ea3b8a67b6f29376f610..623eb622a85c33e6f89d2241737830bd2a0542be 100644 (file)
@@ -69,9 +69,9 @@ void mountinfo_free(struct mountinfo *mi) {
        if(likely(mi->next))
                mountinfo_free(mi->next);
 
-       if(mi->root) free(mi->root);
-       if(mi->mount_point) free(mi->mount_point);
-       if(mi->mount_options) free(mi->mount_options);
+       freez(mi->root);
+       freez(mi->mount_point);
+       freez(mi->mount_options);
 
 /*
        if(mi->optional_fields_count) {
@@ -81,15 +81,14 @@ void mountinfo_free(struct mountinfo *mi) {
        }
        free(mi->optional_fields);
 */
-       free(mi->filesystem);
-       free(mi->mount_source);
-       free(mi->super_options);
-       free(mi);
+       freez(mi->filesystem);
+       freez(mi->mount_source);
+       freez(mi->super_options);
+       freez(mi);
 }
 
-static char *strdup_decoding_octal(const char *string) {
-       char *buffer = strdup(string);
-       if(!buffer) fatal("Cannot allocate memory.");
+static char *strdupz_decoding_octal(const char *string) {
+       char *buffer = strdupz(string);
 
        char *d = buffer;
        const char *s = string;
@@ -137,8 +136,7 @@ struct mountinfo *mountinfo_read() {
                if(procfile_linewords(ff, l) < 5)
                        continue;
 
-               mi = malloc(sizeof(struct mountinfo));
-               if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
+               mi = mallocz(sizeof(struct mountinfo));
 
                if(unlikely(!root))
                        root = last = mi;
@@ -160,16 +158,13 @@ struct mountinfo *mountinfo_read() {
                mi->major = strtoul(major, NULL, 10);
                mi->minor = strtoul(minor, NULL, 10);
 
-               mi->root = strdup(procfile_lineword(ff, l, w)); w++;
-               if(unlikely(!mi->root)) fatal("Cannot allocate memory");
+               mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
                mi->root_hash = simple_hash(mi->root);
 
-               mi->mount_point = strdup_decoding_octal(procfile_lineword(ff, l, w)); w++;
-               if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
+               mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
                mi->mount_point_hash = simple_hash(mi->mount_point);
 
-               mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
-               if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
+               mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
 
                // count the optional fields
 /*
@@ -206,16 +201,13 @@ struct mountinfo *mountinfo_read() {
                if(likely(*s == '-')) {
                        w++;
 
-                       mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
-                       if(!mi->filesystem) fatal("Cannot allocate memory");
+                       mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
                        mi->filesystem_hash = simple_hash(mi->filesystem);
 
-                       mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
-                       if(!mi->mount_source) fatal("Cannot allocate memory");
+                       mi->mount_source = strdupz(procfile_lineword(ff, l, w)); w++;
                        mi->mount_source_hash = simple_hash(mi->mount_source);
 
-                       mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
-                       if(!mi->super_options) fatal("Cannot allocate memory");
+                       mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
                }
                else {
                        mi->filesystem = NULL;
index 020bc5e35a111715d12bae587d53a0e050de8147..d966ab790f9b4b96757672dd8be05cdce393eef3 100644 (file)
@@ -23,10 +23,7 @@ static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
 
        if(lines < allocated) return irrs;
        else {
-               irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
-               if(!irrs)
-                       fatal("Cannot allocate memory for %d interrupts", lines);
-
+               irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
                allocated = lines;
        }
 
index 49f715d474e334c723771df12e1a7daad32cd8fe..efe569e593bd608f8459c7847778ef21530174bf 100644 (file)
@@ -24,13 +24,7 @@ pfwords *pfwords_add(pfwords *fw, char *str) {
        if(unlikely(fw->len == fw->size)) {
                // debug(D_PROCFILE, PF_PREFIX ":       expanding words");
 
-               pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
-               if(unlikely(!new)) {
-                       error(PF_PREFIX ":      failed to expand words");
-                       free(fw);
-                       return NULL;
-               }
-               fw = new;
+               fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
                fw->size += PFWORDS_INCREASE_STEP;
        }
 
@@ -44,9 +38,7 @@ pfwords *pfwords_new(void) {
 
        uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
 
-       pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
-       if(unlikely(!new)) return NULL;
-
+       pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
        new->len = 0;
        new->size = size;
        return new;
@@ -60,7 +52,7 @@ void pfwords_reset(pfwords *fw) {
 void pfwords_free(pfwords *fw) {
        // debug(D_PROCFILE, PF_PREFIX ":       freeing words");
 
-       free(fw);
+       freez(fw);
 }
 
 
@@ -73,13 +65,7 @@ pflines *pflines_add(pflines *fl, uint32_t first_word) {
        if(unlikely(fl->len == fl->size)) {
                // debug(D_PROCFILE, PF_PREFIX ":       expanding lines");
 
-               pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
-               if(unlikely(!new)) {
-                       error(PF_PREFIX ":      failed to expand lines");
-                       free(fl);
-                       return NULL;
-               }
-               fl = new;
+               fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
                fl->size += PFLINES_INCREASE_STEP;
        }
 
@@ -94,9 +80,7 @@ pflines *pflines_new(void) {
 
        uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
 
-       pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
-       if(unlikely(!new)) return NULL;
-
+       pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
        new->len = 0;
        new->size = size;
        return new;
@@ -111,7 +95,7 @@ void pflines_reset(pflines *fl) {
 void pflines_free(pflines *fl) {
        // debug(D_PROCFILE, PF_PREFIX ":       freeing lines");
 
-       free(fl);
+       freez(fl);
 }
 
 
@@ -132,7 +116,7 @@ void procfile_close(procfile *ff) {
        if(likely(ff->words)) pfwords_free(ff->words);
 
        if(likely(ff->fd != -1)) close(ff->fd);
-       free(ff);
+       freez(ff);
 }
 
 procfile *procfile_parser(procfile *ff) {
@@ -293,13 +277,7 @@ procfile *procfile_readall(procfile *ff) {
                if(!x) {
                        debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
 
-                       procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
-                       if(unlikely(!new)) {
-                               error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
-                               procfile_close(ff);
-                               return NULL;
-                       }
-                       ff = new;
+                       ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
                        ff->size += PROCFILE_INCREMENT_BUFFER;
                }
 
@@ -416,13 +394,7 @@ procfile *procfile_open(const char *filename, const char *separators, uint32_t f
        }
 
        size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
-       procfile *ff = malloc(sizeof(procfile) + size);
-       if(unlikely(!ff)) {
-               error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
-               close(fd);
-               return NULL;
-       }
-
+       procfile *ff = mallocz(sizeof(procfile) + size);
        strncpyz(ff->filename, filename, FILENAME_MAX);
 
        ff->fd = fd;
index 0c184a73fb64213a3c4c52c1dbd45fe6de55a023..988ed78d67a3c8203efadbeea141b73ff392bb8d 100644 (file)
@@ -312,14 +312,12 @@ static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen)
                urllen = registry.max_url_length;
 
        debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
-       URL *u = malloc(sizeof(URL) + urllen);
-       if(!u) fatal("Cannot allocate %zu bytes for URL '%s'", sizeof(URL) + urllen, url);
+       URL *u = mallocz(sizeof(URL) + urllen);
 
        // a simple strcpy() should do the job
        // but I prefer to be safe, since the caller specified urllen
-       strncpyz(u->url, url, urllen);
-
-       u->len = urllen;
+       u->len = (uint16_t)urllen;
+       strncpyz(u->url, url, u->len);
        u->links = 0;
 
        registry.urls_memory += sizeof(URL) + urllen;
@@ -356,7 +354,7 @@ static inline void registry_url_unlink_nolock(URL *u) {
        if(!u->links) {
                debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
                dictionary_del(registry.urls, u->url);
-               free(u);
+               freez(u);
        }
        else
                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) {
 static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
        debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
 
-       MACHINE_URL *mu = malloc(sizeof(MACHINE_URL));
-       if(!mu) fatal("registry_machine_link_to_url('%s', '%s'): cannot allocate %zu bytes.", m->guid, u->url, sizeof(MACHINE_URL));
+       MACHINE_URL *mu = mallocz(sizeof(MACHINE_URL));
 
        // mu->persons = dictionary_create(DICTIONARY_FLAGS);
        // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
 
-       mu->first_t = mu->last_t = when;
+       mu->first_t = mu->last_t = (uint32_t)when;
        mu->usages = 1;
        mu->url = u;
        mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
@@ -397,15 +394,14 @@ static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, tim
 static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
        debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
 
-       MACHINE *m = malloc(sizeof(MACHINE));
-       if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
+       MACHINE *m = mallocz(sizeof(MACHINE));
 
        strncpyz(m->guid, machine_guid, 36);
 
        debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
        m->urls = dictionary_create(DICTIONARY_FLAGS);
 
-       m->first_t = m->last_t = when;
+       m->first_t = m->last_t = (uint32_t)when;
        m->usages = 0;
 
        registry.machines_memory += sizeof(MACHINE);
@@ -458,8 +454,7 @@ static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, UR
        debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
                  sizeof(PERSON_URL) + namelen);
 
-       PERSON_URL *pu = malloc(sizeof(PERSON_URL) + namelen);
-       if(!pu) fatal("registry_person_url_allocate('%s', '%s', '%s'): cannot allocate %zu bytes.", p->guid, m->guid, u->url, sizeof(PERSON_URL) + namelen);
+       PERSON_URL *pu = mallocz(sizeof(PERSON_URL) + namelen);
 
        // a simple strcpy() should do the job
        // 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,
        registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
        registry_url_unlink_nolock(u);
 
-       free(pu);
+       freez(pu);
 
        return tpu;
 }
@@ -508,8 +503,7 @@ static inline PERSON *registry_person_allocate(const char *person_guid, time_t w
 
        debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
 
-       p = malloc(sizeof(PERSON));
-       if(!p) fatal("Registry: cannot allocate memory for new person.");
+       p = mallocz(sizeof(PERSON));
 
        if(!person_guid) {
                for (; ;) {
@@ -914,7 +908,7 @@ PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url
 
        dictionary_del(p->urls, dpu->url->url);
        registry_url_unlink_nolock(dpu->url);
-       free(dpu);
+       freez(dpu);
 
        registry_person_urls_unlock(p);
        return p;
@@ -1720,7 +1714,7 @@ void registry_free(void) {
                        registry_url_unlink_nolock(pu->url);
 
                        debug(D_REGISTRY, "Registry: freeing person url");
-                       free(pu);
+                       freez(pu);
                }
 
                debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
@@ -1730,7 +1724,7 @@ void registry_free(void) {
                dictionary_destroy(p->urls);
 
                debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
-               free(p);
+               freez(p);
        }
 
        while(registry.machines->values_index.root) {
@@ -1753,7 +1747,7 @@ void registry_free(void) {
                        registry_url_unlink_nolock(mu->url);
 
                        debug(D_REGISTRY, "Registry: freeing machine url");
-                       free(mu);
+                       freez(mu);
                }
 
                debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
@@ -1763,7 +1757,7 @@ void registry_free(void) {
                dictionary_destroy(m->urls);
 
                debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
-               free(m);
+               freez(m);
        }
 
        // and free the memory of remaining dictionary structures
index 2b50f971c8aed7fefeae8db5266c5b2928debbed..7e34a7f5e7b70b34a5823b6a803b29c2bb2fc581 100644 (file)
--- a/src/rrd.c
+++ b/src/rrd.c
@@ -63,12 +63,9 @@ static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t
 RRDCONTEXT *rrdcontext_create(const char *id) {
     RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
     if(!rc) {
-        rc = calloc(1, sizeof(RRDCONTEXT));
-        if(!rc) fatal("Cannot allocate RRDCONTEXT memory");
-
-        rc->id = strdup(id);
-        if(!rc->id) fatal("Cannot allocate RRDCONTEXT.id memory");
+        rc = callocz(1, sizeof(RRDCONTEXT));
 
+        rc->id = strdupz(id);
         rc->hash = simple_hash(rc->id);
 
         // initialize the variables index
@@ -93,8 +90,8 @@ void rrdcontext_free(RRDCONTEXT *rc) {
         if(rc->variables_root_index.avl_tree.root != NULL)
             fatal("INTERNAL ERROR: Variables index of RRDCONTEXT '%s' that is freed, is not empty.", rc->id);
 
-        free((void *)rc->id);
-        free(rc);
+        freez((void *)rc->id);
+        freez(rc);
     }
 }
 
@@ -465,11 +462,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
         st->variables = NULL;
        }
        else {
-               st = calloc(1, size);
-               if(!st) {
-                       fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
-                       return NULL;
-               }
+               st = callocz(1, size);
                st->mapped = RRD_MEMORY_MODE_RAM;
        }
        st->memsize = size;
@@ -611,12 +604,7 @@ RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier
        else {
                // if we didn't manage to get a mmap'd dimension, just create one
 
-               rd = calloc(1, size);
-               if(!rd) {
-                       fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
-                       return NULL;
-               }
-
+               rd = callocz(1, size);
                rd->mapped = RRD_MEMORY_MODE_RAM;
        }
        rd->memsize = size;
@@ -723,7 +711,7 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
        }
        else {
                debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
-               free(rd);
+               freez(rd);
        }
 }
 
@@ -768,7 +756,7 @@ void rrdset_free_all(void)
                        munmap(st, st->memsize);
                }
                else
-                       free(st);
+                       freez(st);
 
                st = next;
        }
index f6c742010029641862b4c2ebd67c6281b4289d6c..7958bca2ea2dd8cf6b4fc9a18588b89cf0350ed5 100644 (file)
@@ -1150,11 +1150,11 @@ inline static void rrdr_free(RRDR *r)
        }
 
        rrdr_unlock_rrdset(r);
-       if(likely(r->t)) free(r->t);
-       if(likely(r->v)) free(r->v);
-       if(likely(r->o)) free(r->o);
-       if(likely(r->od)) free(r->od);
-       free(r);
+       freez(r->t);
+       freez(r->v);
+       freez(r->o);
+       freez(r->od);
+       freez(r);
 }
 
 inline void rrdr_done(RRDR *r)
@@ -1170,9 +1170,7 @@ static RRDR *rrdr_create(RRDSET *st, long n)
                return NULL;
        }
 
-       RRDR *r = calloc(1, sizeof(RRDR));
-       if(unlikely(!r)) goto cleanup;
-
+       RRDR *r = callocz(1, sizeof(RRDR));
        r->st = st;
 
        rrdr_lock_rrdset(r);
@@ -1182,17 +1180,10 @@ static RRDR *rrdr_create(RRDSET *st, long n)
 
        r->n = n;
 
-       r->t = malloc(n * sizeof(time_t));
-       if(unlikely(!r->t)) goto cleanup;
-
-       r->v = malloc(n * r->d * sizeof(calculated_number));
-       if(unlikely(!r->v)) goto cleanup;
-
-       r->o = malloc(n * r->d * sizeof(uint8_t));
-       if(unlikely(!r->o)) goto cleanup;
-
-       r->od = malloc(r->d * sizeof(uint8_t));
-       if(unlikely(!r->od)) goto cleanup;
+       r->t = mallocz(n * sizeof(time_t));
+       r->v = mallocz(n * r->d * sizeof(calculated_number));
+       r->o = mallocz(n * r->d * sizeof(uint8_t));
+       r->od = mallocz(r->d * sizeof(uint8_t));
 
        // set the hidden flag on hidden dimensions
        int c;
@@ -1202,16 +1193,10 @@ static RRDR *rrdr_create(RRDSET *st, long n)
        }
 
        r->c = -1;
-
        r->group = 1;
        r->update_every = 1;
 
        return r;
-
-cleanup:
-       error("Cannot allocate RRDR memory for %ld entries", n);
-       if(likely(r)) rrdr_free(r);
-       return NULL;
 }
 
 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
index 8c83190b18e5b21fc88bb86ef3eb13427f6290b6..522aaf1744a7c541f3369a44fea19c225f7e55c7 100644 (file)
@@ -261,13 +261,9 @@ void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
                }
 
                if(i != ca->cpus) {
-                       free(ca->cpu_percpu);
-
-                       ca->cpu_percpu = malloc(sizeof(unsigned long long) * i);
-                       if(!ca->cpu_percpu)
-                               fatal("Cannot allocate memory (%zu bytes)", sizeof(unsigned long long) * i);
-
-                       ca->cpus = i;
+                       freez(ca->cpu_percpu);
+                       ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
+                       ca->cpus = (unsigned int)i;
                }
 
                for(i = 0; i < ca->cpus ;i++) {
@@ -612,17 +608,12 @@ void cgroup_get_chart_id(struct cgroup *cg) {
 
                trim(s);
 
-               free(cg->chart_title);
-               cg->chart_title = strdup(s);
-               if(!cg->chart_title)
-                       fatal("CGROUP: Cannot allocate memory for chart name of cgroup '%s' chart name: '%s'", cg->id, s);
-
+               freez(cg->chart_title);
+               cg->chart_title = strdupz(s);
                netdata_fix_chart_name(cg->chart_title);
 
-               free(cg->chart_id);
-               cg->chart_id = strdup(s);
-               if(!cg->chart_id)
-                       fatal("CGROUP: Cannot allocate memory for chart id of cgroup '%s' chart id: '%s'", cg->id, s);
+               freez(cg->chart_id);
+               cg->chart_id = strdupz(s);
 
                netdata_fix_chart_id(cg->chart_id);
 
@@ -683,21 +674,15 @@ struct cgroup *cgroup_add(const char *id) {
                }
        }
 
-       struct cgroup *cg = calloc(1, sizeof(struct cgroup));
-       if(!cg) fatal("Cannot allocate memory for cgroup '%s'", id);
+       struct cgroup *cg = callocz(1, sizeof(struct cgroup));
 
        debug(D_CGROUP, "adding cgroup '%s'", id);
 
-       cg->id = strdup(id);
-       if(!cg->id) fatal("Cannot allocate memory for cgroup '%s'", id);
-
+       cg->id = strdupz(id);
        cg->hash = simple_hash(cg->id);
 
-       cg->chart_id = strdup(chart_id);
-       if(!cg->chart_id) fatal("Cannot allocate memory for cgroup '%s'", id);
-
-       cg->chart_title = strdup(chart_id);
-       if(!cg->chart_title) fatal("Cannot allocate memory for cgroup '%s'", id);
+       cg->chart_id = strdupz(chart_id);
+       cg->chart_title = strdupz(chart_id);
 
        if(!cgroup_root)
                cgroup_root = cg;
@@ -725,22 +710,22 @@ struct cgroup *cgroup_add(const char *id) {
 void cgroup_free(struct cgroup *cg) {
        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");
 
-       free(cg->cpuacct_usage.cpu_percpu);
+       freez(cg->cpuacct_usage.cpu_percpu);
 
-       free(cg->cpuacct_stat.filename);
-       free(cg->cpuacct_usage.filename);
-       free(cg->memory.filename);
-       free(cg->io_service_bytes.filename);
-       free(cg->io_serviced.filename);
-       free(cg->throttle_io_service_bytes.filename);
-       free(cg->throttle_io_serviced.filename);
-       free(cg->io_merged.filename);
-       free(cg->io_queued.filename);
+       freez(cg->cpuacct_stat.filename);
+       freez(cg->cpuacct_usage.filename);
+       freez(cg->memory.filename);
+       freez(cg->io_service_bytes.filename);
+       freez(cg->io_serviced.filename);
+       freez(cg->throttle_io_service_bytes.filename);
+       freez(cg->throttle_io_serviced.filename);
+       freez(cg->io_merged.filename);
+       freez(cg->io_queued.filename);
 
-       free(cg->id);
-       free(cg->chart_id);
-       free(cg->chart_title);
-       free(cg);
+       freez(cg->id);
+       freez(cg->chart_id);
+       freez(cg->chart_title);
+       freez(cg);
 
        cgroup_root_count--;
 }
@@ -833,15 +818,12 @@ void find_dir_in_subdirs(const char *base, const char *this, void (*callback)(co
                        }
 
                        if(enabled) {
-                               char *s = malloc(dirlen + strlen(de->d_name) + 2);
-                               if(s) {
-                                       strcpy(s, this);
-                                       strcat(s, "/");
-                                       strcat(s, de->d_name);
-                                       find_dir_in_subdirs(base, s, callback);
-                                       free(s);
-                               }
-                               else error("Cannot allocate memory.");
+                               char *s = mallocz(dirlen + strlen(de->d_name) + 2);
+                strcpy(s, this);
+                strcat(s, "/");
+                strcat(s, de->d_name);
+                find_dir_in_subdirs(base, s, callback);
+                freez(s);
                        }
                }
        }
@@ -920,7 +902,7 @@ void find_all_cgroups() {
                if(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename) {
                        snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
                        if(stat(filename, &buf) != -1) {
-                               cg->cpuacct_stat.filename = strdup(filename);
+                               cg->cpuacct_stat.filename = strdupz(filename);
                                debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
                        }
                        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() {
                if(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename) {
                        snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
                        if(stat(filename, &buf) != -1) {
-                               cg->cpuacct_usage.filename = strdup(filename);
+                               cg->cpuacct_usage.filename = strdupz(filename);
                                debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
                        }
                        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() {
                if(cgroup_enable_memory && !cg->memory.filename) {
                        snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
                        if(stat(filename, &buf) != -1) {
-                               cg->memory.filename = strdup(filename);
+                               cg->memory.filename = strdupz(filename);
                                debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
                        }
                        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() {
                        if(!cg->io_service_bytes.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->io_service_bytes.filename = strdup(filename);
+                                       cg->io_service_bytes.filename = strdupz(filename);
                                        debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
                                }
                                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() {
                        if(!cg->io_serviced.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->io_serviced.filename = strdup(filename);
+                                       cg->io_serviced.filename = strdupz(filename);
                                        debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
                                }
                                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() {
                        if(!cg->throttle_io_service_bytes.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->throttle_io_service_bytes.filename = strdup(filename);
+                                       cg->throttle_io_service_bytes.filename = strdupz(filename);
                                        debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
                                }
                                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() {
                        if(!cg->throttle_io_serviced.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->throttle_io_serviced.filename = strdup(filename);
+                                       cg->throttle_io_serviced.filename = strdupz(filename);
                                        debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
                                }
                                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() {
                        if(!cg->io_merged.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->io_merged.filename = strdup(filename);
+                                       cg->io_merged.filename = strdupz(filename);
                                        debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
                                }
                                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() {
                        if(!cg->io_queued.filename) {
                                snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
                                if(stat(filename, &buf) != -1) {
-                                       cg->io_queued.filename = strdup(filename);
+                                       cg->io_queued.filename = strdupz(filename);
                                        debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
                                }
                                else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
index 26d53cb81167f5ab32a689a1a3ac6077f5ebd192..44c5a6dc9cc166406e06a7dcd8fe6a742e2dc945 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -20,10 +20,7 @@ char to_hex(char code) {
 char *url_encode(char *str) {
        char *buf, *pbuf;
 
-       pbuf = buf = malloc(strlen(str) * 3 + 1);
-
-       if(!buf)
-               fatal("Cannot allocate memory.");
+       pbuf = buf = mallocz(strlen(str) * 3 + 1);
 
        while (*str) {
                if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
@@ -39,16 +36,9 @@ char *url_encode(char *str) {
        }
        *pbuf = '\0';
 
-       // FIX: I think this is prudent. URLs can be as long as 2 KiB or more.
-       //      We allocated 3 times more space to accomodate %NN encoding of
-       //      non ASCII chars. If URL has none of these kind of chars we will
-       //      end up with a big unused buffer.
-       //
-       //      Try to shrink the buffer...
-       if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1)))
-               buf = pbuf;
-
-       return buf;
+       pbuf = strdupz(buf);
+       freez(buf);
+       return pbuf;
 }
 
 /* Returns a url-decoded version of str */
@@ -56,10 +46,7 @@ char *url_encode(char *str) {
 char *url_decode(char *str) {
        size_t size = strlen(str) + 1;
 
-       char *buf = malloc(size);
-       if(!buf)
-               fatal("Cannot allocate %zu bytes of memory.", size);
-
+       char *buf = mallocz(size);
        return url_decode_r(buf, str, size);
 }
 
index a7c16ec5fe2335cd7dc2c97bdcc8ff1b29b4b830..c4aa2f8d9811e058da9fd5a6ba2e7fedd028f6b4 100644 (file)
@@ -323,18 +323,8 @@ BUFFER *buffer_create(size_t size)
 
        debug(D_WEB_BUFFER, "Creating new web buffer of size %zu.", size);
 
-       b = calloc(1, sizeof(BUFFER));
-       if(!b) {
-               error("Cannot allocate a web_buffer.");
-               return NULL;
-       }
-
-       b->buffer = malloc(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
-       if(!b->buffer) {
-               error("Cannot allocate a buffer of size %zu.", size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
-               free(b);
-               return NULL;
-       }
+       b = callocz(1, sizeof(BUFFER));
+       b->buffer = mallocz(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
        b->buffer[0] = '\0';
        b->size = size;
        b->contenttype = CT_TEXT_PLAIN;
@@ -350,8 +340,8 @@ void buffer_free(BUFFER *b)
 
        debug(D_WEB_BUFFER, "Freeing web buffer of size %zu.", b->size);
 
-       free(b->buffer);
-       free(b);
+       freez(b->buffer);
+       freez(b);
 }
 
 void buffer_increase(BUFFER *b, size_t free_size_required)
@@ -367,12 +357,7 @@ void buffer_increase(BUFFER *b, size_t free_size_required)
 
        debug(D_WEB_BUFFER, "Increasing data buffer from size %zu to %zu.", b->size, b->size + increase);
 
-       b->buffer = realloc(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
-       if(!b->buffer)
-               fatal("Failed to increase data buffer from size %zu to %zu.",
-                       b->size + sizeof(BUFFER_OVERFLOW_EOF) + 2,
-                       b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
-
+       b->buffer = reallocz(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
        b->size += increase;
 
        buffer_overflow_init(b);
index 91ae335d04598e51f55bca905ce7836fa3b4c0e6..721310de6434f04125f2f5c46be029c34282a65d 100644 (file)
@@ -48,12 +48,7 @@ struct web_client *web_client_create(int listener)
 {
        struct web_client *w;
 
-       w = calloc(1, sizeof(struct web_client));
-       if(!w) {
-               error("Cannot allocate new web_client memory.");
-               return NULL;
-       }
-
+       w = callocz(1, sizeof(struct web_client));
        w->id = ++web_clients_count;
        w->mode = WEB_CLIENT_MODE_NORMAL;
 
@@ -67,7 +62,7 @@ struct web_client *web_client_create(int listener)
                w->ifd = accept4(listener, sadr, &addrlen, SOCK_NONBLOCK);
                if (w->ifd == -1) {
                        error("%llu: Cannot accept new incoming connection.", w->id);
-                       free(w);
+                       freez(w);
                        return NULL;
                }
                w->ofd = w->ifd;
@@ -109,32 +104,8 @@ struct web_client *web_client_create(int listener)
        }
 
        w->response.data = buffer_create(INITIAL_WEB_DATA_LENGTH);
-       if(unlikely(!w->response.data)) {
-               // no need for error log - web_buffer_create already logged the error
-               close(w->ifd);
-               free(w);
-               return NULL;
-       }
-
        w->response.header = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
-       if(unlikely(!w->response.header)) {
-               // no need for error log - web_buffer_create already logged the error
-               buffer_free(w->response.data);
-               close(w->ifd);
-               free(w);
-               return NULL;
-       }
-
        w->response.header_output = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
-       if(unlikely(!w->response.header_output)) {
-               // no need for error log - web_buffer_create already logged the error
-               buffer_free(w->response.header);
-               buffer_free(w->response.data);
-               close(w->ifd);
-               free(w);
-               return NULL;
-       }
-
        w->origin[0] = '*';
        w->wait_receive = 1;
 
@@ -263,7 +234,7 @@ struct web_client *web_client_free(struct web_client *w) {
        if(w->response.data) buffer_free(w->response.data);
        if(w->ifd != -1) close(w->ifd);
        if(w->ofd != -1 && w->ofd != w->ifd) close(w->ofd);
-       free(w);
+       freez(w);
 
        global_statistics.connected_clients--;
 
@@ -700,10 +671,8 @@ int web_client_api_v1_badge(struct web_client *w, char *url) {
                        if(!dimensions)
                                dimensions = buffer_create(100);
 
-                       if(dimensions) {
-                               buffer_strcat(dimensions, "|");
-                               buffer_strcat(dimensions, value);
-                       }
+            buffer_strcat(dimensions, "|");
+            buffer_strcat(dimensions, value);
                }
                else if(!strcmp(name, "after")) after_str = value;
                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)
                if(!strcmp(name, "chart")) chart = value;
                else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
                        if(!dimensions) dimensions = buffer_create(100);
-                       if(dimensions) {
-                               buffer_strcat(dimensions, "|");
-                               buffer_strcat(dimensions, value);
-                       }
+            buffer_strcat(dimensions, "|");
+            buffer_strcat(dimensions, value);
                }
                else if(!strcmp(name, "after")) after_str = value;
                else if(!strcmp(name, "before")) before_str = value;
index 9ef011e549d93d26f36241a587b5745bf1943f4a..4bd3f91edc9dd13281e52a9e6d338b020008ab52 100644 (file)
@@ -1,7 +1,7 @@
 #include "common.h"
 
 int listen_backlog = LISTEN_BACKLOG;
-int listen_fds_count = 0;
+size_t listen_fds_count = 0;
 int listen_fds[MAX_LISTEN_FDS] = { [0 ... 99] = -1 };
 char *listen_fds_names[MAX_LISTEN_FDS] = { [0 ... 99] = NULL };
 int listen_port = LISTEN_PORT;
@@ -165,14 +165,14 @@ static inline int add_listen_socket(int fd, const char *ip, int port) {
 
     char buffer[100 + 1];
     snprintfz(buffer, 100, "[%s]:%d", ip, port);
-    listen_fds_names[listen_fds_count] = strdup(buffer);
+    listen_fds_names[listen_fds_count] = strdupz(buffer);
 
     listen_fds_count++;
     return 0;
 }
 
 int is_listen_socket(int fd) {
-    int i;
+    size_t i;
     for(i = 0; i < listen_fds_count ;i++)
         if(listen_fds[i] == fd) return 1;
 
@@ -180,12 +180,12 @@ int is_listen_socket(int fd) {
 }
 
 static inline void close_listen_sockets(void) {
-    int i;
+    size_t i;
     for(i = 0; i < listen_fds_count ;i++) {
         close(listen_fds[i]);
         listen_fds[i] = -1;
 
-        if(listen_fds_names[i]) free(listen_fds_names[i]);
+        freez(listen_fds_names[i]);
         listen_fds_names[i] = NULL;
     }
 
@@ -319,7 +319,7 @@ int create_listen_sockets(void) {
     if(!listen_fds_count)
         fatal("Cannot listen on any socket. Exiting...");
 
-       return listen_fds_count;
+       return (int)listen_fds_count;
 }
 
 // --------------------------------------------------------------------------------------
@@ -367,11 +367,9 @@ void *socket_listen_main_multi_threaded(void *ptr) {
        if(!listen_fds_count)
                fatal("LISTENER: No sockets to listen to.");
 
-       struct pollfd *fds = calloc(sizeof(struct pollfd), listen_fds_count);
-       if(!fds)
-               fatal("LISTENER: Cannot allocate memory for poll fds.");
+       struct pollfd *fds = callocz(sizeof(struct pollfd), listen_fds_count);
 
-       int i;
+       size_t i;
        for(i = 0; i < listen_fds_count ;i++) {
                fds[i].fd = listen_fds[i];
                fds[i].events = POLLIN;
@@ -489,20 +487,17 @@ void *socket_listen_main_single_threaded(void *ptr) {
        struct web_client *w;
        int retval;
 
-       if(ptr) { ; }
-
        if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
                error("Cannot set pthread cancel type to DEFERRED.");
 
        if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
                error("Cannot set pthread cancel state to ENABLE.");
 
-       int i;
-
        if(!listen_fds_count)
                fatal("LISTENER: no listen sockets available.");
 
-       for(i = 0; i < FD_SETSIZE ; i++)
+    size_t i;
+    for(i = 0; i < FD_SETSIZE ; i++)
                single_threaded_clients[i] = NULL;
 
        fd_set ifds, ofds, efds, rifds, rofds, refds;
@@ -549,7 +544,7 @@ void *socket_listen_main_single_threaded(void *ptr) {
                                }
                        }
 
-                       for(i = 0 ; i <= fdmax ; i++) {
+                       for(i = 0 ; i <= (size_t)fdmax ; i++) {
                                if(likely(!FD_ISSET(i, &rifds) && !FD_ISSET(i, &rofds) && !FD_ISSET(i, &refds)))
                                        continue;
 
index a005fd45f4034f5545c92ba1973b8f039aec2ad5..f8832b674372411dfa8f07df24d41a82aece86ec 100644 (file)
@@ -1431,7 +1431,7 @@ var chartData = {
        },
 
        'system.load': {
-               info: 'Current system load read from <code>/proc/loadavg</code>.',
+               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>',
                height: 0.7
        },