Don't allocate a send buffer by default, since an application might only
want to receive data. Also try to shrink the send and receive buffers on
channel close, or even free them if the buffers are empty.
}
static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
}
static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
+ assert(!buf->external);
+
+ if(!newsize) {
+ free(buf->data);
+ buf->data = NULL;
+ buf->size = 0;
+ buf->offset = 0;
+ return true;
+ }
+
char *newdata = realloc(buf->data, newsize);
if(!newdata) {
char *newdata = realloc(buf->data, newsize);
if(!newdata) {
buf->external = true;
} else if(buf->external) {
// Transition from external to internal buf
buf->external = true;
} else if(buf->external) {
// Transition from external to internal buf
- size_t minsize = buf->used < DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used;
- data = malloc(minsize);
+ size_t minsize = buf->used <= DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used;
- if(!data) {
- // Cannot handle this
- abort();
+ if(minsize) {
+ data = malloc(minsize);
+
+ if(!data) {
+ // Cannot handle this
+ abort();
+ }
+
+ buffer_transfer(buf, data, minsize);
+ buf->data = data;
+ } else {
+ buf->data = NULL;
+ buf->size = 0;
- buffer_transfer(buf, data, minsize);
- buf->data = data;
+ } else {
+ // Realloc internal storage
+ size_t minsize = buf->used <= DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used;
+
+ if(minsize) {
+ data = realloc(buf->data, minsize);
+
+ if(data) {
+ buf->data = data;
+ buf->size = minsize;
+ }
+ } else {
+ free(buf->data);
+ buf->data = NULL;
+ buf->size = 0;
+ }
}
static void set_reapable(struct utcp_connection *c) {
}
static void set_reapable(struct utcp_connection *c) {
- if(c->sndbuf.external) {
- set_buffer_storage(&c->sndbuf, NULL, DEFAULT_MTU);
- }
-
- if(c->rcvbuf.external) {
- set_buffer_storage(&c->rcvbuf, NULL, DEFAULT_MTU);
- }
+ set_buffer_storage(&c->sndbuf, NULL, DEFAULT_MTU);
+ set_buffer_storage(&c->rcvbuf, NULL, DEFAULT_MTU);
c->recv = NULL;
c->poll = NULL;
c->recv = NULL;
c->poll = NULL;
#define AUX_TIMESTAMP 4
#define NSACKS 4
#define AUX_TIMESTAMP 4
#define NSACKS 4
-#define DEFAULT_SNDBUFSIZE 4096
+#define DEFAULT_SNDBUFSIZE 0
#define DEFAULT_MAXSNDBUFSIZE 131072
#define DEFAULT_RCVBUFSIZE 0
#define DEFAULT_MAXRCVBUFSIZE 131072
#define DEFAULT_MAXSNDBUFSIZE 131072
#define DEFAULT_RCVBUFSIZE 0
#define DEFAULT_MAXRCVBUFSIZE 131072