Clean-up: simplify the implementation of dynamic buffer set_capacity
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 10:59:18 +0000 (06:59 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 13:19:12 +0000 (09:19 -0400)
Only use realloc() to implement set_capacity's logic. In the case
where buf is NULL, realloc acts like malloc() anyhow.

Moreover, the memory does not need to be zeroed on allocation since
size increases provide this guarantee.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/dynamic-buffer.c

index c295a5d2eb378f58b557fdfa6c841de6667db810..9ed16514fe64fa84dc735f15de862ca662a04fef 100644 (file)
@@ -136,48 +136,33 @@ end:
 }
 
 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
-               size_t new_capacity)
+               size_t demanded_capacity)
 {
        int ret = 0;
-       size_t rounded_capacity = round_to_power_of_2(new_capacity);
+       void *new_buf;
+       size_t new_capacity = round_to_power_of_2(demanded_capacity);
 
-       if (!buffer || new_capacity < buffer->size) {
+       if (!buffer || demanded_capacity < buffer->size) {
+               /*
+                * Shrinking a buffer's size by changing its capacity is
+                * unsupported.
+                */
                ret = -1;
                goto end;
        }
 
-       if (rounded_capacity == buffer->capacity) {
+       if (new_capacity == buffer->capacity) {
                goto end;
        }
 
-       if (!buffer->data) {
-               buffer->data = zmalloc(rounded_capacity);
-               if (!buffer->data) {
-                       ret = -1;
-                       goto end;
-               }
-       } else {
-               void *new_buf;
-
-               new_buf = realloc(buffer->data, rounded_capacity);
-               if (new_buf) {
-                       if (rounded_capacity > buffer->capacity) {
-                               memset(new_buf + buffer->capacity, 0,
-                                               rounded_capacity - buffer->capacity);
-                       }
-               } else {
-                       /* Realloc failed, try to acquire a new block. */
-                       new_buf = zmalloc(rounded_capacity);
-                       if (!new_buf) {
-                               ret = -1;
-                               goto end;
-                       }
-                       memcpy(new_buf, buffer->data, buffer->size);
-                       free(buffer->data);
-               }
-               buffer->data = new_buf;
+       /* Memory is initialized by the size increases. */
+       new_buf = realloc(buffer->data, new_capacity);
+       if (!new_buf) {
+               ret = -1;
+               goto end;
        }
-       buffer->capacity = rounded_capacity;
+       buffer->data = new_buf;
+       buffer->capacity = new_capacity;
 end:
        return ret;
 }
This page took 0.027478 seconds and 5 git commands to generate.