-
Notifications
You must be signed in to change notification settings - Fork 0
/
disastrOS_descriptor.c
137 lines (121 loc) · 3.29 KB
/
disastrOS_descriptor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <assert.h>
#include <stdio.h>
#include "disastrOS_descriptor.h"
#include "pool_allocator.h"
#include "disastrOS_constants.h"
#include "disastrOS_mq.h"
#define DESCRIPTOR_SIZE sizeof(Descriptor)
#define DESCRIPTOR_MEMSIZE (sizeof(Descriptor)+sizeof(int))
#define MAX_NUM_DESCRIPTORS (MAX_NUM_DESCRIPTORS_PER_PROCESS*MAX_NUM_PROCESSES)
#define DESCRIPTOR_BUFFER_SIZE MAX_NUM_DESCRIPTORS*DESCRIPTOR_MEMSIZE
#define DESCRIPTORPTR_SIZE sizeof(DescriptorPtr)
#define DESCRIPTORPTR_MEMSIZE (sizeof(DescriptorPtr)+sizeof(int))
#define DESCRIPTORPTR_BUFFER_SIZE MAX_NUM_DESCRIPTORS*DESCRIPTORPTR_MEMSIZE
static char _descriptor_buffer[DESCRIPTOR_BUFFER_SIZE];
static PoolAllocator _descriptor_allocator;
static char _descriptor_ptr_buffer[DESCRIPTORPTR_BUFFER_SIZE];
static PoolAllocator _descriptor_ptr_allocator;
void Descriptor_init(){
int result=PoolAllocator_init(& _descriptor_allocator,
DESCRIPTOR_SIZE,
MAX_NUM_PROCESSES,
_descriptor_buffer,
DESCRIPTOR_BUFFER_SIZE);
assert(! result);
result=PoolAllocator_init(& _descriptor_ptr_allocator,
DESCRIPTORPTR_SIZE,
MAX_NUM_PROCESSES,
_descriptor_ptr_buffer,
DESCRIPTORPTR_BUFFER_SIZE);
assert(! result);
}
Descriptor* Descriptor_alloc(int fd, Resource* res, PCB* pcb) {
Descriptor* d=(Descriptor*)PoolAllocator_getBlock(&_descriptor_allocator);
if (!d)
return 0;
d->list.prev=d->list.next=0;
d->fd=fd;
d->mq=0;
d->resource=res;
d->pcb=pcb;
return d;
}
Descriptor* Descriptor_alloc_mq(int fd, MessageQueue* mq, PCB* pcb) {
Descriptor* d=(Descriptor*)PoolAllocator_getBlock(&_descriptor_allocator);
if (!d)
return 0;
d->list.prev=d->list.next=0;
d->fd=fd;
d->resource=0;
d->mq=mq;
d->pcb=pcb;
return d;
}
int Descriptor_free(Descriptor* d) {
return PoolAllocator_releaseBlock(&_descriptor_allocator, d);
}
Descriptor* DescriptorList_byFd(ListHead* l, int fd){
ListItem* aux=l->first;
while(aux){
Descriptor* d=(Descriptor*)aux;
if (d->fd==fd)
return d;
aux=aux->next;
}
return 0;
}
DescriptorPtr* DescriptorPtr_alloc(Descriptor* descriptor) {
DescriptorPtr* d=PoolAllocator_getBlock(&_descriptor_ptr_allocator);
if (!d)
return 0;
d->list.prev=d->list.next=0;
d->descriptor=descriptor;
return d;
}
int DescriptorPtr_free(DescriptorPtr* d){
return PoolAllocator_releaseBlock(&_descriptor_ptr_allocator, d);
}
void DescriptorList_print(ListHead* l){
ListItem* aux=l->first;
printf("[");
while(aux){
Descriptor* d=(Descriptor*)aux;
printf("(fd: %d, rid:%d)",
d->fd,
d->resource->id);
if(aux->next)
printf(", ");
aux=aux->next;
}
printf("]");
}
void DescriptorPtrList_print(ListHead* l){
ListItem* aux=l->first;
printf("[");
while(aux){
DescriptorPtr* d=(DescriptorPtr*)aux;
printf("(pid: %d, fd: %d, rid:%d)",
d->descriptor->fd,
d->descriptor->pcb->pid,
d->descriptor->resource->id);
if(aux->next)
printf(", ");
aux=aux->next;
}
printf("]");
}
void DescriptorPtrList_print_mq(ListHead* l){
ListItem* aux=l->first;
printf("[");
while(aux){
DescriptorPtr* d=(DescriptorPtr*)aux;
printf("(fd: %d, pid: %d, mqid:%d)",
d->descriptor->fd,
d->descriptor->pcb->pid,
d->descriptor->mq->id);
if(aux->next)
printf(", ");
aux=aux->next;
}
printf("]");
}