]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/kmem.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / kmem.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
5e656dbb
BN
2
3
9c799827 4#include "libxfs_priv.h"
5e656dbb
BN
5
6/*
7 * Simple memory interface
8 */
9
10kmem_zone_t *
11kmem_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"),
f8149110 17 progname, name, (int)sizeof(kmem_zone_t),
5e656dbb
BN
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
44488491
ES
27int
28kmem_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
5e656dbb
BN
41void *
42kmem_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}
55void *
56kmem_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
65void *
66kmem_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
78void *
79kmem_zalloc(size_t size, int flags)
80{
81 void *ptr = kmem_alloc(size, flags);
82
83 memset(ptr, 0, size);
84 return ptr;
85}
86
87void *
408c66dd 88kmem_realloc(void *ptr, size_t new_size, int flags)
5e656dbb
BN
89{
90 ptr = realloc(ptr, new_size);
91 if (ptr == NULL) {
92 fprintf(stderr, _("%s: realloc failed (%d bytes): %s\n"),
93 progname, (int)new_size, strerror(errno));
94 exit(1);
95 }
96 return ptr;
97}