Beispiel #1
0
void *
CrT_calloc(size_t num, size_t siz, const char *file, int line)
{
#if defined(HAVE_PTHREAD_H)
	LOCKCRT();
#endif
    size_t size = siz * num;
    Block b = block_find(file, line);
    Header m = (Header) calloc(size + CRT_OVERHEAD_BYTES, 1);

    if (!m) {
        fprintf(stderr, "CrT_calloc(): Out of Memory!\n");
        abort();
    }

    m->b = b;
    m->size = size;



#ifdef CRT_DEBUG_ALSO

    /* Look around for trashed ram blocks: */
    CrT_check(file, line);

    /* Remember where end of block is: */
    m->end = &((char *) m)[size + (CRT_OVERHEAD_BYTES - 1)];

    /* Write a sacred value at end of block: */
    *m->end = CRT_MAGIC;

    /* Thread m into linklist of allocated blocks: */
    INSERT(m);

    /* Remember we've touched 'm' recently: */
    just_touched[++next_touched & (CRT_NEW_TO_CHECK - 1)] = m;

#endif



    b->tot_bytes_alloc += size;
    b->tot_allocs_done++;
    b->live_blocks++;
    b->live_bytes += size;

    if (b->live_bytes > b->max_bytes) {
        b->max_bytes = b->live_bytes;
        b->max_bytes_time = time(NULL);
    }
    if (b->live_blocks > b->max_blocks)
        b->max_blocks = b->live_blocks;
#if defined(HAVE_PTHREAD_H)
	UNLOCKCRT();
#endif
    return (void *) (m + 1);
}
Beispiel #2
0
void
CrT_free(void *p, const char *file, int line)
{
#if defined(HAVE_PTHREAD_H)
	LOCKCRT();
#endif
    Header m = ((Header) p) - 1;
    Block b = m->b;

#ifdef CRT_DEBUG_ALSO
    /* Look around for trashed ram blocks: */
    if (*m->end == CRT_FREE_MAGIC)
        crash("Duplicate free()", m, file, line);

    check_block(m, __FILE__, __LINE__);
    CrT_check(file, line);
    /* Don't clobber 'rover': */
    if (rover == m) {
        rover = rover->next;
    }
    /* Remove m from linklist of allocated blocks: */
    REMOVE(m);
    /* Remove m from just_touched[], if present: */
    {
        int i = CRT_NEW_TO_CHECK;

        while (i-- > 0) {
            if (just_touched[i] == m) {
                just_touched[i] = &root;
            }
        }
    }
    /* Mark m so as to give some chance of */
    /* diagnosing repeat free()s of same   */
    /* block:                              */
    *m->end = CRT_FREE_MAGIC;
#endif

    b->live_bytes -= m->size;
    b->live_blocks--;

    free(m);
#if defined(HAVE_PTHREAD_H)
	UNLOCKCRT();
#endif
}
Beispiel #3
0
void *
CrT_realloc(void *p, size_t size, const char *file, int line)
{
	Header m = ((Header) p) - 1;
	Block b = m->b;

#ifdef MALLOC_PROFILING_EXTRA
	/* Look around for trashed ram blocks: */
	check_block(m, __FILE__, __LINE__);
	CrT_check(file, line);
	/* Don't clobber 'rover': */
	if (rover == m) {
		rover = rover->next;
	}
	/* Remove m from linklist of allocated blocks: */
	REMOVE(m);
	/* Remove m from just_touched[], if present: */
	{
		int i = CRT_NEW_TO_CHECK;

		while (i-- > 0) {
			if (just_touched[i] == m) {
				just_touched[i] = &root;
			}
		}
	}
#endif

	m = (Header) realloc(m, size + CRT_OVERHEAD_BYTES);

	if (!m) {
		fprintf(stderr, "CrT_realloc(): Out of Memory!\n");
		abort();
	}

	b->live_bytes -= m->size;
	b->live_bytes += size;

	m->size = size;

#ifdef MALLOC_PROFILING_EXTRA

	/* Remember where end of block is: */
	m->end = &((char *) m)[size + (CRT_OVERHEAD_BYTES - 1)];

	/* Write a sacred value at end of block: */
	*m->end = CRT_MAGIC;

	/* Thread m into linklist of allocated blocks: */
	INSERT(m);

	/* Remember we've touched 'm' recently: */
	just_touched[++next_touched & (CRT_NEW_TO_CHECK - 1)] = m;

#endif



	if (b->live_bytes > b->max_bytes) {
		b->max_bytes = b->live_bytes;
		b->max_bytes_time = time(NULL);
	}

	return (void *) (m + 1);
}