-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisastrOS_mq.c
185 lines (162 loc) · 4.4 KB
/
disastrOS_mq.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "disastrOS_mq.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "disastrOS_descriptor.h"
#include "linked_list.h"
#include "pool_allocator.h"
#define MQ_SIZE sizeof(MessageQueue)
#define MQ_MEMSIZE (sizeof(MessageQueue) + sizeof(int))
#define MQ_BUFFER_SIZE MAX_NUM_MQS* MQ_MEMSIZE
#define QUEUE_SIZE sizeof(Queue)
#define QUEUE_MEMSIZE (sizeof(Queue) + sizeof(int))
#define QUEUE_BUFFER_SIZE MAX_NUM_QUEUES* QUEUE_MEMSIZE
#define MESSAGE_SIZE sizeof(Message)
#define MESSAGE_MEMSIZE (sizeof(Message) + sizeof(int))
#define MESSAGE_BUFFER_SIZE MAX_NUM_MESSAGES* MESSAGE_MEMSIZE
static char _mqs_buffer[MQ_BUFFER_SIZE];
static PoolAllocator _mqs_allocator;
static char _queues_buffer[QUEUE_BUFFER_SIZE];
static PoolAllocator _queues_allocator;
static char _messages_buffer[MESSAGE_BUFFER_SIZE];
static PoolAllocator _messages_allocator;
void MessageQueue_init() {
int result = PoolAllocator_init(&_mqs_allocator, MQ_SIZE, MAX_NUM_MQS,
_mqs_buffer, MQ_BUFFER_SIZE);
assert(!result);
}
void Queue_init() {
int result =
PoolAllocator_init(&_queues_allocator, QUEUE_SIZE, MAX_NUM_QUEUES,
_queues_buffer, QUEUE_BUFFER_SIZE);
assert(!result);
}
void Message_init() {
int result =
PoolAllocator_init(&_messages_allocator, MESSAGE_SIZE, MAX_NUM_MESSAGES,
_messages_buffer, MESSAGE_BUFFER_SIZE);
assert(!result);
}
MessageQueue* MessageQueue_alloc(int id, int priority) {
MessageQueue* r = (MessageQueue*)PoolAllocator_getBlock(&_mqs_allocator);
if (!r)
return 0;
r->list.prev = r->list.next = 0;
r->id = id;
r->priority = priority;
r->num_messages = 0;
List_init(&r->queues);
List_init(&r->descriptors);
return r;
}
Queue* Queue_alloc(int priority) {
Queue* r = (Queue*)PoolAllocator_getBlock(&_queues_allocator);
if (!r)
return 0;
r->list.prev = r->list.next = 0;
r->priority = priority;
List_init(&r->messages);
return r;
}
Message* Message_alloc(char* text, int text_length) {
Message* r = (Message*)PoolAllocator_getBlock(&_messages_allocator);
if (!r)
return 0;
r->text = text;
r->list.prev = r->list.next = 0;
r->text_length = text_length;
return r;
}
int MessageQueue_free(MessageQueue* r) {
assert(r->descriptors.first == 0);
assert(r->descriptors.last == 0);
return PoolAllocator_releaseBlock(&_mqs_allocator, r);
}
int Queue_free(Queue* r) {
assert(r->messages.first == 0);
assert(r->messages.last == 0);
return PoolAllocator_releaseBlock(&_queues_allocator, r);
}
int Message_free(Message* r) {
return PoolAllocator_releaseBlock(&_messages_allocator, r);
}
MessageQueue* MessageQueueList_byId(MessageQueueList* l, int id) {
ListItem* aux = l->first;
while (aux) {
MessageQueue* r = (MessageQueue*)aux;
if (r->id == id)
return r;
aux = aux->next;
}
return 0;
}
Queue* QueueList_byPriority(QueueList* l, int priority) {
ListItem* aux = l->first;
while (aux) {
Queue* r = (Queue*)aux;
if (r->priority == priority)
return r;
aux = aux->next;
}
return 0;
}
void Message_print(Message* r) {
printf("[Message] length: %d ", r->text_length);
printf("text: %s\n", r->text);
}
void MessageList_print(ListHead* l) {
if (!l->size) {
printf("no messages\n");
return;
}
ListItem* aux = l->first;
printf("{\n");
while (aux) {
Message* r = (Message*)aux;
printf("\t");
Message_print(r);
if (aux->next)
printf(",");
printf("\n");
aux = aux->next;
}
printf("}\n");
}
void Queue_print(Queue* r) {
printf("[Priority Queue] priority: %d\n", r->priority);
MessageList_print(&r->messages);
}
void QueueList_print(ListHead* l) {
ListItem* aux = l->first;
printf("{\n");
while (aux) {
Queue* r = (Queue*)aux;
printf("\t");
Queue_print(r);
if (aux->next)
printf(",");
printf("\n");
aux = aux->next;
}
printf("}\n");
}
void MessageQueue_print(MessageQueue* r) {
printf("[Message Queue] id: %d, priority: %d, num_messages: %d \n", r->id,
r->priority, r->num_messages);
QueueList_print(&r->queues);
DescriptorPtrList_print(&r->descriptors);
}
void MessageQueueList_print(ListHead* l) {
ListItem* aux = l->first;
printf("{\n");
while (aux) {
MessageQueue* r = (MessageQueue*)aux;
printf("\t");
MessageQueue_print(r);
if (aux->next)
printf(",");
printf("\n");
aux = aux->next;
}
printf("}\n");
}