cplusplus_to_java_convert.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. /*
  7. * This macro is used for 32 bit OS. In 32 bit OS, the result number is a
  8. negative number if we use reinterpret_cast<jlong>(pointer).
  9. * For example, jlong ptr = reinterpret_cast<jlong>(pointer), ptr is a negative
  10. number in 32 bit OS.
  11. * If we check ptr using ptr > 0, it fails. For example, the following code is
  12. not correct.
  13. * if (jblock_cache_handle > 0) {
  14. std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *pCache =
  15. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
  16. jblock_cache_handle);
  17. options.block_cache = *pCache;
  18. }
  19. * But the result number is positive number if we do
  20. reinterpret_cast<size_t>(pointer) first and then cast it to jlong. size_t is 4
  21. bytes long in 32 bit OS and 8 bytes long in 64 bit OS.
  22. static_cast<jlong>(reinterpret_cast<size_t>(_pointer)) is also working in 64
  23. bit OS.
  24. *
  25. * We don't need an opposite cast because it works from jlong to c++ pointer in
  26. both 32 bit and 64 bit OS.
  27. * For example, the following code is working in both 32 bit and 64 bit OS.
  28. jblock_cache_handle is jlong.
  29. * std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *pCache =
  30. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
  31. jblock_cache_handle);
  32. */
  33. #define GET_CPLUSPLUS_POINTER(_pointer) \
  34. static_cast<jlong>(reinterpret_cast<size_t>(_pointer))