Using Hoard

Using Hoard is easy. On UNIX-based platforms, all you have to do is set one environment variable. You do not need to change any source code, recompile or relink your application. On Windows, you actually have even greater flexibility.

UNIX

In UNIX, you can use the LD_PRELOAD variable to use Hoard instead of the system allocator for any program not linked with the "static option" (that's most programs). Below are settings for Linux and Solaris.

Linux

LD_PRELOAD="/path/libhoard.so"

Solaris

Depending on whether you are using the GNU-compiled version (as produced by compile) or the Sun Workshop-compiled versions (produced by compile-sunw), your settings will be slightly different.

Version Setting
GNU
LD_PRELOAD="/path/libhoard.so"
Sun (32-bits)
LD_PRELOAD="/path/libhoard_32.so:/usr/lib/libCrun.so.1"
Sun (64-bits)
LD_PRELOAD="/path/libhoard_64.so:/usr/lib/64/libCrun.so.1"

For some security-sensitive applications, Solaris requires you place libraries used in LD_PRELOAD into the /usr/lib/secure directory. In that event, after copying these libraries into /usr/lib/secure, set LD_PRELOAD by omitting the absolute locations of the libraries, as follows:

LD_PRELOAD="libhoard.so:libCrun.so.1"

Windows

There are two ways to use Hoard on Windows.

  1. Using winhoard

    The first and best method is to use winhoard. Winhoard replaces malloc/new calls from your program and any DLLs it might use (leaving HeapAlloc calls intact).

    To use the Winhoard version, link your executable with usewinhoard.obj and winhoard.lib, and then use winhoard.dll:

    cl /Ox /MD myprogram.cpp usewinhoard.obj winhoard.lib
  2. Using libhoard

    The other method is to link directly with the libhoard DLL. This approach is simple, but only suitable for small applications, since it will not affect malloc calls in any other DLL you might load. To use this option, you should put the following into your source code as the very first lines:

    #if defined(USE_HOARD)
    #pragma comment(lib, "libhoard.lib")
    #endif

    This stanza should be in the first part of a header file included by all of your code. It ensures that Hoard loads before any other library (you will need libhoard.lib in your path). When you execute your program, as long as libhoard.dll is in your path, your program will run with Hoard instead of the system allocator. Note that you must compile your program with the /MD flag, as in:

    cl /MD /G6 /Ox /DUSE_HOARD=1 myprogram.cpp 

    Hoard will not work if you use another switch (like /MT) to compile your program.