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