]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/kmem.c
xfs: allocate xattr buffer on demand
[thirdparty/xfsprogs-dev.git] / libxfs / kmem.c
1 // SPDX-License-Identifier: GPL-2.0
2
3
4 #include "libxfs_priv.h"
5
6 /*
7 * Simple memory interface
8 */
9
10 kmem_zone_t *
11 kmem_zone_init(int size, char *name)
12 {
13 kmem_zone_t *ptr = malloc(sizeof(kmem_zone_t));
14
15 if (ptr == NULL) {
16 fprintf(stderr, _("%s: zone init failed (%s, %d bytes): %s\n"),
17 progname, name, (int)sizeof(kmem_zone_t),
18 strerror(errno));
19 exit(1);
20 }
21 ptr->zone_unitsize = size;
22 ptr->zone_name = name;
23 ptr->allocated = 0;
24 return ptr;
25 }
26
27 int
28 kmem_zone_destroy(kmem_zone_t *zone)
29 {
30 int leaked = 0;
31
32 if (getenv("LIBXFS_LEAK_CHECK") && zone->allocated) {
33 leaked = 1;
34 fprintf(stderr, "zone %s freed with %d items allocated\n",
35 zone->zone_name, zone->allocated);
36 }
37 free(zone);
38 return leaked;
39 }
40
41 void *
42 kmem_zone_alloc(kmem_zone_t *zone, int flags)
43 {
44 void *ptr = malloc(zone->zone_unitsize);
45
46 if (ptr == NULL) {
47 fprintf(stderr, _("%s: zone alloc failed (%s, %d bytes): %s\n"),
48 progname, zone->zone_name, zone->zone_unitsize,
49 strerror(errno));
50 exit(1);
51 }
52 zone->allocated++;
53 return ptr;
54 }
55 void *
56 kmem_zone_zalloc(kmem_zone_t *zone, int flags)
57 {
58 void *ptr = kmem_zone_alloc(zone, flags);
59
60 memset(ptr, 0, zone->zone_unitsize);
61 return ptr;
62 }
63
64
65 void *
66 kmem_alloc(size_t size, int flags)
67 {
68 void *ptr = malloc(size);
69
70 if (ptr == NULL) {
71 fprintf(stderr, _("%s: malloc failed (%d bytes): %s\n"),
72 progname, (int)size, strerror(errno));
73 exit(1);
74 }
75 return ptr;
76 }
77
78 void *
79 kmem_alloc_large(size_t size, int flags)
80 {
81 return kmem_alloc(size, flags);
82 }
83
84 void *
85 kmem_zalloc(size_t size, int flags)
86 {
87 void *ptr = kmem_alloc(size, flags);
88
89 memset(ptr, 0, size);
90 return ptr;
91 }
92
93 void *
94 kmem_realloc(void *ptr, size_t new_size, int flags)
95 {
96 ptr = realloc(ptr, new_size);
97 if (ptr == NULL) {
98 fprintf(stderr, _("%s: realloc failed (%d bytes): %s\n"),
99 progname, (int)new_size, strerror(errno));
100 exit(1);
101 }
102 return ptr;
103 }