Module Name: src
Committed By: christos
Date: Sun Jul 20 20:17:21 UTC 2014
Modified Files:
src/include: search.h
src/lib/libc/stdlib: hcreate.3 hcreate.c
src/tests/lib/libc/stdlib: t_hsearch.c
Log Message:
amend the new destroy function to take function pointers.
To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/include/search.h
cvs rdiff -u -r1.12 -r1.13 src/lib/libc/stdlib/hcreate.3
cvs rdiff -u -r1.9 -r1.10 src/lib/libc/stdlib/hcreate.c
cvs rdiff -u -r1.3 -r1.4 src/tests/lib/libc/stdlib/t_hsearch.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/include/search.h
diff -u src/include/search.h:1.21 src/include/search.h:1.22
--- src/include/search.h:1.21 Sun Jul 20 09:34:17 2014
+++ src/include/search.h Sun Jul 20 16:17:21 2014
@@ -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 <[email protected]>
@@ -62,12 +62,10 @@ void hdestroy(void);
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 */
Index: src/lib/libc/stdlib/hcreate.3
diff -u src/lib/libc/stdlib/hcreate.3:1.12 src/lib/libc/stdlib/hcreate.3:1.13
--- src/lib/libc/stdlib/hcreate.3:1.12 Sun Jul 20 09:41:14 2014
+++ src/lib/libc/stdlib/hcreate.3 Sun Jul 20 16:17:21 2014
@@ -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 @@ function provided, the
.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 ,
Index: src/lib/libc/stdlib/hcreate.c
diff -u src/lib/libc/stdlib/hcreate.c:1.9 src/lib/libc/stdlib/hcreate.c:1.10
--- src/lib/libc/stdlib/hcreate.c:1.9 Sun Jul 20 09:34:17 2014
+++ src/lib/libc/stdlib/hcreate.c Sun Jul 20 16:17:21 2014
@@ -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 @@ hcreate_r(size_t nel, struct hsearch_dat
}
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 @@ hdestroy1_r(struct hsearch_data *head, i
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 @@ hdestroy1_r(struct hsearch_data *head, i
void
hdestroy_r(struct hsearch_data *head)
{
- hdestroy1_r(head, 0);
+ hdestroy1_r(head, NULL, NULL);
}
ENTRY *
Index: src/tests/lib/libc/stdlib/t_hsearch.c
diff -u src/tests/lib/libc/stdlib/t_hsearch.c:1.3 src/tests/lib/libc/stdlib/t_hsearch.c:1.4
--- src/tests/lib/libc/stdlib/t_hsearch.c:1.3 Thu Sep 15 10:51:06 2011
+++ src/tests/lib/libc/stdlib/t_hsearch.c Sun Jul 20 16:17:21 2014
@@ -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 @@ ATF_TC_BODY(hsearch_basic, tc)
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_TC_BODY(hsearch_basic, tc)
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 @@ ATF_TC_BODY(hsearch_duplicate, tc)
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 @@ ATF_TC_BODY(hsearch_nonexistent, tc)
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_HEAD(hsearch_two, tc)
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();
}
@@ -257,12 +253,12 @@ ATF_TC_BODY(hsearch_r_basic, tc)
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;
ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
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. */
@@ -275,10 +271,10 @@ ATF_TC_BODY(hsearch_r_basic, tc)
ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
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_r(&t);
+ hdestroy1_r(&t, free, NULL);
}
ATF_TC(hsearch_r_duplicate);
@@ -296,23 +292,22 @@ ATF_TC_BODY(hsearch_r_duplicate, tc)
REQUIRE_ERRNO(hcreate_r(16, &t));
- e.key = strdup("a");
- ATF_REQUIRE(e.key != NULL);
- e.data = (void *)(long) 0;
+ e.key = __UNCONST("a");
+ e.data = (void *)(intptr_t) 0;
ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
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;
ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
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_r(&t);
}
@@ -332,7 +327,7 @@ ATF_TC_BODY(hsearch_r_nonexistent, tc)
REQUIRE_ERRNO(hcreate_r(16, &t));
- e.key = strdup("A");
+ e.key = __UNCONST("A");
ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
ATF_REQUIRE_EQ(ep, NULL);
@@ -350,43 +345,39 @@ ATF_TC_HEAD(hsearch_r_two, tc)
ATF_TC_BODY(hsearch_r_two, tc)
{
ENTRY e, *ep, *ep2;
- char *sa, *sb;
struct hsearch_data t;
- ATF_REQUIRE((sa = strdup("a")) != NULL);
- ATF_REQUIRE((sb = strdup("b")) != NULL);
-
REQUIRE_ERRNO(hcreate_r(16, &t));
- e.key = sa;
- e.data = (void*)(long)0;
+ e.key = __UNCONST("a");
+ e.data = (void*)(intptr_t)0;
ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
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;
ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
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");
ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
- e.key = sb;
+ e.key = __UNCONST("b");
ATF_REQUIRE(hsearch_r(e, FIND, &ep2, &t) == 1);
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_r(&t);
}