]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/libpmem2/pmem2_utils.c
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / libpmem2 / pmem2_utils.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2019-2020, Intel Corporation */
3
4 /*
5 * pmem2_utils.c -- libpmem2 utilities functions
6 */
7
8 #include <errno.h>
9 #include "alloc.h"
10 #include "libpmem2.h"
11 #include "out.h"
12 #include "pmem2_utils.h"
13 #include "util.h"
14
15 /*
16 * pmem2_malloc -- allocate a buffer and handle an error
17 */
18 void *
19 pmem2_malloc(size_t size, int *err)
20 {
21 void *ptr = Malloc(size);
22 *err = 0;
23
24 if (ptr == NULL) {
25 ERR("!malloc(%zu)", size);
26 *err = PMEM2_E_ERRNO;
27 }
28
29 return ptr;
30 }
31
32 /*
33 * pmem2_zalloc -- allocate a buffer, zero it and handle an error
34 */
35 void *
36 pmem2_zalloc(size_t size, int *err)
37 {
38 void *ptr = Zalloc(size);
39 *err = 0;
40
41 if (ptr == NULL) {
42 ERR("!malloc(%zu)", size);
43 *err = PMEM2_E_ERRNO;
44 }
45
46 return ptr;
47 }
48
49 /*
50 * pmem2_realloc -- reallocate a buffer and handle an error
51 */
52 void *
53 pmem2_realloc(void *ptr, size_t size, int *err)
54 {
55 void *newptr = Realloc(ptr, size);
56 *err = 0;
57
58 if (newptr == NULL) {
59 ERR("!realloc(%zu)", size);
60 *err = PMEM2_E_ERRNO;
61 }
62
63 return newptr;
64 }
65
66 int
67 pmem2_err_to_errno(int err)
68 {
69 if (err > 0)
70 FATAL("positive error code is a bug in libpmem2");
71
72 if (err == PMEM2_E_NOSUPP)
73 return ENOTSUP;
74
75 if (err <= PMEM2_E_UNKNOWN)
76 return EINVAL;
77
78 return -err;
79 }
80
81 #ifdef _WIN32
82 /*
83 * converts windows error codes to pmem2 error
84 */
85 int
86 pmem2_lasterror_to_err()
87 {
88 int err = util_lasterror_to_errno(GetLastError());
89
90 if (err == -1)
91 return PMEM2_E_UNKNOWN;
92
93 return -err;
94 }
95 #endif