You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerPool.hpp 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SNF4CGP/ScannerPool.hpp
  2. // Copyright (C) 2009 ARM Research Labs, LLC
  3. // See www.armresearch.com for more information.
  4. //
  5. // This module maintains a pool of SNF scanners tied to an active rulebase.
  6. // When a scanner is requested one is returned from the pool or if none are
  7. // currently available in the pool an additional scanner is allocated.
  8. #ifndef IncludedScannerPool
  9. #define IncludedScannerPool
  10. #include "../SNF4CGP/SNFMulti/SNFMulti.hpp"
  11. #include "../SNF4CGP/CodeDweller/threading.hpp"
  12. #include <string>
  13. using namespace std;
  14. class Scanner { // SNF Scanner instance for the pool
  15. private:
  16. snf_RulebaseHandler& Rulebase_; // Rulebase the scanner uses.
  17. snf_EngineHandler* Engine_; // Scanner engine.
  18. public:
  19. Scanner(snf_RulebaseHandler& Rulebase); // When constructed allocates an engine.
  20. ~Scanner(); // When destructed destroys the engine.
  21. snf_RulebaseHandler& Rulebase(); // Provides access to both the Rulebase
  22. snf_EngineHandler& Engine(); // and the Engine.
  23. };
  24. class ScannerPool { // Pool of SNF Scanners
  25. private:
  26. snf_RulebaseHandler* Rulebase; // Allocates an SNF rulebase.
  27. unsigned int ScannerCount; // Allocates and counts scanners.
  28. ProductionQueue<Scanner*> PooledScanners; // Pool of ready-to-go scanners.
  29. public:
  30. ScannerPool(string Configuration); // Constructed with Rulebase config.
  31. ~ScannerPool(); // Cleans up when destroyed.
  32. Scanner* grab(); // Provides a Scanner from the pool.
  33. void drop(Scanner* S); // Returns a Scanner to the pool.
  34. };
  35. // Since Scanners are used for as short a time as possible it is usefule to have them
  36. // allocated from the pool and returned to the pool within the scope of the scanning
  37. // function. An easy way to do that is to use the ScopeScanner which provides a scanner
  38. // from the pool as long as it is in scope and then returns it to the pool when done
  39. // even if something goes wrong and an exception is thrown.
  40. class ScopeScanner { // Temporary handle for a Scanner.
  41. private:
  42. ScannerPool& ScannerPool_; // Knows the scanner pool.
  43. Scanner* Scanner_; // Holds a pointer to a scanner.
  44. public:
  45. ScopeScanner(ScannerPool& P); // Grabs a scanner on construction.
  46. ~ScopeScanner(); // Drops the scanner on destruction.
  47. snf_RulebaseHandler& Rulebase(); // Provides the same access as a
  48. snf_EngineHandler& Engine(); // Scanner object.
  49. }
  50. #endif