1# Programming tips
2
3## Introduction
4
5This section was created to guide users on how to use some of the new
6features going into the Xalan source code base.  Some of the features
7discussed in this section are based on feedback and questions posted on
8the xalan-c-users newsgroup.  This section will cover the benefits of
9certain features and provide users with programming hints on how to
10utilize the features in their applications.
11
12## Pluggable Memory Management
13
14Pluggable memory management was added as a new feature in Xalan-C++
15Version 1.8.  This feature introduces an object called `MemoryManager`
16which allows applications with stricter memory management requirements
17to utilize a more efficient allocation method.  This `MemoryManager`
18object can be applied to each processor instance, thus recovery
19procedures from memory leaks or processor crashes will be applied to
20the associated instance only.
21
22The memory management model is similar to the memory management feature
23provided by the Xerces-C++ XML Parser.
24
25### How To Use This Feature
26
27To apply memory management to your application, the `MemoryManager`
28object needs to be specified in two stages:
29
30* At initialization phase.  The purpose of specifying a `MemoryManager`
31  object during initialization is to create a separate memory manager
32  for the overall application.  Example of how this can be done is
33  shown in the example below:
34
35```c++
36// Initialization step
37static void XalanTransformer::initialize(MemoryManager* initMemoryManager=0);
38```
39
40* Creation of a transformer instance.  This creates a unique memory
41  manager for the instance of the processor.  This step is optional.
42  If no memory manager is provided, the global heap is used as the
43  memory source.  Example of this is shown below:
44
45```c++
46// Create instance of XalanTransformer
47MemoryManager      memMgrA;                // memory manager object
48XalanTransformer   transformerA(&memMgrA);
49
50MemoryManager      memMgrB;
51XalanTransformer   transformerB(&memMgrB);
52XalanTransformer   transformerC(&memMgrB);  // Uses same memory manager object as transformerB
53XalanTransformer   transformerD;           // Uses default static memory manager
54```
55
56The above method demonstrates how users can apply the basic pluggable
57memory management feature.  Users also have the option of implementing
58their own memory manager.  This can be done by simply writing methods
59for:
60
61```c++
62// Method for allocating memory
63void* allocate(size_t size);
64```
65
66and
67
68```c++
69// Method for deallocating memory
70void deallocate(void *p);
71```
72
73For an example of how to use this feature, please see the
74[SimpleTransform](samples.md#simpletransform)
75sample that has been provided in the binary distributions.
76
77## More Topics
78
79Please feel free to give us feedback on what topics you would like to see.
80
81Send comments to the Xalan Development Mailing List.
82