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