File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_mem.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Wed Jul 1 21:48:39 2015 UTC (8 years, 10 months ago) by misho
Branches: MAIN
CVS tags: elwix5_9, elwix5_8, elwix5_7, elwix5_6, elwix5_5, elwix5_4, elwix5_3, elwix5_2, elwix5_11, elwix5_10, elwix5_1, elwix5_0, elwix4_9, elwix4_8, elwix4_7, elwix4_6, elwix4_5, elwix4_4, elwix4_3, elwix4_26, elwix4_25, elwix4_24, elwix4_23, elwix4_22, elwix4_21, elwix4_20, elwix4_2, elwix4_19, elwix4_18, elwix4_17, elwix4_16, elwix4_15, elwix4_14, elwix4_13, elwix4_12, elwix4_11, elwix4_10, elwix4_1, elwix3_9, HEAD, ELWIX5_9, ELWIX5_8, ELWIX5_7, ELWIX5_6, ELWIX5_5, ELWIX5_4, ELWIX5_3, ELWIX5_2, ELWIX5_10, ELWIX5_1, ELWIX5_0, ELWIX4_9, ELWIX4_8, ELWIX4_7, ELWIX4_6, ELWIX4_5, ELWIX4_4, ELWIX4_3, ELWIX4_26, ELWIX4_25, ELWIX4_24, ELWIX4_23, ELWIX4_22, ELWIX4_21, ELWIX4_20, ELWIX4_2, ELWIX4_19, ELWIX4_18, ELWIX4_17, ELWIX4_16, ELWIX4_15, ELWIX4_14, ELWIX4_13, ELWIX4_12, ELWIX4_11, ELWIX4_10, ELWIX4_1, ELWIX4_0, ELWIX3_8
version 3.8

    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, real;
   27: 
   28: 	printf("whether default memory mapper is elwix? %d\n", elwix_mm_inuse());
   29: 
   30: 	for (i = 0; i < 3; i++) {
   31: 		addr = malloc(4000);
   32: 		printf("addr=%p\n", addr);
   33: 		free(addr);
   34: 	}
   35: 
   36: 	mp = mpool_init(0);
   37: 	if (!mp) {
   38: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   39: 		return 1;
   40: 	}
   41: 	/*
   42: 	mpool_getquota(mp, &curr, NULL, NULL);
   43: 	printf("___current=%lu\n", curr);
   44: 	*/
   45: 
   46: 	printf(">>> get 3 allocs\n");
   47: 	for (i = 0; i < 3; i++) {
   48: 		addr = mpool_malloc(mp, 4000, "mdaaa 4000");
   49: 		if (!addr) {
   50: 			printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   51: 			mpool_destroy(&mp);
   52: 			return 2;
   53: 		}
   54: 		printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
   55: 
   56: 		strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n", 
   57: 				mpool_getsizebyaddr(addr));
   58: //		mpool_free(mp, addr, 0);
   59: 	}
   60: 
   61: 	mpool_getquota(mp, &curr, &real, NULL);
   62: 	printf("___current=%lu ___real=%lu\n", curr, real);
   63: 
   64: 	printf("0) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   65: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   66: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   67: 	mpool_statistics(mp, show);
   68: 
   69: 	printf(">>> realloc 4000 to 5010 addr=%p\n", addr);
   70: 	addr = mpool_realloc(mp, addr, 5010, "|||||||||||||");
   71: 	if (!addr)
   72: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   73: 	mpool_getquota(mp, &curr, &real, NULL);
   74: 	printf("addr=%p_current=%lu_real=%lu\n", addr, curr, real);
   75: 
   76: 	printf("1) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   77: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   78: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   79: 	mpool_statistics(mp, show);
   80: 
   81: 
   82: 	printf(">>> free addr=%p\n", addr);
   83: 	if (addr)
   84: 		mpool_free(mp, addr, 0);
   85: 
   86: 	printf("2) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   87: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   88: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
   89: 	mpool_statistics(mp, show);
   90: 
   91: 	printf(">>> purge inactive memory\n");
   92: 	mpool_purge(mp, 0);
   93: 
   94: 	mpool_getquota(mp, &curr, &real, NULL);
   95: 	printf("___current=%lu ___real=%lu\n", curr, real);
   96: 
   97: 	printf("3) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
   98: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
   99: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
  100: 	mpool_statistics(mp, show);
  101: 
  102: 	printf(">>> alloc new 4000\n");
  103: 	addr = mpool_malloc(mp, 4000, "mdaaa 4000");
  104: 	if (!addr) {
  105: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
  106: 		mpool_destroy(&mp);
  107: 		return 2;
  108: 	}
  109: 	printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
  110: 	printf(">>> and free new 4000 addr=%p\n", addr);
  111: 	mpool_free(mp, addr, 0);
  112: 
  113: 	printf("4) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
  114: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
  115: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
  116: 	mpool_statistics(mp, show);
  117: 
  118: 
  119: 	printf("realloc 4000 to 100000 and to 2000000\n");
  120: 	addr = mpool_malloc(mp, 4000, "mdaaa 4000");
  121: 	printf("malloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
  122: 	addr = mpool_realloc(mp, addr, 100000, "ohche");
  123: 	printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
  124: 	addr = mpool_realloc(mp, addr, 2000000, NULL);
  125: 	printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
  126: 	mpool_free(mp, addr, 0);
  127: 	printf("5) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
  128: 			mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
  129: 			mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
  130: 	mpool_statistics(mp, show);
  131: 
  132: 
  133: 	mpool_dump(mp, NULL);
  134: 	mpool_destroy(&mp);
  135: 	return 0;
  136: }

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