From patchwork Sun Oct 11 15:51:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1380399 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=FCDuA9Bp; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C8RD70jMNz9sS8 for ; Mon, 12 Oct 2020 02:53:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B6FD5385ED4D; Sun, 11 Oct 2020 15:53:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6FD5385ED4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1602431587; bh=K467OmKFuye8/53EC3tKRrqeSTTX3QPveu/czbFqnds=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=FCDuA9Bp9VNxeQ3cZggkg87IiCT6SAnG7LHGyrIEr5z5xDbW26Ks0duH7rhryViwh 5jSZZvZGPWEhB7ulQlPA+e2cybItLjTQG7VJJdKUgjGQek667qc9JRQOjn7J4Ytbq5 qQLmVAsynyrHQnzM8ECGxEW5MfCZsJzLygnoAy0Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by sourceware.org (Postfix) with ESMTPS id 891B93857C4E for ; Sun, 11 Oct 2020 15:53:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 891B93857C4E Received: by mail-wr1-x442.google.com with SMTP id g12so16091016wrp.10 for ; Sun, 11 Oct 2020 08:53:05 -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=K467OmKFuye8/53EC3tKRrqeSTTX3QPveu/czbFqnds=; b=tKEkzkdVllDI0oDkGe8SM6mkJ31C8ii4PUdARnj9Xc2leDBhdAo2AokfTjnI4a64JH 10g67Q+VLpEp9DS/jAs7IuYvd23de2IYgPyEfdyHv4lYuSYXZsPP9eb5VcvY+CKdVhz8 hxW9j8+j3EtoRDAcrZsJUHgfhj7/r7t4ZYSALBkRASJMvB+pQP8ThRJrQ5Ysq1eR4wcV G7VixepGYus08fxetXNhkXGva7jy9cUMcWjMw9EK6DRJZCkr2OIrWA5oTPQFYzq390QY AnWdvcE1Nu1nFzrUeanKKXsCe7tgC6Cd0u2QSoiTEvVEC480rIiB0FPngZG8LGYIVtMk uELA== X-Gm-Message-State: AOAM531fW6NhDGO9U+jX45m13zf6Nty3PEkarcAt7nJnEXTdPBBFQVtM A0+B2Dls7ay0C4ZqkDoUB5E= X-Google-Smtp-Source: ABdhPJz3n+gvSOAG5vIzBtyIPrUwENTZDQ04Ac/7Vi/s7l4SWTCHL31ObtAAclkCsL0r/JzRraOc1Q== X-Received: by 2002:adf:c392:: with SMTP id p18mr24539798wrf.88.1602431584551; Sun, 11 Oct 2020 08:53:04 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id c132sm20239094wmf.25.2020.10.11.08.53.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Oct 2020 08:53:03 -0700 (PDT) To: mtk.manpages@gmail.com Subject: [PATCH] queue.3: slist: Complete example Date: Sun, 11 Oct 2020 17:51:21 +0200 Message-Id: <20201011155120.30482-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.6 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 | 102 +++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 43 deletions(-) diff --git a/man3/queue.3 b/man3/queue.3 index ea43f018b..63c43dadc 100644 --- a/man3/queue.3 +++ b/man3/queue.3 @@ -545,49 +545,8 @@ from the list. .\" .Fa head1 .\" and .\" .Fa head2 . -.Ss Singly-linked list example -.Bd -literal -SLIST_HEAD(slisthead, entry) head = - SLIST_HEAD_INITIALIZER(head); -struct slisthead *headp; /* Singly-linked List - head. */ -struct entry { - ... - SLIST_ENTRY(entry) entries; /* Singly-linked List. */ - ... -} *n1, *n2, *n3, *np; - -SLIST_INIT(&head); /* Initialize the list. */ - -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ -SLIST_INSERT_HEAD(&head, n1, entries); - -n2 = malloc(sizeof(struct entry)); /* Insert after. */ -SLIST_INSERT_AFTER(n1, n2, entries); - -SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ -free(n2); - -n3 = SLIST_FIRST(&head); -SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ -free(n3); - /* Forward traversal. */ -SLIST_FOREACH(np, &head, entries) - np\-> ... -.\" /* Safe forward traversal. */ -.\"SLIST_FOREACH_SAFE(np, &head, entries, np_temp) { -.\" np\->do_stuff(); -.\" ... -.\" SLIST_REMOVE(&head, np, entry, entries); -.\" free(np); -.\"} - -while (!SLIST_EMPTY(&head)) { /* List Deletion. */ - n1 = SLIST_FIRST(&head); - SLIST_REMOVE_HEAD(&head, entries); - free(n1); -} -.Ed +.Pp +See the EXAMPLES section below for an example program using a singly-linked list. .Ss Singly-linked tail queues A singly-linked tail queue is headed by a structure defined by the .Nm STAILQ_HEAD @@ -1409,6 +1368,63 @@ while (n1 != (void *)&head) { CIRCLEQ_INIT(&head); .Ed .Sh EXAMPLES +.Ss Singly-linked list example +.Bd -literal + +#include +#include +#include +#include + +struct entry { + int data; + SLIST_ENTRY(entry) entries; /* Singly-linked List. */ +}; + +SLIST_HEAD(slisthead, entry); + +int +main(void) +{ + struct entry *n1, *n2, *n3, *np; + struct slisthead head; /* Singly-linked List + head. */ + + SLIST_INIT(&head); /* Initialize the queue. */ + + n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ + SLIST_INSERT_HEAD(&head, n1, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert after. */ + SLIST_INSERT_AFTER(n1, n2, entries); + + SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ + free(n2); + + n3 = SLIST_FIRST(&head); + SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ + free(n3); + + for (int i = 0; i < 5; i++) { + n1 = malloc(sizeof(struct entry)); + SLIST_INSERT_HEAD(&head, n1, entries); + n1->data = i; + } + + /* Forward traversal. */ + SLIST_FOREACH(np, &head, entries) + printf("%i\en", np->data); + + while (!SLIST_EMPTY(&head)) { /* List Deletion. */ + n1 = SLIST_FIRST(&head); + SLIST_REMOVE_HEAD(&head, entries); + free(n1); + } + SLIST_INIT(&head); + + exit(EXIT_SUCCESS); +} +.Ed .Ss Tail queue example .Bd -literal #include