From patchwork Thu Oct 15 11:29:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 40725 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B203A3857821; Thu, 15 Oct 2020 11:31:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B203A3857821 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1602761465; bh=rS7rZdisqRK25K4NafNDT1h8bP3pxJvDqB9I/lTwFJg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=vN1OtpsW6qqOAaB6Fk50HgyIuM6LHtD1DVAbA7Zx//lFwbwUHI+rcvWwE9tT0g35x bd/6aIQrgiYhCV/ughfLXBZPsSe91VDjcT1F+C6wlSBnhgLQ2IJsfy3aNx4YARB56q +Plylo1iDu7Q+xUIBSvqTLhy7Zmee0E2qW1sPXO8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by sourceware.org (Postfix) with ESMTPS id 454053945C2B for ; Thu, 15 Oct 2020 11:31:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 454053945C2B Received: by mail-wr1-x441.google.com with SMTP id e17so2967982wru.12 for ; Thu, 15 Oct 2020 04:31:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rS7rZdisqRK25K4NafNDT1h8bP3pxJvDqB9I/lTwFJg=; b=TCB4ThwSRyg/IwhaIv2nIRkWNzj7R1Gq5UqO6FHQaaZg3/Z2CDPcok+9ELe+D6b4CF wJ1Ev7GkbDlbFZKFrXHXCrDZDb+2Z0dGVMgNDEaLVfwW0olyhs67O65DYIgNtJCzSfqL 3yetdeQa4PyJAvt3L7QAE7IDoaMj4vl56npKxED4IbMvC+hNHIGzuksva/icsFRGliaq sxBWnHdH2lWKfsPrj88D0uP/SsDEZqouduhwPTMZlPcFsb8W5MrxvEWmkwWBggMUGNCv nKEtNDel672VGAqDq5tUv+WB/Ztta8mS2jHKbJC5hoUf2+KDsC4wRzX8TOa+v6EIfK4+ yEWQ== X-Gm-Message-State: AOAM533mis8Yk/BYa7wJ9uK+QAwLreJcxTJ/hCZA4c/Fo8P/vRYSL42Q Egt2DGUa+HirChnGhF9uHyE= X-Google-Smtp-Source: ABdhPJzIZk5XFbXI6mtOpOz8d3E120nw7OfaEcCKHUMPrGqp4baoW+i+bf3TeZe7AwdQ9IpdbdWDnA== X-Received: by 2002:adf:dd46:: with SMTP id u6mr3785674wrm.295.1602761462259; Thu, 15 Oct 2020 04:31:02 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id a81sm4266523wmf.32.2020.10.15.04.31.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Oct 2020 04:31:01 -0700 (PDT) To: mtk.manpages@gmail.com Subject: [PATCH] queue.3: list: Complete example Date: Thu, 15 Oct 2020 13:29:02 +0200 Message-Id: <20201015112901.2121-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Cc: Alejandro Colomar , linux-man@vger.kernel.org, libc-alpha@sourceware.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Signed-off-by: Alejandro Colomar --- man3/queue.3 | 105 +++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/man3/queue.3 b/man3/queue.3 index fed8d126f..9cd6ff378 100644 --- a/man3/queue.3 +++ b/man3/queue.3 @@ -921,56 +921,8 @@ from the list. .\" .Fa head1 .\" and .\" .Fa head2 . -.Ss List example -.Bd -literal -LIST_HEAD(listhead, entry) head = - LIST_HEAD_INITIALIZER(head); -struct listhead *headp; /* List head. */ -struct entry { - ... - LIST_ENTRY(entry) entries; /* List. */ - ... -} *n1, *n2, *n3, *np, *np_temp; - -LIST_INIT(&head); /* Initialize the list. */ - -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ -LIST_INSERT_HEAD(&head, n1, entries); - -n2 = malloc(sizeof(struct entry)); /* Insert after. */ -LIST_INSERT_AFTER(n1, n2, entries); - -n3 = malloc(sizeof(struct entry)); /* Insert before. */ -LIST_INSERT_BEFORE(n2, n3, entries); - -LIST_REMOVE(n2, entries); /* Deletion. */ -free(n2); - /* Forward traversal. */ -LIST_FOREACH(np, &head, entries) - np\-> ... - -.\" /* Safe forward traversal. */ -.\" LIST_FOREACH_SAFE(np, &head, entries, np_temp) { -.\" np\->do_stuff(); -.\" ... -.\" LIST_REMOVE(np, entries); -.\" free(np); -.\" } -.\" -while (!LIST_EMPTY(&head)) { /* List Deletion. */ - n1 = LIST_FIRST(&head); - LIST_REMOVE(n1, entries); - free(n1); -} - -n1 = LIST_FIRST(&head); /* Faster List Deletion. */ -while (n1 != NULL) { - n2 = LIST_NEXT(n1, entries); - free(n1); - n1 = n2; -} -LIST_INIT(&head); -.Ed +.Pp +See the EXAMPLES section below for an example program using a linked list. .Ss Tail queues A tail queue is headed by a structure defined by the .Nm TAILQ_HEAD @@ -1376,6 +1328,59 @@ main(void) exit(EXIT_SUCCESS); } .Ed +.Ss List example +.Bd -literal +#include +#include +#include +#include + +struct entry { + int data; + LIST_ENTRY(entry) entries; /* List. */ +}; + +LIST_HEAD(listhead, entry); + +int +main(void) +{ + struct entry *n1, *n2, *n3, *np; + struct listhead head; /* List head. */ + int i; + + LIST_INIT(&head); /* Initialize the list. */ + + n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ + LIST_INSERT_HEAD(&head, n1, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert after. */ + LIST_INSERT_AFTER(n1, n2, entries); + + n3 = malloc(sizeof(struct entry)); /* Insert before. */ + LIST_INSERT_BEFORE(n2, n3, entries); + + i = 0; /* Forward traversal. */ + LIST_FOREACH(np, &head, entries) + np->data = i++; + + LIST_REMOVE(n2, entries); /* Deletion. */ + free(n2); + /* Forward traversal. */ + LIST_FOREACH(np, &head, entries) + printf("%i\en", np->data); + /* List Deletion. */ + n1 = LIST_FIRST(&head); + while (n1 != NULL) { + n2 = LIST_NEXT(n1, entries); + free(n1); + n1 = n2; + } + LIST_INIT(&head); + + exit(EXIT_SUCCESS); +} +.Ed .Ss Tail queue example .Bd -literal #include