2016-12-14 23:17:35 +01:00
|
|
|
#include "process.h"
|
2016-12-20 23:36:00 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-14 23:17:35 +01:00
|
|
|
|
2016-12-19 01:08:55 +01:00
|
|
|
// TODO
|
|
|
|
// tout revoir ici
|
|
|
|
|
|
|
|
#if 0
|
2016-12-14 23:17:35 +01:00
|
|
|
struct process * srv_process_copy (const struct process *p)
|
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct process * copy = malloc (sizeof(struct process));
|
|
|
|
memcpy (copy, p, sizeof (struct process));
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
int srv_process_eq (const struct process *p1, const struct process *p2)
|
|
|
|
{
|
|
|
|
return (p1->version == p2->version && p1->index == p2->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void srv_process_gen (struct process *p
|
|
|
|
, unsigned int index, unsigned int version)
|
|
|
|
{
|
|
|
|
p->version = version;
|
|
|
|
p->index = index;
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:36:00 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-12-22 21:48:35 +01:00
|
|
|
int add_proc (struct array_proc *aproc, struct process *p)
|
|
|
|
{
|
2016-12-20 23:36:00 +01:00
|
|
|
assert(aproc != NULL);
|
|
|
|
assert(p != NULL);
|
|
|
|
aproc->size++;
|
2016-12-22 21:48:35 +01:00
|
|
|
aproc->tab_proc = realloc(aproc->tab_proc
|
|
|
|
, sizeof(struct process) * aproc->size);
|
|
|
|
|
2016-12-20 23:36:00 +01:00
|
|
|
if (aproc->tab_proc == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
aproc->tab_proc[aproc->size - 1] = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:48:35 +01:00
|
|
|
int del_proc (struct array_proc *aproc, struct process *p)
|
|
|
|
{
|
2016-12-20 23:36:00 +01:00
|
|
|
assert(aproc != NULL);
|
2016-12-23 01:33:52 +01:00
|
|
|
assert(aproc->tab_proc != NULL);
|
2016-12-20 23:36:00 +01:00
|
|
|
assert(p != NULL);
|
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
if (aproc->tab_proc == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:36:00 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < aproc->size; i++) {
|
|
|
|
if (aproc->tab_proc[i] == p) {
|
2016-12-22 21:48:35 +01:00
|
|
|
|
2016-12-20 23:36:00 +01:00
|
|
|
aproc->tab_proc[i] = aproc->tab_proc[aproc->size-1];
|
|
|
|
aproc->size--;
|
2016-12-23 01:33:52 +01:00
|
|
|
if (aproc->size == 0) {
|
|
|
|
array_proc_free (aproc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aproc->tab_proc = realloc(aproc->tab_proc
|
|
|
|
, sizeof(struct process) * aproc->size);
|
2016-12-22 21:48:35 +01:00
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
if (aproc->tab_proc == NULL) {
|
|
|
|
return -2;
|
|
|
|
}
|
2016-12-20 23:36:00 +01:00
|
|
|
}
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2016-12-20 23:36:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
return -3;
|
2016-12-20 23:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void process_print (struct process *p)
|
2016-12-14 23:17:35 +01:00
|
|
|
{
|
|
|
|
if (p != NULL)
|
2016-12-22 21:48:35 +01:00
|
|
|
printf ("process %d : index %d, version %d\n"
|
|
|
|
, p->proc_fd, p->index, p->version);
|
2016-12-14 23:17:35 +01:00
|
|
|
}
|
2016-12-20 23:36:00 +01:00
|
|
|
|
2016-12-22 21:48:35 +01:00
|
|
|
void array_proc_print (struct array_proc *ap)
|
|
|
|
{
|
2016-12-20 23:36:00 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < ap->size; i++) {
|
|
|
|
printf("%d : ", i);
|
|
|
|
process_print(ap->tab_proc[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:48:35 +01:00
|
|
|
void array_proc_free (struct array_proc *ap)
|
|
|
|
{
|
2016-12-23 01:33:52 +01:00
|
|
|
if (ap->tab_proc != NULL) {
|
|
|
|
free (ap->tab_proc);
|
|
|
|
ap->tab_proc = NULL;
|
|
|
|
}
|
|
|
|
ap->size = 0;
|
2016-12-22 21:48:35 +01:00
|
|
|
}
|