Main Page | Modules | Directories | File List | Globals | Related Pages

Maps

Implements maps (associative arrays) and lists. More...

Data Structures

struct  stat
union  key_data
struct  map_node
struct  map_node_int64
struct  map_node_str
struct  map_node_stat
struct  map_root

Defines

#define key1str(ptr)   (ptr->n.key1.str)
 Extracts string from key1 union.
#define key2str(ptr)   (ptr->n.key2.str)
 Extracts string from key2 union.
#define key1int(ptr)   (ptr->n.key1.val)
 Extracts int from key1 union.
#define key2int(ptr)   (ptr->n.key2.val)
 Extracts int from key2 union.
#define _stp_map_key2(map, key1, key2)
 Macro to call the proper _stp_map_key functions based on the types of the arguments.
#define _stp_map_key(map, key)
 Macro to call the proper _stp_map_key function based on the type of the argument.
#define _stp_map_set(map, val)
 Macro to call the proper _stp_map_set function based on the type of the argument.
#define foreach(map, ptr)
 Loop through all elements of a map or list.

Typedefs

typedef map_root * MAP
 All maps are of this type.

Enumerations

enum  keytype { NONE, LONG, STR }
 keys can be longs or strings
enum  valtype { INT64, STAT, STRING, END }
 values can be either int64, stats or strings

Functions

MAP _stp_map_new (unsigned max_entries, enum valtype type)
 Create a new map.
void _stp_map_key_del (MAP map)
 Deletes the current element.
map_node * _stp_map_start (MAP map)
 Get the first element in a map.
map_node * _stp_map_iter (MAP map, struct map_node *m)
 Get the next element in a map.
void _stp_map_del (MAP map)
 Deletes a map.
void _stp_map_key_long_long (MAP map, long key1, long key2)
 Set the map's key to two longs.
void _stp_map_key_str_str (MAP map, char *key1, char *key2)
 Set the map's key to two strings.
void _stp_map_key_str_long (MAP map, char *key1, long key2)
 Set the map's key to a string and a long.
void _stp_map_key_long_str (MAP map, long key1, char *key2)
 Set the map's key to a long and a string.
void _stp_map_key_str (MAP map, char *key)
 Set the map's key to a string.
void _stp_map_key_long (MAP map, long key)
 Set the map's key to a long.
void _stp_map_set_int64 (MAP map, int64_t val)
 Set the current element's value to an int64.
void _stp_map_add_int64 (MAP map, int64_t val)
 Adds an int64 to the current element's value.
int64_t _stp_map_get_int64 (MAP map)
 Gets the current element's value.
void _stp_map_set_str (MAP map, char *val)
 Set the current element's value to a C string.
void _stp_map_set_string (MAP map, String str)
 Set the current element's value to String.
char * _stp_map_get_str (MAP map)
 Gets the current element's value.
void _stp_map_set_stat (MAP map, stat *stats)
 Set the current element's value to a stat.
stat * _stp_map_get_stat (MAP map)
 Gets the current element's value.
void _stp_map_stat_add (MAP map, int64_t val)
 Add to the current element's statistics.

Variables

enum keytype packed
 keys can be longs or strings

Detailed Description

Implements maps (associative arrays) and lists.

Todo:
Needs to be made SMP-safe for when the big lock is removed from kprobes.

Define Documentation

#define _stp_map_key map,
key   ) 
 

Value:

({                                                              \
    if (__builtin_types_compatible_p (typeof (key), char[]))    \
      _stp_map_key_str (map, (char *)(key));                            \
    else                                                        \
      _stp_map_key_long (map, (long)(key));                             \
  })
Macro to call the proper _stp_map_key function based on the type of the argument.

Note:
May cause compiler warning on some GCCs

Definition at line 141 of file map.h.

#define _stp_map_key2 map,
key1,
key2   ) 
 

Value:

({                                                              \
    if (__builtin_types_compatible_p (typeof (key1), char[]))   \
      if (__builtin_types_compatible_p (typeof (key2), char[])) \
        _stp_map_key_str_str (map, (char *)(key1), (char *)(key2));     \
      else                                                      \
        _stp_map_key_str_long (map, (char *)(key1), (long)(key2));      \
    else                                                        \
      if (__builtin_types_compatible_p (typeof (key2), char[])) \
        _stp_map_key_long_str (map, (long)(key1), (char *)(key2));      \
      else                                                      \
        _stp_map_key_long_long (map, (long)(key1), (long)(key2));       \
  })
Macro to call the proper _stp_map_key functions based on the types of the arguments.

Note:
May cause compiler warning on some GCCs

Definition at line 123 of file map.h.

#define _stp_map_set map,
val   ) 
 

Value:

({                                                              \
    if (__builtin_types_compatible_p (typeof (val), char[]))            \
      _stp_map_set_str (map, (char *)(val));                            \
    else  if (__builtin_types_compatible_p (typeof (val), String))      \
      _stp_map_set_string (map, (String)(val));                         \
    else                                                                \
      _stp_map_set_int64 (map, (int64_t)(val));                         \
  })
Macro to call the proper _stp_map_set function based on the type of the argument.

Note:
May cause compiler warning on some GCCs

Definition at line 153 of file map.h.

#define foreach map,
ptr   ) 
 

Value:

for (ptr = (typeof(ptr))_stp_map_start(map); ptr; \
       ptr = (typeof(ptr))_stp_map_iter (map, (struct map_node *)ptr))
Loop through all elements of a map or list.

Parameters:
map 
ptr pointer to a map_node_stat, map_node_int64 or map_node_str
Example:
/* example showing how to print all the stats in a map using foreach() */

struct map_node_stat *ptr;

foreach (map, ptr)
     printf ("map[%s,%ld] = [c=%lld s=%lld min=%lld max=%lld]\n", key1str(ptr), 
             key2int(ptr), ptr->stats.count, ptr->stats.sum, ptr->stats.min, 
             ptr->stats.max);

Definition at line 171 of file map.h.


Function Documentation

void _stp_map_add_int64 MAP  map,
int64_t  val
 

Adds an int64 to the current element's value.

This adds an int64 to the current element's value. The map must have been created to hold int64s using _stp_map_new()

If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.

Parameters:
map 
val value
See also:
_stp_map_set_int64

Definition at line 559 of file map.c.

void _stp_map_del MAP  map  ) 
 

Deletes a map.

Deletes a map, freeing all memory in all elements. Normally done only when the module exits.

Parameters:
map 

Definition at line 205 of file map.c.

References _stp_vfree().

int64_t _stp_map_get_int64 MAP  map  ) 
 

Gets the current element's value.

Parameters:
map 
Returns:
The value. If the current element is not set or doesn't exist, returns 0.

Definition at line 569 of file map.c.

stat* _stp_map_get_stat MAP  map  ) 
 

Gets the current element's value.

Parameters:
map 
Returns:
A pointer to the stats struct. If the current element is not set or doesn't exist, returns NULL.

Definition at line 750 of file map.c.

char* _stp_map_get_str MAP  map  ) 
 

Gets the current element's value.

Parameters:
map 
Returns:
A string pointer. If the current element is not set or doesn't exist, returns NULL.

Definition at line 667 of file map.c.

struct map_node* _stp_map_iter MAP  map,
struct map_node *  m
 

Get the next element in a map.

Parameters:
map 
m a pointer to the current element, returned from _stp_map_start() or _stp_map_iter().
Returns:
a pointer to the next element. This is typically used with _stp_map_start(). See the foreach() macro for typical usage. It probably does what you want anyway.
See also:
foreach

Definition at line 186 of file map.c.

void _stp_map_key_del MAP  map  ) 
 

Deletes the current element.

If no current element (key) for this map is set, this function does nothing.

Parameters:
map 

Definition at line 118 of file map.c.

References _stp_free().

Referenced by _stp_map_set_stat(), and _stp_map_set_str().

void _stp_map_key_long MAP  map,
long  key
 

Set the map's key to a long.

This sets the current element based on a long key. If the key is not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key 

Definition at line 430 of file map.c.

References _stp_map_key_long_long().

Referenced by _stp_list_add_int64(), _stp_list_add_str(), and _stp_list_add_string().

void _stp_map_key_long_long MAP  map,
long  key1,
long  key2
 

Set the map's key to two longs.

This sets the current element based on a key of two strings. If the keys are not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key1 first key
key2 second key

Definition at line 233 of file map.c.

Referenced by _stp_map_key_long().

void _stp_map_key_long_str MAP  map,
long  key1,
char *  key2
 

Set the map's key to a long and a string.

This sets the current element based on a key of a long and a string. If the keys are not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key1 first key
key2 second key

Definition at line 373 of file map.c.

void _stp_map_key_str MAP  map,
char *  key
 

Set the map's key to a string.

This sets the current element based on a string key. If the key is not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key 

Definition at line 414 of file map.c.

References _stp_map_key_str_str().

void _stp_map_key_str_long MAP  map,
char *  key1,
long  key2
 

Set the map's key to a string and a long.

This sets the current element based on a key of a string and a long. If the keys are not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key1 first key
key2 second key

Definition at line 325 of file map.c.

void _stp_map_key_str_str MAP  map,
char *  key1,
char *  key2
 

Set the map's key to two strings.

This sets the current element based on a key of two strings. If the keys are not found, a new element will not be created until a _stp_map_set_xxx call.

Parameters:
map 
key1 first key
key2 second key

Definition at line 276 of file map.c.

Referenced by _stp_map_key_str().

MAP _stp_map_new unsigned  max_entries,
enum valtype  type
 

Create a new map.

Maps must be created at module initialization time.

Parameters:
max_entries The maximum number of entries allowed. Currently that number will be preallocated. If more entries are required, the oldest ones will be deleted. This makes it effectively a circular buffer. If max_entries is 0, there will be no maximum and entries will be allocated dynamically.
type Type of values stored in this map.
Returns:
A MAP on success or NULL on failure.

Definition at line 57 of file map.c.

References _stp_valloc().

Referenced by _stp_list_new().

void _stp_map_set_int64 MAP  map,
int64_t  val
 

Set the current element's value to an int64.

This sets the current element's value to an int64. The map must have been created to hold int64s using _stp_map_new()

If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.

Parameters:
map 
val new value
See also:
_stp_map_add_int64

_stp_map_set()

Definition at line 542 of file map.c.

Referenced by _stp_list_add_int64().

void _stp_map_set_stat MAP  map,
stat *  stats
 

Set the current element's value to a stat.

This sets the current element's value to an stat struct. The map must have been created to hold stats using _stp_map_new(xxx, STAT). This function would only be used if we wanted to set stats to something other than the normal initial values (count = 0, sum = 0, etc). It may be deleted if it doesn't turn out to be useful.

See also:
_stp_map_stat_add
If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.
Parameters:
map 
stats pointer to stats struct.
Todo:
Histograms don't work yet.

Definition at line 691 of file map.c.

References _stp_calloc(), and _stp_map_key_del().

Referenced by _stp_map_stat_add().

void _stp_map_set_str MAP  map,
char *  val
 

Set the current element's value to a C string.

This sets the current element's value to an C string. The map must have been created to hold int64s using _stp_map_new(xxx, STRING)

If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.

Parameters:
map 
val new string
See also:
_stp_map_set()

Definition at line 590 of file map.c.

References _stp_alloc(), _stp_calloc(), _stp_free(), and _stp_map_key_del().

Referenced by _stp_list_add_str(), _stp_list_add_string(), and _stp_map_set_string().

void _stp_map_set_string MAP  map,
String  str
 

Set the current element's value to String.

This sets the current element's value to a String. The map must have been created to hold int64s using _stp_map_new(xxx, STRING)

If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.

Parameters:
map 
str String containing new value.
See also:
_stp_map_set()

Definition at line 657 of file map.c.

References _stp_map_set_str().

struct map_node* _stp_map_start MAP  map  ) 
 

Get the first element in a map.

Parameters:
map 
Returns:
a pointer to the first element. This is typically used with _stp_map_iter(). See the foreach() macro for typical usage. It probably does what you want anyway.
See also:
foreach

Definition at line 163 of file map.c.

void _stp_map_stat_add MAP  map,
int64_t  val
 

Add to the current element's statistics.

Increments the statistics counter by one and the sum by val. Adjusts minimum, maximum, and histogram.

If the element doesn't exist, it is created. If no current element (key) is set for the map, this function does nothing.

Parameters:
map 
val value to add to the statistics
Todo:
Histograms don't work yet.

Definition at line 771 of file map.c.

References _stp_map_set_stat().