blob: b9b38514ac1ebb0cbb24d208da0c3c15d021c6a7 [file] [log] [blame]
pbrook17e23772008-06-09 13:47:45 +00001/*
2 * malloc-like functions for system emulation.
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010025#include "trace.h"
aliguori8a1d02a2009-02-05 22:05:49 +000026#include <stdlib.h>
27
pbrook17e23772008-06-09 13:47:45 +000028void qemu_free(void *ptr)
29{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010030 trace_qemu_free(ptr);
pbrook17e23772008-06-09 13:47:45 +000031 free(ptr);
32}
33
Anthony Liguori20ff6c82009-12-09 12:59:36 -060034static int allow_zero_malloc(void)
35{
36#if defined(CONFIG_ZERO_MALLOC)
37 return 1;
38#else
39 return 0;
40#endif
41}
42
pbrook17e23772008-06-09 13:47:45 +000043void *qemu_malloc(size_t size)
44{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010045 void *ptr;
Anthony Liguori20ff6c82009-12-09 12:59:36 -060046 if (!size && !allow_zero_malloc()) {
malca7d27b52009-05-19 22:26:42 +040047 abort();
malc26d64a82009-05-19 22:28:26 +040048 }
Jes Sorensenb152aa82010-10-26 10:39:26 +020049 ptr = qemu_oom_check(malloc(size ? size : 1));
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010050 trace_qemu_malloc(size, ptr);
51 return ptr;
pbrook17e23772008-06-09 13:47:45 +000052}
53
ths2137b4c2008-08-06 08:37:17 +000054void *qemu_realloc(void *ptr, size_t size)
55{
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010056 void *newptr;
Markus Armbrusterf8b09532009-12-14 10:48:05 +010057 if (!size && !allow_zero_malloc()) {
58 abort();
malca7d27b52009-05-19 22:26:42 +040059 }
Jes Sorensenb152aa82010-10-26 10:39:26 +020060 newptr = qemu_oom_check(realloc(ptr, size ? size : 1));
Stefan Hajnoczicd245a12010-05-22 18:09:25 +010061 trace_qemu_realloc(ptr, size, newptr);
62 return newptr;
ths2137b4c2008-08-06 08:37:17 +000063}
64
pbrook17e23772008-06-09 13:47:45 +000065void *qemu_mallocz(size_t size)
66{
Jes Sorensen236e2372010-12-06 15:25:34 +010067 void *ptr;
Richard Henderson50401022010-05-21 10:37:51 -070068 if (!size && !allow_zero_malloc()) {
69 abort();
70 }
Jes Sorensen236e2372010-12-06 15:25:34 +010071 ptr = qemu_oom_check(calloc(1, size ? size : 1));
72 trace_qemu_malloc(size, ptr);
73 return ptr;
pbrook17e23772008-06-09 13:47:45 +000074}
75
76char *qemu_strdup(const char *str)
77{
78 char *ptr;
blueswir1363a37d2008-08-21 17:58:08 +000079 size_t len = strlen(str);
80 ptr = qemu_malloc(len + 1);
balrogac4b0d02008-11-09 00:28:40 +000081 memcpy(ptr, str, len + 1);
pbrook17e23772008-06-09 13:47:45 +000082 return ptr;
83}
balrogac4b0d02008-11-09 00:28:40 +000084
85char *qemu_strndup(const char *str, size_t size)
86{
87 const char *end = memchr(str, 0, size);
88 char *new;
89
malc26d64a82009-05-19 22:28:26 +040090 if (end) {
balrogac4b0d02008-11-09 00:28:40 +000091 size = end - str;
malc26d64a82009-05-19 22:28:26 +040092 }
balrogac4b0d02008-11-09 00:28:40 +000093
94 new = qemu_malloc(size + 1);
95 new[size] = 0;
96
97 return memcpy(new, str, size);
98}