Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Simple memory interface
4 : */
5 :
6 : #include "compiler_attributes.h"
7 : #include "linux_types.h"
8 : #include "kmem.h"
9 : #include "defs.h"
10 :
11 19745037692 : static void *kmem_alloc(size_t size)
12 : {
13 19745037692 : void *ptr = malloc(size);
14 :
15 19745037692 : if (ptr == NULL)
16 0 : sys_errmsg("malloc failed (%d bytes)", (int)size);
17 19745037692 : return ptr;
18 : }
19 :
20 805612367 : static void *kmem_zalloc(size_t size)
21 : {
22 805612367 : void *ptr = kmem_alloc(size);
23 :
24 805612367 : if (!ptr)
25 : return ptr;
26 :
27 805612367 : memset(ptr, 0, size);
28 805612367 : return ptr;
29 : }
30 :
31 19745037692 : void *kmalloc(size_t size, gfp_t flags)
32 : {
33 19745037692 : if (flags & __GFP_ZERO)
34 805612367 : return kmem_zalloc(size);
35 18939425325 : return kmem_alloc(size);
36 : }
37 :
38 0 : void *krealloc(void *ptr, size_t new_size, __unused gfp_t flags)
39 : {
40 0 : ptr = realloc(ptr, new_size);
41 0 : if (ptr == NULL)
42 0 : sys_errmsg("realloc failed (%d bytes)", (int)new_size);
43 0 : return ptr;
44 : }
45 :
46 19745 : void *kmalloc_array(size_t n, size_t size, gfp_t flags)
47 : {
48 : size_t bytes;
49 :
50 19745 : if (unlikely(check_mul_overflow(n, size, &bytes)))
51 : return NULL;
52 19745 : return kmalloc(bytes, flags);
53 : }
54 :
55 7472 : void *kmemdup(const void *src, size_t len, gfp_t gfp)
56 : {
57 : void *p;
58 :
59 7472 : p = kmalloc(len, gfp);
60 7472 : if (p)
61 7472 : memcpy(p, src, len);
62 :
63 7472 : return p;
64 : }
|