Reindented server/core/hint.c

This commit is contained in:
Johan Wikman
2015-11-30 13:47:56 +02:00
parent 255a5b53c1
commit 486f724dc1
2 changed files with 120 additions and 97 deletions

View File

@ -26,8 +26,8 @@
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 25/07/14 Mark Riddoch Initial implementation * 25/07/14 Mark Riddoch Initial implementation
* *
* @endverbatim * @endverbatim
*/ */
@ -36,121 +36,142 @@
/** /**
* Duplicate a list of hints * Duplicate a list of hints
* *
* @param hint The hint list to duplicate * @param hint The hint list to duplicate
* @return A duplicate of the list * @return A duplicate of the list
* *
* Note : Optimize this to use version numbering instead of copying memory * Note : Optimize this to use version numbering instead of copying memory
*/ */
HINT * HINT *
hint_dup(HINT *hint) hint_dup(HINT *hint)
{ {
HINT *nlhead = NULL, *nltail = NULL, *ptr1, *ptr2; HINT *nlhead = NULL, *nltail = NULL, *ptr1, *ptr2;
ptr1 = hint; ptr1 = hint;
while (ptr1) while (ptr1)
{ {
if ((ptr2 = (HINT *)malloc(sizeof(HINT))) == NULL) if ((ptr2 = (HINT *)malloc(sizeof(HINT))) == NULL)
return nlhead; {
ptr2->type = ptr1->type; return nlhead;
if (ptr1->data) }
ptr2->data = strdup(ptr1->data); ptr2->type = ptr1->type;
else if (ptr1->data)
ptr2->data = NULL; {
if (ptr1->value) ptr2->data = strdup(ptr1->data);
ptr2->value = strdup(ptr1->value); }
else else
ptr2->value = NULL; {
ptr2->next = NULL; ptr2->data = NULL;
if (nltail) }
{ if (ptr1->value)
nltail->next = ptr2; {
nltail = ptr2; ptr2->value = strdup(ptr1->value);
} }
else else
{ {
nlhead = ptr2; ptr2->value = NULL;
nltail = ptr2; }
} ptr2->next = NULL;
ptr1 = ptr1->next; if (nltail)
} {
return nlhead; nltail->next = ptr2;
nltail = ptr2;
}
else
{
nlhead = ptr2;
nltail = ptr2;
}
ptr1 = ptr1->next;
}
return nlhead;
} }
/** /**
* Create a ROUTE TO type hint * Create a ROUTE TO type hint
* *
* @param head The current hint list * @param head The current hint list
* @param type The HINT_TYPE * @param type The HINT_TYPE
* @param data Data may be NULL or the name of a server to route to * @param data Data may be NULL or the name of a server to route to
* @return The result hint list * @return The result hint list
*/ */
HINT * HINT *
hint_create_route(HINT *head, HINT_TYPE type, char *data) hint_create_route(HINT *head, HINT_TYPE type, char *data)
{ {
HINT *hint; HINT *hint;
if ((hint = (HINT *)malloc(sizeof(HINT))) == NULL) if ((hint = (HINT *)malloc(sizeof(HINT))) == NULL)
return head; {
hint->next = head; return head;
hint->type = type; }
if (data) hint->next = head;
hint->data = strdup(data); hint->type = type;
else if (data)
hint->data = NULL; {
hint->value = NULL; hint->data = strdup(data);
return hint; }
else
{
hint->data = NULL;
}
hint->value = NULL;
return hint;
} }
/** /**
* Create name/value parameter hint * Create name/value parameter hint
* *
* @param head The current hint list * @param head The current hint list
* @param pname The parameter name * @param pname The parameter name
* @param value The parameter value * @param value The parameter value
* @return The result hint list * @return The result hint list
*/ */
HINT * HINT *
hint_create_parameter(HINT *head, char *pname, char *value) hint_create_parameter(HINT *head, char *pname, char *value)
{ {
HINT *hint; HINT *hint;
if ((hint = (HINT *)malloc(sizeof(HINT))) == NULL) if ((hint = (HINT *)malloc(sizeof(HINT))) == NULL)
return head; {
hint->next = head; return head;
hint->type = HINT_PARAMETER; }
hint->data = strdup(pname); hint->next = head;
hint->value = strdup(value); hint->type = HINT_PARAMETER;
return hint; hint->data = strdup(pname);
hint->value = strdup(value);
return hint;
} }
/** /**
* free_hint - free a hint * free_hint - free a hint
* *
* @param hint The hint to free * @param hint The hint to free
*/ */
void void
hint_free(HINT *hint) hint_free(HINT *hint)
{ {
if (hint->data) if (hint->data)
free(hint->data); {
if (hint->value) free(hint->data);
free(hint->value); }
free(hint); if (hint->value)
{
free(hint->value);
}
free(hint);
} }
bool hint_exists( bool hint_exists(HINT** p_hint,
HINT** p_hint, HINT_TYPE type)
HINT_TYPE type)
{ {
bool succp = false; bool succp = false;
while (*p_hint != NULL) while (*p_hint != NULL)
{
if ((*p_hint)->type == type)
{ {
if ((*p_hint)->type == type) succp = true;
{
succp = true;
}
p_hint = &(*p_hint)->next;
} }
return succp; p_hint = &(*p_hint)->next;
}
return succp;
} }

View File

@ -24,8 +24,8 @@
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 10/07/14 Mark Riddoch Initial implementation * 10/07/14 Mark Riddoch Initial implementation
* *
* @endverbatim * @endverbatim
*/ */
@ -36,13 +36,14 @@
/** /**
* The types of hint that are supported by the generic hinting mechanism. * The types of hint that are supported by the generic hinting mechanism.
*/ */
typedef enum { typedef enum
HINT_ROUTE_TO_MASTER = 1, {
HINT_ROUTE_TO_SLAVE, HINT_ROUTE_TO_MASTER = 1,
HINT_ROUTE_TO_NAMED_SERVER, HINT_ROUTE_TO_SLAVE,
HINT_ROUTE_TO_UPTODATE_SERVER, HINT_ROUTE_TO_NAMED_SERVER,
HINT_ROUTE_TO_ALL, /*< not implemented yet */ HINT_ROUTE_TO_UPTODATE_SERVER,
HINT_PARAMETER HINT_ROUTE_TO_ALL, /*< not implemented yet */
HINT_PARAMETER
} HINT_TYPE; } HINT_TYPE;
/** /**
@ -52,18 +53,19 @@ typedef enum {
* specific data. * specific data.
* Multiple hints may be attached to a single buffer. * Multiple hints may be attached to a single buffer.
*/ */
typedef struct hint { typedef struct hint
HINT_TYPE type; /*< The Type of hint */ {
void *data; /*< Type specific data */ HINT_TYPE type; /*< The Type of hint */
void *value; /*< Parameter value for hint */ void *data; /*< Type specific data */
unsigned int dsize; /*< Size of the hint data */ void *value; /*< Parameter value for hint */
struct hint *next; /*< Another hint for this buffer */ unsigned int dsize; /*< Size of the hint data */
struct hint *next; /*< Another hint for this buffer */
} HINT; } HINT;
extern HINT *hint_alloc(HINT_TYPE, void *, unsigned int); extern HINT *hint_alloc(HINT_TYPE, void *, unsigned int);
extern HINT *hint_create_parameter(HINT *, char *, char *); extern HINT *hint_create_parameter(HINT *, char *, char *);
extern HINT *hint_create_route(HINT *, HINT_TYPE, char *); extern HINT *hint_create_route(HINT *, HINT_TYPE, char *);
extern void hint_free(HINT *); extern void hint_free(HINT *);
extern HINT *hint_dup(HINT *); extern HINT *hint_dup(HINT *);
bool hint_exists(HINT **, HINT_TYPE); bool hint_exists(HINT **, HINT_TYPE);
#endif #endif