xref: /illumos-kvm-cmd/qemu-malloc.c (revision 36b02c7d)
1 /*
2  * malloc-like functions for system emulation.
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  * Portions Copyright 2011 Joyent, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include "qemu-common.h"
26 #include "trace.h"
27 #include <stdlib.h>
28 
qemu_free(void * ptr)29 void qemu_free(void *ptr)
30 {
31     trace_qemu_free(ptr);
32     free(ptr);
33 }
34 
allow_zero_malloc(void)35 static int allow_zero_malloc(void)
36 {
37 #if defined(CONFIG_ZERO_MALLOC)
38     return 1;
39 #else
40     return 0;
41 #endif
42 }
43 
qemu_malloc(size_t size)44 void *qemu_malloc(size_t size)
45 {
46     void *ptr;
47     if (!size && !allow_zero_malloc()) {
48         abort();
49     }
50     ptr = qemu_oom_check(malloc(size ? size : 1));
51     trace_qemu_malloc(size, ptr);
52     return ptr;
53 }
54 
qemu_realloc(void * ptr,size_t size)55 void *qemu_realloc(void *ptr, size_t size)
56 {
57     void *newptr;
58     if (!size && !allow_zero_malloc()) {
59         abort();
60     }
61     newptr = qemu_oom_check(realloc(ptr, size ? size : 1));
62     trace_qemu_realloc(ptr, size, newptr);
63     return newptr;
64 }
65 
66 #ifdef CONFIG_SOLARIS
qemu_mallocz(size_t size)67 void *qemu_mallocz(size_t size)
68 {
69     char *ptr;
70     int i;
71     char x;
72 
73     if (!size && !allow_zero_malloc()) {
74         abort();
75     }
76     ptr = qemu_oom_check(calloc(1, size ? size : 1));
77     for (i = 0; i < size; i++)
78         x = (char)*ptr+i;
79 
80     trace_qemu_malloc(size, ptr);
81     return ptr;
82 }
83 
84 #else
qemu_mallocz(size_t size)85 void *qemu_mallocz(size_t size)
86 {
87     void *ptr;
88 
89     if (!size && !allow_zero_malloc()) {
90         abort();
91     }
92     ptr = qemu_oom_check(calloc(1, size ? size : 1));
93     trace_qemu_malloc(size, ptr);
94     return ptr;
95 }
96 #endif /* CONFIG_SOLARIS */
97 
qemu_strdup(const char * str)98 char *qemu_strdup(const char *str)
99 {
100     char *ptr;
101     size_t len = strlen(str);
102     ptr = qemu_malloc(len + 1);
103     memcpy(ptr, str, len + 1);
104     return ptr;
105 }
106 
qemu_strndup(const char * str,size_t size)107 char *qemu_strndup(const char *str, size_t size)
108 {
109     const char *end = memchr(str, 0, size);
110     char *new;
111 
112     if (end) {
113         size = end - str;
114     }
115 
116     new = qemu_malloc(size + 1);
117     new[size] = 0;
118 
119     return memcpy(new, str, size);
120 }
121