Get rid of malloc around layer names

This commit is contained in:
Eric Fischer
2016-04-28 12:20:51 -07:00
parent 3f0904cce8
commit 444de1f086
+33 -89
View File
@@ -21,6 +21,7 @@
#include <pthread.h> #include <pthread.h>
#include <getopt.h> #include <getopt.h>
#include <vector> #include <vector>
#include <string>
#ifdef __APPLE__ #ifdef __APPLE__
#include <sys/types.h> #include <sys/types.h>
@@ -58,27 +59,10 @@ static int prevent[256];
static int additional[256]; static int additional[256];
struct source { struct source {
char *layer; std::string layer;
char *file; std::string file;
struct source *next;
}; };
struct tofree {
void *p;
struct tofree *next;
} *tofree = NULL;
void mustfree(void *p) {
struct tofree *f = (struct tofree *) malloc(sizeof(struct tofree));
if (f == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
f->p = p;
f->next = tofree;
tofree = f;
}
int CPUS; int CPUS;
int TEMP_FILES; int TEMP_FILES;
long long MAX_FILES; long long MAX_FILES;
@@ -858,7 +842,7 @@ void radix(struct reader *reader, int nreaders, FILE *geomfile, int geomfd, FILE
} }
} }
int read_input(int argc, struct source **sourcelist, char *fname, const char *layername, int maxzoom, int minzoom, int basezoom, double basezoom_marker_width, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, const char *tmpdir, double gamma, int *prevent, int *additional, int read_parallel, int forcetable, const char *attribution) { int read_input(std::vector<source> &sources, char *fname, const char *layername, int maxzoom, int minzoom, int basezoom, double basezoom_marker_width, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, const char *tmpdir, double gamma, int *prevent, int *additional, int read_parallel, int forcetable, const char *attribution) {
int ret = EXIT_SUCCESS; int ret = EXIT_SUCCESS;
struct reader reader[CPUS]; struct reader reader[CPUS];
@@ -970,13 +954,13 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
if (layername != NULL) { if (layername != NULL) {
nlayers = 1; nlayers = 1;
} else { } else {
nlayers = argc; nlayers = sources.size();
if (nlayers == 0) { if (nlayers == 0) {
nlayers = 1; nlayers = 1;
} }
} }
int nsources = argc; int nsources = sources.size();
if (nsources == 0) { if (nsources == 0) {
nsources = 1; nsources = 1;
} }
@@ -985,17 +969,17 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
int source; int source;
for (source = 0; source < nsources; source++) { for (source = 0; source < nsources; source++) {
const char *reading; std::string reading;
int fd; int fd;
if (source >= argc) { if (source >= sources.size()) {
reading = "standard input"; reading = "standard input";
fd = 0; fd = 0;
} else { } else {
reading = sourcelist[source]->file; reading = sources[source].file;
fd = open(sourcelist[source]->file, O_RDONLY); fd = open(sources[source].file.c_str(), O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror(sourcelist[source]->file); perror(sources[source].file.c_str());
continue; continue;
} }
} }
@@ -1018,7 +1002,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
} }
if (map != NULL && map != MAP_FAILED) { if (map != NULL && map != MAP_FAILED) {
do_read_parallel(map, st.st_size - off, overall_offset, reading, reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y); do_read_parallel(map, st.st_size - off, overall_offset, reading.c_str(), reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y);
overall_offset += st.st_size - off; overall_offset += st.st_size - off;
checkdisk(reader, CPUS); checkdisk(reader, CPUS);
@@ -1029,7 +1013,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
} else { } else {
FILE *fp = fdopen(fd, "r"); FILE *fp = fdopen(fd, "r");
if (fp == NULL) { if (fp == NULL) {
perror(sourcelist[source]->file); perror(sources[source].file.c_str());
if (close(fd) != 0) { if (close(fd) != 0) {
perror("close source file"); perror("close source file");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -1067,7 +1051,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
int n; int n;
while ((n = fread(buf, sizeof(char), READ_BUF, fp)) > 0) { while ((n = fread(buf, sizeof(char), READ_BUF, fp)) > 0) {
fwrite_check(buf, sizeof(char), n, readfp, reading); fwrite_check(buf, sizeof(char), n, readfp, reading.c_str());
ahead += n; ahead += n;
if (buf[n - 1] == '\n' && ahead > PARSE_MIN) { if (buf[n - 1] == '\n' && ahead > PARSE_MIN) {
@@ -1084,7 +1068,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
} }
fflush(readfp); fflush(readfp);
start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, &parallel_parser, reading, reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y); start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, &parallel_parser, reading.c_str(), reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y);
initial_offset += ahead; initial_offset += ahead;
overall_offset += ahead; overall_offset += ahead;
@@ -1107,7 +1091,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
} }
} }
if (n < 0) { if (n < 0) {
perror(reading); perror(reading.c_str());
} }
if (initial_offset != 0) { if (initial_offset != 0) {
@@ -1120,7 +1104,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
fflush(readfp); fflush(readfp);
if (ahead > 0) { if (ahead > 0) {
start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, &parallel_parser, reading, reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y); start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, &parallel_parser, reading.c_str(), reader, &progress_seq, exclude, include, exclude_all, fname, basezoom, source, nlayers, droprate, initialized, initial_x, initial_y);
if (pthread_join(parallel_parser, NULL) != 0) { if (pthread_join(parallel_parser, NULL) != 0) {
perror("pthread_join"); perror("pthread_join");
@@ -1134,7 +1118,7 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
long long layer_seq = overall_offset; long long layer_seq = overall_offset;
json_pull *jp = json_begin_file(fp); json_pull *jp = json_begin_file(fp);
parse_json(jp, reading, &layer_seq, &progress_seq, &reader[0].metapos, &reader[0].geompos, &reader[0].indexpos, exclude, include, exclude_all, reader[0].metafile, reader[0].geomfile, reader[0].indexfile, reader[0].poolfile, reader[0].treefile, fname, basezoom, source < nlayers ? source : 0, droprate, reader[0].file_bbox, 0, &initialized[0], &initial_x[0], &initial_y[0], reader); parse_json(jp, reading.c_str(), &layer_seq, &progress_seq, &reader[0].metapos, &reader[0].geompos, &reader[0].indexpos, exclude, include, exclude_all, reader[0].metafile, reader[0].geomfile, reader[0].indexfile, reader[0].poolfile, reader[0].treefile, fname, basezoom, source < nlayers ? source : 0, droprate, reader[0].file_bbox, 0, &initialized[0], &initial_x[0], &initial_y[0], reader);
json_end(jp); json_end(jp);
overall_offset = layer_seq; overall_offset = layer_seq;
checkdisk(reader, CPUS); checkdisk(reader, CPUS);
@@ -1193,13 +1177,13 @@ int read_input(int argc, struct source **sourcelist, char *fname, const char *la
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {
char *src; const char *src;
if (argc < 1) { if (sources.size() < 1) {
src = fname; src = fname;
} else if (sourcelist[i]->layer != NULL) { } else if (sources[i].layer.size() != 0) {
src = sourcelist[i]->layer; src = sources[i].layer.c_str();
} else { } else {
src = sourcelist[i]->file; src = sources[i].file.c_str();
} }
char *trunc = layernames[i] = (char *) malloc(strlen(src) + 1); char *trunc = layernames[i] = (char *) malloc(strlen(src) + 1);
@@ -1744,9 +1728,7 @@ int main(int argc, char **argv) {
int buffer = 5; int buffer = 5;
const char *tmpdir = "/tmp"; const char *tmpdir = "/tmp";
const char *attribution = NULL; const char *attribution = NULL;
std::vector<source> sources;
int nsources = 0;
struct source *sources = NULL;
struct pool exclude, include; struct pool exclude, include;
pool_init(&exclude, 0); pool_init(&exclude, 0);
@@ -1855,24 +1837,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "%s: -L requires layername:file\n", argv[0]); fprintf(stderr, "%s: -L requires layername:file\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct source *src = (struct source *) malloc(sizeof(struct source)); struct source src;
if (src == NULL) { src.layer = std::string(optarg).substr(0, cp - optarg);
perror("Out of memory"); src.file = std::string(cp + 1);
exit(EXIT_FAILURE); sources.push_back(src);
}
src->layer = strdup(optarg);
src->file = strdup(cp + 1);
if (src->layer == NULL || src->file == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
mustfree(src->layer);
mustfree(src->file);
src->layer[cp - optarg] = '\0';
src->next = sources;
sources = src;
nsources++;
} break; } break;
case 'z': case 'z':
@@ -2100,26 +2068,13 @@ int main(int argc, char **argv) {
int ret = EXIT_SUCCESS; int ret = EXIT_SUCCESS;
for (i = optind; i < argc; i++) { for (i = optind; i < argc; i++) {
struct source *src = (struct source *) malloc(sizeof(struct source)); struct source src;
if (src == NULL) { src.layer = "";
perror("Out of memory"); src.file = std::string(argv[i]);
exit(EXIT_FAILURE); sources.push_back(src);
}
src->layer = NULL;
src->file = argv[i];
src->next = sources;
sources = src;
nsources++;
} }
struct source *sourcelist[nsources]; ret = read_input(sources, name ? name : outdir, layer, maxzoom, minzoom, basezoom, basezoom_marker_width, outdb, &exclude, &include, exclude_all, droprate, buffer, tmpdir, gamma, prevent, additional, read_parallel, forcetable, attribution);
i = nsources - 1;
for (; sources != NULL; sources = sources->next) {
sourcelist[i--] = sources;
}
ret = read_input(nsources, sourcelist, name ? name : outdir, layer, maxzoom, minzoom, basezoom, basezoom_marker_width, outdb, &exclude, &include, exclude_all, droprate, buffer, tmpdir, gamma, prevent, additional, read_parallel, forcetable, attribution);
mbtiles_close(outdb, argv); mbtiles_close(outdb, argv);
@@ -2134,19 +2089,8 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (i = 0; i < nsources; i++) {
free(sourcelist[i]);
}
pool_free(&exclude); pool_free(&exclude);
pool_free(&include); pool_free(&include);
struct tofree *tf, *next;
for (tf = tofree; tf != NULL; tf = next) {
next = tf->next;
free(tf->p);
free(tf);
}
return ret; return ret;
} }