mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #4843 from ZyX-I/refactor-queue
lib/queue: Refactor queue.h
This commit is contained in:
commit
bd9715a9b5
@ -1,92 +1,94 @@
|
|||||||
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
|
// Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
|
||||||
*
|
//
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
// Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
// purpose with or without fee is hereby granted, provided that the above
|
||||||
* copyright notice and this permission notice appear in all copies.
|
// copyright notice and this permission notice appear in all copies.
|
||||||
*
|
//
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef QUEUE_H_
|
#ifndef NVIM_LIB_QUEUE_H
|
||||||
#define QUEUE_H_
|
#define NVIM_LIB_QUEUE_H
|
||||||
|
|
||||||
typedef void *QUEUE[2];
|
#include <stddef.h>
|
||||||
|
|
||||||
/* Private macros. */
|
#include "nvim/func_attr.h"
|
||||||
#define QUEUE_NEXT(q) (*(QUEUE **) &((*(q))[0]))
|
|
||||||
#define QUEUE_PREV(q) (*(QUEUE **) &((*(q))[1]))
|
|
||||||
#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
|
|
||||||
#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
|
|
||||||
|
|
||||||
/* Public macros. */
|
typedef struct _queue {
|
||||||
#define QUEUE_DATA(ptr, type, field) \
|
struct _queue *next;
|
||||||
((type *) ((char *) (ptr) - ((char *) &((type *) 0)->field)))
|
struct _queue *prev;
|
||||||
|
} QUEUE;
|
||||||
|
|
||||||
#define QUEUE_FOREACH(q, h) \
|
// Public macros.
|
||||||
for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))
|
#define QUEUE_DATA(ptr, type, field) \
|
||||||
|
((type *)((char *)(ptr) - offsetof(type, field)))
|
||||||
|
|
||||||
#define QUEUE_EMPTY(q) \
|
#define QUEUE_FOREACH(q, h) \
|
||||||
((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q))
|
for ( /* NOLINT(readability/braces) */ \
|
||||||
|
(q) = (h)->next; (q) != (h); (q) = (q)->next)
|
||||||
|
|
||||||
#define QUEUE_HEAD(q) \
|
// ffi.cdef is unable to swallow `bool` in place of `int` here.
|
||||||
(QUEUE_NEXT(q))
|
static inline int QUEUE_EMPTY(const QUEUE *const q)
|
||||||
|
FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
|
||||||
|
{
|
||||||
|
return q == q->next;
|
||||||
|
}
|
||||||
|
|
||||||
#define QUEUE_INIT(q) \
|
#define QUEUE_HEAD(q) (q)->next
|
||||||
do { \
|
|
||||||
QUEUE_NEXT(q) = (q); \
|
|
||||||
QUEUE_PREV(q) = (q); \
|
|
||||||
} \
|
|
||||||
while (0)
|
|
||||||
|
|
||||||
#define QUEUE_ADD(h, n) \
|
static inline void QUEUE_INIT(QUEUE *const q) FUNC_ATTR_ALWAYS_INLINE
|
||||||
do { \
|
{
|
||||||
QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \
|
q->next = q;
|
||||||
QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \
|
q->prev = q;
|
||||||
QUEUE_PREV(h) = QUEUE_PREV(n); \
|
}
|
||||||
QUEUE_PREV_NEXT(h) = (h); \
|
|
||||||
} \
|
|
||||||
while (0)
|
|
||||||
|
|
||||||
#define QUEUE_SPLIT(h, q, n) \
|
static inline void QUEUE_ADD(QUEUE *const h, QUEUE *const n)
|
||||||
do { \
|
FUNC_ATTR_ALWAYS_INLINE
|
||||||
QUEUE_PREV(n) = QUEUE_PREV(h); \
|
{
|
||||||
QUEUE_PREV_NEXT(n) = (n); \
|
h->prev->next = n->next;
|
||||||
QUEUE_NEXT(n) = (q); \
|
n->next->prev = h->prev;
|
||||||
QUEUE_PREV(h) = QUEUE_PREV(q); \
|
h->prev = n->prev;
|
||||||
QUEUE_PREV_NEXT(h) = (h); \
|
h->prev->next = h;
|
||||||
QUEUE_PREV(q) = (n); \
|
}
|
||||||
} \
|
|
||||||
while (0)
|
|
||||||
|
|
||||||
#define QUEUE_INSERT_HEAD(h, q) \
|
static inline void QUEUE_SPLIT(QUEUE *const h, QUEUE *const q, QUEUE *const n)
|
||||||
do { \
|
FUNC_ATTR_ALWAYS_INLINE
|
||||||
QUEUE_NEXT(q) = QUEUE_NEXT(h); \
|
{
|
||||||
QUEUE_PREV(q) = (h); \
|
n->prev = h->prev;
|
||||||
QUEUE_NEXT_PREV(q) = (q); \
|
n->prev->next = n;
|
||||||
QUEUE_NEXT(h) = (q); \
|
n->next = q;
|
||||||
} \
|
h->prev = q->prev;
|
||||||
while (0)
|
h->prev->next = h;
|
||||||
|
q->prev = n;
|
||||||
|
}
|
||||||
|
|
||||||
#define QUEUE_INSERT_TAIL(h, q) \
|
static inline void QUEUE_INSERT_HEAD(QUEUE *const h, QUEUE *const q)
|
||||||
do { \
|
FUNC_ATTR_ALWAYS_INLINE
|
||||||
QUEUE_NEXT(q) = (h); \
|
{
|
||||||
QUEUE_PREV(q) = QUEUE_PREV(h); \
|
q->next = h->next;
|
||||||
QUEUE_PREV_NEXT(q) = (q); \
|
q->prev = h;
|
||||||
QUEUE_PREV(h) = (q); \
|
q->next->prev = q;
|
||||||
} \
|
h->next = q;
|
||||||
while (0)
|
}
|
||||||
|
|
||||||
#define QUEUE_REMOVE(q) \
|
static inline void QUEUE_INSERT_TAIL(QUEUE *const h, QUEUE *const q)
|
||||||
do { \
|
FUNC_ATTR_ALWAYS_INLINE
|
||||||
QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \
|
{
|
||||||
QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \
|
q->next = h;
|
||||||
} \
|
q->prev = h->prev;
|
||||||
while (0)
|
q->prev->next = q;
|
||||||
|
h->prev = q;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* QUEUE_H_ */
|
static inline void QUEUE_REMOVE(QUEUE *const q) FUNC_ATTR_ALWAYS_INLINE
|
||||||
|
{
|
||||||
|
q->prev->next = q->next;
|
||||||
|
q->next->prev = q->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // NVIM_LIB_QUEUE_H
|
||||||
|
Loading…
Reference in New Issue
Block a user