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.
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.
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.
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:
There are two ways to use Hoard on Windows.
- 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
- 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:
#pragma comment(lib, "libhoard.lib")
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.