]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/include/rocksdb/cleanable.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / include / rocksdb / cleanable.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 //
9 // An iterator yields a sequence of key/value pairs from a source.
10 // The following class defines the interface. Multiple implementations
11 // are provided by this library. In particular, iterators are provided
12 // to access the contents of a Table or a DB.
13 //
14 // Multiple threads can invoke const methods on an Iterator without
15 // external synchronization, but if any of the threads may call a
16 // non-const method, all threads accessing the same Iterator must use
17 // external synchronization.
18
19 #ifndef INCLUDE_ROCKSDB_CLEANABLE_H_
20 #define INCLUDE_ROCKSDB_CLEANABLE_H_
21
22 namespace rocksdb {
23
24 class Cleanable {
25 public:
26 Cleanable();
27 ~Cleanable();
28 // Clients are allowed to register function/arg1/arg2 triples that
29 // will be invoked when this iterator is destroyed.
30 //
31 // Note that unlike all of the preceding methods, this method is
32 // not abstract and therefore clients should not override it.
33 typedef void (*CleanupFunction)(void* arg1, void* arg2);
34 void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
35 void DelegateCleanupsTo(Cleanable* other);
36 // DoCkeanup and also resets the pointers for reuse
37 inline void Reset() {
38 DoCleanup();
39 cleanup_.function = nullptr;
40 cleanup_.next = nullptr;
41 }
42
43 protected:
44 struct Cleanup {
45 CleanupFunction function;
46 void* arg1;
47 void* arg2;
48 Cleanup* next;
49 };
50 Cleanup cleanup_;
51 // It also becomes the owner of c
52 void RegisterCleanup(Cleanup* c);
53
54 private:
55 // Performs all the cleanups. It does not reset the pointers. Making it
56 // private
57 // to prevent misuse
58 inline void DoCleanup() {
59 if (cleanup_.function != nullptr) {
60 (*cleanup_.function)(cleanup_.arg1, cleanup_.arg2);
61 for (Cleanup* c = cleanup_.next; c != nullptr;) {
62 (*c->function)(c->arg1, c->arg2);
63 Cleanup* next = c->next;
64 delete c;
65 c = next;
66 }
67 }
68 }
69 };
70
71 } // namespace rocksdb
72
73 #endif // INCLUDE_ROCKSDB_CLEANABLE_H_