prfdcach.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape Portable Runtime (NSPR).
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "primpl.h"
#include <string.h>
/*****************************************************************************/
/*****************************************************************************/
/************************** File descriptor caching **************************/
/*****************************************************************************/
/*****************************************************************************/
/*
** This code is built into debuggable versions of NSPR to assist in
** finding misused file descriptors. Since file descritors (PRFileDesc)
** are identified by a pointer to their structure, they can be the
** target of dangling references. Furthermore, NSPR caches and tries
** to aggressively reuse file descriptors, leading to more ambiguity.
** The following code will allow a debugging client to set environment
** variables and control the number of file descriptors that will be
** preserved before they are recycled. The environment variables are
** NSPR_FD_CACHE_SIZE_LOW and NSPR_FD_CACHE_SIZE_HIGH. The former sets
** the number of descriptors NSPR will allocate before beginning to
** recycle. The latter is the maximum number permitted in the cache
** (exclusive of those in use) at a time.
*/
typedef struct _PR_Fd_Cache
{
} _PR_Fd_Cache;
static _PR_Fd_Cache _pr_fd_cache;
/*
** Get a FileDescriptor from the cache if one exists. If not allocate
** a new one from the heap.
*/
PRFileDesc *_PR_Getfd(void)
{
PRFileDesc *fd;
/*
** $$$
** This may look a little wasteful. We'll see. Right now I want to
** be able to toggle between caching and not at runtime to measure
** the differences. If it isn't too annoying, I'll leave it in.
** $$$$
**
** The test is against _pr_fd_cache.limit_high. If that's zero,
** we're not doing the extended cache but going for performance.
*/
if (0 == _pr_fd_cache.limit_high)
{
}
else
{
do
{
/* we "should" be able to extract an fd from the cache */
{
}
else
{
{
}
}
}
return fd;
{
}
else return NULL;
} /* _PR_Getfd */
/*
** Return a file descriptor to the cache unless there are too many in
** there already. If put in cache, clear the fields first.
*/
{
if (0 == _pr_fd_cache.limit_high)
{
}
else
{
{
}
else
{
{
}
else
{
}
}
}
} /* _PR_Putfd */
{
/*
** This can be called at any time, may adjust the cache sizes,
** turn the caches off, or turn them on. It is not dependent
** on the compilation setting of DEBUG.
*/
if (0 == high) /* shutting down or staying down */
{
{
/*
** Hold the lock throughout - nobody's going to want it
** other than another caller to this routine. Just don't
** let that happen.
**
** Put all the cached fds onto the new cache.
*/
{
}
_pr_fd_cache.limit_low = 0;
_pr_fd_cache.count = 0;
}
}
else /* starting up or just adjusting parameters */
{
if (was_using_stack) /* was using stack - feed into cache */
{
{
}
}
}
return PR_SUCCESS;
} /* PR_SetFDCacheSize */
void _PR_InitFdCache(void)
{
/*
** The fd caching is enabled by default for DEBUG builds,
** disabled by default for OPT builds. That default can
** be overridden at runtime using environment variables
** or a super-wiz-bang API.
*/
/*
** _low is allowed to be zero, _high is not.
** If _high is zero, we're not doing the caching.
*/
_pr_fd_cache.limit_low = 0;
#if defined(DEBUG)
#else
_pr_fd_cache.limit_high = 0;
#endif /* defined(DEBUG) */
} /* _PR_InitFdCache */
void _PR_CleanupFdCache(void)
{
{
}
{
}
} /* _PR_CleanupFdCache */
/* prfdcach.c */