Beispiel #1
0
void _util_vec_delete(void *data, size_t line, const char *file) {
    char *ident = (char *)data - IDENT_SIZE;
    if (!strcmp(ident, IDENT_MEM)) {
        stat_mem_block_t *block = (stat_mem_block_t*)((char *)data - IDENT_MEM_TOP);
        VALGRIND_MAKE_MEM_DEFINED(block, sizeof(stat_mem_block_t));
        con_err("internal warning: invalid use of vec_free:\n");
        con_err("internal warning:    memory block last allocated (size: %u (bytes), at %s:%u)\n",
            (unsigned)block->size,
            block->file,
            (unsigned)block->line);
        con_err("internal warning:    released with with wrong routine at %s:%u\n", file, (unsigned)line);
        con_err("internal warning:    forwarding to mem_d, please fix it\n");
        VALGRIND_MAKE_MEM_NOACCESS(block, sizeof(stat_mem_block_t));
        mem_d(data);
        return;
    }
    /* forward */
    stat_mem_deallocate((void*)(ident - sizeof(vector_t)), line, file);
}
Beispiel #2
0
void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) {
    stat_mem_block_t *oldinfo = NULL;
    stat_mem_block_t *newinfo;

    if (GMQCC_UNLIKELY(!ptr))
        return stat_mem_allocate(size, line, file);

    /* stay consistent with glibc */
    if (GMQCC_UNLIKELY(!size)) {
        stat_mem_deallocate(ptr);
        return NULL;
    }

    oldinfo = ((stat_mem_block_t*)ptr - 1);
    newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));

    if (GMQCC_UNLIKELY(!newinfo)) {
        stat_mem_deallocate(ptr);
        return NULL;
    }

    VALGRIND_MALLOCLIKE_BLOCK(newinfo + 1, size, sizeof(stat_mem_block_t), 0);

    /* we need access to the old info redzone */
    VALGRIND_MAKE_MEM_DEFINED(oldinfo, sizeof(stat_mem_block_t));

    memcpy(newinfo+1, oldinfo+1, oldinfo->size);

    if (oldinfo->prev) {
        /* just need access for a short period */
        VALGRIND_MAKE_MEM_DEFINED(oldinfo->prev, sizeof(stat_mem_block_t));
        oldinfo->prev->next = oldinfo->next;
        /* don't need access anymore */
        VALGRIND_MAKE_MEM_NOACCESS(oldinfo->prev, sizeof(stat_mem_block_t));
    }

    if (oldinfo->next) {
        /* just need access for a short period */
        VALGRIND_MAKE_MEM_DEFINED(oldinfo->next, sizeof(stat_mem_block_t));
        oldinfo->next->prev = oldinfo->prev;
        /* don't need access anymore */
        VALGRIND_MAKE_MEM_NOACCESS(oldinfo->next, sizeof(stat_mem_block_t));
    }

    /* move ahead */
    if (oldinfo == stat_mem_block_root)
        stat_mem_block_root = oldinfo->next;

    /* we need access to the redzone for the newinfo block */
    VALGRIND_MAKE_MEM_DEFINED(newinfo, sizeof(stat_mem_block_t));

    newinfo->line = line;
    newinfo->size = size;
    newinfo->file = file;
    newinfo->prev = NULL;
    newinfo->next = stat_mem_block_root;

    /*
     * likely since the only time there is no root is when it's
     * being initialized first.
     */
    if (GMQCC_LIKELY(stat_mem_block_root != NULL)) {
        /* we need access to the root */
        VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t));
        stat_mem_block_root->prev = newinfo;
        /* kill access */
        VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t));
    }

    stat_mem_block_root = newinfo;
    stat_mem_allocated -= oldinfo->size;
    stat_mem_high      -= oldinfo->size;
    stat_mem_allocated += newinfo->size;
    stat_mem_high      += newinfo->size;

    /*
     * we're finished with the redzones, lets kill the access
     * to them.
     */
    VALGRIND_MAKE_MEM_NOACCESS(newinfo, sizeof(stat_mem_block_t));
    VALGRIND_MAKE_MEM_NOACCESS(oldinfo, sizeof(stat_mem_block_t));

    if (stat_mem_high > stat_mem_peak)
        stat_mem_peak = stat_mem_high;

    free(oldinfo);
    VALGRIND_FREELIKE_BLOCK(ptr, sizeof(stat_mem_block_t));
    return newinfo + 1;
}