Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc99baa

Browse files
committed
Improve pg_list.h's linitial(), lsecond() and co macros
Prior to this commit, the linitial(), lsecond(), lthird(), lfourth()macros and their int and Oid list cousins would call their correspondinginlined function to fetch the cell of interest. Those inline functionswere kind enough to return NULL if the particular cell did not exist.Unfortunately, the care that these functions took was of no relevance tothe calling macros as they proceeded to directly dereference the returnedvalue without any regard to whether that value was NULL or not. If it hadbeen, we'd have segfaulted.Of course, the fact that we would have segfaulted on misuse of thesemacros just goes to prove that nobody is relying on the empty or list toosmall checks. So here we just get rid of those checks completely.The existing inline functions have been left alone as someone may be usingthose directly. We just replace the call within each macro to uselist_nth_cell().For the llast*() case we require a new list_last_cell() inline function toget away from the multiple evaluation hazard that we'd get if we fetched->length on the macro's parameter.Author: David RowleyReviewed-by: Tom LaneDiscussion:https://postgr.es/m/CAApHDvpo1zj9KhEpU2cCRZfSM3Q6XGdhzuAS2v79PH7WJBkYVA@mail.gmail.com
1 parent4d29e6d commitcc99baa

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

‎src/include/nodes/pg_list.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,35 +186,34 @@ list_length(const List *l)
186186
* linitial() than lfirst(): given a List, lsecond() returns the data
187187
* in the second list cell.
188188
*/
189-
190189
#definelfirst(lc)((lc)->ptr_value)
191190
#definelfirst_int(lc)((lc)->int_value)
192191
#definelfirst_oid(lc)((lc)->oid_value)
193192
#definelfirst_node(type,lc)castNode(type, lfirst(lc))
194193

195-
#definelinitial(l)lfirst(list_head(l))
196-
#definelinitial_int(l)lfirst_int(list_head(l))
197-
#definelinitial_oid(l)lfirst_oid(list_head(l))
194+
#definelinitial(l)lfirst(list_nth_cell(l, 0))
195+
#definelinitial_int(l)lfirst_int(list_nth_cell(l, 0))
196+
#definelinitial_oid(l)lfirst_oid(list_nth_cell(l, 0))
198197
#definelinitial_node(type,l)castNode(type, linitial(l))
199198

200-
#definelsecond(l)lfirst(list_second_cell(l))
201-
#definelsecond_int(l)lfirst_int(list_second_cell(l))
202-
#definelsecond_oid(l)lfirst_oid(list_second_cell(l))
199+
#definelsecond(l)lfirst(list_nth_cell(l, 1))
200+
#definelsecond_int(l)lfirst_int(list_nth_cell(l, 1))
201+
#definelsecond_oid(l)lfirst_oid(list_nth_cell(l, 1))
203202
#definelsecond_node(type,l)castNode(type, lsecond(l))
204203

205-
#definelthird(l)lfirst(list_third_cell(l))
206-
#definelthird_int(l)lfirst_int(list_third_cell(l))
207-
#definelthird_oid(l)lfirst_oid(list_third_cell(l))
204+
#definelthird(l)lfirst(list_nth_cell(l, 2))
205+
#definelthird_int(l)lfirst_int(list_nth_cell(l, 2))
206+
#definelthird_oid(l)lfirst_oid(list_nth_cell(l, 2))
208207
#definelthird_node(type,l)castNode(type, lthird(l))
209208

210-
#definelfourth(l)lfirst(list_fourth_cell(l))
211-
#definelfourth_int(l)lfirst_int(list_fourth_cell(l))
212-
#definelfourth_oid(l)lfirst_oid(list_fourth_cell(l))
209+
#definelfourth(l)lfirst(list_nth_cell(l, 3))
210+
#definelfourth_int(l)lfirst_int(list_nth_cell(l, 3))
211+
#definelfourth_oid(l)lfirst_oid(list_nth_cell(l, 3))
213212
#definelfourth_node(type,l)castNode(type, lfourth(l))
214213

215-
#definellast(l)lfirst(list_tail(l))
216-
#definellast_int(l)lfirst_int(list_tail(l))
217-
#definellast_oid(l)lfirst_oid(list_tail(l))
214+
#definellast(l)lfirst(list_last_cell(l))
215+
#definellast_int(l)lfirst_int(list_last_cell(l))
216+
#definellast_oid(l)lfirst_oid(list_last_cell(l))
218217
#definellast_node(type,l)castNode(type, llast(l))
219218

220219
/*
@@ -269,6 +268,16 @@ list_nth_cell(const List *list, int n)
269268
return&list->elements[n];
270269
}
271270

271+
/*
272+
* Return the last cell in a non-NIL List.
273+
*/
274+
staticinlineListCell*
275+
list_last_cell(constList*list)
276+
{
277+
Assert(list!=NIL);
278+
return&list->elements[list->length-1];
279+
}
280+
272281
/*
273282
* Return the pointer value contained in the n'th element of the
274283
* specified list. (List elements begin at 0.)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp