fix calloc realloc failure

pull/3815/head
cyy 6 years ago
parent f10c84c218
commit 41144ba358
  1. 3
      src/classifier.c
  2. 3
      src/data.c
  3. 3
      src/detector.c
  4. 6
      src/gemm.c
  5. 23
      src/go.c
  6. 5
      src/list.c
  7. 1
      src/maxpool_layer.c
  8. 3
      src/network.c
  9. 1
      src/normalization_layer.c
  10. 4
      src/option_list.c
  11. 2
      src/parser.c
  12. 1
      src/reorg_layer.c
  13. 1
      src/reorg_old_layer.c
  14. 15
      src/rnn.c
  15. 1
      src/route_layer.c
  16. 1
      src/sam_layer.c
  17. 1
      src/scale_channels_layer.c
  18. 1
      src/shortcut_layer.c
  19. 1
      src/upsample_layer.c
  20. 17
      src/utils.c
  21. 1
      src/utils.h

@ -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;

@ -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;

@ -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;

@ -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;

@ -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;

@ -1,11 +1,12 @@
#include <stdlib.h>
#include <string.h>
#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;

@ -1,5 +1,6 @@
#include "maxpool_layer.h"
#include "dark_cuda.h"
#include "utils.h"
#include "gemm.h"
#include <stdio.h>

@ -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);
}

@ -1,5 +1,6 @@
#include "normalization_layer.h"
#include "blas.h"
#include "utils.h"
#include <stdio.h>
layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa)

@ -48,7 +48,9 @@ metadata get_metadata(char *file)
}
m.classes = option_find_int(options, "classes", 2);
free_list(options);
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;

@ -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;

@ -1,6 +1,7 @@
#include "reorg_layer.h"
#include "dark_cuda.h"
#include "blas.h"
#include "utils.h"
#include <stdio.h>

@ -1,4 +1,5 @@
#include "reorg_old_layer.h"
#include "utils.h"
#include "dark_cuda.h"
#include "blas.h"
#include <stdio.h>

@ -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;
}

@ -1,4 +1,5 @@
#include "route_layer.h"
#include "utils.h"
#include "dark_cuda.h"
#include "blas.h"
#include <stdio.h>

@ -1,4 +1,5 @@
#include "sam_layer.h"
#include "utils.h"
#include "dark_cuda.h"
#include "blas.h"
#include <stdio.h>

@ -1,4 +1,5 @@
#include "scale_channels_layer.h"
#include "utils.h"
#include "dark_cuda.h"
#include "blas.h"
#include <stdio.h>

@ -1,6 +1,7 @@
#include "shortcut_layer.h"
#include "dark_cuda.h"
#include "blas.h"
#include "utils.h"
#include <stdio.h>
#include <assert.h>

@ -1,5 +1,6 @@
#include "upsample_layer.h"
#include "dark_cuda.h"
#include "utils.h"
#include "blas.h"
#include <stdio.h>

@ -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;
}

@ -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();

Loading…
Cancel
Save