Annotation of embedaddon/mpd/src/mbuf.c, revision 1.1.1.3

1.1       misho       1: 
                      2: /*
                      3:  * mbuf.c
                      4:  *
                      5:  * Written by Toshiharu OHNO <tony-o@iij.ad.jp>
                      6:  * Copyright (c) 1993, Internet Initiative Japan, Inc. All rights reserved.
                      7:  * See ``COPYRIGHT.iij''
                      8:  * 
                      9:  * Rewritten by Archie Cobbs <archie@freebsd.org>
                     10:  * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
                     11:  * See ``COPYRIGHT.whistle''
                     12:  */
                     13: 
                     14: #include "ppp.h"
                     15: 
                     16: /*
                     17:  * Malloc()
                     18:  *
                     19:  * Replacement for the usual malloc()
                     20:  */
                     21: 
                     22: void *
                     23: Malloc(const char *type, size_t size)
                     24: {
                     25:     const char **memory;
                     26: 
                     27:     if ((memory = MALLOC(type, sizeof(char *) + size)) == NULL) {
                     28:        Perror("Malloc: malloc");
                     29:        DoExit(EX_ERRDEAD);
                     30:     }
                     31: 
                     32:     memory[0] = type;
                     33:     bzero(memory + 1, size);
                     34:     return (memory + 1);
                     35: }
                     36: 
                     37: /*
                     38:  * Mdup()
                     39:  *
                     40:  * Malloc() + memcpy()
                     41:  */
                     42: 
                     43: void *
                     44: Mdup(const char *type, const void *src, size_t size)
                     45: {
                     46:     const char **memory;
                     47:     if ((memory = MALLOC(type, sizeof(char *) + size)) == NULL) {
                     48:        Perror("Mdup: malloc");
                     49:        DoExit(EX_ERRDEAD);
                     50:     }
                     51: 
                     52:     memory[0] = type;
                     53:     memcpy(memory + 1, src, size);
                     54:     return(memory + 1);
                     55: }
                     56: 
                     57: void *
1.1.1.2   misho      58: Mdup2(const char *type, const void *src, size_t oldsize, size_t newsize)
                     59: {
                     60:     const char **memory;
                     61:     if ((memory = MALLOC(type, sizeof(char *) + newsize)) == NULL) {
                     62:        Perror("Mdup2: malloc");
                     63:        DoExit(EX_ERRDEAD);
                     64:     }
                     65: 
                     66:     memory[0] = type;
                     67:     memcpy(memory + 1, src, oldsize < newsize ? oldsize : newsize);
                     68:     return(memory + 1);
                     69: }
                     70: 
                     71: void *
1.1       misho      72: Mstrdup(const char *type, const void *src)
                     73: {
                     74:     return (Mdup(type, src, strlen(src) + 1));
                     75: }
                     76: 
                     77: /*
                     78:  * Freee()
                     79:  *
                     80:  * Replacement for the ususal free()
                     81:  */
                     82: 
                     83: void
                     84: Freee(void *ptr)
                     85: {
                     86:     if (ptr) {
                     87:        char    **memory = ptr;
                     88:        memory--;
                     89:        FREE(memory[0], memory);
                     90:     }
                     91: }
                     92: 
                     93: /*
                     94:  * mballoc()
                     95:  *
                     96:  * Allocate an mbuf with memory
                     97:  */
                     98: 
                     99: Mbuf
                    100: mballoc(int size)
                    101: {
                    102:     u_char     *memory;
                    103:     int                amount, osize;
                    104:     Mbuf       bp;
                    105: 
                    106:     assert(size >= 0);
                    107: 
                    108:     if (size == 0) {
                    109:        osize = 64 - sizeof(*bp);
                    110:     } else if (size < 512)
                    111:        osize = ((size - 1) / 32 + 1) * 64 - sizeof(*bp);
                    112:     else
                    113:        osize = ((size - 1) / 64 + 1) * 64 + 512 - sizeof(*bp);
                    114:     amount = sizeof(*bp) + osize;
                    115: 
                    116:     if ((memory = MALLOC(MB_MBUF, amount)) == NULL) {
                    117:        Perror("mballoc: malloc");
                    118:        DoExit(EX_ERRDEAD);
                    119:     }
                    120: 
                    121:     /* Put mbuf at front of memory region */
                    122:     bp = (Mbuf)(void *)memory;
                    123:     bp->size = osize;
                    124:     bp->offset = (osize - size) / 2;
                    125:     bp->cnt = 0;
                    126: 
                    127:     return (bp);
                    128: }
                    129: 
                    130: /*
                    131:  * mbfree()
                    132:  *
                    133:  * Free head of chain, return next
                    134:  */
                    135: 
                    136: void
                    137: mbfree(Mbuf bp)
                    138: {
                    139:     if (bp)
                    140:        FREE(MB_MBUF, bp);
                    141: }
                    142: 
                    143: /*
                    144:  * mbread()
                    145:  *
                    146:  * Read contents of an mbuf chain into buffer, consuming len bytes.
                    147:  * If all of the chain is consumed, return NULL.
                    148:  *
                    149:  * This should ALWAYS be called like this:
                    150:  *     bp = mbread(bp, ... );
                    151:  */
                    152: 
                    153: Mbuf
                    154: mbread(Mbuf bp, void *buf, int cnt)
                    155: {
                    156:     int nread;
                    157: 
                    158:     assert(cnt >= 0);
                    159: 
                    160:     if (!bp)
                    161:        return (NULL);
                    162:     if (cnt > bp->cnt)
                    163:        nread = bp->cnt;
                    164:     else
                    165:         nread = cnt;
                    166:     memcpy(buf, MBDATAU(bp), nread);
                    167:     bp->offset += nread;
                    168:     bp->cnt -= nread;
                    169:     if (bp->cnt == 0) {
                    170:        mbfree(bp);
                    171:        return (NULL);
                    172:     }
                    173:     return(bp);
                    174: }
                    175: 
                    176: /*
                    177:  * mbcopy()
                    178:  *
                    179:  * Copy contents of an mbuf chain into buffer, up to "cnt" bytes.
                    180:  * This does not consume any of the mbuf chain. Returns number copied.
                    181:  */
                    182: 
                    183: int
                    184: mbcopy(Mbuf bp, int offset, void *buf, int cnt)
                    185: {
                    186:     int nread;
                    187: 
                    188:     assert(offset >= 0);
                    189:     assert(cnt >= 0);
                    190: 
                    191:     if (!bp)
                    192:        return (0);
                    193:     if (offset >= bp->cnt)
                    194:        return (0);
                    195: 
                    196:     if (cnt > bp->cnt - offset)
                    197:        nread = bp->cnt - offset;
                    198:     else
                    199:         nread = cnt;
                    200:     memcpy(buf, MBDATAU(bp) + offset, nread);
                    201:     return (nread);
                    202: }
                    203: 
                    204: /*
                    205:  * mbcopyback()
                    206:  *
                    207:  * Write bytes from buffer into an mbuf chain. Returns first argument.
                    208:  */
                    209: 
                    210: Mbuf
                    211: mbcopyback(Mbuf bp, int offset, const void *buf, int cnt)
                    212: {
                    213:     int                b, e;
                    214: 
                    215:     if (!bp) {
                    216:        if (offset < 0)
                    217:            offset = 0;
                    218:        bp = mballoc(offset + cnt);
                    219:        memcpy(MBDATAU(bp) + offset, buf, cnt);
                    220:        bp->cnt = offset + cnt;
                    221:        return (bp);
                    222:     }
                    223: 
                    224:     b = (offset > 0) ? 0 : -offset;
                    225:     e = (offset + cnt > bp->cnt) ? offset + cnt - bp->cnt : 0;
                    226:     
                    227:     if (b + bp->cnt + e > bp->size) {
                    228:        Mbuf    nbp = mballoc(b + bp->cnt + e);
                    229:        memcpy(MBDATAU(nbp) + b, MBDATAU(bp), bp->cnt);
                    230:        nbp->cnt = bp->cnt;
                    231:        mbfree(bp);
                    232:        bp = nbp;
                    233:     } else if ((b > bp->offset) || (bp->offset + bp->cnt + e > bp->size)) {
                    234:        int     noff = (bp->size - (b + bp->cnt + e)) / 2;
                    235:        memmove(MBDATAU(bp) - bp->offset + noff + b, MBDATAU(bp), bp->cnt);
                    236:        bp->offset = noff;
                    237:     } else {
                    238:        bp->offset -= b;
                    239:     }
                    240:     bp->cnt = b + bp->cnt + e;
                    241:     memcpy(MBDATAU(bp) + offset + b, buf, cnt);
                    242:     return(bp);
                    243: }
                    244: 
                    245: /*
                    246:  * mbtrunc()
                    247:  *
                    248:  * Truncate mbuf to total of "max" bytes. If max is zero
                    249:  * then a zero length mbuf is returned (rather than a NULL mbuf).
                    250:  */
                    251: 
                    252: Mbuf
                    253: mbtrunc(Mbuf bp, int max)
                    254: {
                    255:     assert(max >= 0);
                    256: 
                    257:     if (!bp)
                    258:        return (NULL);
                    259: 
                    260:     if (bp->cnt > max)
                    261:        bp->cnt = max;
                    262: 
                    263:     return (bp);
                    264: }
                    265: 
                    266: /*
                    267:  * mbadj()
                    268:  *
                    269:  * Truncate mbuf cutting "cnt" bytes from begin or end.
                    270:  */
                    271: 
                    272: Mbuf
                    273: mbadj(Mbuf bp, int cnt)
                    274: {
                    275:     if (!bp)
                    276:        return (NULL);
                    277: 
                    278:     if (cnt >= 0) {
                    279:        if (bp->cnt > cnt) {
                    280:            bp->cnt -= cnt;
                    281:            bp->offset += cnt;
                    282:        } else {
                    283:            bp->cnt = 0;
                    284:        }
                    285:     } else {
                    286:        if (bp->cnt > -cnt) {
                    287:            bp->cnt -= -cnt;
                    288:        } else {
                    289:            bp->cnt = 0;
                    290:        }
                    291:     }
                    292: 
                    293:     return (bp);
                    294: }
                    295: 
                    296: /*
                    297:  * mbsplit()
                    298:  *
                    299:  * Break an mbuf chain after "cnt" bytes.
                    300:  * Return the newly created mbuf chain that
                    301:  * starts after "cnt" bytes. If MBLEN(bp) <= cnt,
                    302:  * then returns NULL.  The first part of
                    303:  * the chain remains pointed to by "bp".
                    304:  */
                    305: 
                    306: Mbuf
                    307: mbsplit(Mbuf bp, int cnt)
                    308: {
                    309:     Mbuf       nbp;
                    310:     
                    311:     assert(cnt >= 0);
                    312: 
                    313:     if (!bp)
                    314:        return (NULL);
                    315: 
1.1.1.3 ! misho     316:     if ((int)MBLEN(bp) <= cnt)
1.1       misho     317:        return (NULL);
                    318: 
                    319:     nbp = mballoc(bp->cnt - cnt);
                    320:     memcpy(MBDATAU(nbp), MBDATAU(bp) + cnt, bp->cnt - cnt);
                    321:     nbp->cnt = bp->cnt - cnt;
                    322:     bp->cnt = cnt;
                    323: 
                    324:     return(nbp);
                    325: }
                    326: 
                    327: /*
                    328:  * MemStat()
                    329:  */
                    330: 
                    331: int
1.1.1.3 ! misho     332: MemStat(Context ctx, int ac, const char *const av[], const void *arg)
1.1       misho     333: {
                    334:     struct typed_mem_stats stats;
                    335:     u_int      i;
                    336:     u_int      total_allocs = 0;
                    337:     u_int      total_bytes = 0;
                    338: 
1.1.1.3 ! misho     339:     (void)ac;
        !           340:     (void)av;
        !           341:     (void)arg;
        !           342: 
1.1       misho     343:     if (typed_mem_usage(&stats))
                    344:        Error("typed_mem_usage() error");
                    345:     
                    346:     /* Print header */
                    347:     Printf("   %-28s %10s %10s\r\n", "Type", "Count", "Total");
                    348:     Printf("   %-28s %10s %10s\r\n", "----", "-----", "-----");
                    349: 
                    350:     for (i = 0; i < stats.length; i++) {
                    351:        struct typed_mem_typestats *type = &stats.elems[i];
                    352: 
                    353:        Printf("   %-28s %10u %10lu\r\n",
                    354:            type->type, (int)type->allocs, (u_long)type->bytes);
                    355:        total_allocs += type->allocs;
                    356:        total_bytes += type->bytes;
                    357:     }
                    358:     /* Print totals */
                    359:     Printf("   %-28s %10s %10s\r\n", "", "-----", "-----");
                    360:     Printf("   %-28s %10lu %10lu\r\n",
                    361:         "Totals", total_allocs, total_bytes);
                    362: 
                    363:     structs_free(&typed_mem_stats_type, NULL, &stats);
                    364:     return(0);
                    365: }
                    366: 

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