xref: /illumos-gate/usr/src/lib/krb5/dyn/dyn_create.c (revision 1e56f352c1c208679012bca47d552e127f5b1072)
1 /*
2  * This file is part of libdyn.a, the C Dynamic Object library.  It
3  * contains the source code for the functions DynCreate() and
4  * DynDestroy().
5  *
6  * There are no restrictions on this code; however, if you make any
7  * changes, I request that you document them so that I do not get
8  * credit or blame for your modifications.
9  *
10  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
11  * and MIT-Project Athena, 1989.
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "dynP.h"
19 
20 #ifndef DEFAULT_INC
21 #define DEFAULT_INC	100
22 #endif
23 
24 static int default_increment = DEFAULT_INC;
25 
26 DynObjectP DynCreate(el_size, inc)
27    int	el_size, inc;
28 {
29      DynObjectP obj;
30 
31      obj = (DynObjectP) malloc(sizeof(DynObjectRecP));
32      if (obj == NULL)
33 	  return NULL;
34 
35 #ifdef USE_DBMALLOC
36      obj->array = (DynPtr) malloc(1);
37 #else
38      obj->array = (DynPtr) malloc(0);
39 #endif
40      obj->el_size = el_size;
41      obj->num_el = obj->size = 0;
42      obj->debug = obj->paranoid = 0;
43      obj->inc = (!! inc) ? inc : default_increment;
44 
45      return obj;
46 }
47 
48 DynObjectP DynCopy(obj)
49    DynObjectP obj;
50 {
51      DynObjectP obj1;
52 
53      obj1 = (DynObjectP) malloc(sizeof(DynObjectRecP));
54      if (obj1 == NULL)
55 	  return NULL;
56 
57      obj1->el_size = obj->el_size;
58      obj1->num_el = obj->num_el;
59      obj1->size = obj->size;
60      obj1->inc = obj->inc;
61      obj1->debug = obj->debug;
62      obj1->paranoid = obj->paranoid;
63      obj1->initzero = obj->initzero;
64      obj1->array = (char *) malloc(obj1->el_size * obj1->size);
65      if (obj1->array == NULL) {
66 	  free(obj1);
67 	  return NULL;
68      }
69      memcpy(obj->array, obj1->array,
70 	    (size_t) (obj1->el_size * obj1->size));
71 
72      return obj1;
73 }
74 
75 int DynDestroy(obj)
76    DynObjectP obj;
77 {
78      if (obj->paranoid) {
79 	  if (obj->debug)
80 	       fprintf(stderr, "dyn: destroy: zeroing %d bytes from %d.\n",
81 		       obj->el_size * obj->size, obj->array);
82 	  memset(obj->array, 0, obj->el_size * obj->size);
83      }
84      free(obj->array);
85      free(obj);
86      return DYN_OK;
87 }
88 
89 int DynRelease(obj)
90    DynObjectP obj;
91 {
92      if (obj->debug)
93 	  fprintf(stderr, "dyn: release: freeing object structure.\n");
94      free(obj);
95      return DYN_OK;
96 }
97