/
priority_queue.c
294 lines (247 loc) · 6.47 KB
/
priority_queue.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* This program will create a priority queue implemented as a heap.
* A heap is a 'balanced tree'
* A priority queue is a nifty thing. You insert nodes into the queue, each
* node has a priority. You can then get the first node which always has the
* top priority.
*
* You won't be able to use this source directly in any project. It's not
* generic enough. ToDo to make it more modular:
* - In the PQUEUE structre; add two new entries:
* 1. A function pointer to a function which will be used to compare
* nodes.
* 2. A node-size variable.
* - You need to design a function which will compare two nodes.
* - If you're planning on using the queue in multithreaded/shared
* application you'll need another variable in the PQUEUE structure:
* a mutex sempahore handle. In OS/2, it's common design to make all
* libraries multithread safe. Remember to request (and release) the mutex
* sempahores in the push and pop functions.
*
* References:
* http://hissa.ncsl.nist.gov/~black/CRCDict/
* http://www.ee.uwa.edu.au/~plsd210/ds/
*/
#pragma strings(readonly)
#define INCL_DOSMEMMGR
#define INCL_DOSEXCEPTIONS
#define INCL_DOSERRORS
#include <os2.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
* Each node in the priority queue
*/
typedef struct _NODE
{
ULONG event;
ULONG priority;
PVOID param1;
PVOID param2;
}NODE, *PNODE;
/*
* Priority queue header
*/
typedef struct _PQUEUE
{
ULONG cNodes;
NODE aNodes[1];
}PQUEUE, *PPQUEUE;
/*
* Function prototypes
*/
int pqueue_push(PPQUEUE pQueue, PNODE pNew);
PNODE pqueue_pop(PPQUEUE pQueue, PNODE pNode);
PNODE pqueue_pop2(PPQUEUE pQueue);
int pqueue_delete(PPQUEUE pQueue);
int _Inline pqueue_shiftup(PPQUEUE pQueue, ULONG i);
static ULONG _System exception_handler(PEXCEPTIONREPORTRECORD pERepRec, PEXCEPTIONREGISTRATIONRECORD pERegRep, PCONTEXTRECORD pCtxRec, PVOID p);
/*
* Push a new node onto the heap/priority queue
*/
int pqueue_push(PPQUEUE pQueue, PNODE pNew)
{
PNODE aNodes = &pQueue->aNodes[0];
int i = 0, j = 0;
pQueue->cNodes++;
for(j = pQueue->cNodes; j > 1; j = i)
{
i = j / 2;
if(aNodes[i].priority >= pNew->priority)
break;
memcpy(&aNodes[j], &aNodes[i], sizeof(NODE));
}
memcpy(&aNodes[j], pNew, sizeof(NODE));
return 1;
}
/*
* Get the top priority queue
*/
PNODE pqueue_pop(PPQUEUE pQueue, PNODE pNode)
{
if(pQueue->cNodes < 1)
{
return NULL;
}
memcpy(pNode, &pQueue->aNodes[1], sizeof(NODE));
pqueue_delete(pQueue);
return pNode;
}
/*
* Since node 0 isn't used, it can be used as a temporary storage
* for the pop:ed node.
* Important node: Unless you protect the first node with a sempahore
* (which isn't good design) this version of getting the top priority node
* is not thread safe!
*/
PNODE pqueue_pop2(PPQUEUE pQueue)
{
if(pQueue->cNodes < 1)
{
return NULL;
}
memcpy(&pQueue->aNodes[0], &pQueue->aNodes[1], sizeof(NODE));
pqueue_delete(pQueue);
return &pQueue->aNodes[0];
}
/*
* Strictly internal function - do not call from application code
*/
int pqueue_delete(PPQUEUE pQueue)
{
if(pQueue->cNodes < 1)
{
return 0;
}
else
{
memcpy(&pQueue->aNodes[1], &pQueue->aNodes[pQueue->cNodes], sizeof(NODE));
pQueue->cNodes--;
pqueue_shiftup(pQueue, 1);
}
return 1;
}
/*
* Strictly internal function - do not call from application code
*/
int _Inline pqueue_shiftup(PPQUEUE pQueue, ULONG i)
{
NODE tmpNode;
int j = 0;
int n = pQueue->cNodes;
PNODE aNodes = &pQueue->aNodes[0];
while((j = 2*i) <= n)
{
if((j < n) && aNodes[j].priority < aNodes[j+1].priority)
j++;
if(aNodes[i].priority < aNodes[j].priority)
{
memcpy(&tmpNode, &aNodes[j], sizeof(NODE));
memcpy(&aNodes[j], &aNodes[i], sizeof(NODE));
memcpy(&aNodes[i], &tmpNode, sizeof(NODE));
i = j;
}
else
{
break;
}
}
return 1;
}
int main(int argc, char *argv)
{
APIRET rc = NO_ERROR;
PVOID pAlloc = NULL;
PPQUEUE heap = NULL;
int iRet = 0;
EXCEPTIONREGISTRATIONRECORD xcpthand = { 0, &exception_handler };
int i = 0;
srand(time(NULL));
/*
* The whole idea with this sample is to demonstrate how to allocate memory
* 'as needed'. The best way to do this is to use an exception handler
*/
rc = DosSetExceptionHandler(&xcpthand);
/*
* Allocate a very large buffer. This will fit approx 524280 nodes
* which is way more than you'll need.
*/
rc = DosAllocMem(&pAlloc, 16*1024*1024, PAG_READ | PAG_WRITE);
if(rc == NO_ERROR)
{
heap = pAlloc;
}
/*
* Add some nodes with random priorities
*/
if(heap)
{
NODE tmpNode = { 0 };
PNODE node = NULL;
puts("Add nodes");
for(i = 0; i < 100; i++)
{
tmpNode.priority = rand();
printf("Added node with priority: %u\n", tmpNode.priority);
pqueue_push(heap, &tmpNode);
}
/*
* Actually, it's neater to use the nodes-counter..
*/
puts("\n\nEmpty heap");
while((node = pqueue_pop2(heap)) != NULL)
{
printf("Priority: %u\n", node->priority);
}
}
/*
* Free heap buffer
*/
if(heap)
{
DosFreeMem(heap);
}
/*
* All registered exception handlers must be deregistered
*/
DosUnsetExceptionHandler(&xcpthand);
return iRet;
}
/*
* Exception handler; attempt to commit memory if access violation occures.
*/
static ULONG _System exception_handler(PEXCEPTIONREPORTRECORD pERepRec, PEXCEPTIONREGISTRATIONRECORD pERegRep, PCONTEXTRECORD pCtxRec, PVOID p)
{
ULONG ulRet = XCPT_CONTINUE_EXECUTION;
switch(pERepRec->ExceptionNum)
{
case XCPT_ACCESS_VIOLATION:
ulRet = XCPT_CONTINUE_SEARCH;
if(pERepRec->ExceptionAddress == (PVOID)XCPT_DATA_UNKNOWN)
{
}
else
{
APIRET rc = NO_ERROR;
ULONG cbMem = 1;
ULONG flMem = 0;
rc = DosQueryMem((PVOID)pERepRec->ExceptionInfo[1], &cbMem, &flMem);
if((flMem & PAG_FREE) == 0)
{
rc = DosSetMem((PVOID)pERepRec->ExceptionInfo[1], 4096, PAG_DEFAULT | PAG_COMMIT);
if(rc == NO_ERROR)
{
ulRet = XCPT_CONTINUE_EXECUTION;
}
}
}
break;
default:
ulRet = XCPT_CONTINUE_SEARCH;
break;
}
return ulRet;
}