File: kmem.c

package info (click to toggle)
xfsprogs 6.17.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 11,324 kB
  • sloc: ansic: 167,334; sh: 4,604; makefile: 1,336; python: 835; cpp: 5
file content (119 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-License-Identifier: GPL-2.0


#include "libxfs_priv.h"

/*
 * Simple memory interface
 */
struct kmem_cache *
kmem_cache_create(const char *name, unsigned int size, unsigned int align,
		unsigned int slab_flags, void (*ctor)(void *))
{
	struct kmem_cache	*ptr = malloc(sizeof(struct kmem_cache));

	if (ptr == NULL) {
		fprintf(stderr, _("%s: cache init failed (%s, %d bytes): %s\n"),
			progname, name, (int)sizeof(struct kmem_cache),
			strerror(errno));
		exit(1);
	}
	ptr->cache_unitsize = size;
	ptr->cache_name = name;
	ptr->allocated = 0;
	ptr->align = align;
	ptr->ctor = ctor;

	return ptr;
}

int
kmem_cache_destroy(struct kmem_cache *cache)
{
	int	leaked = 0;

	if (getenv("LIBXFS_LEAK_CHECK") && cache->allocated) {
		leaked = 1;
		fprintf(stderr, "cache %s freed with %d items allocated\n",
				cache->cache_name, cache->allocated);
	}
	free(cache);
	return leaked;
}

void *
kmem_cache_alloc(struct kmem_cache *cache, gfp_t flags)
{
	void	*ptr = malloc(cache->cache_unitsize);

	if (ptr == NULL) {
		fprintf(stderr, _("%s: cache alloc failed (%s, %d bytes): %s\n"),
			progname, cache->cache_name, cache->cache_unitsize,
			strerror(errno));
		exit(1);
	}
	cache->allocated++;
	return ptr;
}

void *
kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
{
	void	*ptr = kmem_cache_alloc(cache, flags);

	memset(ptr, 0, cache->cache_unitsize);
	return ptr;
}

void *
kvmalloc(size_t size, gfp_t flags)
{
	void	*ptr;

	if (flags & __GFP_ZERO)
		ptr = calloc(1, size);
	else
		ptr = malloc(size);

	if (ptr == NULL) {
		fprintf(stderr, _("%s: malloc failed (%d bytes): %s\n"),
			progname, (int)size, strerror(errno));
		exit(1);
	}
	return ptr;
}

void *
krealloc(void *ptr, size_t new_size, int flags)
{
	/*
	 * If @new_size is zero, Linux krealloc will free the memory and return
	 * NULL, so force that behavior here.  The return value of realloc with
	 * a zero size is implementation dependent, so we cannot use that.
	 */
	if (!new_size) {
		free(ptr);
		return NULL;
	}

	ptr = realloc(ptr, new_size);
	if (ptr == NULL) {
		fprintf(stderr, _("%s: realloc failed (%d bytes): %s\n"),
			progname, (int)new_size, strerror(errno));
		exit(1);
	}
	return ptr;
}

char *kasprintf(gfp_t gfp, const char *fmt, ...)
{
	va_list ap;
	char *p;

	va_start(ap, fmt);
	if (vasprintf(&p, fmt, ap) < 0)
		p = NULL;
	va_end(ap);

	return p;
}