diff --git a/src/classifier.c b/src/classifier.c index d75b320e..e0cc0b34 100644 --- a/src/classifier.c +++ b/src/classifier.c @@ -782,9 +782,6 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi char **names = get_labels(name_list); clock_t time; int* indexes = (int*)xcalloc(top, sizeof(int)); - if(!indexes) { - error("calloc failed"); - } char buff[256]; char *input = buff; //int size = net.w; diff --git a/src/data.c b/src/data.c index 4e265e3e..abe1d98e 100644 --- a/src/data.c +++ b/src/data.c @@ -193,9 +193,6 @@ box_label *read_boxes(char *filename, int *n) int count = 0; while(fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5){ boxes = (box_label*)xrealloc(boxes, (count + 1) * sizeof(box_label)); - if(!boxes) { - error("realloc failed"); - } boxes[count].id = id; boxes[count].x = x; boxes[count].y = y; diff --git a/src/detector.c b/src/detector.c index c48b6449..7d96ab85 100644 --- a/src/detector.c +++ b/src/detector.c @@ -835,9 +835,6 @@ float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, floa if (prob > 0) { detections_count++; detections = (box_prob*)xrealloc(detections, detections_count * sizeof(box_prob)); - if (!detections) { - error("realloc failed"); - } detections[detections_count - 1].b = dets[i].bbox; detections[detections_count - 1].p = prob; detections[detections_count - 1].image_index = image_index; diff --git a/src/gemm.c b/src/gemm.c index 1fec9322..e0295cb2 100644 --- a/src/gemm.c +++ b/src/gemm.c @@ -154,7 +154,7 @@ void gemm_nn_custom_bin_mean(int M, int N, int K, float ALPHA_UNUSED, unsigned char *B, int ldb, float *C, int ldc, float *mean_arr) { - int *count_arr = calloc(M*N, sizeof(int)); + int *count_arr = xcalloc(M*N, sizeof(int)); int i, j, k; for (i = 0; i < M; ++i) { // l.n - filters [16 - 55 - 1024] @@ -184,7 +184,7 @@ void gemm_nn_custom_bin_mean_transposed(int M, int N, int K, float ALPHA_UNUSED, unsigned char *B, int ldb, float *C, int ldc, float *mean_arr) { - int *count_arr = calloc(M*N, sizeof(int)); + int *count_arr = xcalloc(M*N, sizeof(int)); int i, j, k; for (i = 0; i < M; ++i) { // l.n - filters [16 - 55 - 1024] @@ -213,7 +213,7 @@ void gemm_nn_custom_bin_mean(int M, int N, int K, float ALPHA_UNUSED, unsigned char *B, int ldb, float *C, int ldc, float *mean_arr) { - int *count_arr = calloc(M*N, sizeof(int)); + int *count_arr = xcalloc(M*N, sizeof(int)); int i; diff --git a/src/go.c b/src/go.c index 92f2f061..4b01045e 100644 --- a/src/go.c +++ b/src/go.c @@ -19,7 +19,7 @@ char *fgetgo(FILE *fp) { if(feof(fp)) return 0; size_t size = 94; - char* line = (char*)malloc(size * sizeof(char)); + char* line = (char*)xmalloc(size * sizeof(char)); if(size != fread(line, sizeof(char), size, fp)){ free(line); return 0; @@ -128,13 +128,7 @@ void train_go(char *cfgfile, char *weightfile) char buff[256]; float* board = (float*)xcalloc(19 * 19 * net.batch, sizeof(float)); - if(!board) { - error("calloc failed"); - } float* move = (float*)xcalloc(19 * 19 * net.batch, sizeof(float)); - if(!move) { - error("calloc failed"); - } moves m = load_go_moves("backup/go.train"); //moves m = load_go_moves("games.txt"); @@ -417,13 +411,7 @@ void valid_go(char *cfgfile, char *weightfile, int multi) printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); float* board = (float*)xcalloc(19 * 19, sizeof(float)); - if(!board) { - error("calloc failed"); - } float* move = (float*)xcalloc(19 * 19, sizeof(float)); - if(!move) { - error("calloc failed"); - } moves m = load_go_moves("backup/go.test"); int N = m.n; @@ -778,17 +766,8 @@ void self_go(char *filename, char *weightfile, char *f2, char *w2, int multi) set_batch_network(&net, 1); set_batch_network(&net2, 1); float* board = (float*)xcalloc(19 * 19, sizeof(float)); - if(!board) { - error("calloc failed"); - } char* one = (char*)xcalloc(91, sizeof(char)); - if(!one) { - error("calloc failed"); - } char* two = (char*)xcalloc(91, sizeof(char)); - if(!two) { - error("calloc failed"); - } int done = 0; int player = 1; int p1 = 0; diff --git a/src/list.c b/src/list.c index 580826d6..4c24e477 100644 --- a/src/list.c +++ b/src/list.c @@ -1,11 +1,12 @@ #include #include #include "list.h" +#include "utils.h" #include "option_list.h" list *make_list() { - list* l = (list*)malloc(sizeof(list)); + list* l = (list*)xmalloc(sizeof(list)); l->size = 0; l->front = 0; l->back = 0; @@ -40,7 +41,7 @@ void *list_pop(list *l){ void list_insert(list *l, void *val) { - node* newnode = (node*)malloc(sizeof(node)); + node* newnode = (node*)xmalloc(sizeof(node)); newnode->val = val; newnode->next = 0; diff --git a/src/maxpool_layer.c b/src/maxpool_layer.c index 797123f6..b0ea1957 100644 --- a/src/maxpool_layer.c +++ b/src/maxpool_layer.c @@ -1,5 +1,6 @@ #include "maxpool_layer.h" #include "dark_cuda.h" +#include "utils.h" #include "gemm.h" #include diff --git a/src/network.c b/src/network.c index 403c1ca0..886dd118 100644 --- a/src/network.c +++ b/src/network.c @@ -821,9 +821,6 @@ char *detection_to_json(detection *dets, int nboxes, int classes, char **names, int buf_len = strlen(buf); int total_len = send_buf_len + buf_len + 100; send_buf = (char *)xrealloc(send_buf, total_len * sizeof(char)); - if (!send_buf) { - error("realloc failed"); - } strcat(send_buf, buf); free(buf); } diff --git a/src/normalization_layer.c b/src/normalization_layer.c index d8a2ac58..d6af6212 100644 --- a/src/normalization_layer.c +++ b/src/normalization_layer.c @@ -1,5 +1,6 @@ #include "normalization_layer.h" #include "blas.h" +#include "utils.h" #include layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa) diff --git a/src/option_list.c b/src/option_list.c index e4fa7fe4..306f0e37 100644 --- a/src/option_list.c +++ b/src/option_list.c @@ -48,7 +48,9 @@ metadata get_metadata(char *file) } m.classes = option_find_int(options, "classes", 2); free_list(options); - printf("Loaded - names_list: %s, classes = %d \n", name_list, m.classes); + if(name_list) { + printf("Loaded - names_list: %s, classes = %d \n", name_list, m.classes); + } return m; } @@ -72,7 +74,7 @@ int read_option(char *s, list *options) void option_insert(list *l, char *key, char *val) { - kvp* p = (kvp*)malloc(sizeof(kvp)); + kvp* p = (kvp*)xmalloc(sizeof(kvp)); p->key = key; p->val = val; p->used = 0; diff --git a/src/parser.c b/src/parser.c index 3d3c3752..836a129d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1054,7 +1054,7 @@ list *read_cfg(char *filename) strip(line); switch(line[0]){ case '[': - current = (section*)malloc(sizeof(section)); + current = (section*)xmalloc(sizeof(section)); list_insert(sections, current); current->options = make_list(); current->type = line; diff --git a/src/reorg_layer.c b/src/reorg_layer.c index 10c11199..7a4c0aec 100644 --- a/src/reorg_layer.c +++ b/src/reorg_layer.c @@ -1,6 +1,7 @@ #include "reorg_layer.h" #include "dark_cuda.h" #include "blas.h" +#include "utils.h" #include diff --git a/src/reorg_old_layer.c b/src/reorg_old_layer.c index f6ad2a8b..cb715e6e 100644 --- a/src/reorg_old_layer.c +++ b/src/reorg_old_layer.c @@ -1,4 +1,5 @@ #include "reorg_old_layer.h" +#include "utils.h" #include "dark_cuda.h" #include "blas.h" #include diff --git a/src/rnn.c b/src/rnn.c index bfc57b70..6c9b38c8 100644 --- a/src/rnn.c +++ b/src/rnn.c @@ -15,9 +15,6 @@ int *read_tokenized_data(char *filename, size_t *read) size_t count = 0; FILE *fp = fopen(filename, "r"); int* d = (int*)xcalloc(size, sizeof(int)); - if(!d) { - error("calloc failed"); - } int n, one; one = fscanf(fp, "%d", &n); while(one == 1){ @@ -25,18 +22,12 @@ int *read_tokenized_data(char *filename, size_t *read) if(count > size){ size = size*2; d = (int*)xrealloc(d, size * sizeof(int)); - if(!d) { - error("realloc failed"); - } } d[count-1] = n; one = fscanf(fp, "%d", &n); } fclose(fp); d = (int*)xrealloc(d, count * sizeof(int)); - if(!d) { - error("realloc failed"); - } *read = count; return d; } @@ -53,17 +44,11 @@ char **read_tokens(char *filename, size_t *read) if(count > size){ size = size*2; d = (char**)xrealloc(d, size * sizeof(char*)); - if(!d) { - error("realloc failed"); - } } d[count-1] = line; } fclose(fp); d = (char**)xrealloc(d, count * sizeof(char*)); - if(!d) { - error("realloc failed"); - } *read = count; return d; } diff --git a/src/route_layer.c b/src/route_layer.c index a9d35fc3..9217c30e 100644 --- a/src/route_layer.c +++ b/src/route_layer.c @@ -1,4 +1,5 @@ #include "route_layer.h" +#include "utils.h" #include "dark_cuda.h" #include "blas.h" #include diff --git a/src/sam_layer.c b/src/sam_layer.c index fbb1aadb..a2ba5486 100644 --- a/src/sam_layer.c +++ b/src/sam_layer.c @@ -1,4 +1,5 @@ #include "sam_layer.h" +#include "utils.h" #include "dark_cuda.h" #include "blas.h" #include diff --git a/src/scale_channels_layer.c b/src/scale_channels_layer.c index 847eeda4..c1f6af28 100644 --- a/src/scale_channels_layer.c +++ b/src/scale_channels_layer.c @@ -1,4 +1,5 @@ #include "scale_channels_layer.h" +#include "utils.h" #include "dark_cuda.h" #include "blas.h" #include diff --git a/src/shortcut_layer.c b/src/shortcut_layer.c index fdc70aed..37f1bcc4 100644 --- a/src/shortcut_layer.c +++ b/src/shortcut_layer.c @@ -1,6 +1,7 @@ #include "shortcut_layer.h" #include "dark_cuda.h" #include "blas.h" +#include "utils.h" #include #include diff --git a/src/upsample_layer.c b/src/upsample_layer.c index 7e756782..778f5b4d 100644 --- a/src/upsample_layer.c +++ b/src/upsample_layer.c @@ -1,5 +1,6 @@ #include "upsample_layer.h" #include "dark_cuda.h" +#include "utils.h" #include "blas.h" #include diff --git a/src/utils.c b/src/utils.c index 60c2f5a6..416682e3 100644 --- a/src/utils.c +++ b/src/utils.c @@ -23,6 +23,13 @@ #pragma warning(disable: 4996) #endif +void *xmalloc(size_t size) { + void *ptr=malloc(size); + if(!ptr) { + malloc_error(); + } + return ptr; +} void *xcalloc(size_t nmemb, size_t size) { void *ptr=calloc(nmemb,size); if(!ptr) { @@ -319,7 +326,7 @@ void error(const char *s) void malloc_error() { - fprintf(stderr, "Malloc error\n"); + fprintf(stderr, "xMalloc error\n"); exit(EXIT_FAILURE); } @@ -407,7 +414,7 @@ char *fgetl(FILE *fp) { if(feof(fp)) return 0; size_t size = 512; - char* line = (char*)malloc(size * sizeof(char)); + char* line = (char*)xmalloc(size * sizeof(char)); if(!fgets(line, size, fp)){ free(line); return 0; @@ -419,10 +426,6 @@ char *fgetl(FILE *fp) if(curr == size-1){ size *= 2; line = (char*)xrealloc(line, size * sizeof(char)); - if(!line) { - printf("%ld\n", size); - malloc_error(); - } } size_t readsize = size-curr; if(readsize > INT_MAX) readsize = INT_MAX-1; @@ -500,7 +503,7 @@ char *copy_string(char *s) if(!s) { return NULL; } - char* copy = (char*)malloc(strlen(s) + 1); + char* copy = (char*)xmalloc(strlen(s) + 1); strncpy(copy, s, strlen(s)+1); return copy; } diff --git a/src/utils.h b/src/utils.h index 05b6da21..a20b17d8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -13,6 +13,7 @@ extern "C" { LIB_API void free_ptrs(void **ptrs, int n); LIB_API void top_k(float *a, int n, int k, int *index); +void *xmalloc(size_t size); void *xcalloc(size_t nmemb, size_t size); void *xrealloc(void *ptr, size_t size); double what_time_is_it_now();