libctf: drop mmap()-based CTF data allocator
[deliverable/binutils-gdb.git] / libctf / ctf-create.c
index 227f62d8fd73b6cf56eb1523815a6f2806d0877c..86695f5abfb8a35e646e3b90991687a7a24136d2 100644 (file)
@@ -310,7 +310,7 @@ ctf_update (ctf_file_t *fp)
 
   buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
 
-  if ((buf = ctf_data_alloc (buf_size)) == NULL)
+  if ((buf = malloc (buf_size)) == NULL)
     return (ctf_set_errno (fp, EAGAIN));
 
   memcpy (buf, &hdr, sizeof (ctf_header_t));
@@ -344,7 +344,7 @@ ctf_update (ctf_file_t *fp)
     }
   assert (i == nvars);
 
-  qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var, s0);
+  ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var, s0);
   t += sizeof (ctf_varent_t) * nvars;
 
   assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
@@ -449,11 +449,9 @@ ctf_update (ctf_file_t *fp)
   /* Finally, we are ready to ctf_simple_open() the new container.  If this
      is successful, we then switch nfp and fp and free the old container.  */
 
-  ctf_data_protect (buf, buf_size);
-
   if ((nfp = ctf_simple_open (buf, buf_size, NULL, 0, 0, NULL, 0, &err)) == NULL)
     {
-      ctf_data_free (buf, buf_size);
+      ctf_free (buf);
       return (ctf_set_errno (fp, err));
     }
 
@@ -462,7 +460,7 @@ ctf_update (ctf_file_t *fp)
 
   nfp->ctf_refcnt = fp->ctf_refcnt;
   nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
-  nfp->ctf_data.cts_data = NULL;       /* Force ctf_data_free() on close.  */
+  nfp->ctf_data.cts_data = NULL;       /* Force ctf_free() on close.  */
   nfp->ctf_dthash = fp->ctf_dthash;
   nfp->ctf_dtdefs = fp->ctf_dtdefs;
   nfp->ctf_dtbyname = fp->ctf_dtbyname;
@@ -526,18 +524,22 @@ ctf_prefixed_name (int kind, const char *name)
   return prefixed;
 }
 
-void
+int
 ctf_dtd_insert (ctf_file_t *fp, ctf_dtdef_t *dtd)
 {
-  ctf_dynhash_insert (fp->ctf_dthash, (void *) dtd->dtd_type, dtd);
-  ctf_list_append (&fp->ctf_dtdefs, dtd);
+  if (ctf_dynhash_insert (fp->ctf_dthash, (void *) dtd->dtd_type, dtd) < 0)
+    return -1;
+
   if (dtd->dtd_name)
     {
       int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
-      ctf_dynhash_insert (fp->ctf_dtbyname, ctf_prefixed_name (kind,
-                                                              dtd->dtd_name),
-                         dtd);
+      if (ctf_dynhash_insert (fp->ctf_dtbyname,
+                             ctf_prefixed_name (kind, dtd->dtd_name),
+                             dtd) < 0)
+       return -1;
     }
+  ctf_list_append (&fp->ctf_dtdefs, dtd);
+  return 0;
 }
 
 void
@@ -623,11 +625,13 @@ ctf_dynamic_type (const ctf_file_t *fp, ctf_id_t id)
   return NULL;
 }
 
-void
+int
 ctf_dvd_insert (ctf_file_t *fp, ctf_dvdef_t *dvd)
 {
-  ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd);
+  if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
+    return -1;
   ctf_list_append (&fp->ctf_dvdefs, dvd);
+  return 0;
 }
 
 void
@@ -762,7 +766,11 @@ ctf_add_generic (ctf_file_t *fp, uint32_t flag, const char *name,
   if (s != NULL)
     fp->ctf_dtvstrlen += strlen (s) + 1;
 
-  ctf_dtd_insert (fp, dtd);
+  if (ctf_dtd_insert (fp, dtd) < 0)
+    {
+      ctf_free (dtd);
+      return CTF_ERR;                  /* errno is set for us.  */
+    }
   fp->ctf_flags |= LCTF_DIRTY;
 
   *rp = dtd;
@@ -800,7 +808,8 @@ ctf_add_encoded (ctf_file_t *fp, uint32_t flag,
     return CTF_ERR;            /* errno is set for us.  */
 
   dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
-  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
+  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
+                                / CHAR_BIT);
   dtd->dtd_u.dtu_enc = *ep;
 
   return type;
@@ -859,7 +868,8 @@ ctf_add_slice (ctf_file_t *fp, uint32_t flag, ctf_id_t ref,
     return CTF_ERR;            /* errno is set for us.  */
 
   dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
-  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
+  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
+                                / CHAR_BIT);
   dtd->dtd_u.dtu_slice.cts_type = ref;
   dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
   dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
@@ -1338,7 +1348,7 @@ ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
          if (ctf_type_encoding (fp, ltype, &linfo) == 0)
            off += linfo.cte_bits;
          else if ((lsize = ctf_type_size (fp, ltype)) > 0)
-           off += lsize * NBBY;
+           off += lsize * CHAR_BIT;
 
          /* Round up the offset of the end of the last member to
             the next byte boundary, convert 'off' to bytes, and
@@ -1349,9 +1359,9 @@ ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
             packing if the new member is a bit-field, but we're
             the "compiler" and ANSI says we can do as we choose.  */
 
-         off = roundup (off, NBBY) / NBBY;
+         off = roundup (off, CHAR_BIT) / CHAR_BIT;
          off = roundup (off, MAX (malign, 1));
-         dmd->dmd_offset = off * NBBY;
+         dmd->dmd_offset = off * CHAR_BIT;
          ssize = off + msize;
        }
       else
@@ -1360,7 +1370,7 @@ ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
 
          dmd->dmd_offset = bit_offset;
          ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
-         ssize = MAX (ssize, ((signed) bit_offset / NBBY) + msize);
+         ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
        }
     }
   else
@@ -1440,7 +1450,11 @@ ctf_add_variable (ctf_file_t *fp, const char *name, ctf_id_t ref)
   dvd->dvd_type = ref;
   dvd->dvd_snapshots = fp->ctf_snapshots;
 
-  ctf_dvd_insert (fp, dvd);
+  if (ctf_dvd_insert (fp, dvd) < 0)
+    {
+      ctf_free (dvd);
+      return -1;                       /* errno is set for us.  */
+    }
 
   fp->ctf_dtvstrlen += strlen (name) + 1;
   fp->ctf_flags |= LCTF_DIRTY;
@@ -1820,9 +1834,10 @@ ctf_add_type (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
                ctf_type_size (dst_fp, dst_type))
              {
                ctf_dprintf ("Conflict for type %s against ID %lx: "
-                            "union size differs, old %zi, new %zi\n",
-                            name, dst_type, ctf_type_size (src_fp, src_type),
-                            ctf_type_size (dst_fp, dst_type));
+                            "union size differs, old %li, new %li\n",
+                            name, dst_type,
+                            (long) ctf_type_size (src_fp, src_type),
+                            (long) ctf_type_size (dst_fp, dst_type));
                return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
              }
 
@@ -1976,16 +1991,17 @@ ctf_compress_write (ctf_file_t *fp, int fd)
   memcpy (hp, fp->ctf_base, header_len);
   hp->cth_flags |= CTF_F_COMPRESS;
 
-  if ((buf = ctf_data_alloc (max_compress_len)) == NULL)
+  if ((buf = ctf_alloc (max_compress_len)) == NULL)
     return (ctf_set_errno (fp, ECTF_ZALLOC));
 
   compress_len = max_compress_len;
-  if ((rc = compress (buf, (uLongf *) & compress_len,
+  if ((rc = compress (buf, (uLongf *) &compress_len,
                      fp->ctf_base + header_len,
                      fp->ctf_size - header_len)) != Z_OK)
     {
       ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
       err = ctf_set_errno (fp, ECTF_COMPRESS);
+      ctf_free (buf);
       goto ret;
     }
 
@@ -2013,7 +2029,7 @@ ctf_compress_write (ctf_file_t *fp, int fd)
     }
 
 ret:
-  ctf_data_free (buf, max_compress_len);
+  ctf_free (buf);
   return err;
 }
 
This page took 0.027362 seconds and 4 git commands to generate.