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