From 26d4c533ee04e9fe69389db0e125e9bf4502a648 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Wed, 6 Jul 2011 03:47:19 +0200 Subject: [PATCH] Fixed a minor warning and a compile error and improved consistency in comments --- src/SFML/Window/GlResource.cpp | 1 - src/SFML/Window/OSX/AutoreleasePoolWrapper.h | 15 ++ src/SFML/Window/OSX/AutoreleasePoolWrapper.mm | 213 ++++++++++++------ src/SFML/Window/OSX/InputImpl.hpp | 3 +- src/SFML/Window/OSX/InputImpl.mm | 5 +- src/SFML/Window/OSX/JoystickImpl.cpp | 3 +- src/SFML/Window/OSX/JoystickImpl.hpp | 3 +- 7 files changed, 166 insertions(+), 77 deletions(-) diff --git a/src/SFML/Window/GlResource.cpp b/src/SFML/Window/GlResource.cpp index a0fa41aa..c3f26384 100644 --- a/src/SFML/Window/GlResource.cpp +++ b/src/SFML/Window/GlResource.cpp @@ -38,7 +38,6 @@ namespace { // OpenGL resources counter and its mutex unsigned long count = 0; - bool initialized = false; sf::Mutex mutex; } diff --git a/src/SFML/Window/OSX/AutoreleasePoolWrapper.h b/src/SFML/Window/OSX/AutoreleasePoolWrapper.h index 2a67f1b7..47197877 100644 --- a/src/SFML/Window/OSX/AutoreleasePoolWrapper.h +++ b/src/SFML/Window/OSX/AutoreleasePoolWrapper.h @@ -23,6 +23,21 @@ // //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////// +/// \brief Ensure at least one autorelease pool is available on this thread. +/// +/// Increment a retain count. +/// See SPECIAL CONSIDERATION in implementation file. +/// +//////////////////////////////////////////////////////////// void RetainPool(void); + +//////////////////////////////////////////////////////////// +/// \brief Release the pool. +/// +/// Drain the pool if it is no more needed (retain count is zero). +/// See SPECIAL CONSIDERATION in implementation file. +/// +//////////////////////////////////////////////////////////// void ReleasePool(void); diff --git a/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm b/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm index 3391562c..854f44ff 100644 --- a/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm +++ b/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm @@ -23,102 +23,172 @@ // //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// #include +#include #include -#import "AutoreleasePoolWrapper.h" + #import +#import -// Here we manage one and only one pool by thread. This prevents draining one -// pool and making other pools invalid which can lead to a crash on 10.5 and an -// annoying message on 10.6 (*** attempt to pop an unknown autorelease pool). -// Because NSAutoreleasePool cannot be retain we have to do it ourself. -// We use an sf::ThreadLocalPtr to have one PoolWrapper in each thread. +//////////////////////////////////////////////////////////// +/// Here we manage one and only one pool by thread. This prevents draining one +/// pool and making other pools invalid which can lead to a crash on 10.5 and an +/// annoying message on 10.6 (*** attempt to pop an unknown autorelease pool). +/// +/// Because NSAutoreleasePool cannot be retain we have to do it ourself. +/// We use an sf::ThreadLocalPtr to have one PoolWrapper in each thread. +/// +/// SPECIAL CONSIDERATION : +/// ======================= +/// This implies that if RetainPool is called X times in a thread Y then +/// ReleasePool must be called X times too in the same thread Y. +/// +//////////////////////////////////////////////////////////// -// This implies that if RetainPool is called X times in a thread Y then -// ReleasePool must be called X times too in the same thread Y. - -class PoolWrapper { -public: - PoolWrapper() - : count(0) - , pool(0) - { - /* Nothing else */ - } +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief C++ Wrapper of Obj-C Autorelease Pool. +/// +//////////////////////////////////////////////////////////// +class PoolWrapper : NonCopyable { +public : - ~PoolWrapper() - { -#ifdef SFML_DEBUG - if (count < 0) { - sf::Err() << "~PoolWrapper : count is less than zero! " - "You called ReleasePool from a thread too many times." - << std::endl; - } else if (count > 0) { - sf::Err() << "~PoolWrapper : count is greater than zero! " - "You called ReleasePool from a thread to few times." - << std::endl; - } else { // count == 0 - sf::Err() << "~PoolWrapper is HAPPY!" << std::endl; - } -#endif - } + //////////////////////////////////////////////////////////// + /// \brief Default constructor + /// + //////////////////////////////////////////////////////////// + PoolWrapper(); - void Retain() - { - // Increase counter. - ++count; - - // Allocate pool if required. - if (pool == 0) { - pool = [[NSAutoreleasePool alloc] init]; - } - -#ifdef SFML_DEBUG - if (count <= 0) { - sf::Err() << "PoolWrapper::Retain : count <= 0! " << std::endl; - } -#endif - } + //////////////////////////////////////////////////////////// + /// \brief Default destructor + /// + //////////////////////////////////////////////////////////// + ~PoolWrapper(); - void Release() - { - // Decrease counter. - --count; - - // Drain pool if required. - if (count == 0) { - [pool drain]; - pool = 0; - } - -#ifdef SFML_DEBUG - if (count < 0) { - sf::Err() << "PoolWrapper::Release : count < 0! " << std::endl; - } -#endif - } + //////////////////////////////////////////////////////////// + /// \brief Increment retain count and allocate memory if needed + /// + //////////////////////////////////////////////////////////// + void Retain(); + //////////////////////////////////////////////////////////// + /// \brief Decrement retain count and releasing memory if needed + /// + //////////////////////////////////////////////////////////// + void Release(); private: - int count; ///< How many times the pool was retained ? - NSAutoreleasePool* pool; ///< Our pool. + + //////////////////////////////////////////////////////////// + // Member data + //////////////////////////////////////////////////////////// + int count; ///< How many times was the pool retained ? + NSAutoreleasePool* pool; ///< Our dedicated pool }; -// Thread shared variable but with local-only shared content. -sf::ThreadLocalPtr localPool; +//////////////////////////////////////////////////////////// +PoolWrapper::PoolWrapper() +: count(0) +, pool(0) +{ + /* Nothing else */ +} + + +//////////////////////////////////////////////////////////// +PoolWrapper::~PoolWrapper() +{ +#ifdef SFML_DEBUG + if (count < 0) { + sf::Err() << "~PoolWrapper : count is less than zero! " + "You called ReleasePool from a thread too many times." + << std::endl; + } else if (count > 0) { + sf::Err() << "~PoolWrapper : count is greater than zero! " + "You called ReleasePool from a thread to few times." + << std::endl; + } else { // count == 0 + sf::Err() << "~PoolWrapper is HAPPY!" << std::endl; + } +#endif +} + + +//////////////////////////////////////////////////////////// +void PoolWrapper::Retain() +{ + // Increase counter. + ++count; + + // Allocate pool if required. + if (pool == 0) { + pool = [[NSAutoreleasePool alloc] init]; + } + +#ifdef SFML_DEBUG + if (count <= 0) { + sf::Err() << "PoolWrapper::Retain : count <= 0! " << std::endl; + } +#endif +} + + +//////////////////////////////////////////////////////////// +void PoolWrapper::Release() +{ + // Decrease counter. + --count; + + // Drain pool if required. + if (count == 0) { + [pool drain]; + pool = 0; + } + +#ifdef SFML_DEBUG + if (count < 0) { + sf::Err() << "PoolWrapper::Release : count < 0! " << std::endl; + } +#endif +} + + +} // namespace priv + +} // namespace sf + +//////////////////////////////////////////////////////////// +// Private data +//////////////////////////////////////////////////////////// +namespace +{ + // This per-thread variable holds the current autorelease pool for each thread + sf::ThreadLocalPtr localPool; +} + + +//////////////////////////////////////////////////////////// void RetainPool(void) { // First, Check that we have a valid PoolWrapper object in our local pool. if (localPool == NULL) { - localPool = new PoolWrapper(); + localPool = new sf::priv::PoolWrapper(); } // Then retains! localPool->Retain(); } + +//////////////////////////////////////////////////////////// void ReleasePool(void) { #ifdef SFML_DEBUG @@ -136,3 +206,4 @@ void ReleasePool(void) } #endif } + diff --git a/src/SFML/Window/OSX/InputImpl.hpp b/src/SFML/Window/OSX/InputImpl.hpp index 3d52da52..6d6804ec 100644 --- a/src/SFML/Window/OSX/InputImpl.hpp +++ b/src/SFML/Window/OSX/InputImpl.hpp @@ -1,7 +1,8 @@ //////////////////////////////////////////////////////////// // // SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software. diff --git a/src/SFML/Window/OSX/InputImpl.mm b/src/SFML/Window/OSX/InputImpl.mm index 5c7654fb..06ca2116 100644 --- a/src/SFML/Window/OSX/InputImpl.mm +++ b/src/SFML/Window/OSX/InputImpl.mm @@ -1,7 +1,8 @@ //////////////////////////////////////////////////////////// // // SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software. @@ -27,7 +28,7 @@ //////////////////////////////////////////////////////////// #include #include -#import +#import namespace sf { diff --git a/src/SFML/Window/OSX/JoystickImpl.cpp b/src/SFML/Window/OSX/JoystickImpl.cpp index c45c6722..d7df43d7 100644 --- a/src/SFML/Window/OSX/JoystickImpl.cpp +++ b/src/SFML/Window/OSX/JoystickImpl.cpp @@ -1,7 +1,8 @@ //////////////////////////////////////////////////////////// // // SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software. diff --git a/src/SFML/Window/OSX/JoystickImpl.hpp b/src/SFML/Window/OSX/JoystickImpl.hpp index 970a0dd6..eb3679b0 100644 --- a/src/SFML/Window/OSX/JoystickImpl.hpp +++ b/src/SFML/Window/OSX/JoystickImpl.hpp @@ -1,7 +1,8 @@ //////////////////////////////////////////////////////////// // // SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software.