CACAO
util.cpp
Go to the documentation of this file.
1 /* src/toolbox/util.c - contains some utility functions
2 
3  Copyright (C) 1996-2005, 2006, 2008
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 
26 #include "config.h"
27 
28 #include <assert.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <unistd.h>
32 
33 #include "vm/types.hpp"
34 
35 #include "vm/vm.hpp"
36 
37 
38 /* get_variable_message_length *************************************************
39 
40  This function simluates the print of a variable message and
41  determines so the message length;
42 
43 *******************************************************************************/
44 
45 int get_variable_message_length(const char *fmt, va_list ap)
46 {
47  int n;
48 
49  n = vsnprintf(NULL, 0, fmt, ap);
50 
51 #if defined(__IRIX__)
52  /* We know that IRIX returns -1 if the buffer is NULL */
53 
54  if (n == -1) {
55  char *p, *np;
56  s4 size;
57 
58  size = 100; /* start with 100-bytes */
59 
60  p = MNEW(char, size);
61 
62  while (1) {
63  /* Try to print in the allocated space. */
64 
65  n = vsnprintf(p, size, fmt, ap);
66 
67  /* If that worked, return the length. */
68  if (n > -1 && n < size)
69  return n;
70 
71  /* Else try again with more space. */
72  size *= 2; /* twice the old size */
73 
74  if ((np = MREALLOC(p, char, size, size)) == NULL) {
75  assert(0);
76  } else {
77  p = np;
78  }
79  }
80  }
81 #endif
82 
83  return n;
84 }
85 
86 
87 /*
88  * These are local overrides for various environment variables in Emacs.
89  * Please do not remove this and leave it at the end of the file, where
90  * Emacs will automagically detect them.
91  * ---------------------------------------------------------------------
92  * Local variables:
93  * mode: c++
94  * indent-tabs-mode: t
95  * c-basic-offset: 4
96  * tab-width: 4
97  * End:
98  */
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
int32_t s4
Definition: types.hpp:45
int get_variable_message_length(const char *fmt, va_list ap)
Definition: util.cpp:45
#define MNEW(type, num)
Definition: memory.hpp:96
#define MREALLOC(ptr, type, num1, num2)
Definition: memory.hpp:99