cam_queue.c (8bad620d540097ff831bebc109b57c864feb053f) | cam_queue.c (5a526431f84768cc809e654ae2f3e9b66d0066bd) |
---|---|
1/* 2 * CAM request queue management functions. 3 * 4 * Copyright (c) 1997 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * | 1/* 2 * CAM request queue management functions. 3 * 4 * Copyright (c) 1997 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * |
28 * $Id: cam_queue.c,v 1.1 1998/09/15 06:33:23 gibbs Exp $ | 28 * $Id: cam_queue.c,v 1.2 1999/04/07 22:57:48 gibbs Exp $ |
29 */ 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/types.h> 33#include <sys/malloc.h> 34 35#include <cam/cam.h> 36#include <cam/cam_ccb.h> --- 30 unchanged lines hidden (view full) --- 67 camq->array_size = size; 68 if (camq->array_size != 0) { 69 camq->queue_array = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*), 70 M_DEVBUF, M_NOWAIT); 71 if (camq->queue_array == NULL) { 72 printf("camq_init: - cannot malloc array!\n"); 73 return (1); 74 } | 29 */ 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/types.h> 33#include <sys/malloc.h> 34 35#include <cam/cam.h> 36#include <cam/cam_ccb.h> --- 30 unchanged lines hidden (view full) --- 67 camq->array_size = size; 68 if (camq->array_size != 0) { 69 camq->queue_array = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*), 70 M_DEVBUF, M_NOWAIT); 71 if (camq->queue_array == NULL) { 72 printf("camq_init: - cannot malloc array!\n"); 73 return (1); 74 } |
75 /* 76 * Heap algorithms like everything numbered from 1, so 77 * offset our pointer into the heap array by one element. 78 */ 79 camq->queue_array--; |
|
75 } 76 return (0); 77} 78 79/* 80 * Free a camq structure. This should only be called if a controller 81 * driver failes somehow during its attach routine or is unloaded and has 82 * obtained a camq structure. The XPT should ensure that the queue --- 7 unchanged lines hidden (view full) --- 90 free(queue, M_DEVBUF); 91 } 92} 93 94void 95camq_fini(struct camq *queue) 96{ 97 if (queue->queue_array != NULL) { | 80 } 81 return (0); 82} 83 84/* 85 * Free a camq structure. This should only be called if a controller 86 * driver failes somehow during its attach routine or is unloaded and has 87 * obtained a camq structure. The XPT should ensure that the queue --- 7 unchanged lines hidden (view full) --- 95 free(queue, M_DEVBUF); 96 } 97} 98 99void 100camq_fini(struct camq *queue) 101{ 102 if (queue->queue_array != NULL) { |
103 /* 104 * Heap algorithms like everything numbered from 1, so 105 * our pointer into the heap array is offset by one element. 106 */ 107 queue->queue_array++; |
|
98 free(queue->queue_array, M_DEVBUF); 99 } 100} 101 102u_int32_t 103camq_resize(struct camq *queue, int new_size) 104{ 105 cam_pinfo **new_array; --- 4 unchanged lines hidden (view full) --- 110 "queued entries."); 111#endif 112 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *), 113 M_DEVBUF, M_NOWAIT); 114 if (new_array == NULL) { 115 /* Couldn't satisfy request */ 116 return (CAM_RESRC_UNAVAIL); 117 } | 108 free(queue->queue_array, M_DEVBUF); 109 } 110} 111 112u_int32_t 113camq_resize(struct camq *queue, int new_size) 114{ 115 cam_pinfo **new_array; --- 4 unchanged lines hidden (view full) --- 120 "queued entries."); 121#endif 122 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *), 123 M_DEVBUF, M_NOWAIT); 124 if (new_array == NULL) { 125 /* Couldn't satisfy request */ 126 return (CAM_RESRC_UNAVAIL); 127 } |
128 /* 129 * Heap algorithms like everything numbered from 1, so 130 * remember that our pointer into the heap array is offset 131 * by one element. 132 */ |
|
118 if (queue->queue_array != NULL) { | 133 if (queue->queue_array != NULL) { |
134 queue->queue_array++; |
|
119 bcopy(queue->queue_array, new_array, 120 queue->entries * sizeof(cam_pinfo *)); 121 free(queue->queue_array, M_DEVBUF); 122 } | 135 bcopy(queue->queue_array, new_array, 136 queue->entries * sizeof(cam_pinfo *)); 137 free(queue->queue_array, M_DEVBUF); 138 } |
123 queue->queue_array = new_array; | 139 queue->queue_array = new_array-1; |
124 queue->array_size = new_size; 125 return (CAM_REQ_CMP); 126} 127 128/* 129 * camq_insert: Given an array of cam_pinfo* elememnts with | 140 queue->array_size = new_size; 141 return (CAM_REQ_CMP); 142} 143 144/* 145 * camq_insert: Given an array of cam_pinfo* elememnts with |
130 * the Heap(0, num_elements) property and array_size - num_elements >= 1, 131 * output Heap(0, num_elements+1) including new_entry in the array. | 146 * the Heap(1, num_elements) property and array_size - num_elements >= 1, 147 * output Heap(1, num_elements+1) including new_entry in the array. |
132 */ 133void 134camq_insert(struct camq *queue, cam_pinfo *new_entry) 135{ 136#ifdef DIAGNOSTIC 137 if (queue->entries >= queue->array_size) 138 panic("camq_insert: Attempt to insert into a full queue"); 139#endif | 148 */ 149void 150camq_insert(struct camq *queue, cam_pinfo *new_entry) 151{ 152#ifdef DIAGNOSTIC 153 if (queue->entries >= queue->array_size) 154 panic("camq_insert: Attempt to insert into a full queue"); 155#endif |
156 queue->entries++; |
|
140 queue->queue_array[queue->entries] = new_entry; 141 new_entry->index = queue->entries; 142 if (queue->entries != 0) 143 heap_up(queue->queue_array, queue->entries); | 157 queue->queue_array[queue->entries] = new_entry; 158 new_entry->index = queue->entries; 159 if (queue->entries != 0) 160 heap_up(queue->queue_array, queue->entries); |
144 queue->entries++; | |
145} 146 147/* 148 * camq_remove: Given an array of cam_pinfo* elevements with the | 161} 162 163/* 164 * camq_remove: Given an array of cam_pinfo* elevements with the |
149 * Heap(0, num_elements) property and an index such that 0 <= index <= 150 * num_elements, remove that entry and restore the Heap(0, num_elements-1) | 165 * Heap(1, num_elements) property and an index such that 1 <= index <= 166 * num_elements, remove that entry and restore the Heap(1, num_elements-1) |
151 * property. 152 */ 153cam_pinfo * 154camq_remove(struct camq *queue, int index) 155{ 156 cam_pinfo *removed_entry; 157 | 167 * property. 168 */ 169cam_pinfo * 170camq_remove(struct camq *queue, int index) 171{ 172 cam_pinfo *removed_entry; 173 |
158 if ((queue->entries - index) <= 0) | 174 if (index == 0 || index > queue->entries) |
159 return (NULL); 160 removed_entry = queue->queue_array[index]; | 175 return (NULL); 176 removed_entry = queue->queue_array[index]; |
161 queue->entries--; | |
162 if (queue->entries != index) { 163 queue->queue_array[index] = queue->queue_array[queue->entries]; 164 queue->queue_array[index]->index = index; | 177 if (queue->entries != index) { 178 queue->queue_array[index] = queue->queue_array[queue->entries]; 179 queue->queue_array[index]->index = index; |
165 heap_down(queue->queue_array, index, queue->entries); | 180 heap_down(queue->queue_array, index, queue->entries - 1); |
166 } 167 removed_entry->index = CAM_UNQUEUED_INDEX; | 181 } 182 removed_entry->index = CAM_UNQUEUED_INDEX; |
183 queue->entries--; |
|
168 return (removed_entry); 169} 170 171/* 172 * camq_change_priority: Given an array of cam_pinfo* elements with the | 184 return (removed_entry); 185} 186 187/* 188 * camq_change_priority: Given an array of cam_pinfo* elements with the |
173 * Heap(0, num_entries) property, an index such that 0 <= index <= num_elements, | 189 * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements, |
174 * and an new priority for the element at index, change the priority of 175 * element index and restore the Heap(0, num_elements) property. 176 */ 177void 178camq_change_priority(struct camq *queue, int index, u_int32_t new_priority) 179{ 180 if (new_priority > queue->queue_array[index]->priority) { 181 queue->queue_array[index]->priority = new_priority; --- 165 unchanged lines hidden (view full) --- 347 queue_array[j] = queue_array[i]; 348 queue_array[i] = temp_qentry; 349 queue_array[j]->index = j; 350 queue_array[i]->index = i; 351} 352 353/* 354 * heap_up: Given an array of cam_pinfo* elements with the | 190 * and an new priority for the element at index, change the priority of 191 * element index and restore the Heap(0, num_elements) property. 192 */ 193void 194camq_change_priority(struct camq *queue, int index, u_int32_t new_priority) 195{ 196 if (new_priority > queue->queue_array[index]->priority) { 197 queue->queue_array[index]->priority = new_priority; --- 165 unchanged lines hidden (view full) --- 363 queue_array[j] = queue_array[i]; 364 queue_array[i] = temp_qentry; 365 queue_array[j]->index = j; 366 queue_array[i]->index = i; 367} 368 369/* 370 * heap_up: Given an array of cam_pinfo* elements with the |
355 * Heap(0, new_index-1) property and a new element in location 356 * new_index, output Heap(0, new_index). | 371 * Heap(1, new_index-1) property and a new element in location 372 * new_index, output Heap(1, new_index). |
357 */ 358static void 359heap_up(cam_pinfo **queue_array, int new_index) 360{ 361 int child; 362 int parent; 363 364 child = new_index; 365 | 373 */ 374static void 375heap_up(cam_pinfo **queue_array, int new_index) 376{ 377 int child; 378 int parent; 379 380 child = new_index; 381 |
366 while (child != 0) { | 382 while (child != 1) { |
367 | 383 |
368 parent = (child - 1) >> 1; | 384 parent = child >> 1; |
369 if (queue_cmp(queue_array, parent, child) <= 0) 370 break; 371 swap(queue_array, parent, child); 372 child = parent; 373 } 374} 375 376/* 377 * heap_down: Given an array of cam_pinfo* elements with the | 385 if (queue_cmp(queue_array, parent, child) <= 0) 386 break; 387 swap(queue_array, parent, child); 388 child = parent; 389 } 390} 391 392/* 393 * heap_down: Given an array of cam_pinfo* elements with the |
378 * Heap(index + 1, num_entries - 1) property with index containing 379 * an unsorted entry, output Heap(0, num_entries - 1). | 394 * Heap(index + 1, num_entries) property with index containing 395 * an unsorted entry, output Heap(index, num_entries). |
380 */ 381static void 382heap_down(cam_pinfo **queue_array, int index, int num_entries) 383{ 384 int child; 385 int parent; 386 387 parent = index; | 396 */ 397static void 398heap_down(cam_pinfo **queue_array, int index, int num_entries) 399{ 400 int child; 401 int parent; 402 403 parent = index; |
388 child = (parent << 1) + 1; 389 for (; child < num_entries; child = (parent << 1) + 1) { | 404 child = parent << 1; 405 for (; child <= num_entries; child = parent << 1) { |
390 | 406 |
391 if (child + 1 < num_entries) { | 407 if (child < num_entries) { |
392 /* child+1 is the right child of parent */ 393 if (queue_cmp(queue_array, child + 1, child) < 0) 394 child++; 395 } 396 /* child is now the least child of parent */ 397 if (queue_cmp(queue_array, parent, child) <= 0) 398 break; 399 swap(queue_array, child, parent); 400 parent = child; 401 } 402} | 408 /* child+1 is the right child of parent */ 409 if (queue_cmp(queue_array, child + 1, child) < 0) 410 child++; 411 } 412 /* child is now the least child of parent */ 413 if (queue_cmp(queue_array, parent, child) <= 0) 414 break; 415 swap(queue_array, child, parent); 416 parent = child; 417 } 418} |
403 | |