diff options
author | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-07-22 07:32:05 +0000 |
---|---|---|
committer | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-07-22 07:32:05 +0000 |
commit | 9cc3d836eef7e45e68db52f59016aff22835d12e (patch) | |
tree | 0aec127460ba41c5aa1b565e0cf624874bbc0669 /ace | |
parent | b77d0bb15156c7e8d717257b9e817f239524b14a (diff) | |
download | ATCD-9cc3d836eef7e45e68db52f59016aff22835d12e.tar.gz |
ChangeLogTag:Thu Jul 22 02:15:46 1999 Irfan Pyarali <irfan@cs.wustl.edu>
Diffstat (limited to 'ace')
-rw-r--r-- | ace/Handle_Gobbler.h | 63 | ||||
-rw-r--r-- | ace/Handle_Gobbler.i | 81 |
2 files changed, 144 insertions, 0 deletions
diff --git a/ace/Handle_Gobbler.h b/ace/Handle_Gobbler.h new file mode 100644 index 00000000000..9f9dbe7ceb7 --- /dev/null +++ b/ace/Handle_Gobbler.h @@ -0,0 +1,63 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// ace +// +// = FILENAME +// Handle_Gobbler.h +// +// = AUTHOR +// Kirthika Parameswaran <kirthika@cs.wustl.edu> +// Irfan Pyarali <irfan@cs.wustl.edu> +// +// ============================================================================ + +#ifndef ACE_HANDLE_GOBBLER_H +#define ACE_HANDLE_GOBBLER_H + +#include "ace/OS.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#include "ace/Containers_T.h" + +class ACE_Handle_Gobbler +{ + // = TITLE + // This class gobbles up handles. + // + // = DESCRIPTION + // This is useful when we need to control the number of handles + // available for a process. This class is mostly used for + // testing purposes. +public: + + ~ACE_Handle_Gobbler (void); + // Destructor. Cleans up any remaining handles. + + int consume_handles (size_t n_handles_to_keep_available); + // Handles are opened continously until the process runs out of + // them, and then <n_handles_to_keep_available> handles are closed + // (freed) thereby making them usable in the future. + + int free_handles (size_t n_handles); + // Free up <n_handles>. + + void close_remaining_handles (void); + // All remaining handles are closed. + +private: + + typedef ACE_Unbounded_Set<ACE_HANDLE> HANDLE_SET; + + HANDLE_SET handle_set_; + // The container which holds the open descriptors. +}; + +#include "ace/Handle_Gobbler.i" + +#endif /* ACE_HANDLE_GOBBLER_H */ diff --git a/ace/Handle_Gobbler.i b/ace/Handle_Gobbler.i new file mode 100644 index 00000000000..da5a30e697d --- /dev/null +++ b/ace/Handle_Gobbler.i @@ -0,0 +1,81 @@ +// $Id$ + +inline +ACE_Handle_Gobbler::~ACE_Handle_Gobbler (void) +{ + this->close_remaining_handles (); +} + +inline int +ACE_Handle_Gobbler::consume_handles (size_t n_handles_to_keep_available) +{ + int result = 0; + + // On Win32, this style of gobbling doesn't seem to work. +#if !defined (ACE_WIN32) + + while (1) + { + ACE_HANDLE handle = ACE_OS::open (ACE_DEV_NULL, O_WRONLY); + + if (handle == ACE_INVALID_HANDLE) + { + if (ACE::out_of_handles (errno)) + { + result = this->free_handles (n_handles_to_keep_available); + break; + } + else + { + result = -1; + break; + } + } + + result = this->handle_set_.insert (handle); + if (result == -1) + break; + } + +#endif /* ACE_WIN32 */ + + return result; +} + +inline int +ACE_Handle_Gobbler::free_handles (size_t n_handles) +{ + HANDLE_SET::iterator iterator = + this->handle_set_.begin (); + + HANDLE_SET::iterator end = + this->handle_set_.end (); + + for (; + iterator != end && n_handles > 0; + ++iterator, --n_handles) + { + int result = ACE_OS::close (*iterator); + if (result != 0) + return result; + } + + return 0; +} + +inline void +ACE_Handle_Gobbler::close_remaining_handles (void) +{ + HANDLE_SET::iterator iterator = + this->handle_set_.begin (); + + HANDLE_SET::iterator end = + this->handle_set_.end (); + + for (; + iterator != end; + ++iterator) + { + ACE_OS::close (*iterator); + } +} |