Bug 1658457 - Use constexpr static pointers for nsCUPSShim on OS X r=emilio

This uses an additional define to control this, as ideally we would have a
configure option to link directly to CUPS which would be set by default on
OS X.

This single define also makes it easier to test either configuration on OS X
and on other Unix.

Differential Revision: https://phabricator.services.mozilla.com/D86642
This commit is contained in:
Emily McDonough 2020-08-11 00:25:18 +00:00
Родитель 1d6b40b174
Коммит 7a82fe3b08
2 изменённых файлов: 41 добавлений и 16 удалений

Просмотреть файл

@ -10,33 +10,34 @@
#include "mozilla/ArrayUtils.h"
#include "prlink.h"
#ifdef XP_MACOSX
// TODO: On OS X we are guaranteed to have CUPS, so it would be nice to just
// assign the members from the header directly instead of dlopen'ing.
// Alternatively, we could just do some #define's on OS X, but we don't use
// CUPS all that much, so there really isn't too much overhead in storing this
// table of functions even on OS X.
#ifdef CUPS_SHIM_RUNTIME_LINK
// TODO: This is currently pointless as we always use the compile-time linked
// version of CUPS, but in the future this may become a configure option.
// We also cannot use NSPR's library suffix support, since that cannot handle
// version number suffixes.
# ifdef XP_MACOSX
static const char gCUPSLibraryName[] = "libcups.2.dylib";
#else
# else
static const char gCUPSLibraryName[] = "libcups.so.2";
#endif
# endif
template <typename FuncT>
static bool LoadCupsFunc(PRLibrary*& lib, FuncT*& dest,
const char* const name) {
dest = (FuncT*)PR_FindSymbol(lib, name);
if (MOZ_UNLIKELY(!dest)) {
#ifdef DEBUG
# ifdef DEBUG
nsAutoCString msg(name);
msg.AppendLiteral(" not found in CUPS library");
NS_WARNING(msg.get());
#endif
#ifndef MOZ_TSAN
# endif
# ifndef MOZ_TSAN
// With TSan, we cannot unload libcups once we have loaded it because
// TSan does not support unloading libraries that are matched from its
// suppression list. Hence we just keep the library loaded in TSan builds.
PR_UnloadLibrary(lib);
#endif
# endif
lib = nullptr;
return false;
}
@ -56,10 +57,19 @@ bool nsCUPSShim::Init() {
// This is a macro so that it could also load from libcups if we are configured
// to use it as a compile-time dependency.
#define CUPS_SHIM_LOAD(NAME) \
if (!LoadCupsFunc(mCupsLib, NAME, #NAME)) return false;
# define CUPS_SHIM_LOAD(NAME) \
if (!LoadCupsFunc(mCupsLib, NAME, #NAME)) return false;
CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD)
#undef CUPS_SHIM_LOAD
# undef CUPS_SHIM_LOAD
mInited = true;
return true;
}
#else // CUPS_SHIM_RUNTIME_LINK
bool nsCUPSShim::Init() {
mInited = true;
return true;
}
#endif

Просмотреть файл

@ -11,6 +11,11 @@
#include "mozilla/Atomics.h"
#include "mozilla/Mutex.h"
// TODO: This should be a configure option, ideally.
#ifndef XP_MACOSX
# define CUPS_SHIM_RUNTIME_LINK
#endif
struct PRLibrary;
/* Note: this class relies on static initialization. */
@ -43,7 +48,15 @@ class nsCUPSShim {
X(cupsTempFd) \
X(httpClose)
#define CUPS_SHIM_FUNC_DECL(X) decltype(::X)* X;
#ifdef CUPS_SHIM_RUNTIME_LINK
// Define a single field which holds a function pointer.
# define CUPS_SHIM_FUNC_DECL(X) decltype(::X)* X;
#else
// Define a static constexpr function pointer. GCC can sometimes optimize
// away the pointer fetch for this.
# define CUPS_SHIM_FUNC_DECL(X) static constexpr decltype(::X)* const X = ::X;
#endif
CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_FUNC_DECL)
#undef CUPS_SHIM_FUNC_DECL
@ -63,8 +76,10 @@ class nsCUPSShim {
//
// The boolean can't be Relaxed, because it guards our function pointers.
mozilla::Atomic<bool, mozilla::ReleaseAcquire> mInited {false};
#ifdef CUPS_SHIM_RUNTIME_LINK
mozilla::OffTheBooksMutex mInitMutex {"nsCUPSShim::mInitMutex"};
PRLibrary* mCupsLib;
#endif
};
#endif /* nsCUPSShim_h___ */