From 531618b862aeb44746b3de900ef591a633a4a0c7 Mon Sep 17 00:00:00 2001 From: mheily Date: Sat, 15 Jan 2011 03:27:46 +0000 Subject: [PATCH] more use of free() and init() hooks git-svn-id: svn://svn.code.sf.net/p/libkqueue/code/trunk@372 fb4e3144-bc1c-4b72-a658-5bcd248dd7f7 --- config.inc | 6 +++--- src/common/kqueue.c | 30 +--------------------------- src/posix/kqueue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/posix/platform.h | 10 ++++++++++ src/solaris/kqueue.c | 6 ++++++ 5 files changed, 67 insertions(+), 32 deletions(-) create mode 100644 src/posix/kqueue.c diff --git a/config.inc b/config.inc index 968e3dd..a7496d0 100644 --- a/config.inc +++ b/config.inc @@ -50,7 +50,7 @@ pre_configure_hook() { post_configure_hook() { finalize target "$target" - kqueue="" + kqueue="src/posix/kqueue.c" kevent="src/posix/kevent.c" evfilt_signal="src/posix/signal.c" evfilt_proc="src/$target/proc.c" @@ -75,8 +75,8 @@ post_configure_hook() { if [ $target = "solaris" ] ; then cflags="$cflags -D__EXTENSIONS__" - kqueue="src/solaris/kqueue.c" - kevent="src/solaris/kevent.c" + kqueue="$kqueue src/solaris/kqueue.c" + kevent="$kevent src/solaris/kevent.c" evfilt_timer="src/solaris/timer.c" evfilt_user="src/solaris/user.c" evfilt_proc="" diff --git a/src/common/kqueue.c b/src/common/kqueue.c index 2b02c8b..5220f6c 100644 --- a/src/common/kqueue.c +++ b/src/common/kqueue.c @@ -151,11 +151,7 @@ kqueue_get(int kq) int VISIBLE kqueue(void) { -#ifdef _WIN32 - static int kqueue_id = 0; -#endif struct kqueue *kq; - int tmp; kq = calloc(1, sizeof(*kq)); if (kq == NULL) @@ -172,21 +168,8 @@ kqueue(void) KQUEUE_DEBUG = (getenv("KQUEUE_DEBUG") == NULL) ? 0 : 1; #endif -#ifdef _WIN32 - pthread_rwlock_wrlock(&kqtree_mtx); - kqueue_id++; - pthread_rwlock_unlock(&kqtree_mtx); - kq->kq_sockfd[0] = kqueue_id; - kq->kq_sockfd[1] = kqueue_id; -#else - if (socketpair(AF_UNIX, SOCK_STREAM, 0, kq->kq_sockfd) < 0) - goto errout_unlocked; -#endif - -#ifdef kqueue_init_hook if (kqueue_init_hook(kq) < 0) goto errout_unlocked; -#endif pthread_rwlock_wrlock(&kqtree_mtx); if (kqueue_gc() < 0) @@ -204,18 +187,7 @@ errout: pthread_rwlock_unlock(&kqtree_mtx); errout_unlocked: - if (kq->kq_sockfd[0] != kq->kq_sockfd[1]) { - tmp = errno; -#ifndef _WIN32 - (void)close(kq->kq_sockfd[0]); - (void)close(kq->kq_sockfd[1]); -#endif - errno = tmp; - } -#if defined(__sun__) - if (kq->kq_port > 0) - close(kq->kq_port); -#endif + kqueue_free_hook(kq); free(kq); return (-1); } diff --git a/src/posix/kqueue.c b/src/posix/kqueue.c new file mode 100644 index 0000000..b2e4f05 --- /dev/null +++ b/src/posix/kqueue.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2011 Mark Heily + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "private.h" + +int +posix_kqueue_init(struct kqueue *kq) +{ + if (socketpair(AF_UNIX, SOCK_STREAM, 0, kq->kq_sockfd) < 0) { + dbg_perror("socketpair"); + kq->kq_sockfd[0] = -1; + kq->kq_sockfd[1] = -1; + return (-1); + } + + return (0); +} + +void +posix_kqueue_free(struct kqueue *kq) +{ + if (kq->kq_sockfd[0] != -1) + (void) close(kq->kq_sockfd[0]); + if (kq->kq_sockfd[1] != -1) + (void) close(kq->kq_sockfd[1]); +} diff --git a/src/posix/platform.h b/src/posix/platform.h index 44ded19..28c5dc3 100644 --- a/src/posix/platform.h +++ b/src/posix/platform.h @@ -19,6 +19,15 @@ #include "../../include/sys/event.h" +/* + * Hooks and prototypes + */ +#define kqueue_free_hook posix_kqueue_free +void posix_kqueue_free(struct kqueue *); + +#define kqueue_init_hook posix_kqueue_init +int posix_kqueue_init(struct kqueue *); + /* * GCC-compatible atomic integer operations */ @@ -38,3 +47,4 @@ #include #endif /* ! _KQUEUE_POSIX_PLATFORM_H */ + diff --git a/src/solaris/kqueue.c b/src/solaris/kqueue.c index df6a1a2..50feaf7 100644 --- a/src/solaris/kqueue.c +++ b/src/solaris/kqueue.c @@ -22,9 +22,13 @@ #include "sys/event.h" #include "private.h" +extern int posix_kqueue_init(struct kqueue *kq); +extern void posix_kqueue_free(struct kqueue *kq); + void solaris_kqueue_free(struct kqueue *kq) { + posix_kqueue_free(kq); if (kq->kq_port > 0) close(kq->kq_port); } @@ -32,6 +36,8 @@ solaris_kqueue_free(struct kqueue *kq) int solaris_kqueue_init(struct kqueue *kq) { + if (posix_kqueue_init(kq) < 0) + return (-1); if ((kq->kq_port = port_create()) < 0) { dbg_perror("port_create(2)"); return (-1);