File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_mem.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sun Jun 30 22:10:27 2013 UTC (10 years, 11 months ago) by misho
Branches: MAIN
CVS tags: elwix3_6, elwix3_5, elwix3_4, elwix3_3, elwix3_2, elwix3_1, elwix3_0, elwix2_9, elwix2_8, elwix2_7, elwix2_6, elwix2_5, elwix2_4, elwix2_3, elwix2_2, elwix2_1, elwix2_0, HEAD, ELWIX3_5, ELWIX3_4, ELWIX3_3, ELWIX3_2, ELWIX3_1, ELWIX3_0, ELWIX2_9, ELWIX2_8, ELWIX2_7, ELWIX2_6, ELWIX2_5, ELWIX2_4, ELWIX2_3, ELWIX2_2, ELWIX2_1, ELWIX2_0, ELWIX1_9
version 1.9

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

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