SmartHeapϊ
Getting Started and
Platform Guide
for
HP-UX
Version 5.0
SmartHeap and HeapAgent are trademarks of Compuware Corporation.
All other trademarks are the property of their respective holders.
Copyright ©1994-1999 Compuware
Corporation.
All rights reserved.
Printed January 7, 1999
Weβd like to have your ideas and comments about this manual.Κ Your suggestions help us improve our products.Κ Please send comments to:
MicroQuill Software Publishing,
Inc.
11200 Kirkland Way, Suite 310
Kirkland, WashingtonΚ 98033
Voice:ΚΚΚΚΚΚΚΚ (425) 827-7200
Fax:ΚΚΚΚΚΚΚΚΚΚΚΚ (425) 481-4442
Internet:ΚΚΚΚ info@microquill.com
Contents
New in SmartHeap 5.0............................................................................. ΚΚΚΚΚΚΚΚΚ 1
Reduced space overhead........................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 1
New small-block allocator........................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 1
New API to control process heap growth............................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 2
Installing SmartHeap................................................................................ ΚΚΚΚΚΚΚΚΚ 2
Files in the HP-UX SmartHeap release..................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 4
Using SmartHeap with HP-UX............................................................... ΚΚΚΚΚΚΚΚΚ 6
Getting started with the SmartHeap runtime library............................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 6
Adding the SmartHeap header file
to your source files.......................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 6
Specifying the location of SmartHeap files...................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 7
Calling MemRegisterTask.................................................................. ΚΚΚΚΚΚΚΚΚΚΚΚΚ 7
Linking with Runtime SmartHeap...................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 8
Debugging an application with SmartHeap............................................. ΚΚΚΚΚΚΚΚΚΚΚΚΚ 9
Adding the Debug SmartHeap header file
to your source files.......................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 9
Specifying the location of SmartHeap files.................................... ΚΚΚΚΚΚΚΚΚΚΚ 10
Calling MemRegisterTask................................................................ ΚΚΚΚΚΚΚΚΚΚΚ 11
Recompiling with Debug SmartHeap.............................................. ΚΚΚΚΚΚΚΚΚΚΚ 11
Using SmartHeap with other compilers............................................ ΚΚΚΚΚΚΚ 13
SmartHeap HP-UX platform notes..................................................... ΚΚΚΚΚΚΚ 16
Compiler functions that SmartHeap overrides...................................... ΚΚΚΚΚΚΚΚΚΚΚ 16
Multi-threading in HP-UX........................................................................ ΚΚΚΚΚΚΚΚΚΚΚ 16
Using shared memory in SmartHeap for HP-UX................................... ΚΚΚΚΚΚΚΚΚΚΚ 17
Creating heaps in user-supplied regions............................................... ΚΚΚΚΚΚΚΚΚΚΚ 19
SmartHeap HP-UX-specific values......................................................... ΚΚΚΚΚΚΚΚΚΚΚ 20
Default error handling for HP-UX........................................................... ΚΚΚΚΚΚΚΚΚΚΚ 20
SmartHeap 5.0 significantly reduces per-block overhead in its UNIX implementations, from 5 bytes to zero bytes for blocks less than 256 bytes in size (and from 9 bytes to zero bytes in 64-bit implementations).Κ Granularity has also been reduced from 8 bytes to 4 bytes.Κ For example, whereas a 12-byte allocation would have consumed 24 bytes in SmartHeap 4.0 (12 + 5 rounded up to the next 8-byte multiple), the same 12-byte allocation uses only 12 bytes in SmartHeap 5.0.
Moreover, because SmartHeap 5.0 no longer creates multiple default memory pools for multi-threaded applications, space saving is even greater because multiple threads are able to share the same pages of heap memory in a contention-free manner rather than using separate memory pools.
An improved small-block allocator incurs zero bytes per allocation of overhead and recycles space more effectively between block sizes than the SmartHeap 3.x/4.x small-block allocator.Κ If you want to use the old small-block allocator for some reason, you may use MemPoolSetSmallBlockAllocator to change the small-block allocator for a given memory pool.Κ Use MemPoolSetSmallBlockAllocator(MemDefaultPool, MEM_SMALL_BLOCK_SH3) to use the SH 3.0/4.0 small-block allocator.Κ Specify the value MEM_SMALL_BLOCK_SH5 for the new small-block allocator.
SmartHeap 5.0 introduces a new API, MemProcessSetGrowIncrement, that allows you to control how much memory SmartHeap requests from the operating system when a memory pool needs to grow. The memory returned is retained in SmartHeap's free pool and is allocated to any memory pool(s) that subsequently need to grow.Κ By buffering operating system heap requests, SmartHeap 5.0 incurs less system call overhead, less operating system heap and address space fragmentation, and less operating system heap contention.
The default process grow increment is 2MB.Κ The value you specify to MemProcessSetGrowIncrement is in bytes and is rounded up to the next 64K.
The files on the SmartHeap installation media are not copy protected.Κ To install SmartHeap on your hard disk:
1.
Identify the directory you want to install
SmartHeap in, create that directory (if necessary) and cd
to it.
For
example, if you want to install SmartHeap in /opt/sh,
enter the following commands:
mkdir
/opt/sh
cd
/opt/sh
2.
Insert the SmartHeap media into your installation
device and copy the SmartHeap archive files with the following command:
tar
xpvf device_name
where device_name is the name of your installation device (for example, /dev/fd0).
3.
Uncompress the SmartHeap files with the following
command:
uncompress
*.Z
4.
Unarchive each file that ends with the .tar
extension as follows:
tar
xpvf filename.tar
5.
Add the SmartHeap include
directory to your compilerβs header file search path, and add the SmartHeap lib
directory to your linkerβs library search path.
This HP-UX 11 (32-bit and 64-bit) builds support the HP-UX pthreads.Κ
This release includes statically linkable and shared libraries, platform-independent header files, and platform-independent source files for the malloc and C++ operator new definitions.Κ Both single- and multi-thread SmartHeap libraries are included.
The following files are present in this SmartHeap distribution:
DirectoryΚΚΚΚΚΚ FileΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Description
includeΚΚΚ smrtheap.hΚΚΚΚΚΚΚΚΚΚ Header file containing all Runtime SmartHeap C declarations except malloc
includeΚΚΚ smrtheap.hppΚΚΚΚΚΚΚΚ Header file containing Runtime SmartHeap C++ declarations
includeΚΚΚ shmalloc.hΚΚΚΚΚΚΚΚΚΚ Header file containing declarations for Runtime SmartHeap ANSI C functions
libΚΚΚΚΚΚΚ libsmartheap.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Runtime SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapC.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Runtime SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheap.slΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Runtime SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheapd.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Debug SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapCd.aΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Debug SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheapd.slΚΚΚΚΚΚΚΚΚΚΚΚ 32-bit single-thread Debug SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheap_mt.aΚΚΚΚΚΚΚΚΚΚ 32-bit multi-thread Runtime SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapC_mt.aΚΚΚΚΚΚΚ 32-bit multi-thread Runtime SmartHeap HP C++ archive library
DirectoryΚΚΚΚΚΚ FileΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Description
libΚΚΚΚΚΚΚ libsmartheap_mt.slΚΚΚΚΚΚΚΚΚΚΚ 32-bit multi-thread Runtime SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheap_mtd.aΚΚΚΚΚΚΚΚΚΚΚ 32-bit multi-thread Debug SmartHeap HP C archive library
libΚΚΚΚΚΚΚ libsmartheapC_mtd.aΚΚΚΚΚΚΚΚ 32-bit multi-thread Debug SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheap_mtd.slΚΚΚΚΚΚΚΚ 32-bit multi-thread Debug SmartHeap HP C shared library
libΚΚΚΚΚΚΚ libsmartheap64.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Runtime SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapC64.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Runtime SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheap64.slΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Runtime SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheapd64.aΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Debug SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapCd64.aΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Debug SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheapd64.slΚΚΚΚΚΚΚΚΚΚΚ 64-bit single-thread Debug SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheap_mt64.aΚΚΚΚΚΚΚΚ 64-bit multi-thread Runtime SmartHeap C HP archive library
libΚΚΚΚΚΚΚ libsmartheapC_mt64.aΚΚΚΚΚΚ 64-bit multi-thread Runtime SmartHeap HP C++ archive library
libΚΚΚΚΚΚΚ libsmartheap_mt64.slΚΚΚΚΚΚ 64-bit multi-thread Runtime SmartHeap C HP shared library
libΚΚΚΚΚΚΚ libsmartheap_mtd64.aΚΚΚΚΚΚ 64-bit multi-thread Debug SmartHeap HP C archive library
libΚΚΚΚΚΚΚ libsmartheapC_mtd64.aΚΚΚ 64-bit multi-thread Debug SmartHeap C++ HP archive library
libΚΚΚΚΚΚΚ libsmartheap_mtd64.slΚΚΚ 64-bit multi-thread Debug SmartHeap HP C shared library
Hereβs how to get started with SmartHeap on HP-UX.
Hereβs how you set up your application to use the SmartHeap runtime library.Κ For information on debugging your application with Debug SmartHeap, see γDebugging an application with SmartHeap,δ later in this section.
Important!Κ If youβre not calling SmartHeap APIs, skip to the next page.Κ You donβt need to add SmartHeap header files if you simply want SmartHeap to replace malloc and operator new.Κ
For each file that makes SmartHeap API calls:
¬ For C source files, include smrtheap.h, which contains declarations for the C SmartHeap APIs.
ΚΚΚΚΚΚ #include <smrtheap.h>
¬ For C++ source files, include smrtheap.hpp, which contains declarations for SmartHeap versions of operator new and which itself includes smrtheap.h.
ΚΚΚΚΚΚ #include <smrtheap.hpp>
In both cases, the #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h).
To tell your compiler where to find SmartHeap include and library files:
1. Add the SmartHeap include directory to your compilerβs include path, or specify this directory on the compiler command line, for example:
-I/opt/sh/include
2. Add the SmartHeap lib directory to your linkerβs library path, or specify this directory on the linker command line, for example:
-L/opt/sh/lib
If your application is multi-threaded and targets HP-UX 11, you must add a call to MemRegisterTask()before your application creates its second thread.
Caution!Κ In HP-UX 11, SmartHeap does not initialize its heap mutexes until you call MemRegisterTask.Κ Therefore, if your program does not call MemRegisterTask before creating its second thread, memory allocation calls will not be serialized, which will almost certainly result in heap corruption.Κ
To relink your application with Runtime SmartHeap:
1. Add the Runtime SmartHeap Library, after all object files, but before any other libraries, to your linker command line.
If youβre using the SmartHeap
shared library withΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link
with these libraries
A 32-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap.sl
A single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap.sl,
libsmartheapC.a
A 32-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt.sl
A 32-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt.sl,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapC_mt.a
A 64-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap64.sl
A 64-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap64.sl,
libsmartheapC64.a
A 64-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt64.sl
A 64-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt64.sl,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapC_mt64.a
If youβre statically linking SmartHeap withΚΚΚΚΚΚΚΚ Link with these libraries
A 32-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap.a
A 32-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap.a,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ LibsmartheapC.a
A 32-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt.a
A 32-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt.a,
libsmartheapC_mt.a
A 64-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap64.a
A 64-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap64.a,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ LibsmartheapC64.a
A 64-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt64.a
A 64-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mt64.a,
libsmartheapC_mt64.a
2.
Run your make file to relink your application, for
example:
make /f myapp.mak
Hereβs how you set up your application for debugging with Debug SmartHeap.Κ
If you want to call Debug SmartHeap APIs (dbgMemXXX), youβll need to include the Debug SmartHeap header file and recompile your application.Κ
NoteΚ Simply linking with the Debug SmartHeap Library is sufficient to allow SmartHeap to perform full error detection, but in most cases youβll also want to recompile your application so that SmartHeap includes source file names and line numbers in its error reports.
For each file from which you call Debug SmartHeap APIs:
¬ Include heapagnt.h, which contains declarations of memory-allocation functions both for ANSI C (malloc, free, and so on) and for C++ (including the new and delete operators).Κ The #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h):
ΚΚΚΚΚΚ #include <heapagnt.h>
NoteΚ If you already include smrtheap.h, shmalloc.h, or smrtheap.hpp, then you donβt need to also include heapagnt.h.Κ The Runtime SmartHeap header files automatically include heapagnt.h when MEM_DEBUG is defined.
To tell your compiler where to find Debug SmartHeap include and library files:
1. Add the SmartHeap include directory to your compilerβs include path, or specify this directory on the compiler command line, for example:
-I/opt/sh/include
2. Add the SmartHeap lib directory to your linkerβs library path, or specify this directory on the linker command line, for example:
-L/opt/sh/lib
If your application is multi-threaded and targets HP-UX 11, you must add a call to MemRegisterTask()before your application creates its second thread.
Caution!Κ In HP-UX 11, SmartHeap does not initialize its heap mutexes until you call MemRegisterTask.Κ Therefore, if your program does not call MemRegisterTask before creating its second thread, memory allocation calls will not be serialized, which will almost certainly result in heap corruption.Κ
To recompile and relink your application with Debug SmartHeap:
1. Add the following options to your compiler command line:
-DMEM_DEBUG=1 -DDEFINE_NEW_MACRO=1
2. Add the Debug SmartHeap Library, before any other libraries, to your linker command line.
If youβre using the SmartHeap
shared library withΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link
with these libraries
A 32-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd.sl
A 32-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd.sl,
LibsmartheapCd.a
A 32-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd.sl
A 32-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd.sl,
LibsmartheapC_mtd.a
A 64-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd64.sl
A 64-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd64.sl,
LibsmartheapCd64.a
A 64-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd64.sl
A 64-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd64.sl,
LibsmartheapC_mtd64.a
If youβre statically linking SmartHeap withΚΚΚΚΚΚΚΚ Link with these libraries
A 32-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd.a
A 32-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd.a,
LibsmartheapCd.a
A 32-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd.a
A 32-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd.a,
LibsmartheapC_mtd.a
A 64-bit single-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd64.a
A 64-bit single-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheapd64.a,
LibsmartheapCd64.a
A 64-bit multi-threaded C applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd64.a
A 64-bit multi-threaded C++ applicationΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ libsmartheap_mtd64.a,
LibsmartheapC_mtd64.a
These files are in the lib subdirectory under the directory where you installed SmartHeap.
3.
Run your make file to rebuild your application, for
example:
make /f myapp.mak
If youβre developing your application with a compiler other than the HP-UX C or C++ compiler, and your compiler uses a different name-mangling convention from HP C++, hereβs how to get started with SmartHeap.
The source code to SmartHeapβs ANSI definitions for malloc, calloc, realloc, and free, and for the new and delete operators, is included with SmartHeap.Κ This allows you to recompile these definitions for use with a non-supported compiler or if you want to customize them for some reason.
NoteΚ The source code for the ANSI C/C++ SmartHeap definitions appears in the source directory in the SmartHeap directory.Κ
If your compiler uses a different C++ name-mangling convention or different operator new/delete semantics than the HP-UX C++, but you want SmartHeap to define malloc, then you can use the stand SmartHeap C archive or shared library (libsmartheapxxx.a) but do not use the SmartHeap C++ archive library (libsmartheapCxxx.a).Κ Instead, recompile the source file shnew.cxx with your C++ compiler and link with the resulting object file in addition to the SmartHeap C library. Use the following syntax for Runtime SmartHeap:
CC
φc shnew.cxx φo shnew.o
Use the following syntax for Debug SmartHeap:
CC
φc φDMEM_DEBUG=1 shnew.cxx φo shnewd.o
To recompile all SmartHeap source files so you can use Runtime SmartHeap with a non-supported compiler:
1. Compile
each file in the SmartHeap source directory, except those
beginning with γdbδ, which are for Debug SmartHeap
only.Κ For example:
cc -c shmalloc.c
2.ΚΚ Compile the SmartHeap source file shmalloc.c a second time, this
time with the macro constant MALLOC_MACRO defined on the
compiler command line.Κ For example:
cc -c -DMALLOC_MACRO=1 -oshmalmac.o shmalloc.c
3. Create
a new library, add all of the object files you compiled in steps 1
and 2, for example:
ar
ru myshlib.a shmalloc.o shmalmac.o
ΚΚ defpagsz.o defpool.o defflags.o
defsizfs.o
4. Link
the resulting library before the
regular SmartHeap library on the linker command line when building your
application.
You must compile the SmartHeap source files a second time to build a Debug SmartHeap library.Κ To recompile SmartHeap source files so you can use Debug SmartHeap with a non-supported compiler:
1. Compile
each file in the SmartHeap source directory with the macro
constant MEM_DEBUG defined on the compiler
command line, for example:
cc -c -DMEM_DEBUG=1 shmalloc.c
2.ΚΚ Compile the SmartHeap source file shmalloc.c a second time, this
time with the macro constant MALLOC_MACRO also defined on the
compiler command line.Κ For example:
cc
-c -DMEM_DEBUG=1 -DMALLOC_MACRO=1
ΚΚ -oshmalmac.o shmalloc.c
3. Create
a new library, add all of the object files you compiled in steps 1
and 2, for example:
ar
ru myshlib.a shmalloc.o shmalmac.o shnew.o
ΚΚ defpagsz.o defpool.o defflags.o
defsizfs.o
ΚΚ dbgrdfil.o dbgrdsiz.o dbusefil.o
dbfrefil.o
4. Link
the resulting library before the
regular SmartHeap library on the linker command line when building your
application.
Important!Κ You must place the SmartHeap Library before any other libraries on your linker command line to ensure that SmartHeap replaces malloc and operator new.
SmartHeap overrides malloc, calloc, realloc, free, new, and delete on all platforms, as explained in €2.3.2, €2.3.3, and €2.3.4 in the SmartHeap Programmerβs Guide.
If you donβt want SmartHeap to override one or more of these functions, you should remove the applicable object module from the SmartHeap libraries.Κ For example, use the following command to prevent SmartHeap from overriding malloc with the statically linked non-debug single-thread SmartHeap library:
ar d libsmartheap.a shmalloc.o
If you remove the shmalloc.o object module from a SmartHeap library, youβll need to define SmartHeap_malloc at file scope.Κ For example:
int SmartHeap_malloc = 0;
The libsmartheap_mt* SmartHeap libraries are fully thread-reentrant (MT-safe).Κ If you link SmartHeap with an application that creates multiple threads, make sure to call MemRegisterTask before starting the second thread.Κ
Caution!Κ In HP-UX 11, SmartHeap does not initialize its heap mutexes until you call MemRegisterTask.Κ Therefore, if your program does not call MemRegisterTask before creating its second thread, memory allocation calls will not be serialized, which will almost certainly result in heap corruption.Κ
For the HP-UX thread-reentrant libraries, the default memory pool (used by malloc and new) is always serialized after the MemRegisterTask call, unless you define MemDefaultPoolFlags to disable serialization.Κ Memory pools you create with MemPoolInit[FS] are not serialized unless you supply eitherΚ MEM_POOL_DEFAULT or MEM_POOL_SERIALIZE with the flags parameter.
If you use a given memory pool in only one thread at any time, do not supply MEM_POOL_DEFAULT or MEM_POOL_SERIALIZE when initializing the memory pool, or performance will suffer needlessly.Κ
Beginning in version 3.0, SmartHeap supports UNIX shared memory. SmartHeap uses the shared memory and semaphore facilities of the standard InterProcess Communication (IPC) package to implement shared memory pools.
Use MemPoolInitNamedShared to create shared memory pools.Κ The name parameter to MemPoolInitNamedShared must be a valid shared memory object key, according to UNIX conventions.Κ Use ftok() to create a key, and cast the result to (const char *) when passing to MemPoolInitNamedShared.Κ The flags parameter implicitly includes MEM_POOL_SHARED and MEM_POOL_SERIALIZE, so you do not need to specify these two flags.
The size parameter to MemPoolInitNamedShared specifies the total size of the underlying UNIX shared memory segment.Κ This cannot be resized once the memory pool is created.Κ Specify the total amount of memory you wish to make available to the memory pool.
When determining the pool size, keep in mind that each memory pool requires 4K of space for internal data structures and that free space occupies, on average, one third of the total space in a memory pool.Κ Therefore, if X is the maximum number of bytes you will allocate, specify a pool size of at least X * 2 + 4K.Κ You can use the MemPoolSize function to determine the actual memory in use in a pool, and use this as a guideline for the size parameter.Κ Note that Debug SmartHeap uses more memory than Runtime SmartHeap to store debugging state.
Shared memory pools are always serialized with inter-process synchronization mutual exclusion.Κ You can concurrently allocate blocks from a shared pool from multiple processes with complete safety.
Once a shared memory pool has been created, you can access it from other processes by calling MemPoolAttachShared and specifying the shared pool name.Κ Always pass NULL as the pool parameter for UNIX.
The MemPoolInitNamedShared call must have returned in the original process before MemPoolAttachShared will succeed in another process.Κ The reason for this restriction is that the semaphore that provides for serialization is stored in the shared memory object χ there can be no guarantee that the semaphore has been initialized and stored in the shared memory object until the shared memory object itself has been initialized.
You can call MemPoolAttachShared concurrently from as many processesΚ as you like once MemPoolInitNamedShared has returned.
You can use all of the SmartHeap allocation and de-allocation APIs with shared memory pools.Κ In Debug SmartHeap, shared memory pools fully support all of the same debugging facilities as private memory pools.
If SmartHeap detects an error such an overwrite in a shared pool, the error is reported to the process in which the error is detected, regardless of where the shared memory pool was created.
In Debug SmartHeap, when you compile your source files with MEM_DEBUG, SmartHeap stores a pointer to the source file name in each allocated memory block.Κ The actual file name is a string constant, generated by the compiler, that exists only in the current process.Κ For shared memory pools, SmartHeap copies file name strings into space owned by the shared memory pool, so the file names are accessible from all processes that attach to the pool.Κ Only the first 19 characters of file names are stored, so if you allocate shared memory from source files having longer names, the file names reported in memory error reports will be truncated to the first 19 characters.
To end access to a shared pool from a given process, call MemPoolFree from that process.Κ The shared pool will continue to exist until every process that has access to it calls MemPoolFree.Κ The process that creates a shared pool does not need to be the last process to call MemPoolFree; the shared pool is not owned by a particular process.
The SmartHeap API MemPoolInitRegion is now supported in HP and other UNIX platforms in SmartHeap 5.0 and higher. MemPoolInitRegion allows you to create a memory pool within a region of memory you supply.
For details about this API, see the entry for MemPoolInitRegion in €4.2, γFunction reference,δ in the SmartHeap Programmerβs Guide.
The following are some platform-specific defaults and attributes for this version of SmartHeap (all values in bytes, non-debug version of SmartHeap; all values subject to change in future versions):
Default page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize
Default fixed-size block sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 20ΚΚΚΚΚΚΚΚΚ 40ΚΚΚΚΚΚΚΚΚ MemPoolSetBlockSizeFS
Per-alloc overhead, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS
Per-alloc overhead, blocks < 256 bytesΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ MemAllocPtr, malloc, new
Per-alloc overhead, var ptr blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocPtr, malloc, new
Per-alloc overhead, var handle blocksΚΚΚΚΚΚΚΚΚ 16ΚΚΚΚΚΚΚΚΚ 32ΚΚΚΚΚΚΚΚΚ MemAlloc
Granularity, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS
Granularity, blocks < 256 bytesΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocPtr, malloc, new
Granularity, all othersΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocPtr, MemAlloc
Minimum block size, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS
Minimum block size, all othersΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocPtr, MemAlloc
Per-page overhead, all APIsΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 44ΚΚΚΚΚΚΚΚΚ 88
Minimum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize
Maximum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize
Size of empty memory poolΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolInit[FS]
The default error handler in the HP-UX version of SmartHeap prompts with γAbort, Ignore [, Retry]δ to stderr.Κ Retry is present only for recoverable errors.Κ
If you type:
α γaδ for Abort, SmartHeap calls the abort function, which terminates the application.Κ
α γiδ for Ignore, the SmartHeap function that reported the error returns with an error return value.Κ
α γrδ for Retry, SmartHeap retries the operation (for example, attempts a memory allocation again).