POINTER smalloc_malloc P1(size_t, size)
#endif
{
    /* int i; */
    u *temp;

    DEBUG_CHECK(size == 0, "Malloc size 0.\n");
    if (size > SMALL_BLOCK_MAX_BYTES)
	return large_malloc(size, 0);

#if SIZEOF_PTR > SIZEOF_INT
    if (size < SIZEOF_PTR)
	size = SIZEOF_PTR;
#endif

    size = (size + 7) & ~3;	/* block size in bytes */
#define SIZE_INDEX(u_array, size) 	(*(u*) ((char*)u_array-8+size))
#define SIZE_PNT_INDEX(u_array, size)	(*(u**)((char*)u_array-8+size))
    /* i = (size - 8) >> 2; */
    count_up(small_alloc_stat, size);

    SIZE_INDEX(small_count, size) += 1;	/* update statistics */
    SIZE_INDEX(small_total, size) += 1;
    if (SIZE_INDEX(small_count, size) > SIZE_INDEX(small_max, size))
	SIZE_INDEX(small_max, size) = SIZE_INDEX(small_count, size);

    if (temp = SIZE_PNT_INDEX(sfltable, size)) {	/* allocate from the
							 * free list */
	count_back(small_free_stat, size);
	temp++;
	SIZE_PNT_INDEX(sfltable, size) = *(u **) temp;
	fake("From free list.");
	return (char *) temp;
    }				/* else allocate from the chunk */
    if (unused_size < size) {	/* no room in chunk, get another */
	/*
	 * don't waste this smaller block
	 */
	if (unused_size) {
	    count_up(small_free_stat, unused_size);
	    *s_size_ptr(next_unused) = unused_size >> 2;
	    *s_next_ptr(next_unused) = SIZE_PNT_INDEX(sfltable, unused_size);
	    SIZE_PNT_INDEX(sfltable, unused_size) = next_unused;
	}

	fake("Allocating new small chunk.");
	next_unused = (u *) large_malloc(SMALL_CHUNK_SIZE + SIZEOF_PTR, 1);
	if (next_unused == 0)
	    return 0;
	
	*next_unused = (u) last_small_chunk;
	last_small_chunk = next_unused++;
	count_up(small_chunk_stat, SMALL_CHUNK_SIZE + SIZEOF_PTR);
	unused_size = SMALL_CHUNK_SIZE;
    } else
Esempio n. 2
0
char			*ft_strtrim_bis(char const *s)
{
	int		l;
	int		i;
	int		j;
	char	*p;

	j = count_back(s);
	i = count_top(s);
	l = ft_strlen(s);
	if (l - i - j <= 0)
		return (NULL);
	p = ft_strnew(l - i - j);
	if (i == l)
		return (NULL);
	ft_memmove(p, s + i, l - i - j);
	return (p);
}