Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/core/process.c

106 lines
2.3 KiB
C
Raw Normal View History

#include "process.h"
2016-12-20 23:36:00 +01:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
2017-01-19 22:07:52 +01:00
#include <string.h>
2016-12-19 01:08:55 +01:00
2018-10-04 00:30:47 +02:00
struct ipc_client * ipc_server_process_copy (const struct ipc_client *p)
{
if (p == NULL)
return NULL;
2018-10-04 00:30:47 +02:00
struct ipc_client * copy = malloc (sizeof(struct ipc_client));
memcpy (copy, p, sizeof (struct ipc_client));
return copy;
}
2018-10-04 00:30:47 +02:00
int ipc_server_process_eq (const struct ipc_client *p1, const struct ipc_client *p2)
{
2017-01-19 22:07:52 +01:00
return (p1->version == p2->version && p1->index == p2->index
&& p1->proc_fd == p2->proc_fd);
}
2018-10-04 00:30:47 +02:00
void ipc_server_process_gen (struct ipc_client *p
, unsigned int index, unsigned int version)
{
p->version = version;
p->index = index;
}
2018-10-04 00:30:47 +02:00
int ipc_process_add (struct ipc_process_array *aproc, struct ipc_client *p)
2016-12-22 21:48:35 +01:00
{
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
2018-10-04 00:30:47 +02:00
, sizeof(struct ipc_client) * aproc->size);
2016-12-22 21:48:35 +01:00
2016-12-20 23:36:00 +01:00
if (aproc->tab_proc == NULL) {
return -1;
}
aproc->tab_proc[aproc->size - 1] = p;
return 0;
}
2018-10-04 00:30:47 +02:00
int ipc_process_del (struct ipc_process_array *aproc, struct ipc_client *p)
2016-12-22 21:48:35 +01:00
{
2016-12-20 23:36:00 +01:00
assert(aproc != NULL);
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) {
2018-10-03 22:02:37 +02:00
ipc_process_array_free (aproc);
2016-12-23 01:33:52 +01:00
}
else {
aproc->tab_proc = realloc(aproc->tab_proc
2018-10-04 00:30:47 +02:00
, sizeof(struct ipc_client) * 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
}
2018-10-04 00:30:47 +02:00
void process_print (struct ipc_client *p)
{
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-20 23:36:00 +01:00
2018-10-03 22:02:37 +02:00
void ipc_process_array_print (struct ipc_process_array *ap)
2016-12-22 21:48:35 +01:00
{
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]);
}
}
2018-10-03 22:02:37 +02:00
void ipc_process_array_free (struct ipc_process_array *ap)
2016-12-22 21:48:35 +01:00
{
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
}