Previous: , Up: Sample Libraries   [Index]


7.41.2 A Better Sequence Library

There is little to improve on the sequence library. Part of it is observing the application concerns with respect to library object construction.

See Library Construct Generation Flags.

See struct x1f4_lxwide_type.

For the sequence library object construction only the library data allocator concern applies.

Since the concern is only described for the library constructor, the library object definition is extended to allow for saving those features of the allocator needed by the library object destructor:

typedef struct library_type {
    int (*free)(void *, void *), sequence;
    struct x1f4_linetext_type functions[2];
    void *data;
} library_type;

The library object destructor is redefined as:

static int
fini_sequence(void **sequence)
{
    int status;
    struct library_type *library_data;

    library_data = *sequence;
    if (library_data->free) {
	status = library_data->free(library_data->data, library_data);
    } else {
	free(library_data);
	status = 0;
    }

    return status;
}

while the constructor becomes:

static int
tile_sequence(void **sequence, unsigned wide_bits,
	      struct x1f4_lxwide_type *lxwide_data, unsigned bits,
	      const void *data)
{
    int status;
    struct library_type *library_data;

    if (wide_bits & X1f4_LXWIDE_CODELINK) {
	void *library;

	status = lxwide_data->codelink_set.link
	    (lxwide_data->codelink_set.data, &library,
	     sizeof(struct library_type));
	if (status) {
	} else {
	    library_data = library;
	    library_data->data = lxwide_data->codelink_set.data;
	    library_data->free = lxwide_data->codelink_set.free;
	}
    } else {
	library_data = malloc(sizeof(struct library_type));
	if (library_data) {
	    status = 0;
	    library_data->free = NULL;
	} else {
	    status = -1;
	}
    }
    if (status) {
    } else {
	library_data->sequence = 0;

	library_data->functions[0].context = library_data;
	library_data->functions[0].function.args = NULL;
	library_data->functions[0].function.count = 0;
	library_data->functions[0].function.flags =
	    X1f4_E4_KEEP_CALL | X1f4_E4_TEXT_LINK;
	library_data->functions[0].function.function = mode_sequence;
	library_data->functions[0].function.length = 8;
	library_data->functions[0].function.name = "sequence";
	library_data->functions[0].function.type = X1f4_E4_MODE;

	library_data->functions[1].function.name = NULL;

	*sequence = library_data;
    }

    return status;
}