1 | #include "gp_alloc.h"
|
---|
2 | #include "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 | while(cur){
|
---|
64 | old = cur;
|
---|
65 | cur = cur->next;
|
---|
66 | c=old->label;
|
---|
67 | coords = old->coords;
|
---|
68 | if(c){free(c); c=NULL;}
|
---|
69 | if(coords){free(coords) ; coords = NULL;}
|
---|
70 | free(old);
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 | /*
|
---|
75 | * iso_alloc() allocates a iso_curve structure that can hold 'num'
|
---|
76 | * points.
|
---|
77 | */
|
---|
78 | struct iso_curve *
|
---|
79 | iso_alloc(num)
|
---|
80 | int num;
|
---|
81 | {
|
---|
82 | struct iso_curve *ip;
|
---|
83 | ip = (struct iso_curve *) gp_alloc(sizeof(struct iso_curve), "iso curve");
|
---|
84 | ip->p_max = (num >= 0 ? num : 0);
|
---|
85 | if (num > 0) {
|
---|
86 | ip->points = (struct coordinate *)
|
---|
87 | gp_alloc(num * sizeof(struct coordinate), "iso curve points");
|
---|
88 | } else
|
---|
89 | ip->points = (struct coordinate *) NULL;
|
---|
90 | ip->next = NULL;
|
---|
91 | return (ip);
|
---|
92 | }
|
---|
93 | /*
|
---|
94 | * iso_free() releases any memory which was previously malloc()'d to hold
|
---|
95 | * iso curve points.
|
---|
96 | */
|
---|
97 | void iso_free(struct iso_curve *ip)
|
---|
98 | {
|
---|
99 | if (ip) {
|
---|
100 | if (ip->points)
|
---|
101 | free((char *) ip->points);
|
---|
102 | free((char *) ip);
|
---|
103 | }
|
---|
104 | }
|
---|