diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-09-04 19:25:42 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-09-04 19:25:42 +0200 |
commit | d31889bc59313fc68df025d8073ee4faa5bc7c39 (patch) | |
tree | 2979676a49c37f54fd6512167c6f9dbb9994d731 /src/libmdsclient/inbound.c | |
parent | implement message spool and pool (diff) | |
download | mds-d31889bc59313fc68df025d8073ee4faa5bc7c39.tar.gz mds-d31889bc59313fc68df025d8073ee4faa5bc7c39.tar.bz2 mds-d31889bc59313fc68df025d8073ee4faa5bc7c39.tar.xz |
implement use of pool
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsclient/inbound.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/libmdsclient/inbound.c b/src/libmdsclient/inbound.c index cb151a6..58251fd 100644 --- a/src/libmdsclient/inbound.c +++ b/src/libmdsclient/inbound.c @@ -80,22 +80,32 @@ void libmds_message_destroy(libmds_message_t* restrict this) * be done even if initialisation fails * * @param this The message + * @param pool Message allocation pool, may be `NULL` * @return The duplicate, you do not need to call `libmds_message_destroy` * on it before you call `free` on it. However, you cannot use * this is an `libmds_message_t` array (libmds_message_t*), only * in an `libmds_message_t*` array (libmds_message_t**). */ -libmds_message_t* libmds_message_duplicate(libmds_message_t* restrict this) +libmds_message_t* libmds_message_duplicate(libmds_message_t* restrict this, libmds_mpool_t* restrict pool) { - size_t flattened_size, i, n = this->header_count; + size_t flattened_size, reused, i, n = this->header_count; libmds_message_t* rc; flattened_size = sizeof(libmds_message_t) + this->buffer_off * sizeof(char) + n * sizeof(void*); - if (rc = malloc(flattened_size), rc == NULL) + repoll: + reused = 0; + rc = pool == NULL ? NULL : libmds_mpool_poll(pool); + if (rc != NULL) + if ((reused = rc->flattened) < flattened_size) + { + free(rc); + goto repoll; + } + if ((rc == NULL) && (rc = malloc(flattened_size), rc == NULL)) return NULL; *rc = *this; - rc->flattened = flattened_size; + rc->flattened = reused ? reused : flattened_size; rc->buffer_size = this->buffer_off; rc->buffer = ((char*)rc) + sizeof(libmds_message_t) / sizeof(char); |