]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/kmem.c
42d813088d6ada92842f5ce0fd3c6a5fb9533cd9
1 // SPDX-License-Identifier: GPL-2.0
4 #include "libxfs_priv.h"
7 * Simple memory interface
10 kmem_cache_create(const char *name
, unsigned int size
, unsigned int align
,
11 unsigned int slab_flags
, void (*ctor
)(void *))
13 struct kmem_cache
*ptr
= malloc(sizeof(struct kmem_cache
));
16 fprintf(stderr
, _("%s: cache init failed (%s, %d bytes): %s\n"),
17 progname
, name
, (int)sizeof(struct kmem_cache
),
21 ptr
->cache_unitsize
= size
;
22 ptr
->cache_name
= name
;
31 kmem_cache_destroy(struct kmem_cache
*cache
)
35 if (getenv("LIBXFS_LEAK_CHECK") && cache
->allocated
) {
37 fprintf(stderr
, "cache %s freed with %d items allocated\n",
38 cache
->cache_name
, cache
->allocated
);
45 kmem_cache_alloc(struct kmem_cache
*cache
, gfp_t flags
)
47 void *ptr
= malloc(cache
->cache_unitsize
);
50 fprintf(stderr
, _("%s: cache alloc failed (%s, %d bytes): %s\n"),
51 progname
, cache
->cache_name
, cache
->cache_unitsize
,
60 kmem_cache_zalloc(struct kmem_cache
*cache
, gfp_t flags
)
62 void *ptr
= kmem_cache_alloc(cache
, flags
);
64 memset(ptr
, 0, cache
->cache_unitsize
);
69 kmem_alloc(size_t size
, int flags
)
71 void *ptr
= malloc(size
);
74 fprintf(stderr
, _("%s: malloc failed (%d bytes): %s\n"),
75 progname
, (int)size
, strerror(errno
));
82 kvmalloc(size_t size
, gfp_t flags
)
84 if (flags
& __GFP_ZERO
)
85 return kmem_zalloc(size
, 0);
86 return kmem_alloc(size
, 0);
90 kmem_zalloc(size_t size
, int flags
)
92 void *ptr
= kmem_alloc(size
, flags
);
99 krealloc(void *ptr
, size_t new_size
, int flags
)
101 ptr
= realloc(ptr
, new_size
);
103 fprintf(stderr
, _("%s: realloc failed (%d bytes): %s\n"),
104 progname
, (int)new_size
, strerror(errno
));