aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/util')
-rw-r--r--lib/src/util/ip_address.c373
-rw-r--r--lib/src/util/log.c247
-rw-r--r--lib/src/util/pool.c175
-rw-r--r--lib/src/util/ring.c49
-rw-r--r--lib/src/util/slab.c203
-rw-r--r--lib/src/util/types.c56
-rw-r--r--lib/src/util/vector.c101
-rw-r--r--lib/src/util/windows/dlfcn.c84
8 files changed, 997 insertions, 291 deletions
diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c
index 49916547d..b50864c96 100644
--- a/lib/src/util/ip_address.c
+++ b/lib/src/util/ip_address.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2021 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -20,44 +20,58 @@
#include <hicn/util/ip_address.h>
#include <hicn/util/log.h>
+#include <hicn/util/sstrncpy.h>
+#include <hicn/common.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#ifdef __ANDROID__
-#define SWAP(x) bswap_32(x)
+#define SWAP(x) bswap_32 (x)
#else
-#define SWAP(x) __bswap_constant_32(x)
+#define SWAP(x) __bswap_constant_32 (x)
#endif
#else
#define SWAP(x) x
#endif
-
/* No htonl() with const */
-const ip_address_t IPV4_LOOPBACK = {
- .v4.as_inaddr.s_addr = SWAP(INADDR_LOOPBACK),
+const hicn_ip_address_t IPV4_LOOPBACK = {
+ .v4.as_inaddr.s_addr = SWAP (INADDR_LOOPBACK),
+};
+
+const hicn_ip_address_t IPV6_LOOPBACK = {
+ .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT,
};
-const ip_address_t IPV6_LOOPBACK ={
- .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT,
+const hicn_ip_address_t IPV4_ANY = {
+ .v4.as_inaddr.s_addr = INADDR_ANY,
};
-const ip_address_t IPV4_ANY = {
- .v4.as_inaddr.s_addr = INADDR_ANY,
+const hicn_ip_address_t IPV6_ANY = {
+ .v6.as_in6addr = IN6ADDR_ANY_INIT,
};
-const ip_address_t IPV6_ANY = {
- .v6.as_in6addr = IN6ADDR_ANY_INIT,
+const ipv4_address_t IP4_ADDRESS_EMPTY = {
+ .as_u32 = 0,
};
-const ip_address_t IP_ADDRESS_EMPTY = {
- .v6.as_u64 = { 0 },
+const ipv6_address_t IP6_ADDRESS_EMPTY = {
+ .as_u64 = { 0, 0 },
};
+const hicn_ip_address_t IP_ADDRESS_EMPTY = {
+ .v6.as_u64 = { 0, 0 },
+};
/* IP address */
int
-ip_address_get_family (const char * ip_address)
+hicn_ip_address_get_family (const hicn_ip_address_t *address)
+{
+ return hicn_ip_address_is_v4 (address) ? AF_INET : AF_INET6;
+}
+
+int
+hicn_ip_address_str_get_family (const char *ip_address)
{
struct addrinfo hint, *res = NULL;
int rc;
@@ -78,27 +92,41 @@ ip_address_get_family (const char * ip_address)
}
int
-ip_address_len (int family)
+hicn_ip_address_len (int family)
{
return (family == AF_INET6) ? IPV6_ADDR_LEN :
- (family == AF_INET) ? IPV4_ADDR_LEN : 0;
+ (family == AF_INET) ? IPV4_ADDR_LEN :
+ 0;
}
int
-ip_address_ntop (const ip_address_t * ip_address, char *dst, const size_t len,
- int family)
+hicn_ip_address_get_len (const hicn_ip_address_t *ip_address)
{
- const char * s;
- switch(family) {
+ return hicn_ip_address_len (hicn_ip_address_get_family (ip_address));
+}
+
+int
+hicn_ip_address_get_len_bits (const hicn_ip_address_t *ip_address)
+{
+ return bytes_to_bits (hicn_ip_address_get_len (ip_address));
+}
+
+int
+hicn_ip_address_ntop (const hicn_ip_address_t *ip_address, char *dst,
+ const size_t len, int family)
+{
+ const char *s;
+ switch (family)
+ {
case AF_INET:
- s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, (socklen_t)len);
+ s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, (socklen_t) len);
break;
case AF_INET6:
- s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, (socklen_t)len);
+ s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, (socklen_t) len);
break;
default:
return -1;
- }
+ }
return (s ? 1 : -1);
}
@@ -106,22 +134,26 @@ ip_address_ntop (const ip_address_t * ip_address, char *dst, const size_t len,
* Parse ip addresses in presentation format
*/
int
-ip_address_pton (const char *ip_address_str, ip_address_t * ip_address)
+hicn_ip_address_pton (const char *hicn_ip_address_str,
+ hicn_ip_address_t *ip_address)
{
int pton_fd;
int family;
- family = ip_address_get_family (ip_address_str);
+ family = hicn_ip_address_str_get_family (hicn_ip_address_str);
- switch (family) {
+ switch (family)
+ {
case AF_INET:
ip_address->pad[0] = 0;
ip_address->pad[1] = 0;
ip_address->pad[2] = 0;
- pton_fd = inet_pton (AF_INET, ip_address_str, &ip_address->v4.buffer);
+ pton_fd =
+ inet_pton (AF_INET, hicn_ip_address_str, &ip_address->v4.buffer);
break;
case AF_INET6:
- pton_fd = inet_pton (AF_INET6, ip_address_str, &ip_address->v6.buffer);
+ pton_fd =
+ inet_pton (AF_INET6, hicn_ip_address_str, &ip_address->v6.buffer);
break;
default:
return -1;
@@ -132,36 +164,38 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address)
if (pton_fd <= 0)
return -1;
- return 1;
+ return 0;
}
int
-ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family)
+hicn_ip_address_snprintf (char *s, size_t size,
+ const hicn_ip_address_t *ip_address)
{
-
- const char * rc;
- switch(family) {
- case AF_INET:
- if (size < INET_ADDRSTRLEN)
- return -1;
- rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN);
- break;
- case AF_INET6:
- if (size < INET6_ADDRSTRLEN)
- return -1;
- rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN);
- break;
- default:
- return -1;
+ const char *rc;
+ int family = hicn_ip_address_get_family (ip_address);
+ switch (family)
+ {
+ case AF_INET:
+ if (size < INET_ADDRSTRLEN)
+ return -1;
+ rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN);
+ break;
+ case AF_INET6:
+ if (size < INET6_ADDRSTRLEN)
+ return -1;
+ rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN);
+ break;
+ default:
+ return -1;
}
- if (!rc)
- return -1;
- return (int)strlen(s);
+ if (!rc)
+ return -1;
+ return (int) strnlen_s (s, MAXSZ_IP_ADDRESS);
}
int
-ip_address_to_sockaddr(const ip_address_t * ip_address,
- struct sockaddr *sa, int family)
+hicn_ip_address_to_sockaddr (const hicn_ip_address_t *ip_address,
+ struct sockaddr *sa, int family)
{
struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) sa;
struct sockaddr_in *tmp4 = (struct sockaddr_in *) sa;
@@ -187,64 +221,72 @@ ip_address_to_sockaddr(const ip_address_t * ip_address,
}
int
-ip_address_cmp(const ip_address_t * ip1, const ip_address_t * ip2, int family)
+hicn_ip_address_cmp (const hicn_ip_address_t *ip1,
+ const hicn_ip_address_t *ip2)
{
- switch(family) {
- case AF_INET:
- return memcmp(&ip1->v4, &ip2->v4, sizeof(ip1->v4));
- break;
- case AF_INET6:
- return memcmp(&ip1->v6, &ip2->v6, sizeof(ip1->v6));
- break;
- default:
- return memcmp(ip1, ip2, sizeof(ip_address_t));
- }
+ /* This works as soon as all members are initialized */
+ return memcmp (ip1, ip2, sizeof (hicn_ip_address_t));
}
-int
-ip_address_empty(const ip_address_t * ip)
+bool
+hicn_ip_address_equals (const hicn_ip_address_t *ip1,
+ const hicn_ip_address_t *ip2)
{
- return (memcmp(ip, &IP_ADDRESS_EMPTY, sizeof(IP_ADDRESS_EMPTY)) == 0);
+ return hicn_ip_address_cmp (ip1, ip2) == 0;
}
+int
+hicn_ip_address_empty (const hicn_ip_address_t *ip)
+{
+ return (memcmp (ip, &IP_ADDRESS_EMPTY, sizeof (hicn_ip_address_t)) == 0);
+}
+void
+hicn_ip_address_clear (hicn_ip_address_t *address)
+{
+ memset (address, 0, sizeof (hicn_ip_address_t));
+}
/* Prefix */
/* Parse IP Prefixes in presentation format (in bits, separated by a slash) */
int
-ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix)
+hicn_ip_prefix_pton (const char *hicn_ip_address_str,
+ hicn_ip_prefix_t *hicn_ip_prefix)
{
int pton_fd;
char *p;
char *eptr;
- char *addr = strdup (ip_address_str);
+ char *addr = strdup (hicn_ip_address_str);
p = strchr (addr, '/');
- if (!p) {
- ip_prefix->len = ~0; // until we get the ip address family
- } else {
- ip_prefix->len = (u8)strtoul (p + 1, &eptr, 10);
- *p = 0;
- }
+ if (!p)
+ {
+ hicn_ip_prefix->len = ~0; // until we get the ip address family
+ }
+ else
+ {
+ hicn_ip_prefix->len = (u8) strtoul (p + 1, &eptr, 10);
+ *p = 0;
+ }
- ip_prefix->family = ip_address_get_family (addr);
+ hicn_ip_prefix->family = hicn_ip_address_str_get_family (addr);
- switch (ip_prefix->family)
+ switch (hicn_ip_prefix->family)
{
case AF_INET6:
- if (ip_prefix->len == (u8)~0)
- ip_prefix->len = IPV6_ADDR_LEN_BITS;
- if (ip_prefix->len > IPV6_ADDR_LEN_BITS)
- goto ERR;
- pton_fd = inet_pton (AF_INET6, addr, &ip_prefix->address.v6.buffer);
+ if (hicn_ip_prefix->len == (u8) ~0)
+ hicn_ip_prefix->len = IPV6_ADDR_LEN_BITS;
+ if (hicn_ip_prefix->len > IPV6_ADDR_LEN_BITS)
+ goto ERR;
+ pton_fd = inet_pton (AF_INET6, addr, &hicn_ip_prefix->address.v6.buffer);
break;
case AF_INET:
- if (ip_prefix->len == (u8)~0)
- ip_prefix->len = IPV4_ADDR_LEN_BITS;
- if (ip_prefix->len > IPV4_ADDR_LEN_BITS)
- goto ERR;
- pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.v4.buffer);
+ if (hicn_ip_prefix->len == (u8) ~0)
+ hicn_ip_prefix->len = IPV4_ADDR_LEN_BITS;
+ if (hicn_ip_prefix->len > IPV4_ADDR_LEN_BITS)
+ goto ERR;
+ pton_fd = inet_pton (AF_INET, addr, &hicn_ip_prefix->address.v4.buffer);
break;
default:
goto ERR;
@@ -253,139 +295,180 @@ ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix)
// 0 = not in presentation format
// < 0 = other error (use perror)
if (pton_fd <= 0)
- goto ERR;
+ goto ERR;
- free(addr);
- return 1;
+ free (addr);
+ return 0;
ERR:
free (addr);
return -1;
}
int
-ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size)
+hicn_ip_prefix_ntop_short (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst,
+ size_t size)
{
char ip_s[MAXSZ_IP_ADDRESS];
- const char * s;
- switch(ip_prefix->family) {
+ const char *s;
+ switch (hicn_ip_prefix->family)
+ {
case AF_INET:
- s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s,
+ MAXSZ_IP_ADDRESS);
break;
case AF_INET6:
- s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s,
+ MAXSZ_IP_ADDRESS);
break;
default:
return -1;
- }
+ }
if (!s)
- return -1;
- int rc = snprintf(dst, size, "%s", ip_s);
+ return -1;
+ int rc = snprintf (dst, size, "%s", ip_s);
if (rc >= size)
- return (int)size;
+ return (int) size;
return rc;
}
int
-ip_prefix_ntop(const ip_prefix_t * ip_prefix, char *dst, size_t size)
+hicn_ip_prefix_ntop (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst,
+ size_t size)
{
char ip_s[MAXSZ_IP_ADDRESS];
- const char * s;
- switch(ip_prefix->family) {
+ const char *s;
+ switch (hicn_ip_prefix->family)
+ {
case AF_INET:
- s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s,
+ MAXSZ_IP_ADDRESS);
break;
case AF_INET6:
- s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s,
+ MAXSZ_IP_ADDRESS);
break;
default:
return -1;
- }
+ }
if (!s)
- return -1;
- int rc = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len);
+ return -1;
+ int rc = snprintf (dst, size, "%s/%d", ip_s, hicn_ip_prefix->len);
if (rc >= size)
- return (int)size;
+ return (int) size;
return rc;
}
int
-ip_prefix_len (const ip_prefix_t * prefix)
+hicn_ip_prefix_snprintf (char *s, size_t size, const hicn_ip_prefix_t *prefix)
+{
+ return hicn_ip_prefix_ntop (prefix, s, size);
+}
+
+int
+hicn_ip_prefix_len (const hicn_ip_prefix_t *prefix)
{
- return prefix->len; // ip_address_len(&prefix->address, prefix->family);
+ return prefix->len; // hicn_ip_address_len(&prefix->address, prefix->family);
}
const u8 *
-ip_address_get_buffer(const ip_address_t * ip_address, int family)
+hicn_ip_address_get_buffer (const hicn_ip_address_t *ip_address, int family)
{
- switch(family) {
+ switch (family)
+ {
case AF_INET:
return ip_address->v4.buffer;
case AF_INET6:
return ip_address->v6.buffer;
default:
return NULL;
- }
+ }
}
bool
-ip_prefix_empty (const ip_prefix_t * prefix)
+hicn_ip_prefix_empty (const hicn_ip_prefix_t *prefix)
{
return prefix->len == 0;
}
int
-ip_prefix_to_sockaddr(const ip_prefix_t * prefix,
- struct sockaddr *sa)
+hicn_ip_prefix_to_sockaddr (const hicn_ip_prefix_t *prefix,
+ struct sockaddr *sa)
{
- // XXX assert len == ip_address_len
- return ip_address_to_sockaddr(&prefix->address, sa, prefix->family);
+ // XXX assert len == hicn_ip_address_len
+ return hicn_ip_address_to_sockaddr (&prefix->address, sa, prefix->family);
}
int
-ip_prefix_cmp(const ip_prefix_t * prefix1, const ip_prefix_t * prefix2)
+hicn_ip_prefix_cmp (const hicn_ip_prefix_t *prefix1,
+ const hicn_ip_prefix_t *prefix2)
+{
+ if (prefix1->family < prefix2->family)
+ return -1;
+ else if (prefix1->family > prefix2->family)
+ return 1;
+
+ if (prefix1->len < prefix2->len)
+ return -1;
+ else if (prefix1->len > prefix2->len)
+ return 1;
+
+ return hicn_ip_address_cmp (&prefix1->address, &prefix2->address);
+}
+
+/* Network byte order + host bit order */
+
+uint8_t
+hicn_ip_address_get_bit (const hicn_ip_address_t *address, uint8_t pos)
{
- if (prefix1->family < prefix2->family)
- return -1;
- else if (prefix1->family > prefix2->family)
- return 1;
+ return (address->v6.as_u8[pos / 8] >> (7 - pos % 8)) & 0x1;
+}
- if (prefix1->len < prefix2->len)
- return -1;
- else if (prefix1->len > prefix2->len)
- return 1;
+bool
+hicn_ip_address_match_family (const hicn_ip_address_t *address, int family)
+{
+ return hicn_ip_address_get_family (address) == family;
+}
- return ip_address_cmp(&prefix1->address, &prefix2->address, prefix1->family);
+uint32_t
+hicn_ip_address_get_hash (const hicn_ip_address_t *address)
+{
+ return hash32 (address, sizeof (address));
}
/* URL */
#define MAXSZ_PROTO_ 8 /* inetX:// */
-#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM
+#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM
#define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_
#define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_
-#define MAXSZ_URL_ MAXSZ_URL6_
-#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM
-#define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM
-#define MAXSZ_URL MAXSZ_URL_ + NULLTERM
+#define MAXSZ_URL_ MAXSZ_URL6_
+#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM
+#define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM
+#define MAXSZ_URL MAXSZ_URL_ + NULLTERM
int
-url_snprintf(char * s, size_t size, int family,
- const ip_address_t * ip_address, u16 port)
+url_snprintf (char *s, size_t size, const hicn_ip_address_t *ip_address,
+ u16 port)
{
- char ip_address_s[MAXSZ_IP_ADDRESS];
- int rc;
+ char hicn_ip_address_s[MAXSZ_IP_ADDRESS];
+ int rc;
- /* Other address are currently not supported */
- if (!IS_VALID_FAMILY(family))
- return -1;
+ int family = hicn_ip_address_get_family (ip_address);
- rc = ip_address_snprintf(ip_address_s, MAXSZ_IP_ADDRESS, ip_address, family);
- if (rc >= MAXSZ_IP_ADDRESS)
- WARN("[url_snprintf] Unexpected ip_address truncation");
- if (rc < 0)
- return rc;
+ /* Other address are currently not supported */
+ if (!IS_VALID_FAMILY (family))
+ return -1;
- return snprintf(s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6',
- ip_address_s, port);
+ if (!hicn_ip_address_match_family (ip_address, family))
+ return -1;
+ rc =
+ hicn_ip_address_snprintf (hicn_ip_address_s, MAXSZ_IP_ADDRESS, ip_address);
+ if (rc >= MAXSZ_IP_ADDRESS)
+ WARN ("[url_snprintf] Unexpected ip_address truncation");
+ if (rc < 0)
+ return rc;
+
+ return snprintf (s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6',
+ hicn_ip_address_s, port);
}
diff --git a/lib/src/util/log.c b/lib/src/util/log.c
index c1fc999ad..91a87e848 100644
--- a/lib/src/util/log.c
+++ b/lib/src/util/log.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2021 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -18,6 +18,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
+#include <strings.h>
#ifdef __ANDROID__
#include <android/log.h>
@@ -25,154 +26,184 @@
log_conf_t log_conf = DEFAULT_LOG_CONF;
-#define FMT_DATETIME "%02d-%02d-%04d %02d:%02d:%02d"
-#define FMT_DATETIME_LEN 20
-#define snprintf_nowarn(...) (snprintf(__VA_ARGS__) < 0 ? abort() : (void)0)
+#define FMT_DATETIME "%02d-%02d-%04d %02d:%02d:%02d"
+#define FMT_DATETIME_LEN 20
+#define snprintf_nowarn(...) (snprintf (__VA_ARGS__) < 0 ? abort () : (void) 0)
+#define COLOR_RED "\033[31m"
+#define COLOR_YELLOW "\033[33m"
+#define COLOR_RESET "\033[0m"
static char ts[FMT_DATETIME_LEN];
-static char *timestamp(void)
+static char *
+timestamp (void)
{
- time_t tv;
- struct tm *tm;
+ time_t tv;
+ struct tm *tm;
- time(&tv);
- tm = localtime(&tv);
+ time (&tv);
+ tm = localtime (&tv);
- snprintf_nowarn(ts, FMT_DATETIME_LEN, FMT_DATETIME, tm->tm_mday,
- tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min,
- tm->tm_sec);
- return ts;
+ snprintf_nowarn (ts, FMT_DATETIME_LEN, FMT_DATETIME, tm->tm_mday,
+ tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min,
+ tm->tm_sec);
+ return ts;
}
-void _log_va(int level, const char *fmt, va_list ap)
-{
+void
+_log_va (int level, const char *fmt, va_list ap)
+{
#if 0
if (!conf.log_system)
return;
#endif
- char *prefix;
+ char *prefix;
#ifdef __ANDROID__
- int prio = -1;
- if (level > log_conf.log_level)
- return;
-
- switch (level) {
- case LOG_FATAL:
- prio = ANDROID_LOG_FATAL;
- prefix = "FATAL: ";
- break;
- case LOG_ERROR:
- prio = ANDROID_LOG_ERROR;
- prefix = "ERROR: ";
- break;
- case LOG_WARN:
- prio = ANDROID_LOG_WARN;
- prefix = "WARNING: ";
- break;
- case LOG_INFO:
- prio = ANDROID_LOG_INFO;
- prefix = "";
- break;
- case LOG_DEBUG:
- prio = ANDROID_LOG_DEBUG;
- prefix = "DEBUG: ";
- break;
- case LOG_TRACE:
- prio = ANDROID_LOG_DEBUG;
- prefix = "TRACE: ";
- break;
- default:
- prio = ANDROID_LOG_INFO;
- prefix = "";
- break;
- }
-
- if (log_conf.log_file) {
- FILE *f = log_conf.log_file;
- fprintf(f, "%s %s", timestamp(), prefix);
- vfprintf(f, fmt, ap);
- fprintf(f, "\n");
- } else {
- __android_log_vprint(ANDROID_LOG_INFO, "HICN FACEMGR", fmt, ap);
- }
+ int prio = -1;
+ if (level > log_conf.log_level)
+ return;
+
+ switch (level)
+ {
+ case LOG_FATAL:
+ prio = ANDROID_LOG_FATAL;
+ prefix = "FATAL: ";
+ break;
+ case LOG_ERROR:
+ prio = ANDROID_LOG_ERROR;
+ prefix = "ERROR: ";
+ break;
+ case LOG_WARN:
+ prio = ANDROID_LOG_WARN;
+ prefix = "WARNING: ";
+ break;
+ case LOG_INFO:
+ prio = ANDROID_LOG_INFO;
+ prefix = "";
+ break;
+ case LOG_DEBUG:
+ prio = ANDROID_LOG_DEBUG;
+ prefix = "DEBUG: ";
+ break;
+ case LOG_TRACE:
+ prio = ANDROID_LOG_DEBUG;
+ prefix = "TRACE: ";
+ break;
+ default:
+ prio = ANDROID_LOG_INFO;
+ prefix = "";
+ break;
+ }
+
+ if (log_conf.log_file)
+ {
+ FILE *f = log_conf.log_file;
+ fprintf (f, "%s %s", timestamp (), prefix);
+ vfprintf (f, fmt, ap);
+ fprintf (f, "\n");
+ }
+ else
+ {
+ __android_log_vprint (prio, "HICN FACEMGR", fmt, ap);
+ }
#else
- if (level > log_conf.log_level)
- return;
-
- switch (level) {
- case LOG_FATAL:
- prefix = "FATAL: ";
- break;
- case LOG_ERROR:
- prefix = "ERROR: ";
- break;
- case LOG_WARN:
- prefix = "WARNING: ";
- break;
- case LOG_INFO:
- prefix = "";
- break;
- case LOG_DEBUG:
- prefix = "DEBUG: ";
- break;
- case LOG_TRACE:
- prefix = "TRACE: ";
- break;
- default:
- prefix = "";
- break;
- }
- FILE *f = log_conf.log_file ? log_conf.log_file : stdout;
- fprintf(f, "%s %s", timestamp(), prefix);
- vfprintf(f, fmt, ap);
- fprintf(f, "\n");
+ if (level > log_conf.log_level)
+ return;
+
+ char *color = COLOR_RESET;
+ switch (level)
+ {
+ case LOG_FATAL:
+ prefix = "FATAL: ";
+ break;
+ case LOG_ERROR:
+ prefix = "ERROR: ";
+ color = COLOR_RED;
+ break;
+ case LOG_WARN:
+ prefix = "WARNING: ";
+ color = COLOR_YELLOW;
+ break;
+ case LOG_INFO:
+ prefix = "";
+ break;
+ case LOG_DEBUG:
+ prefix = "DEBUG: ";
+ break;
+ case LOG_TRACE:
+ prefix = "TRACE: ";
+ break;
+ default:
+ prefix = "";
+ break;
+ }
+ FILE *f = log_conf.log_file ? log_conf.log_file : stdout;
+ fprintf (f, "%s%s %s", color, timestamp (), prefix);
+ vfprintf (f, fmt, ap);
+ fprintf (f, "%s\n", COLOR_RESET);
#ifdef DEBUG
- fflush(f);
+ fflush (f);
#endif
#endif
}
-void _log(int level, const char *fmt, ...)
+void
+_log (int level, const char *fmt, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, fmt);
- _log_va(level, fmt, ap);
- va_end(ap);
+ va_start (ap, fmt);
+ _log_va (level, fmt, ap);
+ va_end (ap);
}
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
-void print_trace(void)
+void
+print_trace (void)
{
- void *array[32];
- size_t size;
+ void *array[32];
+ size_t size;
- size = backtrace(array, 32);
- fflush(conf.log_file);
- backtrace_symbols_fd(array, size, fileno(conf.log_file));
+ size = backtrace (array, 32);
+ fflush (conf.log_file);
+ backtrace_symbols_fd (array, size, fileno (conf.log_file));
}
#endif
-void fatal(char *fmt, ...)
+void
+fatal (char *fmt, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, fmt);
- _log_va(LOG_FATAL, fmt, ap);
- va_end(ap);
+ va_start (ap, fmt);
+ _log_va (LOG_FATAL, fmt, ap);
+ va_end (ap);
#ifdef HAVE_BACKTRACE
- print_trace();
+ print_trace ();
#endif
- exit(200);
+ exit (200);
}
+
+const char *loglevel_str[] = { "fatal", "error", "warn",
+ "info", "debug", "trace" };
+
+int
+loglevel_from_str (const char *loglevel)
+{
+ for (int i = 0; i <= LOG_TRACE; i++)
+ {
+ if (strcasecmp (loglevel, loglevel_str[i]) == 0)
+ return i;
+ }
+ return -1;
+} \ No newline at end of file
diff --git a/lib/src/util/pool.c b/lib/src/util/pool.c
new file mode 100644
index 000000000..2c1e90b5e
--- /dev/null
+++ b/lib/src/util/pool.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file pool.c
+ * \brief Implementation of fixed-size pool allocator.
+ *
+ * NOTE:
+ * - Ideally, we should have a single realloc per resize, that would encompass
+ * both the free indices vector and bitmap, by nesting data structures.
+ * Because of the added complexity, and by lack of evidence of the need for
+ * this, we currently rely on a simpler implementation.
+ */
+
+#include <assert.h>
+#include <stdlib.h> // calloc
+
+#include <hicn/util/pool.h>
+#include <hicn/util/log.h>
+
+#include <stdio.h> // XXX
+
+void
+_pool_init (void **pool_ptr, size_t elt_size, size_t init_size,
+ size_t max_size)
+{
+ assert (pool_ptr);
+ assert (elt_size);
+
+ init_size = next_pow2 (init_size);
+
+ if (max_size && init_size > max_size)
+ goto ERR_MAX_SIZE;
+
+ /* The initial pool size is rounded to the next power of two */
+ size_t alloc_size = next_pow2 (init_size);
+
+ pool_hdr_t *ph = calloc (POOL_HDRLEN + alloc_size * elt_size, 1);
+ if (!ph)
+ goto ERR_MALLOC;
+
+ ph->elt_size = elt_size;
+ ph->alloc_size = alloc_size;
+ ph->max_size = max_size;
+
+ /* Free indices */
+ off_t *free_indices;
+ vector_init (free_indices, init_size, max_size);
+ for (unsigned i = 0; i < init_size; i++)
+ free_indices[i] = (init_size - 1) - i;
+ vector_len (free_indices) = init_size;
+ ph->free_indices = free_indices;
+
+ /* Free bitmap */
+ bitmap_t *fb = ph->free_bitmap;
+ bitmap_init (fb, init_size, max_size);
+ bitmap_set_to (fb, init_size);
+ ph->free_bitmap = fb;
+
+ *pool_ptr = (uint8_t *) ph + POOL_HDRLEN;
+
+ return;
+
+ERR_MALLOC:
+ERR_MAX_SIZE:
+ *pool_ptr = NULL;
+ return;
+}
+
+void
+_pool_free (void **pool_ptr)
+{
+ pool_hdr_t *ph = pool_hdr (*pool_ptr);
+ vector_free (ph->free_indices);
+ bitmap_free (ph->free_bitmap);
+
+ free (pool_hdr (*pool_ptr));
+ *pool_ptr = NULL;
+}
+
+bool
+_pool_validate_id (void **pool_ptr, off_t id)
+{
+ pool_hdr_t *ph = pool_hdr (*pool_ptr);
+ size_t pool_size = pool_get_alloc_size (*pool_ptr);
+ if (id >= pool_size || !bitmap_is_unset (ph->free_bitmap, id))
+ return false;
+
+ return true;
+}
+
+void
+_pool_resize (void **pool_ptr, size_t elt_size)
+{
+ pool_hdr_t *ph = pool_hdr (*pool_ptr);
+ size_t old_size = ph->alloc_size;
+ size_t new_size = old_size * 2;
+
+ WARN ("pool_resize to %lu", new_size);
+
+ if (ph->max_size && new_size > ph->max_size)
+ goto ERR_MAX_SIZE;
+
+ /* Double pool storage */
+ ph = realloc (ph, POOL_HDRLEN + new_size * elt_size);
+ if (!ph)
+ goto ERR_REALLOC;
+ ph->elt_size = elt_size;
+ ph->alloc_size = new_size;
+
+ /*
+ * After resize, the pool will have new free indices, ranging from
+ * old_size to (new_size - 1)
+ */
+ vector_ensure_pos (ph->free_indices, old_size);
+ for (unsigned i = 0; i < old_size; i++)
+ ph->free_indices[i] = new_size - 1 - i;
+ vector_len (ph->free_indices) = old_size;
+
+ /* We also need to update the bitmap */
+ bitmap_ensure_pos (&(ph->free_bitmap), new_size - 1);
+ bitmap_set_range (ph->free_bitmap, old_size, new_size - 1);
+
+ /* Reassign pool pointer */
+ *pool_ptr = (uint8_t *) ph + POOL_HDRLEN;
+
+ return;
+
+ERR_REALLOC:
+ERR_MAX_SIZE:
+ *pool_ptr = NULL;
+ return;
+}
+
+off_t
+_pool_get (void **pool_ptr, void **elt, size_t elt_size)
+{
+ pool_hdr_t *ph = pool_hdr (*pool_ptr);
+ uint64_t l = vector_len (ph->free_indices);
+ if (l == 0)
+ {
+ _pool_resize (pool_ptr, elt_size);
+ ph = pool_hdr (*pool_ptr);
+ l = vector_len (ph->free_indices);
+ }
+ off_t free_id = ph->free_indices[l - 1];
+ vector_len (ph->free_indices)--;
+ bitmap_unset (ph->free_bitmap, free_id);
+ *elt = *pool_ptr + free_id * elt_size;
+ return free_id;
+}
+
+void
+_pool_put (void **pool_ptr, void **elt, size_t elt_size)
+{
+ pool_hdr_t *ph = pool_hdr (*pool_ptr);
+ uint64_t l = vector_len (ph->free_indices);
+ vector_ensure_pos (ph->free_indices, l);
+ off_t freed_id = (*elt - *pool_ptr) / elt_size;
+ ph->free_indices[l] = freed_id;
+ vector_len (ph->free_indices)++;
+ bitmap_set (ph->free_bitmap, freed_id);
+}
diff --git a/lib/src/util/ring.c b/lib/src/util/ring.c
new file mode 100644
index 000000000..2c722a842
--- /dev/null
+++ b/lib/src/util/ring.c
@@ -0,0 +1,49 @@
+
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file ring.c
+ * \brief Implementation of ring buffer.
+ */
+
+#include <stdlib.h>
+
+#include <hicn/util/ring.h>
+
+void
+_ring_init (void **ring_ptr, size_t elt_size, size_t max_size)
+{
+ assert (ring_ptr);
+ assert (elt_size > 0);
+ // we use a static array, not a vector (for now)
+ assert (max_size != 0);
+
+ ring_hdr_t *rh = malloc (RING_HDRLEN + max_size * elt_size);
+
+ rh->roff = 0;
+ rh->woff = 0;
+ rh->size = 0;
+ rh->max_size = max_size;
+
+ *ring_ptr = (uint8_t *) rh + RING_HDRLEN;
+}
+
+void
+_ring_free (void **ring_ptr)
+{
+ free (ring_hdr (*ring_ptr));
+ *ring_ptr = NULL;
+}
diff --git a/lib/src/util/slab.c b/lib/src/util/slab.c
new file mode 100644
index 000000000..763dcb444
--- /dev/null
+++ b/lib/src/util/slab.c
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hicn/util/pool.h>
+#include <hicn/util/slab.h>
+#include <stdlib.h>
+
+/* BLOCK LINKED LISTS */
+
+static void
+block_add_to_head (block_t **head_ptr, block_t *block)
+{
+ if (head_ptr == NULL || block == NULL)
+ return;
+
+ block->prev = NULL;
+ if (*head_ptr != NULL)
+ {
+ block->next = *head_ptr;
+ block->next->prev = block;
+ }
+
+ *head_ptr = block;
+}
+
+static block_t *
+block_remove_from_head (block_t **head_ptr)
+{
+ if (head_ptr == NULL || *head_ptr == NULL)
+ return NULL;
+
+ block_t *old_head = *head_ptr;
+ *head_ptr = old_head->next;
+
+ if (*head_ptr != NULL)
+ (*head_ptr)->prev = NULL;
+ return old_head;
+}
+
+static void
+block_remove (block_t **head_ptr, block_t *block)
+{
+ if (*head_ptr == NULL || block == NULL)
+ return;
+
+ if (block == *head_ptr)
+ *head_ptr = block->next;
+ if (block->next != NULL)
+ block->next->prev = block->prev;
+ if (block->prev != NULL)
+ block->prev->next = block->next;
+}
+
+static bool
+is_block_list_empty (block_t *block)
+{
+ return block == NULL;
+}
+
+static void
+block_list_free (block_t **head)
+{
+ if (head == NULL || *head == NULL)
+ return;
+
+ block_t *curr_block = *head;
+ while (curr_block != NULL)
+ {
+ block_t *next = curr_block->next;
+ pool_free (curr_block->pool);
+ free (curr_block);
+
+ curr_block = next;
+ }
+
+ *head = NULL;
+}
+
+/* BLOCK */
+
+static bool
+is_block_full (block_t *block)
+{
+ return pool_get_free_indices_size (block->pool) == 0;
+}
+
+static void
+block_set_ptr_in_chunks (block_t *block, size_t chunk_size)
+{
+ void *chunk = block->pool;
+
+ // Cannot use `pool_foreach()` since it requires to know the type
+ // while here we use a generic (void *)
+ for (int i = 0; i < pool_get_alloc_size (block->pool); i++)
+ {
+ chunk_hdr_t *hdr = (chunk_hdr_t *) chunk;
+ hdr->block = block;
+
+ chunk = (uint8_t *) (chunk) + chunk_size; // Move to next chunk
+ }
+}
+
+static block_t *
+block_create (size_t chunk_size, size_t num_chunks)
+{
+ block_t *block = malloc (sizeof (block_t));
+ if (block == NULL)
+ return NULL;
+
+ block->prev = block->next = NULL;
+ _pool_init (&block->pool, chunk_size, num_chunks, 0);
+ block_set_ptr_in_chunks (block, chunk_size);
+
+ return block;
+}
+
+/* SLAB */
+
+slab_t *
+_slab_create (size_t elt_size, size_t num_elts)
+{
+ // Initialize slab
+ slab_t *slab = malloc (sizeof (slab_t));
+ if (slab == NULL)
+ return NULL;
+
+ *slab = (slab_t){ .num_chunks = next_pow2 (num_elts),
+ .chunk_size = CHUNK_HDRLEN + elt_size,
+ .full = NULL,
+ .partial_or_empty = NULL };
+
+ // Add initial empty block to partial or empty list
+ block_t *block = block_create (slab->chunk_size, slab->num_chunks);
+ block_add_to_head (&slab->partial_or_empty, block);
+
+ return slab;
+}
+
+void
+slab_free (slab_t *slab)
+{
+ block_list_free (&slab->full);
+ block_list_free (&slab->partial_or_empty);
+ free (slab);
+}
+
+void *
+_slab_get (slab_t *slab)
+{
+ // Create new empty block if none with available chunks
+ if (is_block_list_empty (slab->partial_or_empty))
+ {
+ block_t *block = block_create (slab->chunk_size, slab->num_chunks);
+ block_add_to_head (&slab->partial_or_empty, block);
+
+ slab->num_chunks *= 2; // Grow size exponentially
+ }
+
+ // Get chunck from first block in 'partial_or_empty' list
+ void *chunk;
+ _pool_get (&slab->partial_or_empty->pool, &chunk, slab->chunk_size);
+
+ // If the current block (i.e. head of 'partial_or_empty' list) if full,
+ // move it to the 'full' list
+ if (is_block_full (slab->partial_or_empty))
+ {
+ block_t *block = block_remove_from_head (&slab->partial_or_empty);
+ block_add_to_head (&slab->full, block);
+ }
+
+ return (uint8_t *) chunk + CHUNK_HDRLEN;
+}
+
+void
+_slab_put (slab_t *slab, void *chunk)
+{
+ // Get which block the chunk (that we want to release) belong to
+ chunk_hdr_t *hdr = chunk_hdr (chunk);
+ block_t *block = hdr->block;
+
+ // Put chunk back into block
+ bool is_full = is_block_full (block);
+ _pool_put (&block->pool, (void *) &hdr, slab->chunk_size);
+
+ // If the block was previously full, move it to 'partial_or_empty' list
+ if (is_full)
+ {
+ block_remove (&slab->full, block);
+ block_add_to_head (&slab->partial_or_empty, block);
+ }
+} \ No newline at end of file
diff --git a/lib/src/util/types.c b/lib/src/util/types.c
new file mode 100644
index 000000000..744192593
--- /dev/null
+++ b/lib/src/util/types.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hicn/util/types.h>
+
+uint32_t
+htonf (float f)
+{
+ uint32_t i;
+ uint32_t sign = 0;
+
+ if (f < 0)
+ {
+ sign = 1;
+ f = -f;
+ }
+
+ // i[31] = sign bit
+ i = sign << 31;
+
+ // i[30 to 16] = int(f)[14 to 0]
+ i |= (((uint32_t) f) & 0x7fff) << 16;
+
+ // i[15 to 0] = fraction(f) bits [15 to 0]
+ i |= (uint32_t) ((f - (uint32_t) f) * 65536.0f) & 0xffff;
+
+ return i;
+}
+
+float
+ntohf (uint32_t i)
+{
+ // integer part = i[14 to 0]
+ float f = (i >> 16) & 0x7fff;
+
+ // fraction part = i[15 to 0]
+ f += (i & 0xffff) / 65536.0f;
+
+ // sign = i[31]
+ if ((i >> 31) & 1)
+ f = -f;
+
+ return f;
+}
diff --git a/lib/src/util/vector.c b/lib/src/util/vector.c
new file mode 100644
index 000000000..1f5cd0269
--- /dev/null
+++ b/lib/src/util/vector.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file vector.c
+ * \brief Implementation of resizeable static array
+ */
+
+#include <assert.h>
+#include <stddef.h> // size_t
+#include <stdlib.h> // calloc
+#include <stdio.h>
+
+#include <hicn/util/vector.h>
+
+#define DEFAULT_VECTOR_SIZE 64
+
+int
+_vector_init (void **vector_ptr, size_t elt_size, size_t init_size,
+ size_t max_size)
+{
+ assert (vector_ptr);
+ assert (max_size == 0 || init_size < max_size);
+
+ if (init_size == 0)
+ init_size = DEFAULT_VECTOR_SIZE;
+
+ *vector_ptr = NULL;
+ int rc = _vector_resize (vector_ptr, elt_size, init_size);
+ if (rc < 0)
+ return -1;
+
+ vector_hdr_t *vh = vector_hdr (*vector_ptr);
+ vh->cur_size = 0;
+ vh->max_size = max_size;
+
+ return 0;
+}
+
+void
+_vector_free (void **vector_ptr)
+{
+ free (vector_hdr (*vector_ptr));
+ *vector_ptr = NULL;
+}
+
+int
+_vector_resize (void **vector_ptr, size_t elt_size, off_t pos)
+{
+ vector_hdr_t *vh;
+ size_t old_size;
+
+ if (*vector_ptr)
+ {
+ vh = vector_hdr (*vector_ptr);
+ old_size = vh->alloc_size;
+ }
+ else
+ {
+ vh = NULL;
+ old_size = 0;
+ }
+
+ /* Round the allocated size to the next power of 2 of the requested position
+ */
+ size_t new_size = next_pow2 (pos);
+
+ /* Don't grow the vector back */
+ if (new_size < old_size)
+ return 0;
+
+ /* Don't exceed maximum size (for init, check is done beforehand) */
+ if (vh && vh->max_size && new_size > vh->max_size)
+ return -1;
+
+ vh = realloc (vh, VECTOR_HDRLEN + new_size * elt_size);
+ if (!vh)
+ return -1;
+ vh->alloc_size = new_size;
+
+ /* Zero out the newly allocated memory (except headers) */
+ memset ((uint8_t *) vh + VECTOR_HDRLEN + old_size * elt_size, 0,
+ (new_size - old_size) * elt_size);
+
+ /* Reassign vector pointer */
+ *vector_ptr = (uint8_t *) vh + VECTOR_HDRLEN;
+
+ return 0;
+}
diff --git a/lib/src/util/windows/dlfcn.c b/lib/src/util/windows/dlfcn.c
index c8173cdb0..5606074a0 100644
--- a/lib/src/util/windows/dlfcn.c
+++ b/lib/src/util/windows/dlfcn.c
@@ -1,4 +1,4 @@
-/* dlfcn.c */
+/* dlfcn.c */
#include <inttypes.h>
#include <stdio.h>
@@ -6,60 +6,68 @@
#include <string.h>
#include <windows.h>
-static struct {
- long lasterror;
- const char *err_rutin;
-} var = {
- 0,
- NULL
-};
+static struct
+{
+ long lasterror;
+ const char *err_rutin;
+} var = { 0, NULL };
-void *dlopen (const char *filename, int flags)
+void *
+dlopen (const char *filename, int flags)
{
- HINSTANCE hInst;
+ HINSTANCE hInst;
- hInst= LoadLibrary (filename);
- if (hInst==NULL) {
- var.lasterror = GetLastError ();
- var.err_rutin = "dlopen";
+ hInst = LoadLibrary (filename);
+ if (hInst == NULL)
+ {
+ var.lasterror = GetLastError ();
+ var.err_rutin = "dlopen";
}
- return hInst;
+ return hInst;
}
-int dlclose (void *handle)
+int
+dlclose (void *handle)
{
- BOOL ok;
- int rc= 0;
+ BOOL ok;
+ int rc = 0;
- ok= FreeLibrary ((HINSTANCE)handle);
- if (! ok) {
- var.lasterror = GetLastError ();
- var.err_rutin = "dlclose";
- rc= -1;
+ ok = FreeLibrary ((HINSTANCE) handle);
+ if (!ok)
+ {
+ var.lasterror = GetLastError ();
+ var.err_rutin = "dlclose";
+ rc = -1;
}
- return rc;
+ return rc;
}
-void *dlsym (void *handle, const char *name)
+void *
+dlsym (void *handle, const char *name)
{
- FARPROC fp;
+ FARPROC fp;
- fp= GetProcAddress ((HINSTANCE)handle, name);
- if (!fp) {
- var.lasterror = GetLastError ();
- var.err_rutin = "dlsym";
+ fp = GetProcAddress ((HINSTANCE) handle, name);
+ if (!fp)
+ {
+ var.lasterror = GetLastError ();
+ var.err_rutin = "dlsym";
}
- return (void *)(intptr_t)fp;
+ return (void *) (intptr_t) fp;
}
-const char *dlerror (void)
+const char *
+dlerror (void)
{
-static char errstr [88];
+ static char errstr[88];
- if (var.lasterror) {
- sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror);
- return errstr;
- } else {
- return NULL;
+ if (var.lasterror)
+ {
+ snprintf (errstr, 88, "%s error #%ld", var.err_rutin, var.lasterror);
+ return errstr;
+ }
+ else
+ {
+ return NULL;
}
} \ No newline at end of file