Skip to content

Commit

Permalink
Replace malloc/free with new/delete to fix parallel-reading crash
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Aug 30, 2016
1 parent 31d2a37 commit d4e1ee0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
13 changes: 9 additions & 4 deletions geojson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void json_context(json_object *j) {
}

fprintf(stderr, "In JSON object %s\n", s);
free(s);
free(s); // stringify
}

long long parse_geometry(int t, json_object *j, long long *bbox, drawvec &out, int op, const char *fname, int line, int *initialized, unsigned *initial_x, unsigned *initial_y, json_object *feature) {
Expand Down Expand Up @@ -251,7 +251,7 @@ int serialize_geometry(json_object *geometry, json_object *properties, json_obje
} else {
char *s = json_stringify(id);
fprintf(stderr, "Warning: Can't represent non-numeric feature ID %s\n", s);
free(s);
free(s); // stringify
}
}

Expand Down Expand Up @@ -304,7 +304,7 @@ int serialize_geometry(json_object *geometry, json_object *properties, json_obje
tas.type = metatype[m] = VT_STRING;
const char *v = json_stringify(properties->values[i]);
metaval[m] = std::string(v);
free((void *) v);
free((void *) v); // stringify
m++;
}

Expand Down Expand Up @@ -622,7 +622,7 @@ ssize_t json_map_read(struct json_pull *jp, char *buffer, size_t n) {
}

struct json_pull *json_begin_map(char *map, long long len) {
struct jsonmap *jm = (struct jsonmap *) malloc(sizeof(struct jsonmap));
struct jsonmap *jm = new jsonmap;
if (jm == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
Expand All @@ -634,3 +634,8 @@ struct json_pull *json_begin_map(char *map, long long len) {

return json_begin(json_map_read, jm);
}

void json_end_map(struct json_pull *jp) {
delete (struct jsonmap *) jp->source;
json_end(jp);
}
2 changes: 2 additions & 0 deletions geojson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ struct parse_json_args {
};

struct json_pull *json_begin_map(char *map, long long len);
void json_end_map(struct json_pull *jp);

void parse_json(json_pull *jp, const char *reading, volatile long long *layer_seq, volatile long long *progress_seq, long long *metapos, long long *geompos, long long *indexpos, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, FILE *metafile, FILE *geomfile, FILE *indexfile, struct memfile *poolfile, struct memfile *treefile, char *fname, int basezoom, int layer, double droprate, long long *file_bbox, int segment, int *initialized, unsigned *initial_x, unsigned *initial_y, struct reader *readers, int maxzoom, std::map<std::string, layermap_entry> *layermap, std::string layername);
void *run_parse_json(void *v);
32 changes: 15 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ void do_read_parallel(char *map, long long len, long long initial_offset, const
perror("pthread_join 370");
}

free(pja[i].jp->source);
json_end(pja[i].jp);
json_end_map(pja[i].jp);
}
}

Expand Down Expand Up @@ -404,37 +403,37 @@ struct read_parallel_arg {
};

void *run_read_parallel(void *v) {
struct read_parallel_arg *a = (struct read_parallel_arg *) v;
struct read_parallel_arg *rpa = (struct read_parallel_arg *) v;

struct stat st;
if (fstat(a->fd, &st) != 0) {
if (fstat(rpa->fd, &st) != 0) {
perror("stat read temp");
}
if (a->len != st.st_size) {
fprintf(stderr, "wrong number of bytes in temporary: %lld vs %lld\n", a->len, (long long) st.st_size);
if (rpa->len != st.st_size) {
fprintf(stderr, "wrong number of bytes in temporary: %lld vs %lld\n", rpa->len, (long long) st.st_size);
}
a->len = st.st_size;
rpa->len = st.st_size;

char *map = (char *) mmap(NULL, a->len, PROT_READ, MAP_PRIVATE, a->fd, 0);
char *map = (char *) mmap(NULL, rpa->len, PROT_READ, MAP_PRIVATE, rpa->fd, 0);
if (map == NULL || map == MAP_FAILED) {
perror("map intermediate input");
exit(EXIT_FAILURE);
}
madvise(map, a->len, MADV_RANDOM); // sequential, but from several pointers at once
madvise(map, rpa->len, MADV_RANDOM); // sequential, but from several pointers at once

do_read_parallel(map, a->len, a->offset, a->reading, a->reader, a->progress_seq, a->exclude, a->include, a->exclude_all, a->fname, a->basezoom, a->source, a->nlayers, a->layermaps, a->droprate, a->initialized, a->initial_x, a->initial_y, a->maxzoom, a->layername);
do_read_parallel(map, rpa->len, rpa->offset, rpa->reading, rpa->reader, rpa->progress_seq, rpa->exclude, rpa->include, rpa->exclude_all, rpa->fname, rpa->basezoom, rpa->source, rpa->nlayers, rpa->layermaps, rpa->droprate, rpa->initialized, rpa->initial_x, rpa->initial_y, rpa->maxzoom, rpa->layername);

madvise(map, a->len, MADV_DONTNEED);
if (munmap(map, a->len) != 0) {
madvise(map, rpa->len, MADV_DONTNEED);
if (munmap(map, rpa->len) != 0) {
perror("munmap source file");
}
if (fclose(a->fp) != 0) {
if (fclose(rpa->fp) != 0) {
perror("close source file");
exit(EXIT_FAILURE);
}

*(a->is_parsing) = 0;
free(a);
*(rpa->is_parsing) = 0;
delete rpa;

return NULL;
}
Expand All @@ -446,7 +445,7 @@ void start_parsing(int fd, FILE *fp, long long offset, long long len, volatile i

*is_parsing = 1;

struct read_parallel_arg *rpa = (struct read_parallel_arg *) malloc(sizeof(struct read_parallel_arg));
struct read_parallel_arg *rpa = new struct read_parallel_arg;
if (rpa == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -1210,7 +1209,6 @@ int read_input(std::vector<source> &sources, char *fname, const char *layername,
fflush(readfp);

if (ahead > 0) {
printf("layer name %s\n", layernames[source < nlayers ? source : 0].c_str());
start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, &parallel_parser, parser_created, reading.c_str(), reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, layermaps, droprate, initialized, initial_x, initial_y, maxzoom, layernames[source < nlayers ? source : 0]);

if (parser_created) {
Expand Down
4 changes: 2 additions & 2 deletions memfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct memfile *memfile_open(int fd) {
return NULL;
}

struct memfile *mf = (struct memfile *) malloc(sizeof(struct memfile));
struct memfile *mf = new memfile;
if (mf == NULL) {
munmap(map, INCREMENT);
return NULL;
Expand All @@ -42,7 +42,7 @@ int memfile_close(struct memfile *file) {
}
}

free(file);
delete file;
return 0;
}

Expand Down

0 comments on commit d4e1ee0

Please sign in to comment.