2 * size - size and sizeof functions are implemented here
4 * Copyright (C) 1999-2007 David I. Bell
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: size.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/size.c,v $
24 * Under source code control: 1997/03/10 01:56:51
25 * File existed as early as: 1997
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
38 * forward declarations
40 S_FUNC
size_t zsize(ZVALUE
);
41 S_FUNC
size_t qsize(NUMBER
*);
42 S_FUNC
size_t csize(COMPLEX
*);
43 S_FUNC
size_t memzsize(ZVALUE
);
44 S_FUNC
size_t memqsize(NUMBER
*);
48 * elm_count - return information about the number of elements
50 * Return information about the number of elements or part of
51 * a value. This is what the size() builtin returns with the
52 * exception of the V_FILE type. To get V_FILE size length,
53 * the getsize(vp->v_file, &len) should be called directly.
55 * This is not the sizeof, see lsizeof() for that information.
58 * vp pointer to a value
69 * return information about the number of elements
71 * This is not the sizeof, see lsizeof() for that information.
72 * This is does not include overhead, see memsize() for that info.
83 result
= vp
->v_mat
->m_size
;
86 result
= vp
->v_list
->l_count
;
89 result
= vp
->v_assoc
->a_count
;
92 result
= vp
->v_obj
->o_actions
->oa_count
;
95 result
= vp
->v_str
->s_len
;
98 result
= (long) vp
->v_block
->datalen
;
101 result
= (long) vp
->v_nblock
->blk
->datalen
;
104 * V_NUM, V_COM, V_RAND, V_RANDOM, V_CONFIG, V_HASH
106 * V_FILE (use getsize(vp->v_file, &len) for file length)
109 result
= (vp
->v_type
> 0);
117 * zsize - calculate memory footprint of a ZVALUE (exlcuding overhead)
119 * The numeric -1, - and 1 storage values are ignored.
122 * z ZVALUE to examine
130 /* ignore the size of 0, 1 and -1 */
131 if (z
.v
!= _zeroval_
&& z
.v
!= _oneval_
&& !zisunit(z
) && !ziszero(z
)) {
132 return z
.len
* sizeof(HALF
);
140 * qsize - calculate memory footprint of a NUMBER (exlcuding overhead)
142 * The numeric -1, - and 1 storage values are ignored. Denominator
143 * parts of integers are ignored.
146 * q pointer to NUMBER to examine
154 /* ingore denominator parts of integers */
156 return zsize(q
->num
);
158 return (zsize(q
->num
) + zsize(q
->den
));
164 * csize - calculate memory footprint of a COMPLEX (exlcuding overhead)
166 * The numeric -1, - and 1 storage values are ignored. Denominator
167 * parts of integers are ignored. Imaginary parts of pure reals
171 * c pointer to COMPLEX to examine
179 /* ingore denominator parts of integers */
181 return qsize(c
->real
);
183 return (qsize(c
->real
) + qsize(c
->imag
));
189 * memzsize - calculate memory footprint of a ZVALUE including overhead
192 * z ZVALUE to examine
200 return sizeof(ZVALUE
) + (z
.len
* sizeof(HALF
));
205 * memqsize - calculate memory footprint of a NUMBER including overhead
208 * q pointer of NUMBER to examine
216 return sizeof(NUMBER
) + memzsize(q
->num
) + memzsize(q
->den
);
221 * lsizeof - calculate memory footprint of a VALUE (not counting overhead)
224 * vp pointer of VALUE to examine
241 * return information about memory footprint
243 * This is not the number of elements, see elm_count() for that info.
244 * This is does not include overhead, see memsize() for that info.
248 if (vp
->v_type
> 0) {
255 s
= qsize(vp
->v_num
);
258 s
= csize(vp
->v_com
);
261 s
= vp
->v_str
->s_len
+ 1;
264 i
= vp
->v_mat
->m_size
;
265 p
= vp
->v_mat
->m_table
;
270 for (ep
= vp
->v_list
->l_first
; ep
; ep
= ep
->e_next
) {
271 s
+= lsizeof(&ep
->e_value
);
275 i
= vp
->v_assoc
->a_size
;
276 ept
= vp
->v_assoc
->a_table
;
278 for (aep
= ept
[i
]; aep
; aep
= aep
->e_next
) {
279 s
+= lsizeof(&aep
->e_value
);
284 oap
= vp
->v_obj
->o_actions
;
286 p
= vp
->v_obj
->o_table
;
291 s
= sizeof(vp
->v_file
);
298 zsize(vp
->v_random
->n
) +
299 zsize(vp
->v_random
->r
);
303 strlen(vp
->v_config
->prompt1
) +
304 strlen(vp
->v_config
->prompt2
) + 2;
307 /* ignore the unused part of the union */
309 vp
->v_hash
->unionsize
-
310 sizeof(vp
->v_hash
->h_union
);
313 s
= vp
->v_block
->maxsize
;
316 s
= vp
->v_nblock
->blk
->maxsize
;
319 math_error("sizeof not defined for value type");
328 * memsize - calculate memory footprint of a VALUE including overhead
331 * vp pointer of VALUE to examine
334 * memory footprint including overhead
348 * return information about memory footprint
350 * This is not the sizeof, see memsize() for that information.
351 * This is not the number of elements, see elm_count() for that info.
355 if (vp
->v_type
> 0) {
362 s
= memqsize(vp
->v_num
);
365 s
= sizeof(COMPLEX
) +
366 memqsize(vp
->v_com
->real
) +
367 memqsize(vp
->v_com
->imag
);
370 s
= sizeof(STRING
) + vp
->v_str
->s_len
+ 1;
374 i
= vp
->v_mat
->m_size
;
375 p
= vp
->v_mat
->m_table
;
381 for (ep
= vp
->v_list
->l_first
; ep
; ep
= ep
->e_next
) {
382 s
+= sizeof(LISTELEM
) - sizeof(VALUE
) +
383 memsize(&ep
->e_value
);
388 i
= vp
->v_assoc
->a_size
;
389 ept
= vp
->v_assoc
->a_table
;
391 s
+= sizeof(ASSOCELEM
*);
392 for (aep
= *ept
++; aep
; aep
= aep
->e_next
) {
393 s
+= sizeof(ASSOCELEM
) - sizeof(VALUE
) +
394 memsize(&aep
->e_value
);
404 oap
= vp
->v_obj
->o_actions
;
406 s
+= (i
+ 3) * sizeof(int);
407 p
= vp
->v_obj
->o_table
;
412 s
= sizeof(vp
->v_file
);
419 memzsize(vp
->v_random
->n
) +
420 memzsize(vp
->v_random
->r
);
423 s
= sizeof(CONFIG
) + 2 +
424 strlen(vp
->v_config
->prompt1
) +
425 strlen(vp
->v_config
->prompt2
);
431 s
= sizeof(BLOCK
) + vp
->v_block
->maxsize
;
434 s
= sizeof(NBLOCK
) + sizeof(BLOCK
) +
435 vp
->v_nblock
->blk
->maxsize
+
436 strlen(vp
->v_nblock
->name
) + 1;
439 math_error("memsize not defined for value type");