Show block_macros.h syntax highlighted
#ifndef _BLOCK_MACROS_H_
#define _BLOCK_MACROS_H_
#include <glib.h>
#include <time.h>
#include "link/link.h"
#include "devel/state/cluster_map.h"
typedef enum allow_flag_enum
{ ALLOW_ALL, ONLY_LOCAL, ONLY_NONLOCAL } allow_flag_enum_t;
static inline int
allow_check (node_id_t id, allow_flag_enum_t flag)
{
int allow = 0;
switch (flag)
{
case (ALLOW_ALL):
allow = 1;
break;
case (ONLY_LOCAL):
if (id == my_node_id)
allow = 1;
break;
case (ONLY_NONLOCAL):
if (id != my_node_id)
allow = 1;
break;
default:
elog (LOG_CRIT, "Unknown meaning of flag: %d", flag);
break;
}
return allow;
}
#define BLOCK_TYPESAFE_INLINES(name) \
\
static inline void \
name##_table_unparse (buf_t * buf, name##_table_t * table, int count) \
{ \
int i; \
for (i = 0; i < count; ++i) \
{ \
const char *seconds = misc_secs_to_str ((int) \
(time (0) - \
table[i].header.rcv_time)); \
bufprintf (buf, " \n\tSRC:%-15s", \
print_if_id (table[i].header.flow_id.src)); \
bufprintf (buf, " HOPS: %d ", table[i].header.hops_away); \
bufprintf (buf, " SECONDS_OLD: %s \n", seconds); \
\
block_unparse (buf, &table[i].name, ""); \
} \
} \
\
static inline GTree * \
name##_table_to_GTreeFull (name##_table_t * table, int count, \
allow_flag_enum_t flag) \
{ \
GTree *tree = \
g_tree_new_full (block_GCompareData, NULL, block_GDestroyNotify, \
block_GDestroyNotify); \
\
int i; \
for (i = 0; i < count; ++i) \
{ \
int allow = allow_check(table[i].header.flow_id.src, flag); \
if (allow) \
{ \
block_t *block = (block_t *) malloc (sizeof (block_t)); \
memset (block, 0, sizeof (block_t)); \
memcpy (block, &table[i].name, sizeof (block_t)); \
g_tree_insert (tree, block, NULL); \
} \
} \
return tree; \
} \
\
static inline GArray * \
name##_table_to_GArray (name##_table_t * table, int count, \
allow_flag_enum_t flag) \
{ \
GArray * array = g_array_new (0, 1, sizeof(block_t)); \
int i; \
for (i = 0; i < count; ++i) \
{ \
int allow = allow_check(table[i].header.flow_id.src, flag); \
if (allow) \
{ \
array = g_array_append_val (array, table[i].name); \
} \
} \
\
return array; \
} \
\
static inline name##_table_t * \
name##_create_table_from_array (GArray * array) \
{ \
elog (LOG_NOTICE, " "); \
int count = array->len; \
name##_table_t * table = \
(name##_table_t *) malloc (sizeof (name##_table_t) * count); \
memset (table, 0, sizeof (name##_table_t) * count); \
int i; \
for (i = 0; i < count; ++i) \
{ \
table[i].name = g_array_index (array, block_t, i); \
} \
return table; \
} \
\
static inline name##_table_t * \
name##_create_table_from_tree (GTree * tree) \
{ \
elog (LOG_NOTICE, " "); \
GArray * array = g_array_new(0, 1, sizeof(block_t)); \
block_append_tree_to_array (tree, &array); \
name##_table_t * table = name##_create_table_from_array(array); \
g_array_free(array, 1); \
return table; \
} \
\
static inline name##_table_t * \
name##_add_empty_block(name##_table_t * table, int count) \
{ \
static uint8_t twiddle = 0; \
++twiddle; \
size_t elem_size = sizeof(name##_table_t); \
size_t old_table_size = elem_size * count; \
size_t new_table_size = elem_size * (count + 1); \
\
name##_table_t * new_table = \
(name##_table_t *) malloc (new_table_size); \
memset (new_table, 0, new_table_size); \
memcpy (new_table, table, old_table_size); \
new_table[ count ].name.uid.total_length = twiddle; \
return new_table; \
} \
\
static inline int \
name##_pub_add_empty(char *prefix, name##_table_t *table, int count, \
flow_id_t *fid) \
{ \
int result = 0; \
/* name##_table_t * new_table = name##_add_empty_block(table, count);*/ \
/* result = name##_pub (prefix, new_table, count + 1, fid); */\
/* free (new_table); */\
result = name##_pub (prefix, table, count, fid);\
return result; \
} \
\
static inline void \
name##_table_pretty_print(buf_t * buf, name##_table_t * table, int count, \
allow_flag_enum_t flag) \
{ \
GArray * array = name##_table_to_GArray(table,count,flag); \
if (array->len) \
{ \
block_pretty_bufprintf_array(buf, array); \
} \
g_array_free(array, 1); \
return; \
} \
\
static inline void \
name##_table_convert_src_to_id(name##_table_t * table, int count, cluster_map_t * cluster_map) \
{ \
int i; \
for (i = 0; i < count; ++i) \
{ \
uint32_t * src = &table[i].header.flow_id.src; \
cl_id_t * cl_id = cluster_map_lookup_by_address(cluster_map, *src, 1, 0); \
if (cl_id) { \
*src = cl_id->node_id; \
} \
else { \
table[i].name.uid.node_id = 0; \
}\
} \
} \
#endif //_BLOCK_MACROS_H_
See more files for this project here