xref: /aosp_15_r20/external/leveldb/port/port_example.h (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors.
4*9507f98cSAndroid Build Coastguard Worker //
5*9507f98cSAndroid Build Coastguard Worker // This file contains the specification, but not the implementations,
6*9507f98cSAndroid Build Coastguard Worker // of the types/operations/etc. that should be defined by a platform
7*9507f98cSAndroid Build Coastguard Worker // specific port_<platform>.h file.  Use this file as a reference for
8*9507f98cSAndroid Build Coastguard Worker // how to port this package to a new platform.
9*9507f98cSAndroid Build Coastguard Worker 
10*9507f98cSAndroid Build Coastguard Worker #ifndef STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
11*9507f98cSAndroid Build Coastguard Worker #define STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
12*9507f98cSAndroid Build Coastguard Worker 
13*9507f98cSAndroid Build Coastguard Worker #include "port/thread_annotations.h"
14*9507f98cSAndroid Build Coastguard Worker 
15*9507f98cSAndroid Build Coastguard Worker namespace leveldb {
16*9507f98cSAndroid Build Coastguard Worker namespace port {
17*9507f98cSAndroid Build Coastguard Worker 
18*9507f98cSAndroid Build Coastguard Worker // TODO(jorlow): Many of these belong more in the environment class rather than
19*9507f98cSAndroid Build Coastguard Worker //               here. We should try moving them and see if it affects perf.
20*9507f98cSAndroid Build Coastguard Worker 
21*9507f98cSAndroid Build Coastguard Worker // ------------------ Threading -------------------
22*9507f98cSAndroid Build Coastguard Worker 
23*9507f98cSAndroid Build Coastguard Worker // A Mutex represents an exclusive lock.
24*9507f98cSAndroid Build Coastguard Worker class LOCKABLE Mutex {
25*9507f98cSAndroid Build Coastguard Worker  public:
26*9507f98cSAndroid Build Coastguard Worker   Mutex();
27*9507f98cSAndroid Build Coastguard Worker   ~Mutex();
28*9507f98cSAndroid Build Coastguard Worker 
29*9507f98cSAndroid Build Coastguard Worker   // Lock the mutex.  Waits until other lockers have exited.
30*9507f98cSAndroid Build Coastguard Worker   // Will deadlock if the mutex is already locked by this thread.
31*9507f98cSAndroid Build Coastguard Worker   void Lock() EXCLUSIVE_LOCK_FUNCTION();
32*9507f98cSAndroid Build Coastguard Worker 
33*9507f98cSAndroid Build Coastguard Worker   // Unlock the mutex.
34*9507f98cSAndroid Build Coastguard Worker   // REQUIRES: This mutex was locked by this thread.
35*9507f98cSAndroid Build Coastguard Worker   void Unlock() UNLOCK_FUNCTION();
36*9507f98cSAndroid Build Coastguard Worker 
37*9507f98cSAndroid Build Coastguard Worker   // Optionally crash if this thread does not hold this mutex.
38*9507f98cSAndroid Build Coastguard Worker   // The implementation must be fast, especially if NDEBUG is
39*9507f98cSAndroid Build Coastguard Worker   // defined.  The implementation is allowed to skip all checks.
40*9507f98cSAndroid Build Coastguard Worker   void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
41*9507f98cSAndroid Build Coastguard Worker };
42*9507f98cSAndroid Build Coastguard Worker 
43*9507f98cSAndroid Build Coastguard Worker class CondVar {
44*9507f98cSAndroid Build Coastguard Worker  public:
45*9507f98cSAndroid Build Coastguard Worker   explicit CondVar(Mutex* mu);
46*9507f98cSAndroid Build Coastguard Worker   ~CondVar();
47*9507f98cSAndroid Build Coastguard Worker 
48*9507f98cSAndroid Build Coastguard Worker   // Atomically release *mu and block on this condition variable until
49*9507f98cSAndroid Build Coastguard Worker   // either a call to SignalAll(), or a call to Signal() that picks
50*9507f98cSAndroid Build Coastguard Worker   // this thread to wakeup.
51*9507f98cSAndroid Build Coastguard Worker   // REQUIRES: this thread holds *mu
52*9507f98cSAndroid Build Coastguard Worker   void Wait();
53*9507f98cSAndroid Build Coastguard Worker 
54*9507f98cSAndroid Build Coastguard Worker   // If there are some threads waiting, wake up at least one of them.
55*9507f98cSAndroid Build Coastguard Worker   void Signal();
56*9507f98cSAndroid Build Coastguard Worker 
57*9507f98cSAndroid Build Coastguard Worker   // Wake up all waiting threads.
58*9507f98cSAndroid Build Coastguard Worker   void SignallAll();
59*9507f98cSAndroid Build Coastguard Worker };
60*9507f98cSAndroid Build Coastguard Worker 
61*9507f98cSAndroid Build Coastguard Worker // ------------------ Compression -------------------
62*9507f98cSAndroid Build Coastguard Worker 
63*9507f98cSAndroid Build Coastguard Worker // Store the snappy compression of "input[0,input_length-1]" in *output.
64*9507f98cSAndroid Build Coastguard Worker // Returns false if snappy is not supported by this port.
65*9507f98cSAndroid Build Coastguard Worker bool Snappy_Compress(const char* input, size_t input_length,
66*9507f98cSAndroid Build Coastguard Worker                      std::string* output);
67*9507f98cSAndroid Build Coastguard Worker 
68*9507f98cSAndroid Build Coastguard Worker // If input[0,input_length-1] looks like a valid snappy compressed
69*9507f98cSAndroid Build Coastguard Worker // buffer, store the size of the uncompressed data in *result and
70*9507f98cSAndroid Build Coastguard Worker // return true.  Else return false.
71*9507f98cSAndroid Build Coastguard Worker bool Snappy_GetUncompressedLength(const char* input, size_t length,
72*9507f98cSAndroid Build Coastguard Worker                                   size_t* result);
73*9507f98cSAndroid Build Coastguard Worker 
74*9507f98cSAndroid Build Coastguard Worker // Attempt to snappy uncompress input[0,input_length-1] into *output.
75*9507f98cSAndroid Build Coastguard Worker // Returns true if successful, false if the input is invalid lightweight
76*9507f98cSAndroid Build Coastguard Worker // compressed data.
77*9507f98cSAndroid Build Coastguard Worker //
78*9507f98cSAndroid Build Coastguard Worker // REQUIRES: at least the first "n" bytes of output[] must be writable
79*9507f98cSAndroid Build Coastguard Worker // where "n" is the result of a successful call to
80*9507f98cSAndroid Build Coastguard Worker // Snappy_GetUncompressedLength.
81*9507f98cSAndroid Build Coastguard Worker bool Snappy_Uncompress(const char* input_data, size_t input_length,
82*9507f98cSAndroid Build Coastguard Worker                        char* output);
83*9507f98cSAndroid Build Coastguard Worker 
84*9507f98cSAndroid Build Coastguard Worker // ------------------ Miscellaneous -------------------
85*9507f98cSAndroid Build Coastguard Worker 
86*9507f98cSAndroid Build Coastguard Worker // If heap profiling is not supported, returns false.
87*9507f98cSAndroid Build Coastguard Worker // Else repeatedly calls (*func)(arg, data, n) and then returns true.
88*9507f98cSAndroid Build Coastguard Worker // The concatenation of all "data[0,n-1]" fragments is the heap profile.
89*9507f98cSAndroid Build Coastguard Worker bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
90*9507f98cSAndroid Build Coastguard Worker 
91*9507f98cSAndroid Build Coastguard Worker // Extend the CRC to include the first n bytes of buf.
92*9507f98cSAndroid Build Coastguard Worker //
93*9507f98cSAndroid Build Coastguard Worker // Returns zero if the CRC cannot be extended using acceleration, else returns
94*9507f98cSAndroid Build Coastguard Worker // the newly extended CRC value (which may also be zero).
95*9507f98cSAndroid Build Coastguard Worker uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
96*9507f98cSAndroid Build Coastguard Worker 
97*9507f98cSAndroid Build Coastguard Worker }  // namespace port
98*9507f98cSAndroid Build Coastguard Worker }  // namespace leveldb
99*9507f98cSAndroid Build Coastguard Worker 
100*9507f98cSAndroid Build Coastguard Worker #endif  // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
101