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