tlv: Implement a memory pool for TLV descriptors.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2018-01-22 22:34:26 -08:00
parent d432cdc52a
commit c8d9d05e7a
3 changed files with 56 additions and 0 deletions

3
msg.c
View File

@ -213,6 +213,9 @@ void msg_cleanup(void)
{ {
struct message_storage *s; struct message_storage *s;
struct ptp_message *m; struct ptp_message *m;
tlv_extra_cleanup();
while ((m = TAILQ_FIRST(&msg_pool)) != NULL) { while ((m = TAILQ_FIRST(&msg_pool)) != NULL) {
TAILQ_REMOVE(&msg_pool, m, list); TAILQ_REMOVE(&msg_pool, m, list);
s = container_of(m, struct message_storage, msg); s = container_of(m, struct message_storage, msg);

32
tlv.c
View File

@ -18,6 +18,7 @@
*/ */
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "port.h" #include "port.h"
@ -29,6 +30,9 @@
uint8_t ieee8021_id[3] = { IEEE_802_1_COMMITTEE }; uint8_t ieee8021_id[3] = { IEEE_802_1_COMMITTEE };
static TAILQ_HEAD(tlv_pool, tlv_extra) tlv_pool =
TAILQ_HEAD_INITIALIZER(tlv_pool);
static void scaled_ns_n2h(ScaledNs *sns) static void scaled_ns_n2h(ScaledNs *sns)
{ {
sns->nanoseconds_msb = ntohs(sns->nanoseconds_msb); sns->nanoseconds_msb = ntohs(sns->nanoseconds_msb);
@ -412,6 +416,34 @@ static void org_pre_send(struct organization_tlv *org)
} }
} }
struct tlv_extra *tlv_extra_alloc(void)
{
struct tlv_extra *extra = TAILQ_FIRST(&tlv_pool);
if (extra) {
TAILQ_REMOVE(&tlv_pool, extra, list);
} else {
extra = calloc(1, sizeof(*extra));
}
return extra;
}
void tlv_extra_cleanup(void)
{
struct tlv_extra *extra;
while ((extra = TAILQ_FIRST(&tlv_pool)) != NULL) {
TAILQ_REMOVE(&tlv_pool, extra, list);
free(extra);
}
}
void tlv_extra_recycle(struct tlv_extra *extra)
{
memset(extra, 0, sizeof(*extra));
TAILQ_INSERT_HEAD(&tlv_pool, extra, list);
}
int tlv_post_recv(struct TLV *tlv, struct tlv_extra *extra) int tlv_post_recv(struct TLV *tlv, struct tlv_extra *extra)
{ {
int result = 0; int result = 0;

21
tlv.h
View File

@ -20,6 +20,8 @@
#ifndef HAVE_TLV_H #ifndef HAVE_TLV_H
#define HAVE_TLV_H #define HAVE_TLV_H
#include <sys/queue.h>
#include "ddt.h" #include "ddt.h"
#include "ds.h" #include "ds.h"
@ -228,11 +230,30 @@ struct mgmt_clock_description {
}; };
struct tlv_extra { struct tlv_extra {
TAILQ_ENTRY(tlv_extra) list;
struct TLV *tlv;
union { union {
struct mgmt_clock_description cd; struct mgmt_clock_description cd;
}; };
}; };
/**
* Allocates a new tlv_extra structure.
* @return Pointer to a new structure on success or NULL otherwise.
*/
struct tlv_extra *tlv_extra_alloc(void);
/**
* Release all of the memory in the tlv_extra cache.
*/
void tlv_extra_cleanup(void);
/**
* Frees a tlv_extra structure.
* @param extra Pointer to the structure to free.
*/
void tlv_extra_recycle(struct tlv_extra *extra);
/** /**
* Converts recognized value sub-fields into host byte order. * Converts recognized value sub-fields into host byte order.
* @param tlv Pointer to a Type Length Value field. * @param tlv Pointer to a Type Length Value field.