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
This commit is contained in:
mheily 2011-01-15 03:27:46 +00:00
parent ce27406fea
commit 531618b862
5 changed files with 67 additions and 32 deletions

View File

@ -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=""

View File

@ -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);
}

47
src/posix/kqueue.c Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2011 Mark Heily <mark@heily.com>
*
* 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 <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#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]);
}

View File

@ -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 <unistd.h>
#endif /* ! _KQUEUE_POSIX_PLATFORM_H */

View File

@ -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);