diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-06-20 17:06:14 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-06-20 21:06:02 +0530 |
commit | af3ee1af961d21f106cf6ccc9606c6043f13d977 (patch) | |
tree | 77edb966dc455af18c82d4c8908cfae32e66d5f5 | |
parent | f99142cb06352a81f874d66696d7e36a6c619ae8 (diff) |
jobs: use pointer arrays for outputs and dependencies
we still get simpler deletions because the order of the array is not important
so we can pop the last item and insert it in place of the deleted one
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | include/jobs.h | 15 | ||||
-rw-r--r-- | include/util.h | 7 | ||||
-rw-r--r-- | src/jobs.c | 89 |
4 files changed, 84 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf8ac42 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vgcore.* diff --git a/include/jobs.h b/include/jobs.h index 3a1fb19..d223ec2 100644 --- a/include/jobs.h +++ b/include/jobs.h @@ -3,23 +3,22 @@ #ifndef JOBS_H -LIST_HEAD(output_dlist, output); struct output { char *name, *store_path; - LIST_ENTRY(output) dlist; }; -LIST_HEAD(job_dlist, job); -CIRCLEQ_HEAD(job_clist, job); struct job { char *name, *drv_path; - struct output_dlist outputs; - struct job_dlist deps; - /* TODO: replace dlist with clist jobs.c */ - LIST_ENTRY(job) dlist; + size_t outputs_size, outputs_filled; + struct output **outputs; + + size_t deps_size, deps_filled; + struct job **deps; + CIRCLEQ_ENTRY(job) clist; }; +CIRCLEQ_HEAD(job_clist, job); typedef enum { JOB_READ_SUCCESS = 0, diff --git a/include/util.h b/include/util.h index 5279a07..130bbdd 100644 --- a/include/util.h +++ b/include/util.h @@ -5,13 +5,6 @@ #define print_err(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) -#define LIST_FOREACH_FREE(cur, next, head, field, func_free) \ - for ((cur) = ((head)->lh_first); (cur);) { \ - (next) = ((cur)->field.le_next); \ - func_free((cur)); \ - (cur) = (next); \ - } - #define CIRCLEQ_FOREACH_FREE(cur, next, head, field, func_free) \ for ((cur) = ((head)->cqh_first); (cur) != (const void *)(head);) { \ (next) = ((cur)->field.cqe_next); \ @@ -12,6 +12,8 @@ static void output_free(struct output *output); static int job_output_insert(struct job *j, char *name, char *store_path); static int job_read_inputdrvs(struct job *job, cJSON *input_drvs); static int job_read_outputs(struct job *job, cJSON *outputs); +static int job_deps_list_insert(struct job *job, struct job *dep); +static int job_output_list_insert(struct job *job, struct output *output); static void output_free(struct output *output) { @@ -24,6 +26,54 @@ static void output_free(struct output *output) free(output); } +static int job_output_list_insert(struct job *job, struct output *output) +{ + size_t newsize = 0; + void *ret; + + if (job->outputs_filled < job->outputs_size) { + job->outputs[job->outputs_filled++] = output; + return 0; + } + + newsize = job->outputs_size == 0 ? 1 : job->outputs_size * 2; + ret = realloc(job->outputs, newsize * sizeof(*job->outputs)); + if (ret == NULL) { + print_err("%s", strerror(errno)); + return -errno; + } + + job->outputs = ret; + job->outputs_size = newsize; + job->outputs[job->outputs_filled++] = output; + + return 0; +} + +static int job_deps_list_insert(struct job *job, struct job *dep) +{ + size_t newsize = 0; + void *ret; + + if (job->deps_filled < job->deps_size) { + job->deps[job->deps_filled++] = dep; + return 0; + } + + newsize = job->deps_size == 0 ? 1 : job->deps_size * 2; + ret = realloc(job->deps, newsize * sizeof(*job->deps)); + if (ret == NULL) { + print_err("%s", strerror(errno)); + return -errno; + } + + job->deps = ret; + job->deps_size = newsize; + job->deps[job->deps_filled++] = dep; + + return 0; +} + static int job_output_insert(struct job *j, char *name, char *store_path) { struct output *o; @@ -52,14 +102,19 @@ static int job_output_insert(struct job *j, char *name, char *store_path) o->store_path = NULL; } + ret = job_output_list_insert(j, o); + if (ret < 0) + goto out_free_store_path; + +out_free_store_path: + if (ret < 0) + free(o->store_path); out_free_name: if (ret < 0) free(o->name); out_free_o: if (ret < 0) free(o); - else - LIST_INSERT_HEAD(&j->outputs, o, dlist); return 0; } @@ -78,13 +133,14 @@ static int job_read_inputdrvs(struct job *job, cJSON *input_drvs) cJSON_ArrayForEach (output, array) { ret = job_output_insert(dep_job, output->valuestring, NULL); - if (ret < 0) { + if (ret < 0) job_free(dep_job); - goto out_free_dep_job; - } } - LIST_INSERT_HEAD(&job->deps, dep_job, dlist); + ret = job_deps_list_insert(job, dep_job); + if (ret < 0) + job_free(dep_job); + dep_job = NULL; } @@ -180,17 +236,19 @@ out_free: void job_free(struct job *job) { - struct job *job_cur, *job_next; - struct output *op_cur, *op_next; - if (job == NULL) return; free(job->name); free(job->drv_path); - LIST_FOREACH_FREE(op_cur, op_next, &job->outputs, dlist, output_free); - LIST_FOREACH_FREE(job_cur, job_next, &job->deps, dlist, job_free); + for (size_t i = 0; i < job->outputs_filled; i++) + output_free(job->outputs[i]); + free(job->outputs); + + for (size_t i = 0; i < job->deps_filled; i++) + job_free(job->deps[i]); + free(job->deps); free(job); } @@ -224,8 +282,13 @@ int job_new(struct job **j, char *name, char *drv_path) goto out_free_name; } - LIST_INIT(&job->deps); - LIST_INIT(&job->outputs); + job->outputs_size = 0; + job->outputs_filled = 0; + job->outputs = NULL; + + job->deps_size = 0; + job->deps_filled = 0; + job->deps = NULL; out_free_name: if (ret < 0) |