![]()
|
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
">Preparing PDUsYou can use the odr_malloc() function (see section Using ODR for details). When you use odr_malloc(), you can release all of the allocated data in a single operation, independent of any pointers and relations between the data. odr_malloc() is based on a "nibble-memory" scheme, in which large portions of memory are allocated, and then gradually handed out with each call to odr_malloc(). The next time you call odr_reset(), all of the memory allocated since the last call is recycled for future use (actually, it is placed on a free-list). You can combine all of the methods described here. This will often be the most practical approach. For instance, you might use odr_malloc() to allocate an entire structure and some of its elements, while you leave other elements pointing to global or per-session default variables. The Z39.50 ASN.1 module provides an important aid in creating new PDUs. For each of the PDU types (say, Z_InitRequest), a function is provided that allocates and initializes an instance of that PDU type for you. In the case of the InitRequest, the function is simply named zget_InitRequest(), and it sets up reasonable default value for all of the mandatory members. The optional members are generally initialized to null pointers. This last aspect is very important: it ensures that if the PDU definitions are extended after you finish your implementation (to accommodate new versions of the protocol, say), you won't get into trouble with uninitialized pointers in your structures. The functions use odr_malloc() to allocate the PDUs and its members, so you can free everything again with a single call to odr_reset(). We strongly recommend that you use the zget_* functions whenever you are preparing a PDU (in a C++ API, the zget_ functions would probably be promoted to constructors for the individual types). The prototype for the individual PDU types generally look like this:
eg.:
The ODR handle should generally be your encoding stream, but it needn't be. As well as the individual PDU functions, a function zget_APDU() is provided, which allocates a top-level Z-APDU of the type requested:
The which parameter is (of course) the discriminator belonging to the Z_APDU CHOICE type. All of the interface described here is provided by the Z39.50 ASN.1 module, and you access it through the proto.h header file. |