Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src amend the new destroy function to take function pointers.



details:   https://anonhg.NetBSD.org/src/rev/a0af9121fcb2
branches:  trunk
changeset: 797538:a0af9121fcb2
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jul 20 20:17:21 2014 +0000

description:
amend the new destroy function to take function pointers.

diffstat:

 include/search.h                  |   8 +--
 lib/libc/stdlib/hcreate.3         |  32 +++++-------
 lib/libc/stdlib/hcreate.c         |  23 ++++----
 tests/lib/libc/stdlib/t_hsearch.c |  95 +++++++++++++++++---------------------
 4 files changed, 72 insertions(+), 86 deletions(-)

diffs (truncated from 421 to 300 lines):

diff -r e47ffd465c59 -r a0af9121fcb2 include/search.h
--- a/include/search.h  Sun Jul 20 19:19:49 2014 +0000
+++ b/include/search.h  Sun Jul 20 20:17:21 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: search.h,v 1.21 2014/07/20 13:34:17 christos Exp $     */
+/*     $NetBSD: search.h,v 1.22 2014/07/20 20:17:21 christos Exp $     */
 
 /*
  * Written by J.T. Conklin <jtc%NetBSD.org@localhost>
@@ -62,12 +62,10 @@
 ENTRY  *hsearch(ENTRY, ACTION);
 
 #ifdef _NETBSD_SOURCE
-#define FREE_KEY 1
-#define FREE_DATA 2
-void    hdestroy1(int);
+void    hdestroy1(void (*)(void *), void (*)(void *));
 int     hcreate_r(size_t, struct hsearch_data *);
 void    hdestroy_r(struct hsearch_data *);
-void    hdestroy1_r(struct hsearch_data *, int);
+void    hdestroy1_r(struct hsearch_data *, void (*)(void *), void (*)(void *));
 int     hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *);
 #endif /* _NETBSD_SOURCE */
 
diff -r e47ffd465c59 -r a0af9121fcb2 lib/libc/stdlib/hcreate.3
--- a/lib/libc/stdlib/hcreate.3 Sun Jul 20 19:19:49 2014 +0000
+++ b/lib/libc/stdlib/hcreate.3 Sun Jul 20 20:17:21 2014 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: hcreate.3,v 1.12 2014/07/20 13:41:14 wiz Exp $
+.\"    $NetBSD: hcreate.3,v 1.13 2014/07/20 20:17:21 christos Exp $
 .\"
 .\" Copyright (c) 1999 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -51,11 +51,11 @@
 .Ft void
 .Fn hdestroy "void"
 .Ft void
-.Fn hdestroy1 "int flags"
+.Fn hdestroy1 "void (*freekey)(void *)" "void (*freedata)(void *)"
 .Ft void
 .Fn hdestroy_r "struct hsearch_data *table"
 .Ft void
-.Fn hdestroy1_r "struct hsearch_data *table" "int flags"
+.Fn hdestroy1_r "struct hsearch_data *table" "void (*freekey)(void *)" "void (*freedata)(void *)"
 .Ft ENTRY *
 .Fn hsearch "ENTRY item" "ACTION action"
 .Ft int
@@ -166,27 +166,23 @@
 .Fn hdestroy1
 and
 .Fn hdestroy1_r
-allow controlling if the
+allow controlling how the
 .Fa key
 or
 .Fa value
 will be freed using the
-.Fa flags
-argument.
-If the bit
-.Dv FREE_KEY
-is set, then the
+provided functions in the
+.Fa freekey
+and
+.Fa freedata
+arguments.
+If they are
+.Dv NULL ,
+then
 .Fa key
-of each entry will be
-passed to
-.Xr free 3 .
-If the bit
-.Dv FREE_VALUE
-is set, then the
+and
 .Fa value
-of each entry will be
-passed to
-.Xr free 3 .
+are not freed.
 .Pp
 The
 .Fn hcreate_r ,
diff -r e47ffd465c59 -r a0af9121fcb2 lib/libc/stdlib/hcreate.c
--- a/lib/libc/stdlib/hcreate.c Sun Jul 20 19:19:49 2014 +0000
+++ b/lib/libc/stdlib/hcreate.c Sun Jul 20 20:17:21 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: hcreate.c,v 1.9 2014/07/20 13:34:17 christos Exp $ */
+/* $NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $ */
 
 /*
  * Copyright (c) 2001 Christopher G. Demetriou
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: hcreate.c,v 1.9 2014/07/20 13:34:17 christos Exp $");
+__RCSID("$NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #if !defined(lint)
@@ -141,20 +141,21 @@
 }
 
 void
-hdestroy1(int flags)
+hdestroy1(void (*freekey)(void *), void (*freedata)(void *))
 {
        _DIAGASSERT(htable.table != NULL);
-       hdestroy1_r(&htable, flags);
+       hdestroy1_r(&htable, freekey, freedata);
 }
 
 void
 hdestroy(void)
 {
-       hdestroy1(0);
+       hdestroy1(NULL, NULL);
 }
 
 void
-hdestroy1_r(struct hsearch_data *head, int flags)
+hdestroy1_r(struct hsearch_data *head, void (*freekey)(void *),
+    void (*freedata)(void *))
 {
        struct internal_entry *ie;
        size_t idx;
@@ -172,10 +173,10 @@
                while (!SLIST_EMPTY(&table[idx])) {
                        ie = SLIST_FIRST(&table[idx]);
                        SLIST_REMOVE_HEAD(&table[idx], link);
-                       if (flags & FREE_KEY)
-                               free(ie->ent.key);
-                       if (flags & FREE_DATA)
-                               free(ie->ent.data);
+                       if (freekey)
+                               (*freekey)(ie->ent.key);
+                       if (freedata)
+                               (*freedata)(ie->ent.data);
                        free(ie);
                }
        }
@@ -185,7 +186,7 @@
 void
 hdestroy_r(struct hsearch_data *head)
 {
-       hdestroy1_r(head, 0);
+       hdestroy1_r(head, NULL, NULL);
 }
 
 ENTRY *
diff -r e47ffd465c59 -r a0af9121fcb2 tests/lib/libc/stdlib/t_hsearch.c
--- a/tests/lib/libc/stdlib/t_hsearch.c Sun Jul 20 19:19:49 2014 +0000
+++ b/tests/lib/libc/stdlib/t_hsearch.c Sun Jul 20 20:17:21 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_hsearch.c,v 1.3 2011/09/15 14:51:06 christos Exp $ */
+/* $NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $ */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -63,12 +63,13 @@
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_hsearch.c,v 1.3 2011/09/15 14:51:06 christos Exp $");
+__RCSID("$NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $");
 
 #include <errno.h>
 #include <search.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include <atf-c.h>
 
@@ -97,13 +98,13 @@
                ch[0] = 'a' + i;
                e.key = strdup(ch);     /* ptr to provided key is kept! */
                ATF_REQUIRE(e.key != NULL);
-               e.data = (void *)(long)i;
+               e.data = (void *)(intptr_t)i;
 
                ep = hsearch(e, ENTER);
 
                ATF_REQUIRE(ep != NULL);
                ATF_REQUIRE_STREQ(ep->key, ch);
-               ATF_REQUIRE_EQ((long)ep->data, i);
+               ATF_REQUIRE_EQ((intptr_t)ep->data, i);
        }
 
        /* e.key should be constant from here on down. */
@@ -117,10 +118,10 @@
 
                ATF_REQUIRE(ep != NULL);
                ATF_REQUIRE_STREQ(ep->key, ch);
-               ATF_REQUIRE_EQ((long)ep->data, i);
+               ATF_REQUIRE_EQ((intptr_t)ep->data, i);
        }
 
-       hdestroy();
+       hdestroy1(free, NULL);
 }
 
 ATF_TC(hsearch_duplicate);
@@ -137,24 +138,23 @@
 
        REQUIRE_ERRNO(hcreate(16));
 
-       e.key = strdup("a");
-       ATF_REQUIRE(e.key != NULL);
-       e.data = (void *)(long) 0;
+       e.key = __UNCONST("a");
+       e.data = (void *)(intptr_t) 0;
 
        ep = hsearch(e, ENTER);
 
        ATF_REQUIRE(ep != NULL);
        ATF_REQUIRE_STREQ(ep->key, "a");
-       ATF_REQUIRE_EQ((long)ep->data, 0);
+       ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
 
-       e.data = (void *)(long)12345;
+       e.data = (void *)(intptr_t)12345;
 
        ep = hsearch(e, ENTER);
        ep = hsearch(e, FIND);
 
        ATF_REQUIRE(ep != NULL);
        ATF_REQUIRE_STREQ(ep->key, "a");
-       ATF_REQUIRE_EQ((long)ep->data, 0);
+       ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
 
        hdestroy();
 }
@@ -173,7 +173,7 @@
 
        REQUIRE_ERRNO(hcreate(16));
 
-       e.key = strdup("A");
+       e.key = __UNCONST("A");
        ep = hsearch(e, FIND);
        ATF_REQUIRE_EQ(ep, NULL);
 
@@ -191,44 +191,40 @@
 ATF_TC_BODY(hsearch_two, tc)
 {
        ENTRY e, *ep, *ep2;
-       char *sa, *sb;
-
-       ATF_REQUIRE((sa = strdup("a")) != NULL);
-       ATF_REQUIRE((sb = strdup("b")) != NULL);
 
        REQUIRE_ERRNO(hcreate(16));
 
-       e.key = sa;
-       e.data = (void*)(long)0;
+       e.key = __UNCONST("a");
+       e.data = (void*)(intptr_t)0;
 
        ep = hsearch(e, ENTER);
 
        ATF_REQUIRE(ep != NULL);
        ATF_REQUIRE_STREQ(ep->key, "a");
-       ATF_REQUIRE_EQ((long)ep->data, 0);
+       ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
 
-       e.key = sb;
-       e.data = (void*)(long)1;
+       e.key = __UNCONST("b");
+       e.data = (void*)(intptr_t)1;
 
        ep = hsearch(e, ENTER);
 
        ATF_REQUIRE(ep != NULL);
        ATF_REQUIRE_STREQ(ep->key, "b");
-       ATF_REQUIRE_EQ((long)ep->data, 1);
+       ATF_REQUIRE_EQ((intptr_t)ep->data, 1);
 
-       e.key = sa;
+       e.key = __UNCONST("a");
        ep = hsearch(e, FIND);
 
-       e.key = sb;
+       e.key = __UNCONST("b");
        ep2 = hsearch(e, FIND);
 
        ATF_REQUIRE(ep != NULL);
        ATF_REQUIRE_STREQ(ep->key, "a");
-       ATF_REQUIRE_EQ((long)ep->data, 0);
+       ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
 
        ATF_REQUIRE(ep2 != NULL);
        ATF_REQUIRE_STREQ(ep2->key, "b");
-       ATF_REQUIRE_EQ((long)ep2->data, 1);
+       ATF_REQUIRE_EQ((intptr_t)ep2->data, 1);
 
        hdestroy();



Home | Main Index | Thread Index | Old Index