Example #1
0
void malloc_free(malloc_t *heap, void *ptr)
{
    int type = heap->mem_map[(uintptr_t)ptr / RUN_SIZE];

    if (ptr == NULL)
        return;

    if (type == MM_UNALLOCATED)
    {
        /* either large or bad address */
        large_free(heap, ptr);
    }
    else if (type == MM_SMALL)
    {
        small_free(heap, ptr);
    }
    else if (type == MM_TINY)
    {
        tiny_free(heap, ptr);
    }
    else
    {
        DBG_ASSERT(0 && "Bad memory type");
    }
}
Example #2
0
int
main(int argc, char* argv[])
{
	time_t start, end;
	int a, b;
	int i, j;
	int count;

	memset(nums, 0, sizeof(nums));
	ncount = 0;

	for(a = 2; a <= 100; ++a)
	{
		start = time(NULL);
		for(b = 2; b <= 100; ++b)
		{
			struct large* num = large_pow(a, b);
			nums[ncount++] = num;
		}
		end = time(NULL);
		printf("%d/%.01f\n", a, difftime(end, start));
	}
	for(i = 0; i < 100 * 100; ++i)
	{
		for(j = i + 1; j < 100 * 100; ++j)
		{
			if(nums[i]==NULL||nums[j]==NULL) continue;
			if(large_compare(nums[i], nums[j])==0)
			{
				large_free(nums[j]);
				nums[j] = NULL;
			}
		}
	}
	count=0;
	for(i = 0; i < 100 * 100; ++i)
	{
		if(nums[i]!=NULL)count++;
	}
	printf("%d\n", count);
	return 0;
}
Example #3
0
STATIC inline int INIT unlz4(u8 *input, int in_len,
				int (*fill) (void *, unsigned int),
				int (*flush) (void *, unsigned int),
				u8 *output, int *posp,
				void (*error) (char *x))
{
	int ret = -1;
	size_t chunksize = 0;
	size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
	u8 *inp;
	u8 *inp_start;
	u8 *outp;
	int size = in_len;
#ifdef PREBOOT
	size_t out_len = get_unaligned_le32(input + in_len);
#endif
	size_t dest_len;


	if (output) {
		outp = output;
	} else if (!flush) {
		error("NULL output pointer and no flush function provided");
		goto exit_0;
	} else {
		outp = large_malloc(uncomp_chunksize);
		if (!outp) {
			error("Could not allocate output buffer");
			goto exit_0;
		}
	}

	if (input && fill) {
		error("Both input pointer and fill function provided,");
		goto exit_1;
	} else if (input) {
		inp = input;
	} else if (!fill) {
		error("NULL input pointer and missing fill function");
		goto exit_1;
	} else {
		inp = large_malloc(lz4_compressbound(uncomp_chunksize));
		if (!inp) {
			error("Could not allocate input buffer");
			goto exit_1;
		}
	}
	inp_start = inp;

	if (posp)
		*posp = 0;

	if (fill) {
		size = fill(inp, 4);
		if (size < 4) {
			error("data corrupted");
			goto exit_2;
		}
	}

	chunksize = get_unaligned_le32(inp);
	if (chunksize == ARCHIVE_MAGICNUMBER) {
		if (!fill) {
			inp += 4;
			size -= 4;
		}	} else {
		error("invalid header");
		goto exit_2;
	}

	if (posp)
		*posp += 4;

	for (;;) {

		if (fill) {
			size = fill(inp, 4);
			if (size == 0)
				break;
			if (size < 4) {
				error("data corrupted");
				goto exit_2;
			}
		}

		chunksize = get_unaligned_le32(inp);
		if (chunksize == ARCHIVE_MAGICNUMBER) {
			if (!fill) {
				inp += 4;
				size -= 4;
			}
			if (posp)
				*posp += 4;
			continue;
		}

		if (posp)
			*posp += 4;

		if (!fill) {
			inp += 4;
			size -= 4;
		} else {
			if (chunksize > lz4_compressbound(uncomp_chunksize)) {
				error("chunk length is longer than allocated");
				goto exit_2;
			}
			size = fill(inp, chunksize);
			if (size < chunksize) {
				error("data corrupted");
				goto exit_2;
			}
		}
#ifdef PREBOOT
		if (out_len >= uncomp_chunksize) {
			dest_len = uncomp_chunksize;
			out_len -= dest_len;
		} else
			dest_len = out_len;
		ret = lz4_decompress(inp, &chunksize, outp, dest_len);
#else
		dest_len = uncomp_chunksize;
		ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp,
				&dest_len);
#endif
		if (ret < 0) {
			error("Decoding failed");
			goto exit_2;
		}

		if (flush && flush(outp, dest_len) != dest_len)
			goto exit_2;
		if (output)
			outp += dest_len;
		if (posp)
			*posp += chunksize;

		if (!fill) {
			size -= chunksize;
			if (size == 0)
				break;
			else if (size < 0) {
				error("data corrupted");
				goto exit_2;
			}
			inp += chunksize;
		}
	}

	ret = 0;
exit_2:
	if (!input)
		large_free(inp_start);
exit_1:
	if (!output)
		large_free(outp);
exit_0:
	return ret;
}