Annotation of embedaddon/ntp/ports/winnt/libntp/transmitbuff.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * DEAD CODE ALERT  --  for whatever reason all this wonderful stuff is
                      3:  *                     unused.  The initialization was the only code
                      4:  *                     exercised as of May 2009 when that was nipped.
                      5:  */
                      6: 
                      7: 
                      8: #ifdef HAVE_CONFIG_H
                      9: # include <config.h>
                     10: #endif
                     11: 
                     12: #include <stdio.h>
                     13: #include "ntp_machine.h"
                     14: #include "ntp_fp.h"
                     15: #include "ntp_stdlib.h"
                     16: #include "ntp_syslog.h"
                     17: 
                     18: #include <isc/list.h>
                     19: #include "transmitbuff.h"
                     20: 
                     21: /*
                     22:  * transmitbuf memory management
                     23:  */
                     24: #define TRANSMIT_INIT          10      /* 10 buffers initially */
                     25: #define TRANSMIT_LOWAT          3      /* when we're down to three buffers get more */
                     26: #define TRANSMIT_INC            5      /* get 5 more at a time */
                     27: #define TRANSMIT_TOOMANY       40      /* this is way too many buffers */
                     28: 
                     29: /*
                     30:  * Memory allocation
                     31:  */
                     32: static volatile u_long full_transmitbufs = 0;  /* number of transmitbufs on fulllist */
                     33: static volatile u_long free_transmitbufs = 0;  /* number of transmitbufs on freelist */
                     34: 
                     35: ISC_LIST(transmitbuf_t)        free_transmit_list;     /* Currently used transmit buffers */
                     36: ISC_LIST(transmitbuf_t)        full_transmit_list;     /* Currently used transmit buffers */
                     37: 
                     38: static u_long total_transmitbufs = 0;          /* total transmitbufs currently in use */
                     39: static u_long lowater_additions = 0;           /* number of times we have added memory */
                     40: 
                     41: static CRITICAL_SECTION TransmitLock;
                     42: # define LOCK(lock)    EnterCriticalSection(lock)
                     43: # define UNLOCK(lock)  LeaveCriticalSection(lock)
                     44: 
                     45: static inline void 
                     46: initialise_buffer(transmitbuf *buff)
                     47: {
                     48:        memset(buff, 0, sizeof(*buff));
                     49: }
                     50: 
                     51: static void
                     52: add_buffer_to_freelist(transmitbuf *tb)
                     53: {
                     54:        ISC_LIST_APPEND(free_transmit_list, tb, link);
                     55:        free_transmitbufs++;
                     56: }
                     57: 
                     58: static void
                     59: create_buffers(int nbufs)
                     60: {
                     61:        transmitbuf_t *buf;
                     62:        int i;
                     63: 
                     64:        buf = emalloc(nbufs * sizeof(*buf));
                     65:        for (i = 0; i < nbufs; i++)
                     66:        {
                     67:                initialise_buffer(buf);
                     68:                add_buffer_to_freelist(buf);
                     69:                total_transmitbufs++;
                     70:                buf++;
                     71:        }
                     72: 
                     73:        lowater_additions++;
                     74: }
                     75: 
                     76: extern void
                     77: init_transmitbuff(void)
                     78: {
                     79:        /*
                     80:         * Init buffer free list and stat counters
                     81:         */
                     82:        ISC_LIST_INIT(full_transmit_list);
                     83:        ISC_LIST_INIT(free_transmit_list);
                     84:        free_transmitbufs = total_transmitbufs = 0;
                     85:        full_transmitbufs = lowater_additions = 0;
                     86:        create_buffers(TRANSMIT_INIT);
                     87: 
                     88:        InitializeCriticalSection(&TransmitLock);
                     89: }
                     90: 
                     91: static void
                     92: delete_buffer_from_full_list(transmitbuf_t *tb) {
                     93: 
                     94:        transmitbuf_t *next = NULL;
                     95:        transmitbuf_t *lbuf = ISC_LIST_HEAD(full_transmit_list);
                     96: 
                     97:        while (lbuf != NULL) {
                     98:                next = ISC_LIST_NEXT(lbuf, link);
                     99:                if (lbuf == tb) {
                    100:                        ISC_LIST_DEQUEUE(full_transmit_list, lbuf, link);
                    101:                        break;
                    102:                }
                    103:                else
                    104:                        lbuf = next;
                    105:        }
                    106:        full_transmitbufs--;
                    107: }
                    108: 
                    109: extern void
                    110: free_transmit_buffer(transmitbuf_t *rb)
                    111: {
                    112:        LOCK(&TransmitLock);
                    113:        delete_buffer_from_full_list(rb);
                    114:        add_buffer_to_freelist(rb);
                    115:        UNLOCK(&TransmitLock);
                    116: }
                    117: 
                    118: 
                    119: extern transmitbuf *
                    120: get_free_transmit_buffer(void)
                    121: {
                    122: 
                    123:        transmitbuf_t * buffer = NULL;
                    124:        LOCK(&TransmitLock);
                    125:        if (free_transmitbufs <= 0) {
                    126:                create_buffers(TRANSMIT_INC);
                    127:        }
                    128:        buffer = ISC_LIST_HEAD(free_transmit_list);
                    129:        if (buffer != NULL)
                    130:        {
                    131:                ISC_LIST_DEQUEUE(free_transmit_list, buffer, link);
                    132:                free_transmitbufs--;
                    133:                ISC_LIST_APPEND(full_transmit_list, buffer, link);
                    134:                full_transmitbufs++;
                    135:        }
                    136:        UNLOCK(&TransmitLock);
                    137:        return (buffer);
                    138: }
                    139: 

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