xref: /titanic_50/usr/src/lib/krb5/dyn/dyn_realloc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * This file is part of libdyn.a, the C Dynamic Object library.  It
5*7c478bd9Sstevel@tonic-gate  * contains the source code for the internal function _DynRealloc().
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * There are no restrictions on this code; however, if you make any
8*7c478bd9Sstevel@tonic-gate  * changes, I request that you document them so that I do not get
9*7c478bd9Sstevel@tonic-gate  * credit or blame for your modifications.
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
12*7c478bd9Sstevel@tonic-gate  * and MIT-Project Athena, 1989.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #include <stdio.h>
16*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include "dynP.h"
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate /*
21*7c478bd9Sstevel@tonic-gate  * Resize the array so that element req exists.
22*7c478bd9Sstevel@tonic-gate  */
_DynResize(obj,req)23*7c478bd9Sstevel@tonic-gate int _DynResize(obj, req)
24*7c478bd9Sstevel@tonic-gate    DynObjectP obj;
25*7c478bd9Sstevel@tonic-gate    int req;
26*7c478bd9Sstevel@tonic-gate {
27*7c478bd9Sstevel@tonic-gate      int cnt, size;
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate      if (obj->size > req)
30*7c478bd9Sstevel@tonic-gate 	  return DYN_OK;
31*7c478bd9Sstevel@tonic-gate      else if (obj->inc > 0)
32*7c478bd9Sstevel@tonic-gate 	  return _DynRealloc(obj, (req - obj->size) / obj->inc + 1);
33*7c478bd9Sstevel@tonic-gate      else {
34*7c478bd9Sstevel@tonic-gate 	  if (obj->size == 0)
35*7c478bd9Sstevel@tonic-gate 	       size = -obj->inc;
36*7c478bd9Sstevel@tonic-gate 	  else
37*7c478bd9Sstevel@tonic-gate 	       size = obj->size;
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 	  while (size <= req)
40*7c478bd9Sstevel@tonic-gate 	       size <<= 1;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 	  return _DynRealloc(obj, size);
43*7c478bd9Sstevel@tonic-gate      }
44*7c478bd9Sstevel@tonic-gate }
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Resize the array by num_incs units.  If obj->inc is positive, this
48*7c478bd9Sstevel@tonic-gate  * means make it obj->inc*num_incs elements larger.  If obj->inc is
49*7c478bd9Sstevel@tonic-gate  * negative, this means make the array num_incs elements long.
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  * Ideally, this function should not be called from outside the
52*7c478bd9Sstevel@tonic-gate  * library.  However, nothing will break if it is.
53*7c478bd9Sstevel@tonic-gate  */
_DynRealloc(obj,num_incs)54*7c478bd9Sstevel@tonic-gate int _DynRealloc(obj, num_incs)
55*7c478bd9Sstevel@tonic-gate    DynObjectP obj;
56*7c478bd9Sstevel@tonic-gate    int num_incs;
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate      DynPtr temp;
59*7c478bd9Sstevel@tonic-gate      int new_size_in_bytes;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate      if (obj->inc > 0)
62*7c478bd9Sstevel@tonic-gate 	  new_size_in_bytes = obj->el_size*(obj->size + obj->inc*num_incs);
63*7c478bd9Sstevel@tonic-gate      else
64*7c478bd9Sstevel@tonic-gate 	  new_size_in_bytes = obj->el_size*num_incs;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate      if (obj->debug)
67*7c478bd9Sstevel@tonic-gate 	  fprintf(stderr,
68*7c478bd9Sstevel@tonic-gate 		  "dyn: alloc: Increasing object by %d bytes (%d incs).\n",
69*7c478bd9Sstevel@tonic-gate 		  new_size_in_bytes - obj->el_size*obj->size,
70*7c478bd9Sstevel@tonic-gate 		  num_incs);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate      temp = (DynPtr) realloc(obj->array, new_size_in_bytes);
73*7c478bd9Sstevel@tonic-gate      if (temp == NULL) {
74*7c478bd9Sstevel@tonic-gate 	  if (obj->debug)
75*7c478bd9Sstevel@tonic-gate 	       fprintf(stderr, "dyn: alloc: Out of memory.\n");
76*7c478bd9Sstevel@tonic-gate 	  return DYN_NOMEM;
77*7c478bd9Sstevel@tonic-gate      }
78*7c478bd9Sstevel@tonic-gate      else {
79*7c478bd9Sstevel@tonic-gate 	  obj->array = temp;
80*7c478bd9Sstevel@tonic-gate 	  if (obj->inc > 0)
81*7c478bd9Sstevel@tonic-gate 	       obj->size += obj->inc*num_incs;
82*7c478bd9Sstevel@tonic-gate 	  else
83*7c478bd9Sstevel@tonic-gate 	       obj->size = num_incs;
84*7c478bd9Sstevel@tonic-gate      }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate      if (obj->debug)
87*7c478bd9Sstevel@tonic-gate 	  fprintf(stderr, "dyn: alloc: done.\n");
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate      return DYN_OK;
90*7c478bd9Sstevel@tonic-gate }
91