12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // "$Id$"
- //
- // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
- // All Rights Reserved.
- //
- // Description:
- // Revisions: Year-Month-Day SVN-Author Modification
- //
- #ifndef DAHUA_MEMORY_DETAIL_POOL_H__
- #define DAHUA_MEMORY_DETAIL_POOL_H__
- // std::less, std::less_equal, std::greater
- #include <functional>
- // std::size_t, std::ptrdiff_t
- #include <cstddef>
- // std::max
- #include <algorithm>
- #include "singleton.h"
- #ifndef DAHUA_PREVENT_MACRO_SUBSTITUTION
- #define DAHUA_PREVENT_MACRO_SUBSTITUTION
- #endif
- namespace Dahua {
- namespace Memory {
- namespace Detail {
- ////////////////////////////////////////////////////////////////////////////////
- /// Ḭ̈߳²È«µÄµ¥¼þÄÚ´æ³Ø
- template <typename Tag, unsigned requestedSize,
- typename Pool,
- typename Mutex,
- unsigned nextSize,
- unsigned maxSize>
- struct singleton_pool
- {
- public:
- typedef Tag tag;
- typedef Pool pool_type;
- typedef Mutex mutex_type;
- typedef size_t size_type;
- typedef std::ptrdiff_t difference_type;
- static const unsigned requested_size = requestedSize;
- static const unsigned next_size = nextSize;
- private:
- struct mem_pool : Mutex
- {
- pool_type p;
- mem_pool() : p(requestedSize, nextSize) {}
- };
- struct guard
- {
- Mutex& m;
- guard(Mutex& mutex) : m(mutex) {m.lock();}
- ~guard() {m.unlock();}
- };
- typedef singleton_default<mem_pool> singleton;
- singleton_pool();
- public:
- static void * malloc DAHUA_PREVENT_MACRO_SUBSTITUTION()
- {
- mem_pool & p = singleton::instance();
- guard g(p);
- return (p.p.malloc)();
- }
- static void free DAHUA_PREVENT_MACRO_SUBSTITUTION(void * const ptr)
- {
- mem_pool & p = singleton::instance();
- guard g(p);
- (p.p.free)(ptr);
- }
- };
- } // namespace Detail
- } // namespace Memory
- } // namespace Dahua
- #endif // DAHUA_MEMORY_DETAIL_POOL_H__
|