// SNF4CGP/ScannerPool.hpp // Copyright (C) 2009 ARM Research Labs, LLC // See www.armresearch.com for more information. // // This module maintains a pool of SNF scanners tied to an active rulebase. // When a scanner is requested one is returned from the pool or if none are // currently available in the pool an additional scanner is allocated. #ifndef IncludedScannerPool #define IncludedScannerPool #include "../SNF4CGP/SNFMulti/SNFMulti.hpp" #include "../SNF4CGP/CodeDweller/threading.hpp" #include using namespace std; class Scanner { // SNF Scanner instance for the pool private: snf_RulebaseHandler& Rulebase_; // Rulebase the scanner uses. snf_EngineHandler* Engine_; // Scanner engine. public: Scanner(snf_RulebaseHandler& Rulebase); // When constructed allocates an engine. ~Scanner(); // When destructed destroys the engine. snf_RulebaseHandler& Rulebase(); // Provides access to both the Rulebase snf_EngineHandler& Engine(); // and the Engine. }; class ScannerPool { // Pool of SNF Scanners private: snf_RulebaseHandler* Rulebase; // Allocates an SNF rulebase. unsigned int ScannerCount; // Allocates and counts scanners. ProductionQueue PooledScanners; // Pool of ready-to-go scanners. public: ScannerPool(string Configuration); // Constructed with Rulebase config. ~ScannerPool(); // Cleans up when destroyed. Scanner* grab(); // Provides a Scanner from the pool. void drop(Scanner* S); // Returns a Scanner to the pool. }; // Since Scanners are used for as short a time as possible it is usefule to have them // allocated from the pool and returned to the pool within the scope of the scanning // function. An easy way to do that is to use the ScopeScanner which provides a scanner // from the pool as long as it is in scope and then returns it to the pool when done // even if something goes wrong and an exception is thrown. class ScopeScanner { // Temporary handle for a Scanner. private: ScannerPool& ScannerPool_; // Knows the scanner pool. Scanner* Scanner_; // Holds a pointer to a scanner. public: ScopeScanner(ScannerPool& P); // Grabs a scanner on construction. ~ScopeScanner(); // Drops the scanner on destruction. snf_RulebaseHandler& Rulebase(); // Provides the same access as a snf_EngineHandler& Engine(); // Scanner object. } #endif