From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by sourceware.org (Postfix) with ESMTPS id 2E5103857807 for ; Thu, 15 Oct 2020 18:50:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2E5103857807 Received: by mail-wm1-x342.google.com with SMTP id e2so305001wme.1 for ; Thu, 15 Oct 2020 11:50:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:cc:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=1HV0Y2mxO9UrXA8TAZdANpysCWD+avfg5PAwJw2MpOs=; b=DwzofVICJtICKmIy0psAqpGbaRO5kGSo8BHi1pIx2jwWf82zN1JjcPXX7mZd1jcd65 o3G9h8VG59wSc0acuFqZNkTfA5YKI62IuQLhBely9Pltq0K9KpnD5o4ht27pJ1IFg0zX xbruA1S0mf3UT39TJ+KEiupsYsMwpVBCd1uW8pF4qQrzAd+2mxPFKf9mnZjr7QABzF5F FzUzSJQ9hOinAkJIfsiBRAexK5kdztSqzruHRdfHy5XTJM6i2+sg9RCYuyOu8hALKCSX 6q7d9apVATZwL6ztXUcUVwOmZ0tEYphWjOwmqXNHJXYbN4pPubqaraojyyqdGG9B7kI1 TVlQ== X-Gm-Message-State: AOAM530xiNnTzouLFMkHnBgd2mwY1uVBK+PwhizL+DE7zJ0aiAANRNKN MHKLwGkUaPmxsD0yndCxVECNC3p8SdOI4A== X-Google-Smtp-Source: ABdhPJz9UtG9/CzuogaJu+PE4fjl0O3z8vX883YpS7Q0zt1Ma8GhPsJA2V7XL/t8iry+L3feQZ52WQ== X-Received: by 2002:a1c:3283:: with SMTP id y125mr49080wmy.61.1602787819715; Thu, 15 Oct 2020 11:50:19 -0700 (PDT) Received: from [192.168.1.10] (static-176-175-73-29.ftth.abo.bbox.fr. [176.175.73.29]) by smtp.gmail.com with ESMTPSA id c1sm236513wru.49.2020.10.15.11.50.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Oct 2020 11:50:18 -0700 (PDT) Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org, libc-alpha@sourceware.org Subject: Re: [PATCH] queue.3: list: Complete example To: Alejandro Colomar References: <20201015112901.2121-1-colomar.6.4.3@gmail.com> From: "Michael Kerrisk (man-pages)" Message-ID: Date: Thu, 15 Oct 2020 20:50:16 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <20201015112901.2121-1-colomar.6.4.3@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, 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-List-Received-Date: Thu, 15 Oct 2020 18:50:23 -0000 On 10/15/20 1:29 PM, Alejandro Colomar wrote: > Signed-off-by: Alejandro Colomar Thanks, Alex. Patch applied. Cheers, Michael > --- > 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 > -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/