PreviousNext

Dynamic Creation of Objects

Objects can be completely dynamically allocated and initialized; however, you have to implement the routines to do this yourself. The examples in this topic are code fragments; for complete examples, see Sample Application Programs.

Initialization of object structures can be automated by declaring macros or functions to do this. For example, the following macro initializes one object descriptor with a full set of appropriate values:

/* Put a C-style (NULL-terminated) string into an object and */

/* set all the other descriptor fields to requested values */

#define FILL_OMD_STRING( desc, index, typ, syntx, val ) \

desc[index].type = typ; \

desc[index].syntax = syntx; \

desc[index].value.string.length = \

(OM_element_position)strlen(val); \

desc[index].value.string.elements = val;

When generating objects, use malloc( ) to allocate space for the number of objects desired, and then use macros (or functions) such as the preceding one to initialize the descriptors. The following code fragment shows how this can be done for the top-level object of a DS_C_DS_DN, such as the one described near the beginning of this topic. Recall that the DS_C_DS_DN has a separate DS_RDNS descriptor for each name piece in the full name.

/* Calculate number of "DS_RDNS" attributes there should be ... */

numberOfPieces = number_of_name_pieces;


/* Allocate space for that many descriptors, plus one for the */

/* object class at the front, and a NULL descriptor at the */

/* back... */

Name_Object = (OM_object)malloc((numberOfPieces + 2) \

* sizeof(OM_descriptor));

if(Name_Object == NULL) /* "malloc()" failed */

return OM_MEMORY_INSUFFICIENT;


/* Initialize it as a DS_C_DS_DN object by placing that class */

/* identifier in the first position... */


FILL_OMD_XOM_STRING(Name_Object, 0, OM_CLASS,

OM_S_OBJECT_IDENTIFIER_STRING, DS_C_DS_DN)

Note that all of these steps would have to be repeated for each of the DS_C_DS_RDN objects required as attribute values of the DS_C_DS_DN. Then a tier of DS_C_AVA objects would have to be created in the same way, since each of the DS_C_DS_RDNs requires one of them as its attribute value.

You could now use om_create( ) and om_put( ) to generate a private copy of this object, if so desired.

The application is responsible for managing the memory it allocates for such dynamic object creation.