From 2e501c68a8641a2b3c430b55f13491a9c1c5d0f5 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov 6.5 64-bit Integer Type 6.6 Serializer Reuse
- 6.7 Support for Polymorphism
+ 6.8 A Minimal Example
+ 6.8 Custom Allocators
@@ -3664,7 +3665,116 @@ main ()
on root elements requires a number of special actions as shown in
the 6.9 A Minimal Example polyroot
example.
By default the XSD/e runtime and generated code use
+ the standard operators new
and delete
+ to manage dynamic memory. However, it is possible to instead
+ use custom allocator functions provided by your application.
+ To achieve this, configure the XSD/e runtime library to use
+ custom allocator functions as well as pass the
+ --custom-allocator
option to the XSD/e compiler
+ when translating your schemas. The signatures of the custom
+ allocator functions that should be provided by your application
+ are listed below. Their semantics should be equivalent to the
+ standard C malloc()
, realloc()
, and
+ free()
functions.
+extern "C" void* +xsde_alloc (size_t); + +extern "C" void* +xsde_realloc (void*, size_t); + +extern "C" void +xsde_free (void*); ++ +
Note also that when custom allocators are enabled, any
+ dynamically-allocated object of which the XSD/e runtime
+ or generated code assume ownership should be allocated
+ using the custom allocation function. Similarly, if your
+ application assumes ownership of any dynamically-allocated
+ object returned by the XSD/e runtime or the generated code,
+ then such an object should be disposed of using the custom
+ deallocation function. To help with these tasks the generated
+ xml_schema
namespace defines the following two
+ helper functions and, if C++ exceptions are enabled, automatic
+ pointer class:
+namespace xml_schema +{ + void* + alloc (size_t); + + void + free (void*); + + struct alloc_guard + { + alloc_guard (void*); + ~alloc_guard (); + + void* + get () const; + + void + release (); + + private: + ... + }; +} ++ +
If C++ exceptions are disabled, these functions are equivalent
+ to xsde_alloc()
and xsde_free()
.
+ If exceptions are enabled, xml_schema::alloc()
+ throws std::bad_alloc
on memory allocation failure.
The following code fragment shows how to create and destroy a + dynamically-allocated object with custom allocators when C++ + exceptions are disabled:
+ ++void* v = xml_schema::alloc (sizeof (type)); + +if (v == 0) +{ + // Handle out of memory condition. +} + +type* x = new (v) type (1, 2); + +... + +if (x) +{ + x->~type (); + xml_schema::free (x); +} ++ +
The equivalent code fragment for configurations with C++ exceptions + enabled is shown below:
+ ++xml_schema::alloc_guard g (xml_schema::alloc (sizeof (type))); +type* x = new (g.get ()) type (1, 2); +g.release (); + +... + +if (x) +{ + x->~type (); + xml_schema::free (x); +} ++ +
The following example is a re-implementation of the person records example presented in Chapter 4, -- cgit v1.1