root / qemu-malloc.c @ 38ec7b53
History | View | Annotate | Download (2.4 kB)
1 | 17e2377a | pbrook | /*
|
---|---|---|---|
2 | 17e2377a | pbrook | * malloc-like functions for system emulation.
|
3 | 17e2377a | pbrook | *
|
4 | 17e2377a | pbrook | * Copyright (c) 2006 Fabrice Bellard
|
5 | 17e2377a | pbrook | *
|
6 | 17e2377a | pbrook | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 17e2377a | pbrook | * of this software and associated documentation files (the "Software"), to deal
|
8 | 17e2377a | pbrook | * in the Software without restriction, including without limitation the rights
|
9 | 17e2377a | pbrook | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 17e2377a | pbrook | * copies of the Software, and to permit persons to whom the Software is
|
11 | 17e2377a | pbrook | * furnished to do so, subject to the following conditions:
|
12 | 17e2377a | pbrook | *
|
13 | 17e2377a | pbrook | * The above copyright notice and this permission notice shall be included in
|
14 | 17e2377a | pbrook | * all copies or substantial portions of the Software.
|
15 | 17e2377a | pbrook | *
|
16 | 17e2377a | pbrook | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 17e2377a | pbrook | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 17e2377a | pbrook | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 17e2377a | pbrook | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 17e2377a | pbrook | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 17e2377a | pbrook | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 17e2377a | pbrook | * THE SOFTWARE.
|
23 | 17e2377a | pbrook | */
|
24 | 17e2377a | pbrook | #include "qemu-common.h" |
25 | 8a1d02ab | aliguori | #include <stdlib.h> |
26 | 8a1d02ab | aliguori | |
27 | 8a1d02ab | aliguori | static void *oom_check(void *ptr) |
28 | 8a1d02ab | aliguori | { |
29 | 26d64a85 | malc | if (ptr == NULL) { |
30 | 62a3fe29 | aliguori | abort(); |
31 | 26d64a85 | malc | } |
32 | 8a1d02ab | aliguori | return ptr;
|
33 | 8a1d02ab | aliguori | } |
34 | 17e2377a | pbrook | |
35 | 17e2377a | pbrook | void *get_mmap_addr(unsigned long size) |
36 | 17e2377a | pbrook | { |
37 | 17e2377a | pbrook | return NULL; |
38 | 17e2377a | pbrook | } |
39 | 17e2377a | pbrook | |
40 | 17e2377a | pbrook | void qemu_free(void *ptr) |
41 | 17e2377a | pbrook | { |
42 | 17e2377a | pbrook | free(ptr); |
43 | 17e2377a | pbrook | } |
44 | 17e2377a | pbrook | |
45 | 20ff6c80 | Anthony Liguori | static int allow_zero_malloc(void) |
46 | 20ff6c80 | Anthony Liguori | { |
47 | 20ff6c80 | Anthony Liguori | #if defined(CONFIG_ZERO_MALLOC)
|
48 | 20ff6c80 | Anthony Liguori | return 1; |
49 | 20ff6c80 | Anthony Liguori | #else
|
50 | 20ff6c80 | Anthony Liguori | return 0; |
51 | 20ff6c80 | Anthony Liguori | #endif
|
52 | 20ff6c80 | Anthony Liguori | } |
53 | 20ff6c80 | Anthony Liguori | |
54 | 17e2377a | pbrook | void *qemu_malloc(size_t size)
|
55 | 17e2377a | pbrook | { |
56 | 20ff6c80 | Anthony Liguori | if (!size && !allow_zero_malloc()) {
|
57 | a7d27b53 | malc | abort(); |
58 | 26d64a85 | malc | } |
59 | 20ff6c80 | Anthony Liguori | return oom_check(malloc(size ? size : 1)); |
60 | 17e2377a | pbrook | } |
61 | 17e2377a | pbrook | |
62 | 2137b4cc | ths | void *qemu_realloc(void *ptr, size_t size) |
63 | 2137b4cc | ths | { |
64 | 26d64a85 | malc | if (size) {
|
65 | 322691a5 | aliguori | return oom_check(realloc(ptr, size));
|
66 | 20ff6c80 | Anthony Liguori | } else if (allow_zero_malloc()) { |
67 | 20ff6c80 | Anthony Liguori | return oom_check(realloc(ptr, size ? size : 1)); |
68 | a7d27b53 | malc | } |
69 | a7d27b53 | malc | abort(); |
70 | 2137b4cc | ths | } |
71 | 2137b4cc | ths | |
72 | 17e2377a | pbrook | void *qemu_mallocz(size_t size)
|
73 | 17e2377a | pbrook | { |
74 | 17e2377a | pbrook | void *ptr;
|
75 | 17e2377a | pbrook | ptr = qemu_malloc(size); |
76 | 17e2377a | pbrook | memset(ptr, 0, size);
|
77 | 17e2377a | pbrook | return ptr;
|
78 | 17e2377a | pbrook | } |
79 | 17e2377a | pbrook | |
80 | 17e2377a | pbrook | char *qemu_strdup(const char *str) |
81 | 17e2377a | pbrook | { |
82 | 17e2377a | pbrook | char *ptr;
|
83 | 363a37d5 | blueswir1 | size_t len = strlen(str); |
84 | 363a37d5 | blueswir1 | ptr = qemu_malloc(len + 1);
|
85 | ac4b0d0c | balrog | memcpy(ptr, str, len + 1);
|
86 | 17e2377a | pbrook | return ptr;
|
87 | 17e2377a | pbrook | } |
88 | ac4b0d0c | balrog | |
89 | ac4b0d0c | balrog | char *qemu_strndup(const char *str, size_t size) |
90 | ac4b0d0c | balrog | { |
91 | ac4b0d0c | balrog | const char *end = memchr(str, 0, size); |
92 | ac4b0d0c | balrog | char *new;
|
93 | ac4b0d0c | balrog | |
94 | 26d64a85 | malc | if (end) {
|
95 | ac4b0d0c | balrog | size = end - str; |
96 | 26d64a85 | malc | } |
97 | ac4b0d0c | balrog | |
98 | ac4b0d0c | balrog | new = qemu_malloc(size + 1);
|
99 | ac4b0d0c | balrog | new[size] = 0;
|
100 | ac4b0d0c | balrog | |
101 | ac4b0d0c | balrog | return memcpy(new, str, size);
|
102 | ac4b0d0c | balrog | } |