From bf2d66357b9135442f9dec277678b0597abc1d15 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 29 Jun 2024 07:21:15 +0530 Subject: solver_util: init --- src/build.c | 7 +++--- src/evanix-py.c | 5 +--- src/jobs.c | 19 ++++++++------- src/meson.build | 3 ++- src/queue.c | 1 + src/solver_util.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 src/solver_util.c (limited to 'src') diff --git a/src/build.c b/src/build.c index 87238e9..30a821a 100644 --- a/src/build.c +++ b/src/build.c @@ -1,8 +1,8 @@ #include +#include #include #include #include -#include #include #include "build.h" @@ -54,8 +54,9 @@ static int build(struct queue *queue) if (ret < 0) return ret; - if (job->attr) { - ret = snprintf(out_link, sizeof(out_link), "result-%s", job->attr); + if (job->nix_attr_name) { + ret = snprintf(out_link, sizeof(out_link), "result-%s", + job->nix_attr_name); if (ret < 0 || (size_t)ret > sizeof(out_link)) { ret = -ENAMETOOLONG; print_err("%s", strerror(-ret)); diff --git a/src/evanix-py.c b/src/evanix-py.c index c010e27..1ee2ec0 100644 --- a/src/evanix-py.c +++ b/src/evanix-py.c @@ -8,7 +8,4 @@ static struct PyModuleDef module = { PyModuleDef_HEAD_INIT, "evanix", NULL, -1, methods, }; -PyMODINIT_FUNC PyInit_evanix(void) -{ - return PyModule_Create(&module); -} +PyMODINIT_FUNC PyInit_evanix(void) { return PyModule_Create(&module); } diff --git a/src/jobs.c b/src/jobs.c index 7851fef..04afa4a 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -1,8 +1,8 @@ #include -#include #include #include #include +#include #include @@ -352,12 +352,12 @@ void job_free(struct job *job) free(job->drv_path); free(job->name); - free(job->attr); + free(job->nix_attr_name); free(job); } -static int job_new(struct job **j, char *name, char *drv_path, - char *attr, struct job *parent) +static int job_new(struct job **j, char *name, char *drv_path, char *attr, + struct job *parent) { struct job *job; int ret = 0; @@ -368,6 +368,7 @@ static int job_new(struct job **j, char *name, char *drv_path, return -errno; } job->scheduled = false; + job->id = -1; job->outputs_size = 0; job->outputs_filled = 0; @@ -382,14 +383,14 @@ static int job_new(struct job **j, char *name, char *drv_path, job->parents = NULL; if (attr != NULL) { - job->attr = strdup(attr); - if (job->attr == NULL) { + job->nix_attr_name = strdup(attr); + if (job->nix_attr_name == NULL) { print_err("%s", strerror(errno)); ret = -errno; goto out_free_job; } } else { - job->attr = NULL; + job->nix_attr_name = NULL; } if (name != NULL) { @@ -424,7 +425,7 @@ out_free_name: free(job->name); out_free_attr: if (ret < 0) - free(job->attr); + free(job->nix_attr_name); out_free_job: if (ret < 0) free(job); @@ -434,7 +435,7 @@ out_free_job: return ret; } -int jobs_init(FILE **stream, const char *expr) +int jobs_init(FILE **stream, char *expr) { size_t argindex; char *args[6]; diff --git a/src/meson.build b/src/meson.build index ea955bc..7a42999 100644 --- a/src/meson.build +++ b/src/meson.build @@ -7,10 +7,11 @@ e = executable( 'queue.c', 'build.c', 'htab.c', + 'solver_util.c', ], include_directories: evanix_inc, - dependencies: cjson_dep, + dependencies: [ cjson_dep, highs_dep ], install: true, ) diff --git a/src/queue.c b/src/queue.c index 3a7ca52..75a3e14 100644 --- a/src/queue.c +++ b/src/queue.c @@ -221,6 +221,7 @@ int queue_thread_new(struct queue_thread **queue_thread, FILE *stream) ret = -errno; goto out_free_qt; } + qt->queue->job_ids = NULL; qt->queue->state = Q_SEM_WAIT; ret = sem_init(&qt->queue->sem, 0, 0); if (ret < 0) { diff --git a/src/solver_util.c b/src/solver_util.c new file mode 100644 index 0000000..9efb65a --- /dev/null +++ b/src/solver_util.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include "jobs.h" +#include "solver_util.h" +#include "util.h" + +static int dag_id_assign(struct job *j, struct job_ids *job_ids) +{ + size_t newsize; + void *ret; + + if (j->id >= 0) + return 0; + + for (size_t i = 0; i < j->deps_filled; i++) + return dag_id_assign(j->deps[i], job_ids); + + if (job_ids->size < job_ids->filled) { + j->id = job_ids->filled++; + job_ids->jobs[j->id] = j; + return 0; + } + + newsize = job_ids->size == 0 ? 2 : job_ids->size * 2; + ret = realloc(job_ids->jobs, newsize * sizeof(*job_ids->jobs)); + if (ret == NULL) { + print_err("%s", strerror(errno)); + return -errno; + } + job_ids->jobs = ret; + + j->id = job_ids->filled++; + job_ids->jobs[j->id] = j; + + return 0; +} + +int queue_id_assign(struct job_clist *q, struct job_ids **job_ids) +{ + struct job_ids *ji; + struct job *j; + int ret; + + ji = malloc(sizeof(*ji)); + if (ji == NULL) { + print_err("%s", strerror(errno)); + return -errno; + } + ji->jobs = NULL; + ji->size = 0; + ji->filled = 0; + + CIRCLEQ_FOREACH (j, q, clist) { + ret = dag_id_assign(j, ji); + if (ret < 0) { + goto out_free_js; + } + } + +out_free_js: + if (ret < 0) { + free(ji->jobs); + free(ji); + } else { + *job_ids = ji; + } + + return ret; +} -- cgit v1.2.3