MArray Class

Overview

The MArray class is a subclass of the MBuf class, so it shares all its basic features (being malloc'ed, multi-chunk-supporting, etc.). The big difference is that all the size, growth, and limit values are no longer character sizes, but counts of ints (ints large enough to each hold a pointer). Additionally, there are special fetch and store routines that are used to access the value of particular ints/pointers.

The MArray_new() function is used to create a new MArray object. You pass it two parameters to indicate how the memory management for this array will happen. The first number indicates the incremental steps that the array will be grown by (in count * PTRSIZE bytes). The second number indicates how big each chunk of data can grow to be before we stop reallocating the current chunk and move on to a new chunk of data that will be appended to the MArray object (in count * PTRSIZE bytes). As a special case, if you specify "0" for the second number, the array will array will never stop reallocating the final chunk (which will also be the only chunk of data unless you manually add other data chunks into the MArray object via one of the MBuf_{prepend,append}* calls).

The MArray_delete() function destroys an MArray object and all the data chunks that it contains.

The MArray_append() function appends a single integer onto the end of the array, expanding it an int in size.

The MArray_appendPtr() function works just like MArray_append, except that it takes a void pointer value rather than an int.

The MArray_itemCnt() call returns the current number of items that are stored in the array. The currently allocated size may be slightly larger.

The MArray_truncate() call trims the array back to the indicated number of items. If the length is short enough that one or more chunks of data are no longer needed, they will be freed.

The MArray_setFetchPos() function sets the point where the next fetch will occur. By default, all arrays start out with a fetch position of 0.

The MArray_getReadPos() function returns the offset of the current fetch position within the list of all array chunks.

The MArray_fetch() function returns a single integer at the current fetch position, and bumps the fetch position forward to the next element.

The MArray_fetchPtr() function returns a single void pointer at the current fetch position, and bumps the fetch position forward to the next element.

The MArray_fetchAt() function returns a single integer at the indicated position, and does not affect the fetch position at all.

The MArray_fetchPtrAt() function returns a single void pointer at indicated position, and does not affect the fetch position at all.

The MArray_dataPtrAt() function returns a pointer to a series of ints or pointers (i.e. whatever you've chosen to store in the arraay), allowing you to access them as a group. The code currently expects you to know how many ints/pointers you can access at the position you've requested (and the fetch position is not affected).