| 1 | #include "gp_alloc.h" | 
|---|
| 2 | /* #include "gp_graph3d.h" */ | 
|---|
| 3 | #include <math.h> | 
|---|
| 4 | #define TRACE_ALLOC(x) | 
|---|
| 5 | struct frame_struct { | 
|---|
| 6 | char *use; | 
|---|
| 7 | int requested_size; | 
|---|
| 8 | int pad;                    /* preserve 8-byte alignment */ | 
|---|
| 9 | int checksum; | 
|---|
| 10 | }; | 
|---|
| 11 |  | 
|---|
| 12 | #define CHECKSUM_INT 0xcaac5e1f | 
|---|
| 13 | #define RESERVED_SIZE sizeof(struct frame_struct) | 
|---|
| 14 | #define CHECKSUM_CHAR 0xc5 | 
|---|
| 15 |  | 
|---|
| 16 | static long bytes_allocated = 0; | 
|---|
| 17 |  | 
|---|
| 18 | static void | 
|---|
| 19 | mark(p, size, usage) | 
|---|
| 20 | struct frame_struct *p; | 
|---|
| 21 | unsigned long size; | 
|---|
| 22 | char *usage; | 
|---|
| 23 | { | 
|---|
| 24 | p->use = usage; | 
|---|
| 25 | p->requested_size = size; | 
|---|
| 26 | p->checksum = (CHECKSUM_INT ^ (int) (p->use) ^ size); | 
|---|
| 27 | ((unsigned char *) (p + 1))[size] = CHECKSUM_CHAR; | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | void * | 
|---|
| 31 | gp_alloc(size, usage) | 
|---|
| 32 | size_t size; | 
|---|
| 33 | const char *usage; | 
|---|
| 34 | { | 
|---|
| 35 | struct frame_struct *p; | 
|---|
| 36 | size_t total_size = size + RESERVED_SIZE + 1; | 
|---|
| 37 |  | 
|---|
| 38 | /*TRACE_ALLOC(("gp_alloc %d for %s\n", (int) size, usage ? usage : "<unknown>"));*/ | 
|---|
| 39 | /*printf("gp_alloc %d for %s\n", (int) total_size, usage ? usage : "<unknown>");*/ | 
|---|
| 40 | p = malloc(total_size); | 
|---|
| 41 | if (!p){ printf("gp_alloc :  Out of memory !!!! \n"); exit(-99);} | 
|---|
| 42 | /*int_error(NO_CARET, "Out of memory");*/ | 
|---|
| 43 |  | 
|---|
| 44 | bytes_allocated += size; | 
|---|
| 45 |  | 
|---|
| 46 | /*mark(p, size, usage);*/ | 
|---|
| 47 |  | 
|---|
| 48 | /*return (char *) (p+1);*/ | 
|---|
| 49 | return (char *) (p); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void gp_free(void *p){ | 
|---|
| 53 | free(p); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void contour_free(struct gnuplot_contours *ptr){ | 
|---|
| 57 | //char *c; | 
|---|
| 58 | struct coordinate *coords; | 
|---|
| 59 | struct gnuplot_contours *old; | 
|---|
| 60 | struct gnuplot_contours *cur; | 
|---|
| 61 |  | 
|---|
| 62 | cur = ptr; | 
|---|
| 63 | //printf("\n contour_free  %x \n",cur); | 
|---|
| 64 | while(cur){ | 
|---|
| 65 | /*printf(" contour_free  %x \n",cur);*/ | 
|---|
| 66 | old = cur; | 
|---|
| 67 | cur = cur->next; | 
|---|
| 68 | coords = old->coords; | 
|---|
| 69 | if(coords){free(coords) ; coords = NULL;} | 
|---|
| 70 | free(old); | 
|---|
| 71 | old=NULL; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | } | 
|---|
| 76 | /* | 
|---|
| 77 | * iso_alloc() allocates a iso_curve structure that can hold 'num' | 
|---|
| 78 | * points. | 
|---|
| 79 | */ | 
|---|
| 80 | struct iso_curve * | 
|---|
| 81 | iso_alloc(num) | 
|---|
| 82 | int num; | 
|---|
| 83 | { | 
|---|
| 84 | struct iso_curve *ip; | 
|---|
| 85 | ip = (struct iso_curve *) gp_alloc(sizeof(struct iso_curve), "iso curve"); | 
|---|
| 86 | ip->p_max = (num >= 0 ? num : 0); | 
|---|
| 87 | if (num > 0) { | 
|---|
| 88 | ip->points = (struct coordinate  *) | 
|---|
| 89 | gp_alloc(num * sizeof(struct coordinate), "iso curve points"); | 
|---|
| 90 | } else | 
|---|
| 91 | ip->points = (struct coordinate  *) NULL; | 
|---|
| 92 | ip->next = NULL; | 
|---|
| 93 | return (ip); | 
|---|
| 94 | } | 
|---|
| 95 | /* | 
|---|
| 96 | * iso_free() releases any memory which was previously malloc()'d to hold | 
|---|
| 97 | *   iso curve points. | 
|---|
| 98 | */ | 
|---|
| 99 | void iso_free(struct iso_curve *ip) | 
|---|
| 100 | { | 
|---|
| 101 | if (ip) { | 
|---|
| 102 | if (ip->points) | 
|---|
| 103 | free((char *) ip->points); | 
|---|
| 104 | free((char *) ip); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|