summaryrefslogtreecommitdiffstats
path: root/utils.c
blob: 9e71a736b011c64d56070e48efa47fec1d5fd2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
  This file is part of gluster-block.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/


# include "utils.h"



int
gbAlloc(void *ptrptr, size_t size,
        const char *filename, const char *funcname, size_t linenr)
{
  *(void **)ptrptr = calloc(1, size);
  if (*(void **)ptrptr == NULL) {
    ERROR("%s", "Error: memory allocation failed");
    errno = ENOMEM;
    return -1;
  }
  return 0;
}


int
gbAllocN(void *ptrptr, size_t size, size_t count,
         const char *filename, const char *funcname, size_t linenr)
{
  *(void**)ptrptr = calloc(count, size);
  if (*(void**)ptrptr == NULL) {
    ERROR("%s", "Error: memory allocation failed");
    errno = ENOMEM;
    return -1;
  }
  return 0;
}


void
gbFree(void *ptrptr)
{
  int save_errno = errno;

  free(*(void**)ptrptr);
  *(void**)ptrptr = NULL;
  errno = save_errno;
}


int
gbStrdup(char **dest, const char *src,
         const char *filename, const char *funcname, size_t linenr)
{
    *dest = NULL;
    if (!src)
        return 0;
    if (!(*dest = strdup(src))) {
      ERROR("%s", "Error: string duplication failed");
        return -1;
    }

    return 0;
}