1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef QUEUES_INCLUDED
24 #define QUEUES_INCLUDED
25 
26 /**
27   @file storage/myisam/queues.h
28   Code for handling of priority queues.
29   Implementation of queues from "Algoritms in C" by Robert Sedgewick.
30 */
31 
32 #include <stddef.h>
33 #include <sys/types.h>
34 
35 #include "my_inttypes.h"
36 #include "mysql/psi/psi_memory.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 struct QUEUE {
43   uchar **root;
44   void *first_cmp_arg;
45   uint elements;
46   uint max_elements;
47   uint offset_to_key; /* compare is done on element+offset */
48   int max_at_top;     /* Normally 1, set to -1 if queue_top gives max */
49   int (*compare)(void *, uchar *, uchar *);
50   uint auto_extent;
51 };
52 
53 void _downheap(QUEUE *queue, uint idx);
54 void queue_fix(QUEUE *queue);
55 
56 #define queue_top(queue) ((queue)->root[1])
57 #define queue_element(queue, index) ((queue)->root[index + 1])
58 #define queue_end(queue) ((queue)->root[(queue)->elements])
59 
queue_replaced(QUEUE * queue)60 static inline void queue_replaced(QUEUE *queue) { _downheap(queue, 1); }
61 
queue_set_max_at_top(QUEUE * queue,int set_arg)62 static inline void queue_set_max_at_top(QUEUE *queue, int set_arg) {
63   queue->max_at_top = set_arg ? -1 : 1;
64 }
65 
66 typedef int (*queue_compare)(void *, uchar *, uchar *);
67 
68 int init_queue(QUEUE *queue, PSI_memory_key psi_key, uint max_elements,
69                uint offset_to_key, bool max_at_top, queue_compare compare,
70                void *first_cmp_arg);
71 int reinit_queue(QUEUE *queue, PSI_memory_key psi_key, uint max_elements,
72                  uint offset_to_key, bool max_at_top, queue_compare compare,
73                  void *first_cmp_arg);
74 void delete_queue(QUEUE *queue);
75 void queue_insert(QUEUE *queue, uchar *element);
76 uchar *queue_remove(QUEUE *queue, uint idx);
77 
queue_remove_all(QUEUE * queue)78 static inline void queue_remove_all(QUEUE *queue) { queue->elements = 0; }
79 
queue_is_full(QUEUE * queue)80 static inline bool queue_is_full(QUEUE *queue) {
81   return queue->elements == queue->max_elements;
82 }
83 
is_queue_inited(QUEUE * queue)84 static inline bool is_queue_inited(QUEUE *queue) {
85   return queue->root != nullptr;
86 }
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif  // QUEUES_INCLUDED
93