glusterfs  8.2
About: GlusterFS is a network/cluster filesystem. The storage server (or each in a cluster) runs glusterfsd and the clients use mount command or glusterfs client to mount the exported filesystem. Release series 8.x (latest version).
  Fossies Dox: glusterfs-8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

strfd.c
Go to the documentation of this file.
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 *
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,
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
88 {
89  GF_FREE(strfd->data);
90  GF_FREE(strfd);
91 
92  return 0;
93 }
gf_common_mt_strfd_data_t
@ gf_common_mt_strfd_data_t
Definition: mem-types.h:96
strfd_t::size
size_t size
Definition: strfd.h:17
mem-pool.h
strfd_t::alloc_size
size_t alloc_size
Definition: strfd.h:16
GF_FREE
#define GF_FREE(free_ptr)
Definition: mem-pool.h:159
strfd_t::data
void * data
Definition: strfd.h:15
gf_roundup_next_power_of_two
int32_t gf_roundup_next_power_of_two(int32_t nr)
Definition: common-utils.c:3039
strvprintf
int strvprintf(strfd_t *strfd, const char *fmt, va_list ap)
Definition: strfd.c:29
max
#define max(a, b)
Definition: common-utils.h:58
strfd_close
int strfd_close(strfd_t *strfd)
Definition: strfd.c:87
GF_REALLOC
#define GF_REALLOC(ptr, size)
Definition: mem-pool.h:157
strfd_open
strfd_t * strfd_open()
Definition: strfd.c:19
gf_common_mt_strfd_t
@ gf_common_mt_strfd_t
Definition: mem-types.h:95
GF_CALLOC
#define GF_CALLOC(nmemb, size, type)
Definition: mem-pool.h:153
common-utils.h
strprintf
int strprintf(strfd_t *strfd, const char *fmt,...)
Definition: strfd.c:74
strfd.h
strfd_t
Definition: strfd.h:14
mem-types.h