summaryrefslogtreecommitdiffstats
path: root/common/mem.c
blob: 427ab43f0bdd52bd97dd0ef770c1a79d3e2750ab (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2010 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "mem.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifndef MALLOC_ERROR
#define MALLOC_ERROR(format, ...) {                             \
    printf(format "\n", ## __VA_ARGS__);   \
    abort();                                                    \
}
#endif

size_t spice_strnlen(const char *str, size_t max_len)
{
    size_t len = 0;

    while (len < max_len && *str != 0) {
        len++;
        str++;
    }

    return len;
}

char *spice_strdup(const char *str)
{
    char *copy;

    if (str == NULL) {
        return NULL;
    }

    copy = (char *)spice_malloc(strlen(str) + 1);
    strcpy(copy, str);
    return copy;
}

char *spice_strndup(const char *str, size_t n_bytes)
{
    char *copy;

    if (str == NULL) {
        return NULL;
    }

    copy = (char *)spice_malloc(n_bytes + 1);
    strncpy(copy, str, n_bytes);
    copy[n_bytes] = 0;
    return copy;
}

void *spice_memdup(const void *mem, size_t n_bytes)
{
    void *copy;

    copy = spice_malloc(n_bytes);
    memcpy(copy, mem, n_bytes);
    return copy;
}

void *spice_malloc(size_t n_bytes)
{
    void *mem;

    if (SPICE_LIKELY(n_bytes)) {
        mem = malloc(n_bytes);

        if (SPICE_LIKELY(mem != NULL)) {
            return mem;
        }

        MALLOC_ERROR("spice_malloc: panic: unable to allocate %lu bytes\n",
                     (unsigned long)n_bytes);
    }
    return NULL;
}

void *spice_malloc0(size_t n_bytes)
{
    void *mem;

    if (SPICE_LIKELY(n_bytes)) {
        mem = calloc(1, n_bytes);

        if (SPICE_LIKELY(mem != NULL)) {
            return mem;
        }

        MALLOC_ERROR("spice_malloc0: panic: unable to allocate %lu bytes\n",
                     (unsigned long)n_bytes);
    }
    return NULL;
}

void *spice_realloc(void *mem, size_t n_bytes)
{
    if (SPICE_LIKELY(n_bytes)) {
        mem = realloc(mem, n_bytes);

        if (SPICE_LIKELY(mem != NULL)) {
            return mem;
        }

        MALLOC_ERROR("spice_realloc: panic: unable to allocate %lu bytes\n",
                     (unsigned long)n_bytes);
    }

    if (mem) {
        free(mem);
    }

    return NULL;
}

#define SIZE_OVERFLOWS(a,b) (SPICE_UNLIKELY ((a) > SIZE_MAX / (b)))

void *spice_malloc_n(size_t n_blocks, size_t n_block_bytes)
{
    if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
        MALLOC_ERROR("spice_malloc_n: overflow allocating %lu*%lu bytes",
                     (unsigned long)n_blocks, (unsigned long)n_block_bytes);
    }

    return spice_malloc(n_blocks * n_block_bytes);
}

void *spice_malloc_n_m(size_t n_blocks, size_t n_block_bytes, size_t extra_size)
{
    size_t size1, size2;
    if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
        MALLOC_ERROR("spice_malloc_n: overflow allocating %lu*%lu + %lubytes",
                     (unsigned long)n_blocks, (unsigned long)n_block_bytes, (unsigned long)extra_size);
    }
    size1 = n_blocks * n_block_bytes;
    size2 = size1 + extra_size;
    if (size2 < size1) {
        MALLOC_ERROR("spice_malloc_n: overflow allocating %lu*%lu + %lubytes",
                     (unsigned long)n_blocks, (unsigned long)n_block_bytes, (unsigned long)extra_size);
    }
    return spice_malloc(size2);
}


void *spice_malloc0_n(size_t n_blocks, size_t n_block_bytes)
{
    if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
        MALLOC_ERROR("spice_malloc0_n: overflow allocating %lu*%lu bytes",
                     (unsigned long)n_blocks, (unsigned long)n_block_bytes);
    }

    return spice_malloc0 (n_blocks * n_block_bytes);
}

void *spice_realloc_n(void *mem, size_t n_blocks, size_t n_block_bytes)
{
    if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
        MALLOC_ERROR("spice_realloc_n: overflow allocating %lu*%lu bytes",
                     (unsigned long)n_blocks, (unsigned long)n_block_bytes);
  }

    return spice_realloc(mem, n_blocks * n_block_bytes);
}

SpiceChunks *spice_chunks_new(uint32_t count)
{
    SpiceChunks *chunks;

    chunks = (SpiceChunks *)spice_malloc_n_m(count, sizeof(SpiceChunk), sizeof(SpiceChunks));
    chunks->flags = 0;
    chunks->num_chunks = count;

    return chunks;
}

SpiceChunks *spice_chunks_new_linear(uint8_t *data, uint32_t len)
{
    SpiceChunks *chunks;

    chunks = spice_chunks_new(1);
    chunks->data_size = chunks->chunk[0].len = len;
    chunks->chunk[0].data = data;
    return chunks;
}

void spice_chunks_destroy(SpiceChunks *chunks)
{
    int i;

    if (chunks->flags & SPICE_CHUNKS_FLAGS_FREE) {
        for (i = 0; i < chunks->num_chunks; i++) {
            free(chunks->chunk[i].data);
        }
    }

    free(chunks);
}

void spice_chunks_linearize(SpiceChunks *chunks)
{
    uint8_t *data, *p;
    int i;

    if (chunks->num_chunks > 1) {
        data = (uint8_t*)spice_malloc(chunks->data_size);
        for (p = data, i = 0; i < chunks->num_chunks; i++) {
            memcpy(p, chunks->chunk[i].data,
                   chunks->chunk[i].len);
            p += chunks->chunk[i].len;
        }
        if (chunks->flags & SPICE_CHUNKS_FLAGS_FREE) {
            for (i = 0; i < chunks->num_chunks; i++) {
                free(chunks->chunk[i].data);
            }
        }
        chunks->num_chunks = 1;
        chunks->flags |= SPICE_CHUNKS_FLAGS_FREE;
        chunks->flags &= ~SPICE_CHUNKS_FLAGS_UNSTABLE;
        chunks->chunk[0].data = data;
        chunks->chunk[0].len = chunks->data_size;
    }
}