• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef UTILS_SCOPEDAUTORELEASEPOOL_H_
16 #define UTILS_SCOPEDAUTORELEASEPOOL_H_
17 
18 #include "common/Compiler.h"
19 
20 #include <cstddef>
21 
22 namespace utils {
23 
24     /**
25      * ScopedAutoreleasePool is a scoped class which initializes an NSAutoreleasePool on
26      * creation, and drains it on destruction. On non-Apple platforms, ScopedAutoreleasePool
27      * is a no-op.
28      *
29      * An autoreleasepool is needed when using protocol objects in Objective-C because Cocoa
30      * expects a pool to always be available in each thread. If a pool is not available, then
31      * autoreleased objects will never be released and will leak.
32      *
33      * In long-running blocks of code or loops, it is important to periodically create and drain
34      * autorelease pools so that memory is recycled. In Dawn's tests, we have an autoreleasepool
35      * per-test. In graphics applications it's advised to create an autoreleasepool around the
36      * frame loop. Ex.)
37      *   void frame() {
38      *     // Any protocol objects will be reclaimed when this object falls out of scope.
39      *     utils::ScopedAutoreleasePool pool;
40      *
41      *     // do rendering ...
42      *   }
43      */
44     class DAWN_NO_DISCARD ScopedAutoreleasePool {
45       public:
46         ScopedAutoreleasePool();
47         ~ScopedAutoreleasePool();
48 
49         ScopedAutoreleasePool(const ScopedAutoreleasePool&) = delete;
50         ScopedAutoreleasePool& operator=(const ScopedAutoreleasePool&) = delete;
51 
52         ScopedAutoreleasePool(ScopedAutoreleasePool&&);
53         ScopedAutoreleasePool& operator=(ScopedAutoreleasePool&&);
54 
55       private:
56         void* mPool = nullptr;
57     };
58 
59 }  // namespace utils
60 
61 #endif  // UTILS_SCOPEDAUTORELEASEPOOL_H_
62