File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / contrib / Attic / test_mem.c
Revision 1.1.2.10: download - view: text, annotated - select for diffs - revision graph
Tue Feb 28 12:05:58 2012 UTC (12 years, 4 months ago) by misho
Branches: sess3_1
bug fix for forgotten pointer reassign after realloc

    1: #include <stdio.h>
    2: #include <aitsess.h>
    3: 
    4: 
    5: void show(u_int size, u_int act, u_int inact)
    6: {
    7: 	if (!act && !inact)
    8: 		return;
    9: 
   10: 	if (size < 1024)
   11: 		printf("Statistics:: BUCKET %uB size, %u active, %u inactive\n", size, act, inact);
   12: 	else if (size < 1024 * 1024)
   13: 		printf("Statistics:: BUCKET %uKB size, %u active, %u inactive\n", size / 1024, act, inact);
   14: 	else
   15: 		printf("Statistics:: BUCKET %uMB size, %u active, %u inactive\n", size / (1024 * 1024), act, inact);
   16: }
   17: 
   18: int
   19: main(int argc, char **argv)
   20: {
   21: 	mpool_t *mp;
   22: 	void *addr;
   23: 	int i;
   24: 	u_long curr;
   25: 
   26: 	for (i = 0; i < 3; i++) {
   27: 		addr = malloc(4000);
   28: 		printf("addr=%p\n", addr);
   29: 		free(addr);
   30: 	}
   31: 
   32: 	mp = mpool_init(0);
   33: 	if (!mp) {
   34: 		printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
   35: 		return 1;
   36: 	}
   37: 
   38: 	for (i = 0; i < 3; i++) {
   39: 		addr = mpool_malloc(mp, 4000, "mdaaa 4000");
   40: 		if (!addr) {
   41: 			printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
   42: 			mpool_destroy(&mp);
   43: 			return 2;
   44: 		}
   45: 		printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
   46: 
   47: 		strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n", 
   48: 				mpool_getsizebyaddr(addr));
   49: //		mpool_free(mp, addr, 0);
   50: 	}
   51: 
   52: 	mpool_getquota(mp, &curr, NULL);
   53: 	printf("___current=%lu\n", curr);
   54: 
   55: 	printf("0) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   56: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   57: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   58: 	mpool_statistics(mp, show);
   59: 
   60: 	addr = mpool_realloc(mp, addr, 1000, "|||||||||||||");
   61: 	if (!addr)
   62: 		printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
   63: 	mpool_getquota(mp, &curr, NULL);
   64: 	printf("addr=%p_current=%lu\n", addr, curr);
   65: 
   66: 	printf("1) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   67: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   68: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   69: 	mpool_statistics(mp, show);
   70: 
   71: 
   72: 	if (addr)
   73: 		mpool_free(mp, addr, 0);
   74: 
   75: 	printf("2) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   76: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   77: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   78: 	mpool_statistics(mp, show);
   79: 
   80: 	mpool_purge(mp, 0);
   81: 
   82: 	mpool_getquota(mp, &curr, NULL);
   83: 	printf("___current=%lu\n", curr);
   84: 
   85: 	printf("3) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   86: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   87: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   88: 	mpool_statistics(mp, show);
   89: 
   90: 	addr = mpool_malloc(mp, 4000, "mdaaa 4000");
   91: 	if (!addr) {
   92: 		printf("Error:: #%d - %s\n", sess_GetErrno(), sess_GetError());
   93: 		mpool_destroy(&mp);
   94: 		return 2;
   95: 	}
   96: 	printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
   97: 	mpool_free(mp, addr, 0);
   98: 
   99: 	printf("4) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
  100: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
  101: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
  102: 	mpool_statistics(mp, show);
  103: 	mpool_destroy(&mp);
  104: 	return 0;
  105: }

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