Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cpp/wrap.h header only #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions common/util/Util/FFI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import System.IO.Unsafe (unsafePerformIO)
import Control.Exception
import Control.Monad

foreign import ccall unsafe "&hs_ffi_free_error" hs_ffi_free_error
:: FunPtr (CString -> IO ())

newtype FFIError = FFIError (ForeignPtr CChar)

ffiErrorMessage :: FFIError -> String
Expand All @@ -39,7 +36,13 @@ call :: IO CString -> IO ()
call f = do
p <- f
when (p /= nullPtr) $ do
fp <- newForeignPtr hs_ffi_free_error p
fp <- do
b <- peek p
-- A '\1' prefix indicates that we shouldn't free the string (e.g.,
-- because it's static) - cf. ffi::wrap.
if b /= toEnum 1
then newForeignPtr finalizerFree p
else newForeignPtr_ (p `plusPtr` 1)
throwIO $ FFIError fp

infixr 5 :>
Expand Down
31 changes: 0 additions & 31 deletions common/util/cpp/ffi.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions common/util/cpp/ffi.h

This file was deleted.

10 changes: 7 additions & 3 deletions common/util/cpp/wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ namespace facebook {
namespace hs {
namespace ffi {

extern const char* outOfMemory;
extern const char* unknownError;

template <typename F>
const char* wrap(F&& f) noexcept {
// The '\1' prefix instructs the marshaller in FFI.hs to not free those
// strings. The prefix itself will be stripped out. This is only really
// necessary specifically for the outOfMemory message where we might not be
// able to allocate a new message string - although arguably, we might just
// want to abort in such a case.
static const char *outOfMemory = "\1out of memory";
static const char *unknownError = "\1unknown error";
try {
f();
return nullptr;
Expand Down
2 changes: 0 additions & 2 deletions common/util/fb-util.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ library

cxx-sources:
cpp/cdynamic.cpp
cpp/ffi.cpp
cpp/logging.cpp
cpp/HsStruct.cpp
cpp/IOBuf.cpp
Expand All @@ -171,7 +170,6 @@ library
cpp/HsStdVariant.h
cpp/HsStruct.h
cpp/HsStructDefines.h
cpp/ffi.h
cpp/memory.h
cpp/wrap.h
Util/AsanAlloc.h
Expand Down