File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / contrib / Attic / test_mem.c
Revision 1.1.2.6: download - view: text, annotated - select for diffs - revision graph
Tue Feb 28 09:36:36 2012 UTC (12 years, 4 months ago) by misho
Branches: sess3_1
added UT for statistics

#include <stdio.h>
#include <aitsess.h>


void show(u_int size, u_int act, u_int inact)
{
	if (!act && !inact)
		return;

	if (size < 1024)
		printf("Statistics:: BUCKET %uB size, %u active, %u inactive\n", size, act, inact);
	else if (size < 1024 * 1024)
		printf("Statistics:: BUCKET %uKB size, %u active, %u inactive\n", size / 1024, act, inact);
	else
		printf("Statistics:: BUCKET %uMB size, %u active, %u inactive\n", size / (1024 * 1024), act, inact);
}

int
main(int argc, char **argv)
{
	mpool_t *mp;
	void *addr;
	int i;

	for (i = 0; i < 3; i++) {
		addr = malloc(4000);
		printf("addr=%p\n", addr);
		free(addr);
	}

	mp = mpool_init(0);
	if (!mp) {
		printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
		return 1;
	}

	for (i = 0; i < 3; i++) {
		addr = mpool_malloc(mp, 4000, "mdaaa 4000");
		if (!addr) {
			printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
			mpool_destroy(&mp);
			return 2;
		}
		printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));

		strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n", 
				mpool_getsizebyaddr(addr));
//		mpool_free(mp, addr, 0);
	}

	printf("1)\n");
	mpool_statistics(mp, show);

	mpool_free(mp, addr, 0);

	printf("2)\n");
	mpool_statistics(mp, show);

	mpool_purge(mp, 0);

	printf("3)\n");
	mpool_statistics(mp, show);

	addr = mpool_malloc(mp, 4000, "mdaaa 4000");
	if (!addr) {
		printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
		mpool_destroy(&mp);
		return 2;
	}
	printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
	mpool_free(mp, addr, 0);

	printf("Done.\n");
	mpool_statistics(mp, show);
	mpool_destroy(&mp);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>