SmartHeapϊ
for SMP
Getting Started and
Platform Guide
for
Windows NT
Intel
Windows NT Alpha
Κ
Version 5.0
SmartHeap is a trademark and HeapAgent is a registered trademark of Compuware Corporation.Κ
Microsoft, Windows and Win32 are registered trademarks and Visual C++, Win95, and Windows NT are trademarks of Microsoft Corporation.Κ
All other trademarks are the property of their respective holders.
Copyright © 1994-1999 Compuware
Corporation.
All rights reserved.
Printed March 17, 1998
Weβd like to have your comments about this manual because 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
Improved SMP design... ... 1
New small-block allocator... ... 1
Reduced space overhead... ... 2
New API to control process heap growth... ... 2
Installing SmartHeapΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 3
Files in the Win32 SmartHeap release... ... 3
Using SmartHeap with Microsoft Visual C++ΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 6
Quick start... ... 6
Specifying the location of SmartHeap files........... ........... 7
Setting up a Visual C++ MFC project........... ........... 7
Setting up a Visual C++ non-MFC project........... ........... 8
Setting up your application for the SmartHeap runtime library... ... 9
Adding the SmartHeap header file to your source files........... ........... 9
Specifying the location of SmartHeap files........... ........... 9
Setting up the Visual C++ project file........... ........... 10
Debugging an application with SmartHeap... ... 12
Adding the Debug SmartHeap header file to your source files........... ........... 12
Specifying the location of Debug SmartHeap files........... ........... 13
Setting up the Visual C++ project file........... ........... 14
Recompiling or relinking your application........... ........... 16
SmartHeap/Microsoft Visual C++ compatibility issues... ... 17
Resolving Visual C++ duplicate-definition linker errors........... ........... 17
Using SmartHeap with Visual C++ 4.x Debug C runtime libraries........... ........... 17
Using SmartHeap with MFC 3.0/4.x........... ........... 18
Compiler functions that SmartHeap overrides........... ........... 19
Using SmartHeap with Borland C++ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 20
Getting started with the Runtime SmartHeap library... ... 20
Adding the SmartHeap header file to your source files........... ........... 20
Setting up the Borland C++ project file........... ........... 21
Debugging an application with SmartHeap... ... 22
Adding the Debug SmartHeap header file to your source files........... ........... 22
Setting up the Borland C++ project file........... ........... 23
Recompiling your application........... ........... 24
SmartHeap/Borland C++ compatibility issues... ... 24
Using SmartHeap with OWL........... ........... 24
SmartHeap Win32 platform notesΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 25
Multi-threading in Win32... ... 25
Using shared memory in SmartHeap for Win32... ... 25
SmartHeapβs automatic DLL patching... ... 29
First-chance exceptions reported by your debugger... ... 33
SmartHeap for Win32 calling convention... ... 33
SmartHeap Win32χspecific values... ... 33
Default error handling in Win32... ... 35
SmartHeap 5.0 introduces a new SMP-specific heap design that is both more speed and space efficient than SmartHeap 4.0.Κ SmartHeap 5.0 no longer creates multiple default memory pools for malloc but instead implements contention-free serialization within a single memory pool using a proprietary technique.Κ These changes improve performance even on uni-processor systems because SmartHeap now avoids the operating systemβs slow mutex serialization primitives.
Because multiple pools are no longer needed for optimal SMP performance, all non-shared SmartHeap memory pools created with MEM_POOL_SERIALIZE now provide contention-free SMP heap performance.Κ This means that you can now use all the SmartHeap APIs, including MemPoolInitRegion, for optimal performance on SMP systems.
In SmartHeap 4.0, only the default memory pool (that used by malloc and operator new) implemented any SMP optimization.
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.
In addition to the more space-efficient small-block allocator, SmartHeap 5.0 no longer creates multiple default memory pools for multi-threaded applications.Κ Hence, multiple threads are able to share the same pages of heap memory in a contention-free manner rather than using separate memory pools.
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 disks are not compressed or copy protected.Κ
― To install SmartHeap on your hard disk:
1.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Copy the files in the include directory
to a directory on your compilerβs include file path.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Copy the files in the library
directory corresponding to your compiler (msvc) to a directory on your linkerβs
library file path.
3.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Copy the files in the bin directory to
the Windows NT system32
or Windows 95 system
directory, or some other directory on the PATH.
4.ΚΚΚΚΚΚΚΚΚΚΚ (optional)Κ Copy the files from source and/or samples to examine
or modify the SmartHeap source files and sample applications.
The Win32 version of SmartHeap includes:
α Import libraries for Microsoft Visual C++.
α Statically linkable libraries for Microsoft Visual C++.
α Compiler-independent DLLs.
α Platform-independent header files.
α Platform-independent source files for the malloc and C++ operator new definitions.
The SmartHeap diskette contains the following Win32φspecific files:
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,
including operator
new.
includeΚΚΚΚΚΚΚΚΚΚΚ shmalloc.hΚΚΚΚΚΚΚΚΚΚΚ Header file containing declarations for
Runtime
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ SmartHeap ANSI C
functions (malloc,
etc.).
includeΚΚΚΚΚΚΚΚΚΚΚ heapagnt.hΚΚΚΚΚΚΚΚΚΚΚ Header file containing all Debug SmartHeap
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ declarations.
binΚΚΚΚΚΚΚΚΚΚΚ shsmp.dllΚΚΚΚΚΚΚΚΚΚΚ Runtime SmartHeap dynamic link library.
binΚΚΚΚΚΚΚΚΚΚΚ shsmp.dbgΚΚΚΚΚΚΚΚΚΚΚ Symbols for Runtime SmartHeap DLL.
binΚΚΚΚΚΚΚΚΚΚΚ ha312w32.dllΚΚΚΚΚΚΚΚΚΚΚ Debug SmartHeap dynamic link library.
msvcΚΚΚΚΚΚΚΚΚΚΚ shsmp.cΚΚΚΚΚΚΚΚΚΚΚ Source
file for γquick startδ procedure when using
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ SmartHeap with a
non-MFC application..
msvcΚΚΚΚΚΚΚΚΚΚΚ shmfcsmp.cppΚΚΚΚΚΚΚΚΚΚΚ Source
file for γquick startδ procedure when using
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ SmartHeap with
an MFC application..
msvcΚΚΚΚΚΚΚΚΚΚΚ shlsmpmt.libΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Runtime
SmartHeap multi-thread statically linkable
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ library for
Microsoft Visual C++.
msvcΚΚΚΚΚΚΚΚΚΚΚ shdsmpmt.libΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Runtime
SmartHeap multi-thread DLL import library for
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Microsoft Visual
C++.
msvcΚΚΚΚΚΚΚΚΚΚΚ shmfc4mt.libΚΚΚΚΚΚΚΚΚΚΚ Statically
linkable multi-thread library file for Microsoft
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Visual
C++/MFC 4.x/5.x χ for statically linked Release
ΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚ MFC only; must link before SmartHeap
library.
DirectoryΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ FileΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Description
msvcΚΚΚΚΚΚΚΚΚΚΚ haw32m.libΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Debug
SmartHeap DLL import library for Microsoft
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Visual C++.
msvcΚΚΚΚΚΚΚΚΚΚΚ hamfc32m.libΚΚΚΚΚΚΚΚΚΚΚ Statically
linkable library file for Microsoft Visual C++
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 2.x/MFC 3.0 χ
for statically linked _DEBUG
MFC only;
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ must link before
Debug SmartHeap library.
msvcΚΚΚΚΚΚΚΚΚΚΚ hamfc4m.libΚΚΚΚΚΚΚΚΚΚΚ Statically
linkable library file for Microsoft Visual
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ C++/MFC 4.x/5.x χ
for statically linked _DEBUG
MFC
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ only; must link before
Debug SmartHeap library.
borlandΚΚΚΚΚΚΚΚΚΚΚ shdsmpbt.libΚΚΚΚΚΚΚΚΚΚΚ Runtime SmartHeap multi-thread DLL import
library for
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Borland C++
(Intel only).
borlandΚΚΚΚΚΚΚΚΚΚΚ haw32b.libΚΚΚΚΚΚΚΚΚΚΚ Debug SmartHeap DLL import library for Borland
C++
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (Intel only).
sourceΚΚΚΚΚΚΚΚΚΚΚ shmalloc.cΚΚΚΚΚΚΚΚΚΚΚ SmartHeap
definition for ANSI C (malloc,
etc.)
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ functions χ
provided so you can customize these.
sourceΚΚΚΚΚΚΚΚΚΚΚ shmalmac.cΚΚΚΚΚΚΚΚΚΚΚ Macro
versions of SmartHeap ANSI C (malloc,
etc.)
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ functions.
sourceΚΚΚΚΚΚΚΚΚΚΚ shnew.cppΚΚΚΚΚΚΚΚΚΚΚ SmartHeap
definition for operator
new χ provided
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ so you can
customize and/or build a library for
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ non-supported
compilers.
sourceΚΚΚΚΚΚΚΚΚΚΚ shnewhnd.cppΚΚΚΚΚΚΚΚΚΚΚ SmartHeap new handler definitions
sourceΚΚΚΚΚΚΚΚΚΚΚ def*.c,ΚΚΚΚΚΚΚΚΚΚΚ Default memory pool definitions
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ db*.cΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ used by the above malloc and new definitions.
samplesΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚ Various
sample SmartHeap applications with Microsoft
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Visual C++ and
Borland C++ make files.
If youβre developing an application with Microsoft Visual C++ for 32-bit Windows, hereβs how to get started with SmartHeap.Κ
If your goal is to quickly get SmartHeap into your project so that it simply replaces malloc and operator new in your application, follow the instructions in this section.Κ This procedure uses a source file with #pragma statements that force references to the correct set of SmartHeap libraries for your application based on your projectβs compile-time flags.Κ This method is better than explicitly referencing SmartHeap libraries, because it ensures that the correct SmartHeap libraries are linked φ both initially and whenever you subsequently change your project settings.
If you want to use SmartHeap APIs, or if you want explicit control of which SmartHeap libraries your application uses, see the detailed instructions in the sections γSetting up your application for the SmartHeap runtime libraryδ and γDebugging an application with SmartHeap,δ later in this booklet.
― To tell Visual C++ where to find SmartHeap include and library files:
1. From the Visual C++ Tools menu, choose Options, and the Options dialog box appears.
2. Choose the Directories tab.Κ
3. Under Show Directories For, choose γInclude filesδ.
4. Choose the Add button, and add the path of the SmartHeap include directory to the include path list, for example:Κ
ΚΚ c:\smrtheap\include
5. Choose the Add button again, and add the path of the SmartHeap msvc directory to the include path list, for example:
ΚΚ c:\smrtheap\msvc
6. Under Show Directories For, choose γLibrary filesδ.
7. Choose the Add button, and add the path of the SmartHeap msvc directory to the library path list, for example:
ΚΚ c:\smrtheap\msvc
8. Choose OK to save your changes.
― To set up a Visual C++ MFC project to work with SmartHeap:
1.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Open the file stdafx.cpp in your project.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ At the beginning of stdafx.cpp, before any #include statements, insert the following text:
ΚΚ #include "shmfcsmp.cpp"
3.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Save your changes to stdafx.cpp.
4.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose Rebuild All from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x).
5.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Repeat the Rebuild All command for both the Release and Debug project configurations.
― To set up a Visual C++ project to work with SmartHeap if the project does not use MFC:
1.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Open your applicationβs project file.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Add the file shsmp.c to your project.Κ This file is located in the SmartHeap msvc directory. Κ
3.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Display the Project Settings dialog box.
4.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Under Settings For, choose γWin32 Releaseδ, if it isnβt chosen already.
5.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose the Link tab.
6.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ At the beginning of γObject/library modulesδ, before any other object files or libraries, enter the following:
ΚΚ .\Release\shsmp.obj
7.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Under Settings For, choose γWin32 Debugδ.
8.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose the Link tab.
9.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ At the beginning of γObject/library modulesδ, before any other object files or libraries, enter the following:
ΚΚ .\Debug\shsmp.obj
10.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save changes to Project Settings.Κ
11.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Rebuild both Release and Debug versions of the project.
Hereβs the detailed procedure for setting 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 booklet.
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 or afx.h).
― To tell Visual C++ where to find SmartHeap include and library files:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Options from the Visual C++ Tools menu, and the Options dialog box appears.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Directories tab.Κ
3.ΚΚΚΚΚΚΚΚΚΚΚ Under Show Directories For, choose γInclude files.δ
4.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Add button, and add the path of the SmartHeap include directory to the include path list, for example:Κ
c:\smrtheap\include
5.ΚΚΚΚΚΚΚΚΚΚΚ Again under Show Directories For, choose γLibrary files.δ
6.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Add button, and add the path of the SmartHeap msvc directory to the library path list, for example:Κ
c:\smrtheap\msvc
7.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save your changes.
Hereβs how you set up a Visual C++ project file to work with SmartHeap.Κ
NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ
― To set up a Visual C++ project to work with SmartHeap:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Workspace from the Visual C++ File menu, and open your applicationβs project file.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.
3.ΚΚΚΚΚΚΚΚΚΚΚ Under Settings For, choose γWin32 Release,δ if it isnβt chosen already.
4.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Link tab.
5.ΚΚΚΚΚΚΚΚΚΚΚ Specify the SmartHeap libraries that you want to link with.Κ At the beginning of Object/Library Modules, before any Visual C++ libraries, enter the appropriate libraries:
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these
libraries
If youβre using the SmartHeap DLL withΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (in
the order shown)
An EXE or DLL in a multi-threaded app,
without MFC, with statically linked MFC 3.0,
or the MFC 3.0/4.x DLLΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shdsmpmt.lib
An EXE or DLL with a multi-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4mt.lib, shdsmpmt.lib
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these
libraries
If youβre statically linking SmartHeap withΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (in
the order shown)
An EXE or DLL in a multi-threaded app,
without MFC, or with statically-linked MFC 3.0ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shlsmpmt.lib
An EXE or DLL in a multi-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4mt.lib, shlsmpmt.lib
NoteΚ The above library files appear in the msvc sub-directory under the directory where you installed SmartHeap.Κ
MFC users!Κ If your application uses the DLL version of MFC, you must use the DLL version of SmartHeap (shdsmpmt.lib).Κ If your application links with Debug MFC, you must link with Debug SmartHeap.
6.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save changes
to Project Settings.Κ
7.ΚΚΚΚΚΚΚΚΚΚΚ Choose Build filename
from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and
Visual C++ relinks the application.
Hereβs the detailed procedure for setting 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.Κ Moreover, if your application is compiled with Visual C++ PDB debugging information, Debug SmartHeap obtains file names and line numbers directly from the debugging information.Κ So if you arenβt calling Debug SmartHeap APIs, there is no need to recompile your application for Debug SmartHeap.
Alpha users!Κ For non-Intel platforms, Debug SmartHeap is not able to obtain file and line information from Visual C++ PDB debug information.Κ Therefore, if youβre using the Alpha version of SmartHeap and if you want Debug SmartHeap to include file names and line numbers in error reports, you must include heapagnt.h in your source files and define the preprocessor constants MEM_DEBUG and DEFINE_NEW_MACRO.
― 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 or afx.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 Visual C++ where to find Debug SmartHeap include and library files:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Options from the Visual C++ Tools menu, and the Options dialog box appears.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Directories tab.Κ
3.ΚΚΚΚΚΚΚΚΚΚΚ Under Show Directories For, choose γInclude files.δ
4.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Add button, and add the path of the Debug SmartHeap include directory to the include path list, for example:Κ
ΚΚ c:\smrtheap\include
5.ΚΚΚΚΚΚΚΚΚΚΚ Again under Show Directories For, choose γLibrary files.δ
6.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Add button, and add the path of the Debug SmartHeap msvc directory to the library path list, for example:Κ
ΚΚ c:\smrtheap\msvc
7.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save your changes.
Hereβs how you set up a Visual C++ project file so you can debug the corresponding EXE or DLL with Debug SmartHeap.Κ
NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ
― To set up a Visual C++ project to work with Debug SmartHeap:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Workspace from the Visual C++ File menu, and open your applicationβs project file.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.
3.ΚΚΚΚΚΚΚΚΚΚΚ Under Settings For, choose γWin32 Debug,δ if it isnβt chosen already.
4.ΚΚΚΚΚΚΚΚΚΚΚ Choose the C/C++ tab and, under Debug Info, choose γProgram Database.δ
5.ΚΚΚΚΚΚΚΚΚΚΚ (required for Alpha and PowerPC, optional otherwise)Κ If youβre using Intel SmartHeap, and if your application doesnβt call Debug SmartHeap APIs, you can skip this step.
Still at the C/C++ tab, in Preprocessor Definitions, add:
ΚΚ MEM_DEBUG=1
6.ΚΚΚΚΚΚΚΚΚΚΚ (Alpha and PowerPC only)Κ Also in Preprocessor Definitions, add:
ΚΚ DEFINE_NEW_MACRO=1
7.ΚΚΚΚΚΚΚΚΚΚΚ Choose the Link tab.
8.ΚΚΚΚΚΚΚΚΚΚΚ Check the Generate Debug Info checkbox.
9.ΚΚΚΚΚΚΚΚΚΚΚ Still at the link tab, based on the following table, specify the Debug SmartHeap libraries that you want to link with.Κ At the beginning of Object/Library Modules, before the Visual C++ libraries, enter the appropriate libraries:
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these
libraries
If youβre debuggingΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (in
the order shown)
An
EXE or DLL without MFC, with non-Debug MFC,
or with the Debug MFC DLLΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib
An
EXE or DLL with Debug MFC 3.0 statically linkedΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ hamfc32m.lib,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib
An
EXE or DLL with Debug MFC 4.x statically linkedΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ hamfc4m.lib,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib
NoteΚ The files haw32m.lib, hamfc32m.lib, and hamfc4m.lib appear in the msvc sub-directory under the directory where you installed SmartHeap.Κ
10.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save changes
to Project Settings.Κ
If youβre calling Debug SmartHeap APIs, or if youβre using the Alpha version of SmartHeap, you need to include the Debug SmartHeap header file and recompile your application.Κ Information on including this header file appears at the beginning of the section.
NoteΚ You only need to recompile your application if youβve included the Debug SmartHeap header file in one or more of your source files.Κ However, you must always relink each EXE and DLL in your application for Debug SmartHeap.
― To recompile your application:
1.ΚΚΚΚΚΚΚΚΚΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Rebuild All from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and Visual C++ compiles the application.
― To relink your application:
1.ΚΚΚΚΚΚΚΚΚΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Build filename from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and Visual C++ relinks the application.
If your application does not reference the functions malloc and free, you may receive duplicate-definition linker errors when you try to link the SmartHeap library into your application.Κ To eliminate these linker errors, use the following procedure:
1.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.
3.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Choose the Link tab, and select the Input category.
4.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ In Force Symbol References, add the symbol _SmartHeap_malloc.
Alternatively, if youβre using the command-line linker, add the following linker option:
-include:_SmartHeap_malloc
Visual C++ 4.0 and higher includes debugging versions of the C runtime libraries.Κ Debug SmartHeap supports these libraries, but Runtime SmartHeap does not.Κ Therefore, if your application links with the Debug C runtime library, it must also link with the Debug SmartHeap library (haw32m.lib) rather than the Runtime SmartHeap library (shlsmpmt.lib or shdsmpmt.lib).
Debug SmartHeap overrides the Visual C++ functions _calloc_dbg, _realloc_dbg, _free_dbg, _msize_dbg, _expand_dbg, _CrtCheckMemory, _CrtDumpMemoryLeaks, _CrtIsValidHeapPointer, _CrtIsMemoryBlock, _CrtSetAllocHook, and _CrtSetBreakAlloc.Κ Debug SmartHeap emulates the behavior of each of these Visual C++ routines, so you can use these routines interchangeably with Debug SmartHeap APIs.
Debug SmartHeap defines replacements for all of the public memory-debugging APIs of the Microsoft Foundation Class Library.Κ This allows Debug SmartHeap to be used with _DEBUG MFC.Κ Runtime SmartHeap is not compatible with Debug MFC.Κ Debug SmartHeap emulates all MFC APIs except for AfxDoForAllObjects and the CMemoryState member functions.Κ For the unsupported APIs, SmartHeap defines stubs that print error messages that point to the corresponding SmartHeap APIs offering similar functionality.
If your application uses the statically linked Debug version of MFC, you must link with the SmartHeap/MFC integration library:Κ hamfc32m.lib for MFC 3, or hamfc4m.lib for MFC 4.x.Κ Be sure to place this library before either the SmartHeap or MFC libraries on the linker command line.Κ If your application uses the statically linked Release version of MFC 4.x, you must link with shmfc4m.lib.Κ Note that no integration is required for the Release (non-debug) version of MFC 3.0, since this library doesnβt override new/delete.
The DLL version of MFC 3.0 and higher requires applications to share the same memory manager that the MFC DLL itself uses.Κ (This is a change from MFC 2.x, where the MFC DLL called back to the client application for memory management.)Κ To allow SmartHeap applications to work with the DLL version of MFC, the SmartHeap DLLs automatically patch the memory-management functions in the MFC and CRT DLLs.Κ
ImportantΚ The statically linked SmartHeap library (shlsmpmt.lib) does not patch MFC or CRT DLLs, so if your application uses the MFC or CRT DLLs, you must use the DLL version of SmartHeap (shdsmpmt.lib or haw32m.lib).
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 of the SmartHeap Programmerβs Guide.Κ In addition, the Microsoft Visual C++ SmartHeap libraries override the following functions:
FunctionΚΚΚΚΚ SmartHeap library module
_expandΚΚ shmalloc
_heapchkΚΚ shmalloc
_heapminΚΚ shmalloc
_heapsetΚΚ shmalloc
_heapwalkΚΚ shmalloc
_msizeΚΚ shmalloc
If you donβt want SmartHeap to override these C runtime functions, you should remove the applicable object module from the SmartHeap library that you link with.Κ For example, use the following command to prevent SmartHeap from overriding malloc with the statically linked Microsoft Runtime SmartHeap library:
lib shlsmpmt.lib /remove:shmalloc.obj
If you remove the shmalloc object module from a SmartHeap library, you will need to define SmartHeap_malloc at file scope.Κ For example:
int SmartHeap_malloc = 0;
If youβre developing an application with Borland C++ version 4.x or 5.x for 32-bit Windows, hereβs how to get started with SmartHeap.Κ
Hereβs how you set up your application to use the Runtime SmartHeap library.Κ For information on debugging your application with Debug SmartHeap, see γDebugging an application with SmartHeap,δ later in this booklet.
Important!Κ If youβre not calling SmartHeap APIs, skip to the next section.Κ 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 or afx.h).
Hereβs how you set up a Borland C++ project file to work with Runtime SmartHeap.
NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ
― To set up a Borland C++ project to work with Runtime SmartHeap:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Project from the Borland C++ Project menu, and open your .ide file.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Project from the Options menu, and the Project Options dialog box appears.
3.ΚΚΚΚΚΚΚΚΚΚΚ From the Topics list, choose Directories (if it isnβt already chosen), and the directories options appear.
4.ΚΚΚΚΚΚΚΚΚΚΚ In the Include list, add the path of the SmartHeap include directory, for example:Κ
c:\smrtheap\include
5.ΚΚΚΚΚΚΚΚΚΚΚ In the Library list, add the path of the SmartHeap borland directory, for example:Κ
c:\smrtheap\borland
6.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save your changes.
7.ΚΚΚΚΚΚΚΚΚΚΚ Add the Runtime SmartHeap Libraries that are pertinent to your application.Κ To add each library, select your projectβs EXE or DLL in the Project window, press the Insert key, and add the file shdsmpbt.lib in the Add to Project List dialog box.
This file appears in the borland sub-directory under the directory where you installed SmartHeap.Κ
OWL users!Κ If your application uses the DLL version of OWL, you must use the DLL version of SmartHeap (shdsmpbt.lib).
8.ΚΚΚΚΚΚΚΚΚΚΚ Choose Make All from the
Project menu, and Borland C++ links the application.
Hereβs how you set up your application for debugging with Debug SmartHeap.Κ
If you want Debug SmartHeap to include source file and line information in its error reports, or if you want to call Debug SmartHeap APIs (dbgMemXXX), you 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 for which you want SmartHeap to report source file and line information:
¬ 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 or afx.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.
Hereβs how you set up a Borland C++ project file to work with Debug SmartHeap.
NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ
― To set up a Borland C++ project to work with Debug SmartHeap:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Project from the Borland C++ Project menu, and open your .ide file.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Project from the Options menu, and the Project Options dialog box appears.
3.ΚΚΚΚΚΚΚΚΚΚΚ From the Topics list, choose Directories (if it isnβt already chosen), and the directories options appear.
4.ΚΚΚΚΚΚΚΚΚΚΚ In the Include list, add the path of the SmartHeap include directory, for example:Κ
c:\smrtheap\include
5.ΚΚΚΚΚΚΚΚΚΚΚ In the Library list, add the path of the SmartHeap borland directory, for example:Κ
c:\smrtheap\borland
6.ΚΚΚΚΚΚΚΚΚΚΚ Now double-click Compiler in the Topics list, and the list expands to show the compiler options.
7.ΚΚΚΚΚΚΚΚΚΚΚ Choose Defines from the list, and enter this value in Defines:
MEM_DEBUG=1
DEFINE_NEW_MACRO=1
8.ΚΚΚΚΚΚΚΚΚΚΚ Choose OK to save your changes.
9.ΚΚΚΚΚΚΚΚΚΚΚ Add the Debug SmartHeap library, haw32b.lib.Κ Select your projectβs EXE or DLL in the Project window, press the Insert key, and specify the file in the Add to Project List dialog box:
This file appears in the borland sub-directory under the directory where you installed SmartHeap.Κ
If you want SmartHeap to include source file and line number information in its error reports, you need to include the Debug SmartHeap header file and recompile your application.Κ Information on including this header file appears at the beginning of the section.
NoteΚ You only need to recompile your application if youβve included the Debug SmartHeap header file in one or more of your source files.Κ However, you must always relink each EXE and DLL in your application for SmartHeap.
― To recompile your application:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Project from the Project menu, and open the .ide file, if you havenβt already done so.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Build All from the Project menu, and Borland C++ compiles the application.
― Or, to relink your application:
1.ΚΚΚΚΚΚΚΚΚΚΚ Choose Open Project from the Project menu, and open the .ide file, if you havenβt already done so.
2.ΚΚΚΚΚΚΚΚΚΚΚ Choose Make All from the Project menu, and Borland C++ links the application.
The DLL version of the Borland Object Windows Library (OWL) requires applications to share the same memory manager that the OWL DLL itself uses. To allow SmartHeap applications to work with the DLL version of OWL, the SmartHeap DLLs automatically patch the memory-management functions in the OWL and CRT DLLs.Κ
SmartHeap for SMP is fully thread-reentrant and thread-enabled, meaning that multiple threads calling malloc/free/new/delete can concurrently proceed with no blocking.
NoteΚ It is not necessary to call MemRegisterTask or define MemDefaultPoolFlags in SmartHeap 4.0 and higher as it in previous SmartHeap versions and on some other platforms.ΚΚ However, if you create your own SmartHeap memory pools, you must still specify MEM_POOL_SHARED in the flags parameter if the pool is referenced in more than one thread.
Beginning in version 3.1, SmartHeap supports Windows 95 and Windows NT shared memory.Κ SmartHeap uses memory mapped files to implement shared memory pools.Κ All of the SmartHeap allocation APIs that accept a memory pool parameter, including MemAllocPtr, MemAllocFS, MemAlloc, and operator new with the placement syntax, support Win32 shared memory.
To create shared memory pools in Win95 or NT, use MemPoolInitNamedShared or MemPoolInitNamedSharedEx.Κ You must specify both the name and size parameters as non-zero.Κ
α The name parameter is passed through to CreateFileMapping.Κ It can contain any characters but backslash (\).Κ
α The size parameter is used to reserve address space for the memory pool.Κ The memory pool cannot grow larger than this size, but SmartHeap will commit memory to the shared pool based on allocation requests.Κ Therefore, you should specify a size that is the maximum (high water mark) memory requirement for the pool.Κ SmartHeap will commit only as much physical memory as you allocate from the pool regardless of the size parameter you specify.Κ Nevertheless, you should not specify an excessively large size parameter, or you will waste address space.Κ This is particularly important in Win95, where shared memory address space is common to all processes.Κ To help in choosing a suitable size value, you can use MemPoolSize to determine the total memory actually consumed by a memory pool at any time.
Once youβve created a shared memory pool, you can call MemPoolAttachShared from other processes, specifying the same name you previously supplied to MemPoolInitNamedShared[Ex].Κ If you donβt know which of your processes will create and which will attach to a shared pool, you can call MemPoolInitNamedShared[Ex] from each process, specifying the same name in each call.Κ If a shared pool of the given name already exists, MemPoolInitNamedShared[Ex] will attach to and return that pool.Κ In this case, the other parameters to MemPoolInitNamedShared[Ex] are ignored.
Because SmartHeap allocation APIs return direct pointers to memory, SmartHeap requires shared memory pools to be mapped to the same address in each process.Κ In Windows 95, this is not a problem since shared memory is always mapped to the same address in each process.Κ NT, however, does not guarantee that shared memory is mapped to the same address in each process.Κ If you create a shared pool in one process and call MemPoolAttachShared in another process in which the address of the memory pool is unavailable, MemPoolAttachShared will fail (it will return NULL after reporting a MEM_OUT_OF_MEMORY error).
To solve this problem, SmartHeap 3.1 includes a new API, MemPoolInitNamedSharedEx.Κ This API lets you specify the address at which a shared memory pool should be mapped (addr) and/or an array of process IDs that will access the shared pool (pids).Κ If you specify a non-NULL value for pids, SmartHeap will search the address space of each of these processes to find a suitable address that is available in all of the processes.
MemPoolInitNamedSharedEx also lets you specify security Win32 attributes for the shared pool.Κ The security parameter you specify is passed through to CreateFileMapping.
In choosing the addr parameter, you should examine the DLLs in each process that will use the shared pool, and find an address space location that is available in each process.Κ You should use MemPoolAttachShared as early as possible during process initialization to ensure that the address range you selected is not used by a call to VirtualAlloc.
If you specify addr as NULL, SmartHeap chooses a random address in the upper half of the application address space for NT, or a random address in the shared memory address space for Win95.Κ This minimizes the chance of collisions with other shared pools or VirtualAlloc objects (which are normally allocated from the beginning of the address space).
If all of the processes that will share a memory pool are running at the time that you create the memory pool, you can have SmartHeap find an address automatically by specifying pidCount and pids parameters.Κ In this case, the shared pool will be mapped into each processβs address space before the MemPoolInitNamedSharedEx call returns.Κ If there is no address space region of suitable size available in every process, MemPoolInitNamedSharedEx will fail (this would be very unusual considering that each process has 2 GB of address space).
If youβve specified a pids array to MemPoolInitNamedSharedEx, and if youβre running on NT, and if youβre using the SmartHeap DLL, then when you call MemPoolAttachShared from one of the processes that you previously specified to MemPoolInitNamedSharedEx, you may specify the return value of MemPoolInitNamedSharedEx as the pool parameter along with a NULL name parameter.Κ This is the only case where the name parameter of MemPoolAttachShared is optional in the Win32 SmartHeap implementation.Κ You may also specify both name and pool parameters as non-NULL, or just the name parameter as non-NULL if you wish.Κ For Win95 compatibility, the name parameter must always be specified as non-NULL.
CautionΚ The address-space searching feature of MemPoolInitNamedSharedEx is implemented fully only in the DLL version of SmartHeap (both Debug and Runtime).Κ In the DLL version (shdsmpmt.lib), MemPoolInitNamedSharedEx will cause the new pool to be mapped into each specified process before the call returns, which guarantees that the pool can be shared between the given processes.Κ In the statically linked version of SmartHeap (shlsmpmt.lib), MemPoolInitNamedSharedEx searches each specified process and reserves space at a suitable address region, but does not actually map the pool into the other processes during the call to MemPoolInitNamedSharedEx.Κ If the chosen address region becomes unavailable in a given process before MemPoolAttachShared is called in that process, then MemPoolAttachShared will fail.Κ Therefore, to guarantee that a set of processes will be able to successfully share a memory pool in NT, you must use the DLL version of SmartHeap (shdsmpmt.lib).
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.
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.Κ Debug SmartHeap error reports in Win32 include the ID of both the thread and the process where the error wasΚ detected, as well as the thread/process IDs where the erroneous object 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 private address space of the 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; shared pools are not owned by a particular process.
See also €2.4.5, γShared memory,δ and the entries for MemPoolInitRegion, MemPoolInitNamedShared, MemPoolInitNamedSharedEx, and MemPoolAttachShared in €4.2, γFunction reference,δ in the SmartHeap Programmerβs Guide.
NoteΚ You can skip this section if you statically link SmartHeap.Κ Only the SmartHeap DLL does any patching of DLLs.
When you link with the DLL version of SmartHeap, SmartHeap automatically patches heap routines in compiler DLLs, such as the C runtime and MFC DLLs.Κ If youβre using MFC, OWL, or other DLLs that share heap memory with their client EXE, this patching is required to ensure that objects allocated in these DLLs can be safely freed by your applicationβs EXE.
NoteΚ SmartHeap γpatchingδ does not affect the on-disk copy of any DLLs, and it does not affect other applications that use the DLLs that SmartHeap patches.Κ SmartHeap modifies only the in-memory copy of the DLL that is private to your applicationβs process.Κ Runtime SmartHeap patches only those DLLs in your process that export heap APIs such as malloc or new.Κ Debug SmartHeap also patches all DLLs that have PDB debug information and that contain statically linked heap API definitions.
In some cases, you may want to disable SmartHeapβs patching of compiler DLLs.Κ You can selectively or globally disable SmartHeapβs patching by adding values to the registry or by adding an API definition to your EXE or DLL.Κ These two techniques are described in the following two sections.
You can disable patching either for all of the DLLs in your application or for selected DLLs by adding keys to the Windows NT or Windows 95 registry, as described in the following procedures.Κ You can add registry keys in either of two ways:
α Use the Registry Editor (regedt32.exe on NT, regedit.exe on Win95) to add these registry entries manually.
α Use the Win32 registry API to add registry keys and values programmatically.Κ This is the method you should use if your application uses the Runtime SmartHeap DLL and you donβt want SmartHeap to patch one or more of the DLLs used by your application.
Important!Κ If youβre using MFC, OWL, or other DLLs that share heap memory with their client EXE, do not disable patching for those DLLs, or you may encounter incompatibilities and unexpected GPFs.
NoteΚ SmartHeap stores its settings under the HKEY_LOCAL_MACHINE registry key for compatibility with NT services.Κ Previous SmartHeap releases (version 3.01 and earlier) stored these settings in HKEY_CURRENT_USER.
― To disable SmartHeap patching for all DLLs:
1.ΚΚΚΚΚΚΚΚΚΚΚ Add the following key to
the Windows NT or Windows 95 registry:
For Runtime SmartHeap, add the registry key:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\<app-name>
For Debug SmartHeap, add the registry key:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\Apps\<app-name>
where <app-name> is the full path of your applicationβs EXE.
Important!Κ Use forward slashes (/) as directory delimiters.Κ Backslash characters (\) are not legal in registry keys.Κ
For example, if your applicationβs full path is c:\apps\foo.exe and you want to disable patching for Runtime SmartHeap, youβd add the registry key:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\c:/apps/foo.exe
2.ΚΚΚΚΚΚΚΚΚΚΚ At the Runtime SmartHeap
or Debug SmartHeap registry key that you added in step 1, add an entry with the
following values:
α Value name:Κ PatchProcessOn
α Data type:Κ REG_DWORD
α Value:Κ 0 (zero).Κ Zero means SmartHeap should not patch DLLs, and a non-zero value means that SmartHeap should patch DLLs.
Optionally, the SmartHeap DLL can patch calls to the system
heap APIs LocalAlloc,
GlobalAlloc,
and HeapAlloc
with faster SmartHeap equivalents.Κ
These APIs are replaced on Windows NT only, not on Windows 95. Beginning
in SmartHeap 4.0, patching of system heap APIs is disabled by default.
― To enable SmartHeap patching of system heap APIs:
1.ΚΚΚΚΚΚΚΚΚΚΚ Add the Runtime SmartHeap or Debug SmartHeap registry key as described in step 1 of γTo disable SmartHeap patching for all DLLsδ above.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ At the registry key that you added in step 1, add an entry with the following values:
αΚΚΚΚΚΚΚ Value name:Κ PatchSystemAllocsOn
αΚΚΚΚΚΚΚ Data type:Κ REG_DWORD
αΚΚΚΚΚΚΚ Value:Κ 1 (one).Κ A non-zero value means SmartHeap shouldΚ patch system heap APIs, and a zero value means that SmartHeap should not patch system APIs.
― To disable patching in certain DLLs in your application but not in the entire process:
1.ΚΚΚΚΚΚΚΚΚΚΚ Add the following key and value to the Windows NT or Windows 95 registry:
For Runtime SmartHeap, add the registry key:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\<app-name>\SkipDLLs
For Debug SmartHeap, add the registry key:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\Apps\<app-name>\SkipDLLs
Alternatively, if youβre using Debug SmartHeap with more than one application, or if youβre running your application from more than one directory, you can use the following registry key to have Debug SmartHeap skip DLLs in all applications you use with Debug SmartHeap:
HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\SkipDLLs
2.ΚΚΚΚΚΚΚΚΚΚΚ At the Runtime SmartHeap or Debug SmartHeap registry key that you added in step 1, add DLLs that you do not want SmartHeap to patch.Κ For each DLL you want SmartHeap to skip (that is, not patch), add an entry with the following values:
α Value name:Κ File<n> where <n> is 0 (zero) for the first DLL you add, 1 (one) for the second, and so on
α Data type:Κ REG_SZ
α Value:Κ the DLL name (not the full path of the DLL χ just file name and extension)
3.ΚΚΚΚΚΚΚΚΚΚΚ To the same registry key,
add another entry with the following values:
α Value name:Κ FileCount
α Data type:Κ REG_DWORD
α Value:Κ the number of DLLs youβve added to the registry key
For example, if you want to skip patching DLLs foo.dll and bar.dll, youβd add the following three registry values to the Runtime SmartHeap or Debug SmartHeap registry, as appropriate:
α Name File0, type REG_SZ, value foo.dll
α Name File1, type REG_SZ, value bar.dll
α Name FileCount, type REG_DWORD, value 2
When you load a Debug SmartHeap application and Debug SmartHeap encounters DLLs it does not recognize and that do not have either PDB debugging information or exports of heap APIs, Debug SmartHeap will prompt you with the following message:
HeapAgent is unable to find memory allocation function symbols in module <unknown.dll>. Do you want to add <unknown.dll> to the list of DLLs HeapAgent skips in subsequent loads (choose γYesδ only if this DLL does not share the heap with your EXE or other DLLs)?
If you choose the Yes button, Debug
SmartHeap adds the DLL name to the registry entry HKEY_LOCAL_MACHINE\Software\
MicroQuill\HeapAgent\SkipDLLs, so this DLL will be skipped whenever
Debug SmartHeap encounters it in any application.
― To trace SmartHeap patching activity to a log file:
1.ΚΚΚΚΚΚΚΚΚΚΚ Add the Runtime SmartHeap or Debug SmartHeap registry key as described in step 1 of γTo disable SmartHeap patching for all DLLs,δ above.
2.ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ At the registry key that you added in step 1, add an entry with the following values:
αΚΚΚΚΚΚΚ Value name:Κ PatchTraceFile
αΚΚΚΚΚΚΚ Data type:Κ REG_SZ
αΚΚΚΚΚΚΚ Value:Κ The full path of a file name to which SmartHeap will log all patching activity.Κ SmartHeap will create the file if it does not exist.
When you run your application under both SmartHeap and your source debugger at the same time, your debugger may report one or more first-chance exceptions in the Debug SmartHeap DLL, ha312w32.dll.Κ These exceptions are caused by SmartHeapβs address validation, which uses Win32 structured-exception handling.Κ The exceptions are handled by SmartHeap and are not errors in your application or in SmartHeap.
In SmartHeap versions 2.0 and 2.1 for Win32, APIs were defined with the __cdecl calling convention.Κ SmartHeap 2.2 and higher uses __stdcall calling convention to define its APIs, which is the calling convention used for the Windows NT and Windows 95 system DLLs.
The following are some platform-specific defaults and attributes for the Win32 version of SmartHeap (all values in bytes, non-debug version of SmartHeap; all values subject to change in future versions):
DescriptionΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Intel/[Alpha]ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Related API, if any
Default
page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemPoolSetPageSize
Default
γsmallδ block thresholdΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 256ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemPoolSetSmallBlockSize
Per-alloc
overhead, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS
Per-alloc
overhead, γsmallδ blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr
Per-alloc
overhead, var ptr blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 2ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr
Per-alloc
overhead, var handle blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemAlloc
Granularity,
block typesΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4/8ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ all
Minimum
block size, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4/8ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS
Minimum
block size, γsmallδ blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4/8ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr
Minimum
block size, all othersΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemAlloc
Per-page overhead, all APIsΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 58
Minimum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemPoolSetPageSize
Maximum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemPoolSetPageSize
Size of empty memory poolΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemPoolInit[FS]
The default error handler in the Windows version of Runtime SmartHeap uses a task-modal message box (see MemDefaultErrorHandler in €4.2, γFunction referenceδ in the SmartHeap Programmerβs Guide).Κ If there is insufficient memory to display a task-modal message box, a system modal message box is displayed.
For non-recoverable errors, the default error handler shows an OK button and a Cancel button on the message box.Κ For recoverable errors, Abort, Retry, and Ignore buttons are shown.
Choosing either the Cancel or Abort button causes an int 3 (breakpoint interrupt) to be executed, which will stop in the debugger if a debugger is currently running.Κ If no debugger is running, SmartHeap will call the abort function, which terminates the current task.
Choosing either OK or Ignore causes the SmartHeap function that reported the error to return with an error return value.Κ Choosing Retry causes SmartHeap to retry the operation (for example, memory allocation).
In Debug SmartHeap, an Error Report dialog box is displayed if DBGMEM_OUTPUT_PROMPT is supplied to dbgMemSetDefaultErrorOutput (it is by default).Κ The Debug SmartHeap Error Report dialog box contains the following buttons:
αΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Debug breaks to your source debugger, or if youβre not currently running under a debugger, this button opens the current Win32 γjust-in-timeδ debugger if you have installed one.
αΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Continue resumes execution of your application.
αΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Retry retries the failed operation -- this button is enabled only for out-of-memory errors.
αΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Terminate closes your application.
αΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ The remaining buttons on the error report dialog box are available only if you have installed SmartHeapβs sister product, HeapAgent.Κ HeapAgent provides a graphical user interface for controlling heap debugging settings and diagnosing heap errors.Κ If you have HeapAgent, these buttons are used to open HeapAgent browsers, suppress errors, or get online help about a particular error.
If DBGMEM_OUTPUT_CONSOLE is supplied to dbgMemSetDefaultErrorOutput, output is sent to the OutputDebugString Win32 API, and will be displayed in the output window of the active debugger.Κ DBGMEM_OUTPUT_BEEP causes a MessageBeep on errors.