| 1 |
// Emacs style mode select -*- C -*- |
| 2 |
//----------------------------------------------------------------------------- |
| 3 |
// |
| 4 |
// Copyright(C) 2009 Simon Howard |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or |
| 7 |
// modify it under the terms of the GNU General Public License |
| 8 |
// as published by the Free Software Foundation; either version 2 |
| 9 |
// of the License, or (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, write to the Free Software |
| 18 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 |
// 02111-1307, USA. |
| 20 |
// |
| 21 |
// DESCRIPTION: |
| 22 |
// Queue of waiting callbacks, stored in a binary min heap, so that we |
| 23 |
// can always get the first callback. |
| 24 |
// |
| 25 |
//----------------------------------------------------------------------------- |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include "lprintf.h" |
| 30 |
|
| 31 |
#include "opl_queue.h" |
| 32 |
|
| 33 |
#define MAX_OPL_QUEUE 64 |
| 34 |
|
| 35 |
typedef struct |
| 36 |
{ |
| 37 |
opl_callback_t callback; |
| 38 |
void *data; |
| 39 |
uint64_t time; |
| 40 |
} opl_queue_entry_t; |
| 41 |
|
| 42 |
struct opl_callback_queue_s |
| 43 |
{ |
| 44 |
opl_queue_entry_t entries[MAX_OPL_QUEUE]; |
| 45 |
unsigned int num_entries; |
| 46 |
}; |
| 47 |
|
| 48 |
opl_callback_queue_t *OPL_Queue_Create(void) |
| 49 |
{ |
| 50 |
opl_callback_queue_t *queue; |
| 51 |
|
| 52 |
queue = (opl_callback_queue_t*)malloc(sizeof(opl_callback_queue_t)); |
| 53 |
queue->num_entries = 0; |
| 54 |
|
| 55 |
return queue; |
| 56 |
} |
| 57 |
|
| 58 |
void OPL_Queue_Destroy(opl_callback_queue_t *queue) |
| 59 |
{ |
| 60 |
free(queue); |
| 61 |
} |
| 62 |
|
| 63 |
int OPL_Queue_IsEmpty(opl_callback_queue_t *queue) |
| 64 |
{ |
| 65 |
return queue->num_entries == 0; |
| 66 |
} |
| 67 |
|
| 68 |
void OPL_Queue_Clear(opl_callback_queue_t *queue) |
| 69 |
{ |
| 70 |
queue->num_entries = 0; |
| 71 |
} |
| 72 |
|
| 73 |
void OPL_Queue_Push(opl_callback_queue_t *queue, |
| 74 |
opl_callback_t callback, void *data, |
| 75 |
uint64_t time) |
| 76 |
{ |
| 77 |
int entry_id; |
| 78 |
int parent_id; |
| 79 |
|
| 80 |
if (queue->num_entries >= MAX_OPL_QUEUE) |
| 81 |
{ |
| 82 |
lprintf (LO_WARN, "OPL_Queue_Push: Exceeded maximum callbacks\n"); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// Add to last queue entry. |
| 87 |
|
| 88 |
entry_id = queue->num_entries; |
| 89 |
++queue->num_entries; |
| 90 |
|
| 91 |
// Shift existing entries down in the heap. |
| 92 |
|
| 93 |
while (entry_id > 0) |
| 94 |
{ |
| 95 |
parent_id = (entry_id - 1) / 2; |
| 96 |
|
| 97 |
// Is the heap condition satisfied? |
| 98 |
|
| 99 |
if (time >= queue->entries[parent_id].time) |
| 100 |
{ |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
// Move the existing entry down in the heap. |
| 105 |
|
| 106 |
memcpy(&queue->entries[entry_id], |
| 107 |
&queue->entries[parent_id], |
| 108 |
sizeof(opl_queue_entry_t)); |
| 109 |
|
| 110 |
// Advance to the parent. |
| 111 |
|
| 112 |
entry_id = parent_id; |
| 113 |
} |
| 114 |
|
| 115 |
// Insert new callback data. |
| 116 |
|
| 117 |
queue->entries[entry_id].callback = callback; |
| 118 |
queue->entries[entry_id].data = data; |
| 119 |
queue->entries[entry_id].time = time; |
| 120 |
} |
| 121 |
|
| 122 |
int OPL_Queue_Pop(opl_callback_queue_t *queue, |
| 123 |
opl_callback_t *callback, void **data) |
| 124 |
{ |
| 125 |
opl_queue_entry_t *entry; |
| 126 |
int child1, child2; |
| 127 |
int i, next_i; |
| 128 |
|
| 129 |
// Empty? |
| 130 |
|
| 131 |
if (queue->num_entries <= 0) |
| 132 |
{ |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
// Store the result: |
| 137 |
|
| 138 |
*callback = queue->entries[0].callback; |
| 139 |
*data = queue->entries[0].data; |
| 140 |
|
| 141 |
// Decrease the heap size, and keep pointer to the last entry in |
| 142 |
// the heap, which must now be percolated down from the top. |
| 143 |
|
| 144 |
--queue->num_entries; |
| 145 |
entry = &queue->entries[queue->num_entries]; |
| 146 |
|
| 147 |
// Percolate down. |
| 148 |
|
| 149 |
i = 0; |
| 150 |
|
| 151 |
for (;;) |
| 152 |
{ |
| 153 |
child1 = i * 2 + 1; |
| 154 |
child2 = i * 2 + 2; |
| 155 |
|
| 156 |
if (child1 < queue->num_entries |
| 157 |
&& queue->entries[child1].time < entry->time) |
| 158 |
{ |
| 159 |
// Left child is less than entry. |
| 160 |
// Use the minimum of left and right children. |
| 161 |
|
| 162 |
if (child2 < queue->num_entries |
| 163 |
&& queue->entries[child2].time < queue->entries[child1].time) |
| 164 |
{ |
| 165 |
next_i = child2; |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
next_i = child1; |
| 170 |
} |
| 171 |
} |
| 172 |
else if (child2 < queue->num_entries |
| 173 |
&& queue->entries[child2].time < entry->time) |
| 174 |
{ |
| 175 |
// Right child is less than entry. Go down the right side. |
| 176 |
|
| 177 |
next_i = child2; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
// Finished percolating. |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
// Percolate the next value up and advance. |
| 186 |
|
| 187 |
memcpy(&queue->entries[i], |
| 188 |
&queue->entries[next_i], |
| 189 |
sizeof(opl_queue_entry_t)); |
| 190 |
i = next_i; |
| 191 |
} |
| 192 |
|
| 193 |
// Store the old last-entry at its new position. |
| 194 |
|
| 195 |
memcpy(&queue->entries[i], entry, sizeof(opl_queue_entry_t)); |
| 196 |
|
| 197 |
return 1; |
| 198 |
} |
| 199 |
|
| 200 |
uint64_t OPL_Queue_Peek(opl_callback_queue_t *queue) |
| 201 |
{ |
| 202 |
if (queue->num_entries > 0) |
| 203 |
{ |
| 204 |
return queue->entries[0].time; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
return 0; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void OPL_Queue_AdjustCallbacks(opl_callback_queue_t *queue, |
| 213 |
uint64_t time, float factor) |
| 214 |
{ |
| 215 |
int64_t offset; |
| 216 |
int i; |
| 217 |
|
| 218 |
for (i = 0; i < queue->num_entries; ++i) |
| 219 |
{ |
| 220 |
offset = queue->entries[i].time - time; |
| 221 |
queue->entries[i].time = time + (uint64_t) (offset / factor); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
#ifdef TEST |
| 226 |
|
| 227 |
#include <assert.h> |
| 228 |
|
| 229 |
static void PrintQueueNode(opl_callback_queue_t *queue, int node, int depth) |
| 230 |
{ |
| 231 |
int i; |
| 232 |
|
| 233 |
if (node >= queue->num_entries) |
| 234 |
{ |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
for (i=0; i<depth * 3; ++i) |
| 239 |
{ |
| 240 |
printf(" "); |
| 241 |
} |
| 242 |
|
| 243 |
printf("%i\n", queue->entries[node].time); |
| 244 |
|
| 245 |
PrintQueueNode(queue, node * 2 + 1, depth + 1); |
| 246 |
PrintQueueNode(queue, node * 2 + 2, depth + 1); |
| 247 |
} |
| 248 |
|
| 249 |
static void PrintQueue(opl_callback_queue_t *queue) |
| 250 |
{ |
| 251 |
PrintQueueNode(queue, 0, 0); |
| 252 |
} |
| 253 |
|
| 254 |
int main() |
| 255 |
{ |
| 256 |
opl_callback_queue_t *queue; |
| 257 |
int iteration; |
| 258 |
|
| 259 |
queue = OPL_Queue_Create(); |
| 260 |
|
| 261 |
for (iteration=0; iteration<5000; ++iteration) |
| 262 |
{ |
| 263 |
opl_callback_t callback; |
| 264 |
void *data; |
| 265 |
unsigned int time; |
| 266 |
unsigned int newtime; |
| 267 |
int i; |
| 268 |
|
| 269 |
for (i=0; i<MAX_OPL_QUEUE; ++i) |
| 270 |
{ |
| 271 |
time = rand() % 0x10000; |
| 272 |
OPL_Queue_Push(queue, NULL, NULL, time); |
| 273 |
} |
| 274 |
|
| 275 |
time = 0; |
| 276 |
|
| 277 |
for (i=0; i<MAX_OPL_QUEUE; ++i) |
| 278 |
{ |
| 279 |
assert(!OPL_Queue_IsEmpty(queue)); |
| 280 |
newtime = OPL_Queue_Peek(queue); |
| 281 |
assert(OPL_Queue_Pop(queue, &callback, &data)); |
| 282 |
|
| 283 |
assert(newtime >= time); |
| 284 |
time = newtime; |
| 285 |
} |
| 286 |
|
| 287 |
assert(OPL_Queue_IsEmpty(queue)); |
| 288 |
assert(!OPL_Queue_Pop(queue, &callback, &data)); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
#endif |