-
Notifications
You must be signed in to change notification settings - Fork 0
/
disastrOS_semdescriptor.c
119 lines (104 loc) · 3.14 KB
/
disastrOS_semdescriptor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <assert.h>
#include <stdio.h>
#include "disastrOS_semdescriptor.h"
#include "pool_allocator.h"
#include "disastrOS_constants.h"
#define SEMDESCRIPTOR_SIZE sizeof(SemDescriptor)
#define SEMDESCRIPTOR_MEMSIZE (sizeof(SemDescriptor)+sizeof(int))
#define MAX_NUM_SEMDESCRIPTORS (MAX_NUM_SEMDESCRIPTORS_PER_PROCESS*MAX_NUM_PROCESSES)
#define SEMDESCRIPTOR_BUFFER_SIZE MAX_NUM_SEMDESCRIPTORS*SEMDESCRIPTOR_MEMSIZE
#define SEMDESCRIPTORPTR_SIZE sizeof(SemDescriptorPtr)
#define SEMDESCRIPTORPTR_MEMSIZE (sizeof(SemDescriptorPtr)+sizeof(int))
#define SEMDESCRIPTORPTR_BUFFER_SIZE MAX_NUM_SEMDESCRIPTORS*SEMDESCRIPTORPTR_MEMSIZE
static char _sem_descriptor_buffer[SEMDESCRIPTOR_BUFFER_SIZE];
static PoolAllocator _sem_descriptor_allocator;
static char _sem_descriptor_ptr_buffer[SEMDESCRIPTORPTR_BUFFER_SIZE];
static PoolAllocator _sem_descriptor_ptr_allocator;
void SemDescriptor_init(){
int result=PoolAllocator_init(& _sem_descriptor_allocator,
SEMDESCRIPTOR_SIZE,
MAX_NUM_PROCESSES,
_sem_descriptor_buffer,
SEMDESCRIPTOR_BUFFER_SIZE);
assert(! result);
result=PoolAllocator_init(& _sem_descriptor_ptr_allocator,
SEMDESCRIPTORPTR_SIZE,
MAX_NUM_PROCESSES,
_sem_descriptor_ptr_buffer,
SEMDESCRIPTORPTR_BUFFER_SIZE);
assert(! result);
}
SemDescriptor* SemDescriptor_alloc(int fd, Semaphore* res, PCB* pcb) {
SemDescriptor* d=(SemDescriptor*)PoolAllocator_getBlock(&_sem_descriptor_allocator);
if (!d)
return 0;
d->list.prev=d->list.next=0;
d->fd=fd;
d->semaphore=res;
d->pcb=pcb;
return d;
}
int SemDescriptor_free(SemDescriptor* d) {
return PoolAllocator_releaseBlock(&_sem_descriptor_allocator, d);
}
SemDescriptor* SemDescriptorList_byFd(ListHead* l, int fd){
ListItem* aux=l->first;
while(aux){
SemDescriptor* d=(SemDescriptor*)aux;
if (d->fd==fd)
return d;
aux=aux->next;
}
return 0;
}
SemDescriptorPtr* SemDescriptorPtr_alloc(SemDescriptor* descriptor) {
SemDescriptorPtr* d=PoolAllocator_getBlock(&_sem_descriptor_ptr_allocator);
if (!d)
return 0;
d->list.prev=d->list.next=0;
d->descriptor=descriptor;
return d;
}
int SemDescriptorPtr_free(SemDescriptorPtr* d){
return PoolAllocator_releaseBlock(&_sem_descriptor_ptr_allocator, d);
}
void SemDescriptorList_print(ListHead* l){
ListItem* aux=l->first;
printf("[");
while(aux){
SemDescriptor* d=(SemDescriptor*)aux;
printf("(fd: %d, rid:%d)",
d->fd,
d->semaphore->id);
if(aux->next)
printf(", ");
aux=aux->next;
}
printf("]");
}
void SemDescriptorPtrList_print(ListHead* l){
ListItem* aux=l->first;
printf("[");
while(aux){
SemDescriptorPtr* d=(SemDescriptorPtr*)aux;
printf("(pid: %d, fd: %d, rid:%d)",
d->descriptor->pcb->pid,
d->descriptor->fd,
d->descriptor->semaphore->id);
if(aux->next)
printf(", ");
aux=aux->next;
}
printf("]");
}
//EDITED
SemDescriptor* SemDescriptorFind_byID(ListHead* des_list, int id) {
ListItem* aux = des_list->first;
SemDescriptor* sem = 0;
while(aux){
SemDescriptor* descriptor = (SemDescriptor*)aux;
if(id == descriptor->semaphore->id) sem = descriptor;
aux = aux->next;
}
return sem;
}