"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/libglusterfs/src/strfd.c" (16 Sep 2020, 2128 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "strfd.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2    Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com>
    3    This file is part of GlusterFS.
    4 
    5    This file is licensed to you under your choice of the GNU Lesser
    6    General Public License, version 3 or any later version (LGPLv3 or
    7    later), or the GNU General Public License, version 2 (GPLv2), in all
    8    cases as published by the Free Software Foundation.
    9 */
   10 
   11 #include <stdarg.h>
   12 
   13 #include "glusterfs/mem-types.h"
   14 #include "glusterfs/mem-pool.h"
   15 #include "glusterfs/strfd.h"
   16 #include "glusterfs/common-utils.h"
   17 
   18 strfd_t *
   19 strfd_open()
   20 {
   21     strfd_t *strfd = NULL;
   22 
   23     strfd = GF_CALLOC(1, sizeof(*strfd), gf_common_mt_strfd_t);
   24 
   25     return strfd;
   26 }
   27 
   28 int
   29 strvprintf(strfd_t *strfd, const char *fmt, va_list ap)
   30 {
   31     char *str = NULL;
   32     int size = 0;
   33 
   34     size = vasprintf(&str, fmt, ap);
   35 
   36     if (size < 0)
   37         return size;
   38 
   39     if (!strfd->alloc_size) {
   40         strfd->data = GF_CALLOC(max(size + 1, 4096), 1,
   41                                 gf_common_mt_strfd_data_t);
   42         if (!strfd->data) {
   43             free(str); /* NOT GF_FREE */
   44             return -1;
   45         }
   46         strfd->alloc_size = max(size + 1, 4096);
   47     }
   48 
   49     if (strfd->alloc_size <= (strfd->size + size)) {
   50         char *tmp_ptr = NULL;
   51         int new_size = max(
   52             (strfd->alloc_size * 2),
   53             gf_roundup_next_power_of_two(strfd->size + size + 1));
   54         tmp_ptr = GF_REALLOC(strfd->data, new_size);
   55         if (!tmp_ptr) {
   56             free(str); /* NOT GF_FREE */
   57             return -1;
   58         }
   59         strfd->alloc_size = new_size;
   60         strfd->data = tmp_ptr;
   61     }
   62 
   63     /* Copy the trailing '\0', but do not account for it in ->size.
   64        This allows safe use of strfd->data as a string. */
   65     memcpy(strfd->data + strfd->size, str, size + 1);
   66     strfd->size += size;
   67 
   68     free(str); /* NOT GF_FREE */
   69 
   70     return size;
   71 }
   72 
   73 int
   74 strprintf(strfd_t *strfd, const char *fmt, ...)
   75 {
   76     int ret = 0;
   77     va_list ap;
   78 
   79     va_start(ap, fmt);
   80     ret = strvprintf(strfd, fmt, ap);
   81     va_end(ap);
   82 
   83     return ret;
   84 }
   85 
   86 int
   87 strfd_close(strfd_t *strfd)
   88 {
   89     GF_FREE(strfd->data);
   90     GF_FREE(strfd);
   91 
   92     return 0;
   93 }