maug
Quick and dirty C mini-augmentation library.
Data Structures | Macros | Typedefs | Functions
Data Memory String Pools

Structure for storing a compact group of mutable, variable-length character strings. More...

Collaboration diagram for Data Memory String Pools:

Data Structures

struct  MDATA_STRPOOL
 A pool of immutable text strings. Deduplicates strings to save memory. More...
 

Macros

#define MDATA_STRPOOL_FLAG_IS_LOCKED   0x01
 
#define MDATA_STRPOOL_FLAG_DEDUPE   0x02
 
#define MDATA_STRPOOL_IDX_ERROR   0
 
#define mdata_strpool_ct(sp)   ((sp)->str_ct)
 Get the number of strings in a strpool.
 
#define mdata_strpool_sz(sp)   ((sp)->str_sz_max)
 Get the number of bytes allocated to a strpool.
 
#define mdata_strpool_is_locked(sp)
 
#define mdata_strpool_lock(sp)
 
#define mdata_strpool_unlock(sp)
 
#define mdata_strpool_get(sp, idx)    ((idx >= 1 && idx < (sp)->str_sz) ? &((sp)->str_p[idx]) : NULL)
 Get a string by the index of its first character in the strpool.
 
#define mdata_strpool_get_sz(sp, idx)
 Get the size of a string in a MDATA_STRPOOL. More...
 
#define mdata_strpool_get_len(sp, idx)
 
#define mdata_strpool_padding(str_sz)    (sizeof( size_t ) - ((str_sz + 1 /* NULL */) % sizeof( size_t )))
 

Typedefs

typedef size_t mdata_strpool_idx_t
 

Functions

ssize_t mdata_strpool_get_idx_from_linear (struct MDATA_STRPOOL *sp, size_t linear)
 Get the native strpool index, given a linear index from 0 to mdata_strpool_ct(). More...
 
MERROR_RETVAL mdata_strpool_dump (struct MDATA_STRPOOL *sp)
 
MERROR_RETVAL mdata_strpool_check_idx (struct MDATA_STRPOOL *sp, mdata_strpool_idx_t idx)
 Verify if the given mdata_strpool_idx_t is valid in the given strpool.
 
mdata_strpool_idx_t mdata_strpool_find (struct MDATA_STRPOOL *sp, const char *str, size_t str_sz)
 
MAUG_MHANDLE mdata_strpool_extract (struct MDATA_STRPOOL *sp, mdata_strpool_idx_t idx)
 Return a dynamically-allocated memory handle containing the contents of the string at the given index.
 
mdata_strpool_idx_t mdata_strpool_append (struct MDATA_STRPOOL *sp, const char *str, size_t str_sz, uint8_t flags)
 
MERROR_RETVAL mdata_strpool_remove (struct MDATA_STRPOOL *sp, mdata_strpool_idx_t idx)
 
MERROR_RETVAL mdata_strpool_alloc (struct MDATA_STRPOOL *sp, size_t alloc_sz)
 
void mdata_strpool_free (struct MDATA_STRPOOL *sp)
 

Detailed Description

Structure for storing a compact group of mutable, variable-length character strings.

Macro Definition Documentation

◆ mdata_strpool_get_len

#define mdata_strpool_get_len (   sp,
  idx 
)
Value:
((idx >= sizeof( size_t ) && idx < (sp)->str_sz) ? \
(maug_strlen( &((sp)->str_p[idx]) )) : 0)

◆ mdata_strpool_get_sz

#define mdata_strpool_get_sz (   sp,
  idx 
)
Value:
((idx >= sizeof( size_t ) && idx < (sp)->str_sz) ? \
(size_t)(*(&((sp)->str_p[idx - sizeof( size_t )]))) : 0)

Get the size of a string in a MDATA_STRPOOL.

Warning
This requires the strpool to be locked before use!
Note
This returns the amount of space allocated for the string. This may not be the value you expect due to padding to keep size_t values aligned in memory, or if the string was allocated with too much space in the first place!

◆ mdata_strpool_is_locked

#define mdata_strpool_is_locked (   sp)
Value:
(MDATA_STRPOOL_FLAG_IS_LOCKED == \
(MDATA_STRPOOL_FLAG_IS_LOCKED & (sp)->flags))

◆ mdata_strpool_lock

#define mdata_strpool_lock (   sp)
Value:
mdata_debug_lock_printf( "locking strpool %p...", sp ); \
if( NULL != (sp)->str_p ) { \
error_printf( "str_p not null! double lock?" ); \
retval = MERROR_ALLOC; \
goto cleanup; \
} \
maug_mlock( (sp)->str_h, (sp)->str_p ); \
maug_cleanup_if_null_lock( char*, (sp)->str_p ); \
(sp)->flags |= MDATA_STRPOOL_FLAG_IS_LOCKED;

◆ mdata_strpool_unlock

#define mdata_strpool_unlock (   sp)
Value:
mdata_debug_lock_printf( "unlocking strpool %p...", sp ); \
if( NULL != (sp)->str_p ) { \
maug_munlock( (sp)->str_h, (sp)->str_p ); \
(sp)->flags &= ~MDATA_STRPOOL_FLAG_IS_LOCKED; \
}

Function Documentation

◆ mdata_strpool_get_idx_from_linear()

ssize_t mdata_strpool_get_idx_from_linear ( struct MDATA_STRPOOL sp,
size_t  linear 
)

Get the native strpool index, given a linear index from 0 to mdata_strpool_ct().

Note
MDATA_STRPOOL normally returns the index of the first character of a word for most of its functions. If you're e.g. trying to get a random string from the strpool, this may be what you're looking for.