Fix memory leaks.
[pspp-builds.git] / src / case.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #ifndef HEADER_CASE
21 #define HEADER_CASE
22
23 #include <stddef.h>
24 #include "val.h"
25
26 /* Opaque structure that represents a case.  Use accessor
27    functions instead of accessing any members directly.  Use
28    case_move() or case_clone() instead of copying.  */
29 struct ccase 
30   {
31     struct case_data *case_data;
32 #if GLOBAL_DEBUGGING
33     struct ccase *this;
34 #endif
35   };
36
37 /* Invisible to user code. */
38 struct case_data
39   {
40     size_t value_cnt;
41     unsigned ref_cnt;
42     union value values[1];
43   };
44
45 #ifdef GLOBAL_DEBUGGING
46 #define CASE_INLINE
47 #else
48 #define CASE_INLINE static
49 #endif
50
51 CASE_INLINE void case_nullify (struct ccase *);
52 CASE_INLINE int case_is_null (const struct ccase *);
53
54 void case_create (struct ccase *, size_t value_cnt);
55 CASE_INLINE void case_clone (struct ccase *, const struct ccase *);
56 CASE_INLINE void case_move (struct ccase *, struct ccase *);
57 CASE_INLINE void case_destroy (struct ccase *);
58
59 int case_try_create (struct ccase *, size_t value_cnt);
60 int case_try_clone (struct ccase *, const struct ccase *);
61
62 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
63                             const struct ccase *src, size_t src_idx,
64                             size_t cnt);
65
66 CASE_INLINE size_t case_serial_size (size_t value_cnt);
67 CASE_INLINE void case_serialize (const struct ccase *, void *, size_t);
68 CASE_INLINE void case_unserialize (struct ccase *, const void *, size_t);
69
70 CASE_INLINE const union value *case_data (const struct ccase *, size_t idx);
71 CASE_INLINE double case_num (const struct ccase *, size_t idx);
72 CASE_INLINE const char *case_str (const struct ccase *, size_t idx);
73
74 CASE_INLINE union value *case_data_rw (struct ccase *, size_t idx);
75
76 void case_unshare (struct ccase *);
77
78 #ifndef GLOBAL_DEBUGGING
79 #include <stdlib.h>
80 #include "str.h"
81
82 static inline void
83 case_nullify (struct ccase *c) 
84 {
85   c->case_data = NULL;
86 }
87
88 static inline int
89 case_is_null (const struct ccase *c) 
90 {
91   return c->case_data == NULL;
92 }
93
94 static inline void
95 case_clone (struct ccase *clone, const struct ccase *orig)
96 {
97   *clone = *orig;
98   orig->case_data->ref_cnt++;
99 }
100
101 static inline void
102 case_move (struct ccase *dst, struct ccase *src) 
103 {
104   *dst = *src;
105   src->case_data = NULL;
106 }
107
108 static inline void
109 case_destroy (struct ccase *c) 
110 {
111   struct case_data *cd = c->case_data;
112   if (cd != NULL && --cd->ref_cnt == 0)
113     free (cd);
114 }
115
116 static inline void
117 case_copy (struct ccase *dst, size_t dst_idx,
118            const struct ccase *src, size_t src_idx,
119            size_t value_cnt) 
120 {
121   if (dst->case_data->ref_cnt > 1)
122     case_unshare (dst);
123   if (dst->case_data != src->case_data || dst_idx != src_idx) 
124     memmove (dst->case_data->values + dst_idx,
125              src->case_data->values + src_idx,
126              sizeof *dst->case_data->values * value_cnt); 
127 }
128
129 static inline size_t
130 case_serial_size (size_t value_cnt) 
131 {
132   return value_cnt * sizeof (union value);
133 }
134
135 static inline void
136 case_serialize (const struct ccase *c, void *output,
137                 size_t output_size UNUSED) 
138 {
139   memcpy (output, c->case_data->values,
140           case_serial_size (c->case_data->value_cnt));
141 }
142
143 static inline void
144 case_unserialize (struct ccase *c, const void *input,
145                   size_t input_size UNUSED) 
146 {
147   if (c->case_data->ref_cnt > 1)
148     case_unshare (c);
149   memcpy (c->case_data->values, input,
150           case_serial_size (c->case_data->value_cnt));
151 }
152
153 static inline const union value *
154 case_data (const struct ccase *c, size_t idx) 
155 {
156   return &c->case_data->values[idx];
157 }
158
159 static inline double
160 case_num (const struct ccase *c, size_t idx) 
161 {
162   return c->case_data->values[idx].f;
163 }
164
165 static inline const char *
166 case_str (const struct ccase *c, size_t idx)
167 {
168   return c->case_data->values[idx].s;
169 }
170
171 static inline union value *
172 case_data_rw (struct ccase *c, size_t idx)
173 {
174   if (c->case_data->ref_cnt > 1)
175     case_unshare (c);
176   return &c->case_data->values[idx];
177 }
178 #endif /* !GLOBAL_DEBUGGING */
179
180 #endif /* case.h */