1
1

Use thread specific data and static buffers for the return type of

opal_net_get_hostname() rather than malloc, because no one was freeing
the buffer and the common use case was for printfs, where calling
free is a pain.

This commit was SVN r15494.
Этот коммит содержится в:
Brian Barrett 2007-07-18 20:25:01 +00:00
родитель c5d0066c27
Коммит 916397f358
4 изменённых файлов: 103 добавлений и 2 удалений

Просмотреть файл

@ -25,6 +25,7 @@
#include "opal/util/output.h"
#include "opal/util/malloc.h"
#include "opal/util/if.h"
#include "opal/util/net.h"
#include "opal/util/keyval_parse.h"
#include "opal/memoryhooks/memory.h"
#include "opal/mca/base/base.h"
@ -53,6 +54,8 @@ opal_finalize_util(void)
close when not opened internally */
opal_iffinalize();
opal_net_finalize();
/* keyval lex-based parser */
opal_util_keyval_parse_finalize();

Просмотреть файл

@ -28,6 +28,7 @@
#include "opal/memoryhooks/memory.h"
#include "opal/mca/base/base.h"
#include "opal/runtime/opal.h"
#include "opal/util/net.h"
#include "opal/mca/installdirs/base/base.h"
#include "opal/mca/memory/base/base.h"
#include "opal/mca/memcpy/base/base.h"
@ -165,6 +166,11 @@ opal_init_util(void)
goto return_error;
}
if (OPAL_SUCCESS != (ret = opal_net_init())) {
error = "opal_net_init";
goto return_error;
}
/* Setup the parameter system */
if (OPAL_SUCCESS != (ret = mca_base_param_init())) {
error = "mca_base_param_init";

Просмотреть файл

@ -74,9 +74,62 @@
#include "opal/util/output.h"
#include "opal/util/strncpy.h"
#include "opal/constants.h"
#include "opal/threads/tsd.h"
#ifdef HAVE_STRUCT_SOCKADDR_IN
#if OPAL_WANT_IPV6
static opal_tsd_key_t hostname_tsd_key;
static void
hostname_cleanup(void *value)
{
opal_output(0, "cleaning up buffer: 0x%lx", value);
if (NULL != value) free(value);
}
static char*
get_hostname_buffer(void)
{
void *buffer;
int ret;
ret = opal_tsd_getspecific(hostname_tsd_key, &buffer);
if (OPAL_SUCCESS != ret) return NULL;
if (NULL == buffer) {
opal_output(0, "getting a buffer");
buffer = (void*) malloc((NI_MAXHOST + 1) * sizeof(char));
ret = opal_tsd_setspecific(hostname_tsd_key, buffer);
}
opal_output(0, "returning buffer: 0x%lx", buffer);
return (char*) buffer;
}
#endif
int
opal_net_init()
{
#if OPAL_WANT_IPV6
return opal_tsd_key_create(&hostname_tsd_key, hostname_cleanup);
#else
return OPAL_SUCCESS;
#endif
}
int
opal_net_finalize()
{
return OPAL_SUCCESS;
}
/* convert a CIDR prefixlen to netmask (in network byte order) */
uint32_t
opal_net_prefix2netmask(uint32_t prefixlen)
@ -225,7 +278,7 @@ char*
opal_net_get_hostname(struct sockaddr *addr)
{
#if OPAL_WANT_IPV6
char *name = (char *)malloc((NI_MAXHOST + 1) * sizeof(char));
char *name = get_hostname_buffer();
int error;
socklen_t addrlen;
@ -297,6 +350,19 @@ opal_net_get_port(struct sockaddr *addr)
#else /* HAVE_STRUCT_SOCKADDR_IN */
int
opal_net_init()
{
return OPAL_SUCCESS;
}
int
opal_net_finalize()
{
return OPAL_SUCCESS;
}
uint32_t
opal_net_prefix2netmask(uint32_t prefixlen)

Просмотреть файл

@ -35,6 +35,31 @@
BEGIN_C_DECLS
/**
* Intiailize the network helper subsystem
*
* Initialize the network helper subsystem. Should be called exactly
* once for any process that will use any function in the network
* helper subsystem.
*
* @retval OPAL_SUCCESS Success
* @retval OPAL_ERR_TEMP_OUT_OF_RESOURCE Not enough memory for static
* buffer creation
*/
OPAL_DECLSPEC int opal_net_init(void);
/**
* Finalize the network helper subsystem
*
* Finalize the network helper subsystem. Should be called exactly
* once for any process that will use any function in the network
* helper subsystem.
*
* @retval OPAL_SUCCESS Success
*/
OPAL_DECLSPEC int opal_net_finalize(void);
/**
* Calculate netmask in network byte order from CIDR notation
@ -90,7 +115,8 @@ OPAL_DECLSPEC bool opal_net_addr_isipv4public(struct sockaddr *addr);
* Get string version of address
*
* Return the un-resolved address in a string format. The string will
* be created with malloc and the user must free the string.
* be returned in a per-thread static buffer and should not be freed
* by the user.
*
* @param addr struct sockaddr of address
* @return literal representation of \c addr