aboutsummaryrefslogtreecommitdiff
path: root/ubifs-utils/common/kmem.c
blob: e926a137a1bf1f096290a77945713027f0e01047 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Simple memory interface
 */

#include "compiler_attributes.h"
#include "linux_types.h"
#include "kmem.h"
#include "defs.h"

static void *kmem_alloc(size_t size)
{
	void *ptr = malloc(size);

	if (ptr == NULL)
		sys_errmsg("malloc failed (%d bytes)", (int)size);
	return ptr;
}

static void *kmem_zalloc(size_t size)
{
	void *ptr = kmem_alloc(size);

	if (!ptr)
		return ptr;

	memset(ptr, 0, size);
	return ptr;
}

void *kmalloc(size_t size, gfp_t flags)
{
	if (flags & __GFP_ZERO)
		return kmem_zalloc(size);
	return kmem_alloc(size);
}

void *krealloc(void *ptr, size_t new_size, __unused gfp_t flags)
{
	ptr = realloc(ptr, new_size);
	if (ptr == NULL)
		sys_errmsg("realloc failed (%d bytes)", (int)new_size);
	return ptr;
}

void *kmalloc_array(size_t n, size_t size, gfp_t flags)
{
	size_t bytes;

	if (unlikely(check_mul_overflow(n, size, &bytes)))
		return NULL;
	return kmalloc(bytes, flags);
}

void *kmemdup(const void *src, size_t len, gfp_t gfp)
{
	void *p;

	p = kmalloc(len, gfp);
	if (p)
		memcpy(p, src, len);

	return p;
}