sg_vector_create(3)

NAME
sg_vector_create, sg_vector_clear, sg_vector_resize, sg_vector_free, sg_vector_clone, sg_vector_clone_into, sg_vector_compute_diff, sg_prove_vector, sg_get_nelements, sg_free_stats_buf - statgrab vector management

SYNOPSIS
\FC#include "statgrab.h"
#include "vector.h"\F[]

\FCstruct sg_vector *sg_vector_create\F[]
\FC(size_t block_size, size_t alloc_count, size_t initial_used, const sg_vector_init_info * const info);\F[]

\FCvoid sg_vector_clear\F[]
\FC(struct sg_vector *vector);\F[]

\FCstruct sg_vector *sg_vector_resize\F[]
\FC(struct sg_vector *vector);\F[]

\FCvoid sg_vector_free\F[]
\FC(struct sg_vector *vector);\F[]

\FCstruct sg_vector *sg_vector_clone\F[]
\FC(const struct sg_vector *src);\F[]

\FCsg_error sg_vector_clone_into\F[]
\FC(struct sg_vector **dest, const struct sg_vector *src);\F[]

\FCsg_error sg_vector_compute_diff\F[]
\FC(struct sg_vector **dest, const struct sg_vector *cur_vector, const struct sg_vector *last_vector);\F[]

\FCsg_error sg_prove_vector\F[]
\FC(const struct sg_vector *vec);\F[]

\FCsize_t sg_get_nelements\F[]
\FC(const void *data);\F[]

\FCsg_error sg_free_stats_buf\F[]
\FC(void *data);\F[]

DESCRIPTION
\FCsg_vector_create\F[]() allocates and initialises a new statgrab vector with \FCinitial_used\F[] elements ready for use. Space for \FCalloc_count\F[] elements is initially allocated (to avoid too many calls to \FCrealloc\F[]() during later \FCsg_vector_resize\F[]() calls). The value of \FCblock_size\F[] must be a power of 2, it's rounded up to the next power of 2 when it's not. If \FCalloc_count\F[] is not a multiple of \FCblock_size\F[], it's rounded up to the next multiple of \FCblock_size\F[]. It returns a pointer to the newly created vector.

\FCsg_vector_clear\F[]() destroys all elements contained in the given vector. In opposite to \FCsg_vector_resize\F[]( x, 0 ) the allocated size of the vector remains untouched.

\FCsg_vector_resize\F[]() increases or decreases the amount of allocated elements in the specified vector. The amount of allocated elements is always a multiple of the intialisation parameter \FCblock_size\F[]. In the special case, \FCsg_vector_resize\F[]() is called with 0 in argument \FCnew_count\F[], the vector is freed after all vector elements had been destroyed. It returns the pointer to the resized vector.

\FCsg_vector_free\F[]() destroys all vector elements and deallocates the storage belonging to the given vector.

\FCsg_vector_clone\F[]() clones all elements of the given vector into a new vector created with the same specification as the referenced one. It returns a pointer to the cloned vector.

\FCsg_vector_clone_into\F[]() clones all elements of the given source vector into the given target vector. The target vector must be created for the same element data type as the source vector. It returns an error code != to SG_ERROR_NONE if something went wrong.

\FCsg_vector_compute_diff\F[]() computes a difference vector between the vector containing current statistics and another vector containing older statistics. If an element exists in the current vector but not in the opposite one, it's cloned into the result vector. If an element exists only in the opposite vector, it doesn't appear in the target vector. \FCsg_vector_compute_diff\F[]() returns an error code != to SG_ERROR_NONE if something went wrong.

\FCsg_prove_vector\F[]() proves whether a pointer to a vector really points to a vector. In case the given vector pointer points to corrupted data, the program is aborted. When \FCsg_prove_vector\F[]() returns, it returns SG_ERROR_NONE.

\FCsg_get_nelements\F[]() returns the number of elements the given data area, encompasses by a statgrab vector, contains. The vector head is internally calculated from the given pointer to the first vector element.

\FCsg_free_stats_buf\F[]() frees the vector emcompassing the given data area.

NOTES
Except \FCsg_get_nelements\F[]() and \FCsg_free_stats_buf\F[]() none of above functions can be called from outside of the libstatgrab sources. The documented structures and APIs may change without warning. The description of all other API is intended to be read from libstatgrab developers only.

Each vector is created from two elements: the vector information and the list of elements:
\FC
template <class T, class Impl>
struct sg_vector {
        size_t used_count;
        size_t alloc_count;
        size_t block_shift;
        Impl vector_implementation;
        T elements[alloc_count];
};
      \F[]
Of course, it is not valid C, so being tricky was the solution:
\FC
typedef struct sg_vector {
	size_t used_count;
	size_t alloc_count;
	size_t block_shift;
	struct sg_vector_init_info info;
} sg_vector;
struct sg_vector_size_helper {
	struct sg_vector v;
	long long ll;
};
#define VECTOR_SIZE offsetof(struct sg_vector_size_helper,ll)
/* Return the data ptr of a vector */
#define VECTOR_DATA(vector) \
	(vector ? (void *)(((char *)vector)+VECTOR_SIZE) : NULL)
#define VECTOR_ADDR_ARITH(ptr) \
	(sg_vector *)(((char *)(ptr))-VECTOR_SIZE)
/* Return the vector for a data */
#define VECTOR_ADDRESS(ptr) \
	((ptr) ? (SG_ERROR_NONE == sg_prove_vector(VECTOR_ADDR_ARITH(ptr)) ? VECTOR_ADDR_ARITH(ptr) : NULL ) : NULL)
      \F[]

This also allows user functions as \FCsg_get_nelements\F[]() and \FCsg_free_stats_buf\F[]() to switch easily between the vector structure and the content.

  THE VECTOR SPECIALISATION STRUCTURE
As mentioned, the vector implementation uses strategies from the object oriented programming concept named "polymorphism". A vector is described by a small object containing inherent attributes like element size and a bunch of "virtual methods" to do element related tasks like initialising or destroying elements.

\FC
typedef void (*vector_init_function)(void *item);
typedef sg_error (*vector_copy_function)(const void *src, void *dst);
typedef sg_error (*vector_compute_diff_function)(void *dst, const void *src);
typedef int (*vector_compare_function)(const void *a, const void *b);
typedef void (*vector_destroy_function)(void *item);
struct sg_vector_init_info {
        size_t item_size;
        vector_init_function init_fn;
        vector_copy_function copy_fn;
        vector_compute_diff_function compute_diff_fn;
        vector_compare_function compare_fn;
        vector_destroy_function destroy_fn;
};
      \F[]

The instances of struct \FCsg_vector_init_info\F[] are conceptional statically initialised by using either the preprocessor macro \FCVECTOR_INIT_INFO_FULL_INIT\F[](\FCtype\F[]) or \FCVECTOR_INIT_INFO_EMPTY_INIT\F[](\FCtype\F[]). Here're some examples to demonstrate how it's meant: Initialising CPU statistics vector description

\FC
VECTOR_INIT_INFO_EMPTY_INIT(sg_cpu_stats);
          \F[]

Initialising Host-Info statistics vector description

\FC
static void sg_os_stats_item_init(sg_os_stats *d);
static void sg_os_stats_item_destroy(sg_os_stats *d);
#define sg_os_stats_item_copy NULL
#define sg_os_stats_item_compute_diff NULL
#define sg_os_stats_item_compare NULL
VECTOR_INIT_INFO_FULL_INIT(sg_os_stats);
          \F[]

Initialising Disk-IO statistics vector description

\FC
static void sg_disk_io_stats_item_init(sg_disk_io_stats *d);
static sg_error sg_disk_io_stats_item_copy(sg_disk_io_stats *d, const sg_disk_io_stats *s);
static sg_error sg_disk_io_stats_item_compute_diff(const sg_disk_io_stats *s, sg_disk_io_stats *d);
static int sg_disk_io_stats_item_compare(const sg_disk_io_stats *a, const sg_disk_io_stats *b);
static void sg_disk_io_stats_item_destroy(sg_disk_io_stats *d);
VECTOR_INIT_INFO_FULL_INIT(sg_disk_io_stats);
          \F[]

  WORKING WITH VECTORS
To simplify the working with the vector management functions, some preprocessor macros are available. They are shown here as if they were functions to ease understanding.

\FCstruct sg_vector *VECTOR_CREATE\F[]
\FC(identifier type, size_t block_size);\F[]

\FCvoid VECTOR_CLEAR\F[]
\FC(struct sg_vector *vector);\F[]

\FCstruct sg_vector *VECTOR_CREATE_OR_RESIZE\F[]
\FC(struct sg_vector *vector, size_t new_count, identifier type);\F[]

\FCvoid VECTOR_UPDATE\F[]
\FC(struct sg_vector **vectorptr, size_t new_count, datatype *data, identifier datatype);\F[]

\FCvoid VECTOR_ITEM_COUNT\F[]
\FC(struct sg_vector *vector);\F[]

\FCVECTOR_CREATE\F[]() calls \FCsg_vector_create\F[]() with \FCalloc_count = block_size\F[] and \FCinitial_used = 0\F[] using the vector specialisation \FCtype##_vector_init_info\F[].

\FCVECTOR_CLEAR\F[]() simply calls \FCsg_vector_clear\F[](). This macro exists only for conformity.

\FCVECTOR_CREATE_OR_RESIZE\F[]() calls \FCsg_vector_create\F[]() when the given vector pointer points to \FCNULL\F[] or \FCsg_vector_resize\F[]() otherwise. The result of the appropriate function is returned.

\FCVECTOR_UPDATE\F[]() calls \FCVECTOR_CREATE_OR_RESIZE\F[]() and sets data to the first element of the resulting vector when a non-NULL pointer got, to NULL otherwise. When \FCVECTOR_CREATE_OR_RESIZE\F[]() returns a NULL pointer and \FCnew_count\F[] is not equal to 0 (zero), the intructions from the macro \FCVECTOR_UPDATE_ERROR_CLEANUP\F[] are executed to cleanup before returning from current subroutine with the error which has been occurred.

\FCVECTOR_ITEM_COUNT\F[]() returns 0 for a non-existing vector (\FCvector\F[] == 0) and the number of containing elements otherwise.

RETURN VALUES
Beside error codes, the return values, if any, are always a pointer to vector structures (struct \FCsg_vector\F[] *).

SEE ALSO
statgrab(3)

WEBSITE
⟨https://libstatgrab.org/⟩