FOR HELP

mem.c mem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.cmem.c

 

 

/* -*- 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>
#include "log.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;

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

    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;
        }
#ifdef WIN32
  LOG_INFO("spice_malloc: panic: unable to allocat %lu bytes", (unsigned long)n_bytes);
#endif
        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;
        }
#ifdef WIN32
  LOG_INFO("spice_malloc0: panic: unable to allocate %lu bytes", (unsigned long)n_bytes);
#endif
        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));
    /* chenbin added <2012-01-16 14:38:50> */
    if (NULL == chunks)
        return NULL;

    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);
    /* chenbin added <2012-01-16 15:54:14> */
    if (NULL == chunks)
        return NULL;
    chunks->data_size = chunks->chunk[0].len = len;
    chunks->chunk[0].data = data;
    return chunks;
}

void spice_chunks_destroy(SpiceChunks *chunks)
{
    unsigned 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;
    unsigned 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;
    }
}

void spice_buffer_reserve(SpiceBuffer *buffer, size_t len)
{
    if ((buffer->capacity - buffer->offset) < len) {
        buffer->capacity += (len + 1024);
        buffer->buffer = (uint8_t*)spice_realloc(buffer->buffer, buffer->capacity);
    }
}

int spice_buffer_empty(SpiceBuffer *buffer)
{
    return buffer->offset == 0;
}

uint8_t *spice_buffer_end(SpiceBuffer *buffer)
{
    return buffer->buffer + buffer->offset;
}

void spice_buffer_reset(SpiceBuffer *buffer)
{
    buffer->offset = 0;
}

void spice_buffer_free(SpiceBuffer *buffer)
{
    free(buffer->buffer);
    buffer->offset = 0;
    buffer->capacity = 0;
    buffer->buffer = NULL;
}

void spice_buffer_append(SpiceBuffer *buffer, const void *data, size_t len)
{
    spice_buffer_reserve(buffer, len);
    memcpy(buffer->buffer + buffer->offset, data, len);
    buffer->offset += len;
}

size_t spice_buffer_copy(SpiceBuffer *buffer, void *dest, size_t len)
{
    len = MIN(buffer->offset, len);
    memcpy(dest, buffer->buffer, len);
    return len;
}

size_t spice_buffer_remove(SpiceBuffer *buffer, size_t len)
{
    len = MIN(buffer->offset, len);
    memmove(buffer->buffer, buffer->buffer + len, buffer->offset - len);
    buffer->offset -= len;
    return len;
}

 

 

 

/* -*- 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/>.
*/

#ifndef _H_MEM
#define _H_MEM

#include <stdlib.h>
#include "macros.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct SpiceChunk {
    uint8_t *data;
    uint32_t len;
} SpiceChunk;

enum {
    SPICE_CHUNKS_FLAGS_UNSTABLE = (1<<0),
    SPICE_CHUNKS_FLAGS_FREE = (1<<1)
};

typedef struct SpiceChunks {
    uint32_t     data_size;
    uint32_t     num_chunks;
    uint32_t     flags;
    SpiceChunk   chunk[0];
} SpiceChunks;

typedef struct SpiceBuffer
{
    size_t capacity;
    size_t offset;
    uint8_t *buffer;
} SpiceBuffer;

char *spice_strdup(const char *str) SPICE_GNUC_MALLOC;
char *spice_strndup(const char *str, size_t n_bytes) SPICE_GNUC_MALLOC;
void *spice_memdup(const void *mem, size_t n_bytes) SPICE_GNUC_MALLOC;
void *spice_malloc(size_t n_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE(1);
void *spice_malloc0(size_t n_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE(1);
void *spice_realloc(void *mem, size_t n_bytes) SPICE_GNUC_WARN_UNUSED_RESULT;
void *spice_malloc_n(size_t n_blocks, size_t n_block_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE2(1,2);
void *spice_malloc_n_m(size_t n_blocks, size_t n_block_bytes, size_t extra_size) SPICE_GNUC_MALLOC;
void *spice_malloc0_n(size_t n_blocks, size_t n_block_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE2(1,2);
void *spice_realloc_n(void *mem, size_t n_blocks, size_t n_block_bytes) SPICE_GNUC_WARN_UNUSED_RESULT;
SpiceChunks *spice_chunks_new(uint32_t count) SPICE_GNUC_MALLOC;
SpiceChunks *spice_chunks_new_linear(uint8_t *data, uint32_t len) SPICE_GNUC_MALLOC;
void spice_chunks_destroy(SpiceChunks *chunks);
void spice_chunks_linearize(SpiceChunks *chunks);

size_t spice_strnlen(const char *str, size_t max_len);

/* Optimize: avoid the call to the (slower) _n function if we can
 * determine at compile-time that no overflow happens.
 */
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
#  define _SPICE_NEW(struct_type, n_structs, func)        \
    (struct_type *) (__extension__ ({                     \
        size_t __n = (size_t) (n_structs);                \
        size_t __s = sizeof (struct_type);                \
        void *__p;                                        \
        if (__s == 1)                                     \
            __p = spice_##func (__n);                     \
        else if (__builtin_constant_p (__n) &&            \
                 __n <= SIZE_MAX / __s)                   \
            __p = spice_##func (__n * __s);               \
        else                                              \
            __p = spice_##func##_n (__n, __s);            \
        __p;                                              \
    }))
#  define _SPICE_RENEW(struct_type, mem, n_structs, func) \
    (struct_type *) (__extension__ ({                             \
        size_t __n = (size_t) (n_structs);                \
        size_t __s = sizeof (struct_type);                \
        void *__p = (void *) (mem);                       \
        if (__s == 1)                                     \
            __p = spice_##func (__p, __n);                \
        else if (__builtin_constant_p (__n) &&            \
                 __n <= SIZE_MAX / __s)                   \
            __p = spice_##func (__p, __n * __s);          \
        else                                              \
            __p = spice_##func##_n (__p, __n, __s);       \
        __p;                                              \
    }))
#else

/* Unoptimized version: always call the _n() function. */

#define _SPICE_NEW(struct_type, n_structs, func)                        \
    ((struct_type *) spice_##func##_n ((n_structs), sizeof (struct_type)))
#define _SPICE_RENEW(struct_type, mem, n_structs, func)                 \
    ((struct_type *) spice_##func##_n (mem, (n_structs), sizeof (struct_type)))

#endif

#define spice_new(struct_type, n_structs) _SPICE_NEW(struct_type, n_structs, malloc)
#define spice_new0(struct_type, n_structs) _SPICE_NEW(struct_type, n_structs, malloc0)
#define spice_renew(struct_type, mem, n_structs) _SPICE_RENEW(struct_type, mem, n_structs, realloc)

/* Buffer management */
void spice_buffer_reserve(SpiceBuffer *buffer, size_t len);
int spice_buffer_empty(SpiceBuffer *buffer);
uint8_t *spice_buffer_end(SpiceBuffer *buffer);
void spice_buffer_reset(SpiceBuffer *buffer);
void spice_buffer_free(SpiceBuffer *buffer);
void spice_buffer_append(SpiceBuffer *buffer, const void *data, size_t len);
size_t spice_buffer_copy(SpiceBuffer *buffer, void *dest, size_t len);
size_t spice_buffer_remove(SpiceBuffer *buffer, size_t len);

#ifdef __cplusplus
}
#endif

#endif

你可能感兴趣的:(FOR HELP)