Annotation of libelwix/example/test_mem.c, revision 1.7

1.1       misho       1: #include <stdio.h>
1.2       misho       2: #include <string.h>
1.1       misho       3: #include <stdlib.h>
                      4: #include <elwix.h>
                      5: 
                      6: 
1.6       misho       7: int show(unsigned int size, unsigned int act, unsigned int inact, void *data, unsigned int dlen)
1.1       misho       8: {
                      9:        if (!act && !inact)
1.6       misho      10:                return -1;
1.1       misho      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);
1.6       misho      18: 
                     19:        return 0;
1.1       misho      20: }
                     21: 
                     22: int
                     23: main(int argc, char **argv)
                     24: {
                     25:        mpool_t *mp;
                     26:        void *addr;
                     27:        int i;
1.4       misho      28:        u_long curr, real;
1.1       misho      29: 
1.3       misho      30:        printf("whether default memory mapper is elwix? %d\n", elwix_mm_inuse());
                     31: 
1.1       misho      32:        for (i = 0; i < 3; i++) {
                     33:                addr = malloc(4000);
                     34:                printf("addr=%p\n", addr);
                     35:                free(addr);
                     36:        }
                     37: 
                     38:        mp = mpool_init(0);
                     39:        if (!mp) {
                     40:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     41:                return 1;
                     42:        }
                     43:        /*
1.4       misho      44:        mpool_getquota(mp, &curr, NULL, NULL);
1.1       misho      45:        printf("___current=%lu\n", curr);
                     46:        */
                     47: 
1.5       misho      48:        printf(">>> get 3 allocs\n");
1.1       misho      49:        for (i = 0; i < 3; i++) {
                     50:                addr = mpool_malloc(mp, 4000, "mdaaa 4000");
                     51:                if (!addr) {
                     52:                        printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     53:                        mpool_destroy(&mp);
                     54:                        return 2;
                     55:                }
                     56:                printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
                     57: 
                     58:                strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n", 
                     59:                                mpool_getsizebyaddr(addr));
1.7     ! misho      60:                mpool_free(mp, addr, 0);
1.1       misho      61:        }
                     62: 
1.4       misho      63:        mpool_getquota(mp, &curr, &real, NULL);
                     64:        printf("___current=%lu ___real=%lu\n", curr, real);
1.1       misho      65: 
                     66:        printf("0) 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);
1.6       misho      69:        mpool_statistics(mp, show, NULL, 0);
1.1       misho      70: 
1.5       misho      71:        printf(">>> realloc 4000 to 5010 addr=%p\n", addr);
1.1       misho      72:        addr = mpool_realloc(mp, addr, 5010, "|||||||||||||");
                     73:        if (!addr)
                     74:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
1.4       misho      75:        mpool_getquota(mp, &curr, &real, NULL);
                     76:        printf("addr=%p_current=%lu_real=%lu\n", addr, curr, real);
1.1       misho      77: 
                     78:        printf("1) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
                     79:                        mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
                     80:                        mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
1.6       misho      81:        mpool_statistics(mp, show, NULL, 0);
1.1       misho      82: 
                     83: 
1.5       misho      84:        printf(">>> free addr=%p\n", addr);
1.1       misho      85:        if (addr)
                     86:                mpool_free(mp, addr, 0);
                     87: 
                     88:        printf("2) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
                     89:                        mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
                     90:                        mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
1.6       misho      91:        mpool_statistics(mp, show, NULL, 0);
1.1       misho      92: 
1.5       misho      93:        printf(">>> purge inactive memory\n");
1.1       misho      94:        mpool_purge(mp, 0);
                     95: 
1.4       misho      96:        mpool_getquota(mp, &curr, &real, NULL);
                     97:        printf("___current=%lu ___real=%lu\n", curr, real);
1.1       misho      98: 
                     99:        printf("3) 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);
1.6       misho     102:        mpool_statistics(mp, show, NULL, 0);
1.1       misho     103: 
1.5       misho     104:        printf(">>> alloc new 4000\n");
1.1       misho     105:        addr = mpool_malloc(mp, 4000, "mdaaa 4000");
                    106:        if (!addr) {
                    107:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                    108:                mpool_destroy(&mp);
                    109:                return 2;
                    110:        }
                    111:        printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
1.5       misho     112:        printf(">>> and free new 4000 addr=%p\n", addr);
1.1       misho     113:        mpool_free(mp, addr, 0);
                    114: 
                    115:        printf("4) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
                    116:                        mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
                    117:                        mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
1.6       misho     118:        mpool_statistics(mp, show, NULL, 0);
1.5       misho     119: 
                    120: 
                    121:        printf("realloc 4000 to 100000 and to 2000000\n");
                    122:        addr = mpool_malloc(mp, 4000, "mdaaa 4000");
                    123:        printf("malloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
                    124:        addr = mpool_realloc(mp, addr, 100000, "ohche");
                    125:        printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
                    126:        addr = mpool_realloc(mp, addr, 2000000, NULL);
                    127:        printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
                    128:        mpool_free(mp, addr, 0);
                    129:        printf("5) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n", 
                    130:                        mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free, 
                    131:                        mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
1.6       misho     132:        mpool_statistics(mp, show, NULL, 0);
1.5       misho     133: 
1.7     ! misho     134:        addr = mpool_calloc(mp, 56, 1, NULL);
        !           135:        printf("calloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
        !           136:        mpool_free(mp, addr, 0);
        !           137:        addr = mpool_calloc(mp, 60, 1, "alabala");
        !           138:        printf("calloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
        !           139:        mpool_free(mp, addr, 0);
1.5       misho     140: 
                    141:        mpool_dump(mp, NULL);
1.1       misho     142:        mpool_destroy(&mp);
                    143:        return 0;
                    144: }

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