Skip to content

Commit 6507744

Browse files
author
Seppo Takalo
committed
Move string allocation to a point where get_ip_address() is called
This saves RAM/stack on typical socket usage where human readable format is not exchanged so often.
1 parent 26a7e44 commit 6507744

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

features/netsocket/SocketAddress.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,36 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes)
106106

107107
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
108108
{
109-
_ip_address[0] = '\0';
109+
_ip_address = NULL;
110110
set_addr(addr);
111111
set_port(port);
112112
}
113113

114114
SocketAddress::SocketAddress(const char *addr, uint16_t port)
115115
{
116-
_ip_address[0] = '\0';
116+
_ip_address = NULL;
117117
set_ip_address(addr);
118118
set_port(port);
119119
}
120120

121121
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
122122
{
123-
_ip_address[0] = '\0';
123+
_ip_address = NULL;
124124
set_ip_bytes(bytes, version);
125125
set_port(port);
126126
}
127127

128128
SocketAddress::SocketAddress(const SocketAddress &addr)
129129
{
130-
_ip_address[0] = '\0';
130+
_ip_address = NULL;
131131
set_addr(addr.get_addr());
132132
set_port(addr.get_port());
133133
}
134134

135135
bool SocketAddress::set_ip_address(const char *addr)
136136
{
137-
_ip_address[0] = '\0';
137+
delete[] _ip_address;
138+
_ip_address = NULL;
138139

139140
if (addr && ipv4_is_valid(addr)) {
140141
_addr.version = NSAPI_IPv4;
@@ -166,7 +167,8 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
166167

167168
void SocketAddress::set_addr(nsapi_addr_t addr)
168169
{
169-
_ip_address[0] = '\0';
170+
delete[] _ip_address;
171+
_ip_address = NULL;
170172
_addr = addr;
171173
}
172174

@@ -181,7 +183,8 @@ const char *SocketAddress::get_ip_address() const
181183
return NULL;
182184
}
183185

184-
if (!_ip_address[0]) {
186+
if (!_ip_address) {
187+
_ip_address = new char[NSAPI_IP_SIZE];
185188
if (_addr.version == NSAPI_IPv4) {
186189
ipv4_to_address(_ip_address, _addr.bytes);
187190
} else if (_addr.version == NSAPI_IPv6) {
@@ -235,6 +238,14 @@ SocketAddress::operator bool() const
235238
}
236239
}
237240

241+
SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
242+
{
243+
delete[] _ip_address;
244+
_ip_address = NULL;
245+
set_addr(addr.get_addr());
246+
set_port(addr.get_port());
247+
}
248+
238249
bool operator==(const SocketAddress &a, const SocketAddress &b)
239250
{
240251
if (!a && !b) {
@@ -257,7 +268,7 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
257268

258269
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
259270
{
260-
_ip_address[0] = '\0';
271+
_ip_address = NULL;
261272

262273
// gethostbyname must check for literals, so can call it directly
263274
int err = iface->gethostbyname(host, this);
@@ -267,3 +278,8 @@ void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16
267278
_port = 0;
268279
}
269280
}
281+
282+
SocketAddress::~SocketAddress()
283+
{
284+
delete[] _ip_address;
285+
}

features/netsocket/SocketAddress.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class NetworkInterface;
2828

2929
/** SocketAddress class
3030
*
31-
* Representation of an IP address and port pair.
31+
* Representation of an IP address and port pair.
3232
* @addtogroup netsocket
3333
*/
3434
class SocketAddress {
@@ -83,7 +83,10 @@ class SocketAddress {
8383
* @param addr SocketAddress to copy
8484
*/
8585
SocketAddress(const SocketAddress &addr);
86-
86+
87+
/** Destructor */
88+
~SocketAddress();
89+
8790
/** Set the IP address
8891
*
8992
* @param addr Null-terminated represention of the IP address
@@ -110,8 +113,11 @@ class SocketAddress {
110113
* @param port 16-bit port
111114
*/
112115
void set_port(uint16_t port);
113-
114-
/** Get the IP address
116+
117+
/** Get the human readable IP address
118+
*
119+
* Allocates memory for a string and converts binary address to
120+
* human readable format. String is freed in the destructor.
115121
*
116122
* @return Null-terminated representation of the IP Address
117123
*/
@@ -134,7 +140,7 @@ class SocketAddress {
134140
* @return Raw IP address
135141
*/
136142
nsapi_addr_t get_addr() const;
137-
143+
138144
/** Get the port
139145
*
140146
* @return The 16-bit port
@@ -147,6 +153,12 @@ class SocketAddress {
147153
*/
148154
operator bool() const;
149155

156+
/** Copy addres from another SocketAddress
157+
*
158+
* @param addr SocketAddress to copy
159+
*/
160+
SocketAddress &operator=(const SocketAddress &addr);
161+
150162
/** Compare two addresses for equality
151163
*
152164
* @return True if both addresses are equal
@@ -162,7 +174,7 @@ class SocketAddress {
162174
private:
163175
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
164176

165-
mutable char _ip_address[NSAPI_IP_SIZE];
177+
mutable char *_ip_address;
166178
nsapi_addr_t _addr;
167179
uint16_t _port;
168180
};

0 commit comments

Comments
 (0)