/* data initialization and C<->Scheme data conversion */
-#include <stdio.h>
-
#include "libguile/gh.h"
#ifdef HAVE_STRING_H
#include <string.h>
effective_length = ((unsigned) len < dst_len) ? len : dst_len;
memmove (dst_ptr + start, src, effective_length);
- scm_remember (&dst);
+ scm_remember_upto_here_1 (dst);
}
/* Return the symbol named SYMBOL_STR. */
SCM
gh_symbol2scm (const char *symbol_str)
{
- return SCM_CAR (scm_intern (symbol_str, strlen (symbol_str)));
+ return scm_str2symbol(symbol_str);
}
SCM
gh_ints2scm (int *d, int n)
{
int i;
- SCM v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED);
+ SCM v = scm_c_make_vector (n, SCM_UNSPECIFIED);
SCM *velts = SCM_VELTS(v);
for (i = 0; i < n; ++i)
- velts[i] = (d[i] >= SCM_MOST_NEGATIVE_FIXNUM
- && d[i] <= SCM_MOST_POSITIVE_FIXNUM
- ? SCM_MAKINUM (d[i])
- : scm_long2big (d[i]));
+ velts[i] = (SCM_FIXABLE (d[i]) ? SCM_MAKINUM (d[i]) : scm_long2big (d[i]));
+
return v;
}
gh_doubles2scm (const double *d, int n)
{
int i;
- SCM v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED);
+ SCM v = scm_c_make_vector (n, SCM_UNSPECIFIED);
SCM *velts = SCM_VELTS(v);
for(i = 0; i < n; i++)
/* Convert a vector, weak vector, string, substring or uniform vector
into an array of chars. If result array in arg 2 is NULL, malloc a
- new one. */
+ new one. If out of memory, return NULL. */
char *
gh_scm2chars (SCM obj, char *m)
{
}
if (m == 0)
m = (char *) malloc (n * sizeof (char));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
m[i] = SCM_INUM (SCM_VELTS (obj)[i]);
break;
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (char *) malloc (n * sizeof (char));
+ if (m == NULL)
+ return NULL;
memcpy (m, SCM_VELTS (obj), n * sizeof (char));
break;
#endif
n = SCM_STRING_LENGTH (obj);
if (m == 0)
m = (char *) malloc (n * sizeof (char));
+ if (m == NULL)
+ return NULL;
memcpy (m, SCM_VELTS (obj), n * sizeof (char));
break;
default:
}
/* Convert a vector, weak vector or uniform vector into an array of
- shorts. If result array in arg 2 is NULL, malloc a new one. */
+ shorts. If result array in arg 2 is NULL, malloc a new one. If
+ out of memory, return NULL. */
short *
gh_scm2shorts (SCM obj, short *m)
{
}
if (m == 0)
m = (short *) malloc (n * sizeof (short));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
m[i] = SCM_INUM (SCM_VELTS (obj)[i]);
break;
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (short *) malloc (n * sizeof (short));
+ if (m == NULL)
+ return NULL;
memcpy (m, SCM_VELTS (obj), n * sizeof (short));
break;
#endif
}
/* Convert a vector, weak vector or uniform vector into an array of
- longs. If result array in arg 2 is NULL, malloc a new one. */
+ longs. If result array in arg 2 is NULL, malloc a new one. If out
+ of memory, return NULL. */
long *
gh_scm2longs (SCM obj, long *m)
{
}
if (m == 0)
m = (long *) malloc (n * sizeof (long));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
{
val = SCM_VELTS (obj)[i];
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (long *) malloc (n * sizeof (long));
+ if (m == NULL)
+ return NULL;
memcpy (m, SCM_VELTS (obj), n * sizeof (long));
break;
#endif
}
/* Convert a vector, weak vector or uniform vector into an array of
- floats. If result array in arg 2 is NULL, malloc a new one. */
+ floats. If result array in arg 2 is NULL, malloc a new one. If
+ out of memory, return NULL. */
float *
gh_scm2floats (SCM obj, float *m)
{
}
if (m == 0)
m = (float *) malloc (n * sizeof (float));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
{
val = SCM_VELTS (obj)[i];
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (float *) malloc (n * sizeof (float));
+ if (m == NULL)
+ return NULL;
memcpy (m, (float *) SCM_VELTS (obj), n * sizeof (float));
break;
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (float*) malloc (n * sizeof (float));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
m[i] = ((double *) SCM_VELTS (obj))[i];
break;
}
/* Convert a vector, weak vector or uniform vector into an array of
- doubles. If result array in arg 2 is NULL, malloc a new one. */
+ doubles. If result array in arg 2 is NULL, malloc a new one. If
+ out of memory, return NULL. */
double *
gh_scm2doubles (SCM obj, double *m)
{
}
if (m == 0)
m = (double *) malloc (n * sizeof (double));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
{
val = SCM_VELTS (obj)[i];
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (double *) malloc (n * sizeof (double));
+ if (m == NULL)
+ return NULL;
for (i = 0; i < n; ++i)
m[i] = ((float *) SCM_VELTS (obj))[i];
break;
n = SCM_UVECTOR_LENGTH (obj);
if (m == 0)
m = (double*) malloc (n * sizeof (double));
+ if (m == NULL)
+ return NULL;
memcpy (m, SCM_VELTS (obj), n * sizeof (double));
break;
#endif
non-null, set *lenp to the string's length.
This function uses malloc to obtain storage for the copy; the
- caller is responsible for freeing it.
+ caller is responsible for freeing it. If out of memory, NULL is
+ returned.
Note that Scheme strings may contain arbitrary data, including null
characters. This means that null termination is not a reliable way
len = SCM_STRING_LENGTH (str);
- ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char),
- "gh_scm2newstr");
+ ret_str = (char *) malloc ((len + 1) * sizeof (char));
+ if (ret_str == NULL)
+ return NULL;
/* so we copy tmp_str to ret_str, which is what we will allocate */
memcpy (ret_str, SCM_STRING_CHARS (str), len);
- /* from now on we don't mind if str gets GC collected. */
- scm_remember (&str);
+ scm_remember_upto_here_1 (str);
/* now make sure we null-terminate it */
ret_str[len] = '\0';
-
if (lenp != NULL)
{
*lenp = len;
effective_length = (len < src_len) ? len : src_len;
memcpy (dst + start, SCM_STRING_CHARS (src), effective_length * sizeof (char));
/* FIXME: must signal an error if len > src_len */
- scm_remember (&src);
+ scm_remember_upto_here_1 (src);
}
string's length.
This function uses malloc to obtain storage for the copy; the
- caller is responsible for freeing it. */
+ caller is responsible for freeing it. If out of memory, NULL is
+ returned.*/
char *
gh_symbol2newstr (SCM sym, int *lenp)
{
len = SCM_SYMBOL_LENGTH (sym);
- ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char),
- "gh_symbol2newstr");
+ ret_str = (char *) malloc ((len + 1) * sizeof (char));
+ if (ret_str == NULL)
+ return NULL;
/* so we copy sym to ret_str, which is what we will allocate */
memcpy (ret_str, SCM_SYMBOL_CHARS (sym), len);
- /* from now on we don't mind if sym gets GC collected. */
- scm_remember (&sym);
+ scm_remember_upto_here_1 (sym);
/* now make sure we null-terminate it */
ret_str[len] = '\0';