Skip to content

Commit c8ad4d8

Browse files
committed
Constify the arguments of ilist.c/h functions
Const qualifiers ensure that we don't do something stupid in the function implementation. Additionally they clarify the interface. As an example: void slist_delete(slist_head *head, const slist_node *node) Here one can instantly tell that node->next is not going to be set to NULL. Finally, const qualifiers potentially allow the compiler to do more optimizations. This being said, no benchmarking was done for this patch. The functions that return non-const pointers like slist_next_node(), dclist_next_node() etc. are not affected by the patch intentionally. Author: Aleksander Alekseev Reviewed-by: Andres Freund Discussion: https://postgr.es/m/CAJ7c6TM2%3D08mNKD9aJg8vEY9hd%2BG4L7%2BNvh30UiNT3kShgRgNg%40mail.gmail.com
1 parent 881fa86 commit c8ad4d8

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/backend/lib/ilist.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Caution: this is O(n); consider using slist_delete_current() instead.
2929
*/
3030
void
31-
slist_delete(slist_head *head, slist_node *node)
31+
slist_delete(slist_head *head, const slist_node *node)
3232
{
3333
slist_node *last = &head->head;
3434
slist_node *cur;
@@ -57,7 +57,7 @@ slist_delete(slist_head *head, slist_node *node)
5757
* Validate that 'node' is a member of 'head'
5858
*/
5959
void
60-
dlist_member_check(dlist_head *head, dlist_node *node)
60+
dlist_member_check(const dlist_head *head, const dlist_node *node)
6161
{
6262
dlist_iter iter;
6363

@@ -73,7 +73,7 @@ dlist_member_check(dlist_head *head, dlist_node *node)
7373
* Verify integrity of a doubly linked list
7474
*/
7575
void
76-
dlist_check(dlist_head *head)
76+
dlist_check(const dlist_head *head)
7777
{
7878
dlist_node *cur;
7979

@@ -110,7 +110,7 @@ dlist_check(dlist_head *head)
110110
* Verify integrity of a singly linked list
111111
*/
112112
void
113-
slist_check(slist_head *head)
113+
slist_check(const slist_head *head)
114114
{
115115
slist_node *cur;
116116

src/include/lib/ilist.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ typedef struct slist_mutable_iter
286286
/* Prototypes for functions too big to be inline */
287287

288288
/* Caution: this is O(n); consider using slist_delete_current() instead */
289-
extern void slist_delete(slist_head *head, slist_node *node);
289+
extern void slist_delete(slist_head *head, const slist_node *node);
290290

291291
#ifdef ILIST_DEBUG
292-
extern void dlist_member_check(dlist_head *head, dlist_node *node);
293-
extern void dlist_check(dlist_head *head);
294-
extern void slist_check(slist_head *head);
292+
extern void dlist_member_check(const dlist_head *head, const dlist_node *node);
293+
extern void dlist_check(const dlist_head *head);
294+
extern void slist_check(const slist_head *head);
295295
#else
296296
/*
297297
* These seemingly useless casts to void are here to keep the compiler quiet
@@ -322,7 +322,7 @@ dlist_init(dlist_head *head)
322322
* An empty list has either its first 'next' pointer set to NULL, or to itself.
323323
*/
324324
static inline bool
325-
dlist_is_empty(dlist_head *head)
325+
dlist_is_empty(const dlist_head *head)
326326
{
327327
dlist_check(head);
328328

@@ -465,7 +465,7 @@ dlist_move_tail(dlist_head *head, dlist_node *node)
465465
* Caution: unreliable if 'node' is not in the list.
466466
*/
467467
static inline bool
468-
dlist_has_next(dlist_head *head, dlist_node *node)
468+
dlist_has_next(const dlist_head *head, const dlist_node *node)
469469
{
470470
return node->next != &head->head;
471471
}
@@ -475,7 +475,7 @@ dlist_has_next(dlist_head *head, dlist_node *node)
475475
* Caution: unreliable if 'node' is not in the list.
476476
*/
477477
static inline bool
478-
dlist_has_prev(dlist_head *head, dlist_node *node)
478+
dlist_has_prev(const dlist_head *head, const dlist_node *node)
479479
{
480480
return node->prev != &head->head;
481481
}
@@ -629,7 +629,7 @@ dclist_init(dclist_head *head)
629629
* Returns true if the list is empty, otherwise false.
630630
*/
631631
static inline bool
632-
dclist_is_empty(dclist_head *head)
632+
dclist_is_empty(const dclist_head *head)
633633
{
634634
Assert(dlist_is_empty(&head->dlist) == (head->count == 0));
635635
return (head->count == 0);
@@ -773,7 +773,7 @@ dclist_move_tail(dclist_head *head, dlist_node *node)
773773
* Caution: 'node' must be a member of 'head'.
774774
*/
775775
static inline bool
776-
dclist_has_next(dclist_head *head, dlist_node *node)
776+
dclist_has_next(const dclist_head *head, const dlist_node *node)
777777
{
778778
dlist_member_check(&head->dlist, node);
779779
Assert(head->count > 0);
@@ -788,7 +788,7 @@ dclist_has_next(dclist_head *head, dlist_node *node)
788788
* Caution: 'node' must be a member of 'head'.
789789
*/
790790
static inline bool
791-
dclist_has_prev(dclist_head *head, dlist_node *node)
791+
dclist_has_prev(const dclist_head *head, const dlist_node *node)
792792
{
793793
dlist_member_check(&head->dlist, node);
794794
Assert(head->count > 0);
@@ -866,7 +866,7 @@ dclist_tail_node(dclist_head *head)
866866
* Returns the stored number of entries in 'head'
867867
*/
868868
static inline uint32
869-
dclist_count(dclist_head *head)
869+
dclist_count(const dclist_head *head)
870870
{
871871
Assert(dlist_is_empty(&head->dlist) == (head->count == 0));
872872

@@ -929,7 +929,7 @@ slist_init(slist_head *head)
929929
* Is the list empty?
930930
*/
931931
static inline bool
932-
slist_is_empty(slist_head *head)
932+
slist_is_empty(const slist_head *head)
933933
{
934934
slist_check(head);
935935

@@ -977,7 +977,7 @@ slist_pop_head_node(slist_head *head)
977977
* Check whether 'node' has a following node.
978978
*/
979979
static inline bool
980-
slist_has_next(slist_head *head, slist_node *node)
980+
slist_has_next(const slist_head *head, const slist_node *node)
981981
{
982982
slist_check(head);
983983

0 commit comments

Comments
 (0)