+ AV *av = newAV ();
+ CAMLreturn (Val_av (av));
+}
+
+/* We don't know in advance how long the list will be, which makes this
+ * a little harder.
+ */
+CAMLprim value
+perl4caml_av_of_sv_list (value svlistv)
+{
+ CAMLparam1 (svlistv);
+ CAMLlocal1 (svv);
+ SV *sv, **svlist = 0;
+ int alloc = 0, size = 0;
+ AV *av;
+
+ for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
+ {
+ svv = Field (svlistv, 0);
+ sv = Sv_val (svv);
+ if (size >= alloc) {
+ alloc = alloc == 0 ? 1 : alloc * 2;
+ svlist = realloc (svlist, alloc * sizeof (SV *));
+ }
+ svlist[size++] = sv;
+ }
+
+ av = av_make (size, svlist);
+
+ if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
+
+ CAMLreturn (Val_av (av));
+}
+
+/* XXX av_map would be faster if we also had sv_list_of_av. */
+
+CAMLprim value
+perl4caml_av_push (value avv, value svv)
+{
+ CAMLparam2 (avv, svv);
+ AV *av = Av_val (avv);
+ SV *sv = Sv_val (svv);
+ av_push (av, sv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_av_pop (value avv)
+{
+ CAMLparam1 (avv);
+ AV *av = Av_val (avv);
+ SV *sv = av_pop (av);
+ /* Increment the reference count because we're creating another
+ * value pointing at the referenced AV.
+ */
+ sv = SvREFCNT_inc (sv);
+ CAMLreturn (Val_sv (sv));
+}
+
+CAMLprim value
+perl4caml_av_unshift (value avv, value svv)
+{
+ CAMLparam2 (avv, svv);
+ AV *av = Av_val (avv);
+ SV *sv = Sv_val (svv);
+ av_unshift (av, 1);
+ SvREFCNT_inc (sv);
+ if (av_store (av, 0, sv) == 0)
+ SvREFCNT_dec (sv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_av_shift (value avv)
+{
+ CAMLparam1 (avv);
+ AV *av = Av_val (avv);
+ SV *sv = av_shift (av);
+ /* Increment the reference count because we're creating another
+ * value pointing at the referenced AV.
+ */
+ sv = SvREFCNT_inc (sv);
+ CAMLreturn (Val_sv (sv));
+}
+
+CAMLprim value
+perl4caml_av_length (value avv)
+{
+ CAMLparam1 (avv);
+ AV *av = Av_val (avv);
+ CAMLreturn (Val_int (av_len (av) + 1));
+}
+
+CAMLprim value
+perl4caml_av_set (value avv, value i, value svv)
+{
+ CAMLparam3 (avv, i, svv);
+ AV *av = Av_val (avv);
+ SV *sv = Sv_val (svv);
+ SvREFCNT_inc (sv);
+ if (av_store (av, Int_val (i), sv) == 0)
+ SvREFCNT_dec (sv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_av_get (value avv, value i)
+{
+ CAMLparam2 (avv, i);
+ AV *av = Av_val (avv);
+ SV **svp = av_fetch (av, Int_val (i), 0);
+ if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
+ /* Increment the reference count because we're creating another
+ * value pointing at the referenced AV.
+ */
+ *svp = SvREFCNT_inc (*svp);
+ CAMLreturn (Val_sv (*svp));
+}
+
+CAMLprim value
+perl4caml_av_clear (value avv)
+{
+ CAMLparam1 (avv);
+ AV *av = Av_val (avv);
+ av_clear (av);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_av_undef (value avv)
+{
+ CAMLparam1 (avv);
+ AV *av = Av_val (avv);
+ av_undef (av);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_av_extend (value avv, value i)
+{
+ CAMLparam2 (avv, i);
+ AV *av = Av_val (avv);
+ av_extend (av, Int_val (i));
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_hv_empty (value unit)
+{
+ CAMLparam1 (unit);
+ HV *hv = newHV ();
+ CAMLreturn (Val_hv (hv));
+}
+
+CAMLprim value
+perl4caml_hv_set (value hvv, value key, value svv)
+{
+ CAMLparam3 (hvv, key, svv);
+ HV *hv = Hv_val (hvv);
+ SV *sv = Sv_val (svv);
+ SvREFCNT_inc (sv);
+ if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
+ SvREFCNT_dec (sv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_hv_get (value hvv, value key)
+{
+ CAMLparam2 (hvv, key);
+ HV *hv = Hv_val (hvv);
+ SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
+ if (svp == 0) caml_raise_not_found ();
+ /* Increment the reference count because we're creating another
+ * value pointing at the referenced SV.
+ */
+ SvREFCNT_inc (*svp);
+ CAMLreturn (Val_sv (*svp));
+}
+
+CAMLprim value
+perl4caml_hv_exists (value hvv, value key)
+{
+ CAMLparam2 (hvv, key);
+ HV *hv = Hv_val (hvv);
+ bool r = hv_exists (hv, String_val (key), caml_string_length (key));
+ CAMLreturn (r ? Val_true : Val_false);
+}
+
+CAMLprim value
+perl4caml_hv_delete (value hvv, value key)
+{
+ CAMLparam2 (hvv, key);
+ HV *hv = Hv_val (hvv);
+ hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_hv_clear (value hvv)
+{
+ CAMLparam1 (hvv);
+ HV *hv = Hv_val (hvv);
+ hv_clear (hv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_hv_undef (value hvv)
+{
+ CAMLparam1 (hvv);
+ HV *hv = Hv_val (hvv);
+ hv_undef (hv);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value
+perl4caml_hv_iterinit (value hvv)
+{
+ CAMLparam1 (hvv);
+ HV *hv = Hv_val (hvv);
+ int i = hv_iterinit (hv);
+ CAMLreturn (caml_copy_int32 (i));
+}
+
+CAMLprim value
+perl4caml_hv_iternext (value hvv)
+{
+ CAMLparam1 (hvv);
+ CAMLlocal1 (hev);
+ HV *hv = Hv_val (hvv);
+ HE *he = hv_iternext (hv);
+ if (he == NULL) caml_raise_not_found ();
+ hev = Val_he (he);
+ CAMLreturn (hev);
+}
+
+CAMLprim value
+perl4caml_hv_iterkey (value hev)
+{
+ CAMLparam1 (hev);
+ CAMLlocal1 (strv);
+ HE *he = He_val (hev);
+ I32 len;
+ char *str = hv_iterkey (he, &len);
+ strv = caml_alloc_string (len);
+ memcpy (String_val (strv), str, len);
+ CAMLreturn (strv);
+}
+
+CAMLprim value
+perl4caml_hv_iterval (value hvv, value hev)
+{
+ CAMLparam2 (hvv, hev);
+ CAMLlocal1 (svv);
+ HV *hv = Hv_val (hvv);
+ HE *he = He_val (hev);
+ SV *sv = hv_iterval (hv, he);
+ SvREFCNT_inc (sv);
+ svv = Val_sv (sv);
+ CAMLreturn (svv);
+}
+
+CAMLprim value
+perl4caml_hv_iternextsv (value hvv)
+{
+ CAMLparam1 (hvv);
+ CAMLlocal3 (strv, svv, rv);
+ HV *hv = Hv_val (hvv);
+ char *str; I32 len;
+ SV *sv = hv_iternextsv (hv, &str, &len);
+ if (sv == NULL) caml_raise_not_found ();
+ SvREFCNT_inc (sv);
+ svv = Val_sv (sv);
+ strv = caml_alloc_string (len);
+ memcpy (String_val (strv), str, len);
+ /* Construct a tuple (strv, svv). */
+ rv = caml_alloc_tuple (2);
+ Field (rv, 0) = strv;
+ Field (rv, 1) = svv;
+ CAMLreturn (rv);