maug
Quick and dirty C mini-augmentation library.
Data Fields | Related Functions
MDATA_VECTOR Struct Reference

A vector of uniformly-sized objects, stored contiguously. More...

#include <mdata.h>

Data Fields

uint8_t flags
 
MAUG_MHANDLE data_h
 Handle for allocated items (unlocked).
 
uint8_t * data_bytes
 Handle for allocated items (locked).
 
size_t ct_max
 Maximum number of items currently allocated for.
 
size_t ct
 Maximum number of items actually used.
 
size_t ct_step
 Number of items added when more space is needed.
 
size_t item_sz
 Size, in bytes, of each item. More...
 
ssize_t locks
 Lock count, if MDATA_VECTOR_FLAG_REFCOUNT is enabled.
 

Related Functions

(Note that these are not member functions.)

#define MDATA_VECTOR_FLAG_REFCOUNT   0x01
 Flag for MDATA_VECTOR::flags indicating that vector may uses reference counting for locking. More...
 
#define MDATA_VECTOR_INIT_STEP_SZ   10
 Default initial value for MDATA_VECTOR::ct_step.
 
ssize_t mdata_vector_append (struct MDATA_VECTOR *v, const void *item, size_t item_sz)
 Append an item to the specified vector. More...
 
MERROR_RETVAL mdata_vector_remove (struct MDATA_VECTOR *v, size_t idx)
 Remove item at the given index, shifting subsequent items up by 1. More...
 
void * mdata_vector_get_void (const struct MDATA_VECTOR *v, size_t idx)
 Get a generic pointer to an item in the MDATA_VECTOR. More...
 
#define mdata_vector_lock(v)
 Lock the vector. This should be done when items from the vector are actively being referenced, so the system is not allowed to move the vector's data allocation block. More...
 
#define mdata_vector_unlock(v)
 Unlock the vector so items may be added and removed. More...
 
#define mdata_vector_ct(v)   ((v)->ct)
 Number of items of MDATA_VECTOR::item_sz bytes actively stored in this vector. More...
 
#define mdata_vector_sz(v)   (((v)->ct_max) * ((v)->item_sz))
 Number of bytes of heap memory occupied by this vector.
 
#define mdata_vector_fill(v, ct_new, sz)
 Allocate and mark the new slots as active. More...
 

Detailed Description

A vector of uniformly-sized objects, stored contiguously.

May be initialized with mdata_vector_alloc() before use, but mdata_vector_append() will attempt to do this automatically.

Warning
Internal details may be subject to change, and should be accessed with wrapper macros and convenience functions.

Friends And Related Function Documentation

◆ mdata_vector_ct

#define mdata_vector_ct (   v)    ((v)->ct)
related

Number of items of MDATA_VECTOR::item_sz bytes actively stored in this vector.

Warning
This is not neccessarily equal to amount of memory occupied by the vector, as it may have allocated more than are currently occupied!

◆ mdata_vector_fill

#define mdata_vector_fill (   v,
  ct_new,
  sz 
)
related
Value:
retval = mdata_vector_alloc( v, sz, ct_new ); \
maug_cleanup_if_not_ok(); \
(v)->ct = (ct_new);
MERROR_RETVAL mdata_vector_alloc(struct MDATA_VECTOR *v, size_t item_sz, size_t item_ct_init)
size_t ct
Maximum number of items actually used.
Definition: mdata.h:102

Allocate and mark the new slots as active.

◆ MDATA_VECTOR_FLAG_REFCOUNT

#define MDATA_VECTOR_FLAG_REFCOUNT   0x01
related

Flag for MDATA_VECTOR::flags indicating that vector may uses reference counting for locking.

Such a mobile may be locked multiple times, but then must be unlocked an equal number of times to unlock.

◆ mdata_vector_lock

#define mdata_vector_lock (   v)
related
Value:
if( (MAUG_MHANDLE)NULL == (v)->data_h && NULL == (v)->data_bytes ) { \
mdata_debug_printf( "locking empty vector..." ); \
(v)->flags |= MDATA_VECTOR_FLAG_IS_LOCKED; \
} else if( \
mdata_vector_get_flag( v, MDATA_VECTOR_FLAG_REFCOUNT ) && \
0 < (v)->locks \
) { \
(v)->locks++; \
mdata_debug_printf( "vector " #v " locks: " SSIZE_T_FMT, (v)->locks ); \
} else { \
/* assert( !mdata_vector_is_locked( v ) ); */ \
if( mdata_vector_is_locked( v ) ) { \
error_printf( "attempting to double-lock vector!" ); \
retval = MERROR_OVERFLOW; \
goto cleanup; \
} \
if( (MAUG_MHANDLE)NULL == (v)->data_h ) { \
error_printf( "invalid data handle!" ); \
retval = MERROR_ALLOC; \
goto cleanup; \
} \
maug_mlock( (v)->data_h, (v)->data_bytes ); \
maug_cleanup_if_null_lock( uint8_t*, (v)->data_bytes ); \
(v)->flags |= MDATA_VECTOR_FLAG_IS_LOCKED; \
mdata_debug_printf( "locked vector " #v ); \
}
MAUG_MHANDLE data_h
Handle for allocated items (unlocked).
Definition: mdata.h:96
uint8_t * data_bytes
Handle for allocated items (locked).
Definition: mdata.h:98
ssize_t locks
Lock count, if MDATA_VECTOR_FLAG_REFCOUNT is enabled.
Definition: mdata.h:111
#define MDATA_VECTOR_FLAG_REFCOUNT
Flag for MDATA_VECTOR::flags indicating that vector may uses reference counting for locking.
Definition: mdata.h:34

Lock the vector. This should be done when items from the vector are actively being referenced, so the system is not allowed to move the vector's data allocation block.

Warning
mdata_vector_lock() should never be called after the cleanup label! (This is fine for mdata_vector_unlock(), however.)

◆ mdata_vector_unlock

#define mdata_vector_unlock (   v)
related
Value:
if( (MAUG_MHANDLE)NULL == (v)->data_h && NULL == (v)->data_bytes ) { \
mdata_debug_printf( "locking empty vector..." ); \
(v)->flags &= ~MDATA_VECTOR_FLAG_IS_LOCKED; \
} else { \
if( \
mdata_vector_get_flag( v, MDATA_VECTOR_FLAG_REFCOUNT ) && \
0 < (v)->locks \
) { \
(v)->locks--; \
mdata_debug_printf( "vector " #v " locks: " SSIZE_T_FMT, \
(v)->locks ); \
} \
if( 0 == (v)->locks && NULL != (v)->data_bytes ) { \
assert( mdata_vector_is_locked( v ) ); \
maug_munlock( (v)->data_h, (v)->data_bytes ); \
(v)->flags &= ~MDATA_VECTOR_FLAG_IS_LOCKED; \
mdata_debug_printf( "unlocked vector " #v ); \
} \
}

Unlock the vector so items may be added and removed.

Note
mdata_vector_unlock() may be called after the cleanup label.

Field Documentation

◆ item_sz

size_t MDATA_VECTOR::item_sz

Size, in bytes, of each item.

Warning
Attempting to store items with a different size will fail!

The documentation for this struct was generated from the following file: