1: /*
   2: **  NEED.C -- general buffer allocation routines
   3: **
   4: **	allow buffers with LIFO de-allocation
   5: **
   6: **	Defines:
   7: **		need()
   8: **		initbuf()
   9: **
  10: **	History:
  11: **		10/10/78 -- (marc) modified from need in ovqp.
  12: **			New are : err_num, err_func stuff, and
  13: **			seterr().
  14: */
  15: 
  16: 
  17: 
  18: 
  19: 
  20: 
  21: /* structure that the routines use to allocate space */
  22: struct nodbuffer
  23: {
  24:     int     nleft;      /* bytes left */
  25:     int     err_num;    /* error code on overflow */
  26:     int     (*err_func)();  /* error function on overflow */
  27:     char        *xfree;     /* next free byte */
  28:     char        buffer [];  /*beginning of buffer area */
  29: };
  30: 
  31: /*
  32: **  NEED -- allocate space from a buffer
  33: **
  34: **	On buffer overflow, calls err_func from that field
  35: **	in the buffer with the error code err_code from that field
  36: **	in the buffer, then returns 0.
  37: **	need() guarantees an even adress on return.
  38: **
  39: **	Parameters:
  40: **		bf -- buffer
  41: **		nbytes -- number of bytes desired
  42: **
  43: **	Returns:
  44: **		pointer to allocated area
  45: **		on  buffer overflow returns 0.
  46: **
  47: **	Side Effects:
  48: **		adjusts buffer structure to reflect allocation.
  49: **
  50: */
  51: 
  52: char *
  53: need(bf, nbytes)
  54: struct nodbuffer    *bf;
  55: int         nbytes;
  56: {
  57:     register char           *x;
  58:     register struct nodbuffer   *buf;
  59:     register int            i;
  60: 
  61:     buf = bf;
  62:     i = nbytes;
  63:     if (i > buf->nleft)
  64:     {
  65:             (*buf->err_func)(buf->err_num);
  66:             return (0);
  67:     }
  68:     i += i & 01;
  69:     x = buf->xfree;
  70:     buf->xfree += i;
  71:     buf->nleft -= i;
  72:     return(x);
  73: }
  74: 
  75: /*
  76: **  INITBUF -- initialize a buffer
  77: **
  78: **	Must be called before the first need() call on the buffer.
  79: **
  80: **	Parameters:
  81: **		bf -- buffer
  82: **		size -- size fo buffer area
  83: **		err_num -- error code for overflow
  84: **		err_func -- function to call with err_code on error
  85: **
  86: **	Returns:
  87: **		none
  88: **
  89: **	Side Effects:
  90: **		initializes buffer structure
  91: **
  92: **	Diagnostics:
  93: **		"initbuf : odd buffer adress 0%o" -- buffers must start
  94: **			at an even adress.
  95: */
  96: 
  97: initbuf(bf, size, err_num, err_func)
  98: struct nodbuffer    *bf;
  99: int         size;
 100: int         err_num;
 101: int         (*err_func)();
 102: {
 103:     register struct nodbuffer   *buf;
 104: 
 105:     buf = bf;
 106:     if ((unsigned int) buf & 01)
 107:         syserr("initbuf : odd buffer adress 0%o", buf);
 108:     buf->nleft = size - sizeof *buf;
 109:     buf->xfree = buf->buffer;
 110:     buf->err_num = err_num;
 111:     buf->err_func = err_func;
 112: }

Defined functions

Defined struct's

nodbuffer defined in line 22; used 8 times
Last modified: 1995-02-09
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 566
Valid CSS Valid XHTML 1.0 Strict