xref: /aosp_15_r20/external/cronet/base/mac/scoped_mach_msg_destroy.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MAC_SCOPED_MACH_MSG_DESTROY_H_
6 #define BASE_MAC_SCOPED_MACH_MSG_DESTROY_H_
7 
8 #include <mach/message.h>
9 
10 #include "base/memory/raw_ptr.h"
11 
12 namespace base {
13 
14 // Calls mach_msg_destroy on the specified message when the object goes out
15 // of scope.
16 class ScopedMachMsgDestroy {
17  public:
ScopedMachMsgDestroy(mach_msg_header_t * header)18   explicit ScopedMachMsgDestroy(mach_msg_header_t* header) : header_(header) {}
19 
20   ScopedMachMsgDestroy(const ScopedMachMsgDestroy&) = delete;
21   ScopedMachMsgDestroy& operator=(const ScopedMachMsgDestroy&) = delete;
22 
~ScopedMachMsgDestroy()23   ~ScopedMachMsgDestroy() {
24     if (header_) {
25       mach_msg_destroy(header_);
26     }
27   }
28 
29   // Prevents the message from being destroyed when it goes out of scope.
Disarm()30   void Disarm() { header_ = nullptr; }
31 
32  private:
33   raw_ptr<mach_msg_header_t> header_;
34 };
35 
36 }  // namespace base
37 
38 #endif  // BASE_MAC_SCOPED_MACH_MSG_DESTROY_H_
39