void TraceContainerWriter::add(frame &f) throw (TraceException) {
    /* Is is time for a toc entry? */
    if (num_frames > 0 && (num_frames % frames_per_toc_entry) == 0) {
      /* Yes.  Add the file offset where we will insert this frame to
         toc. */
      toc.push_back(TELL(ofs));
    }

    num_frames++;

    /* Serialize to string so we can get the length. */
    std::string s;
    if (!(f.SerializeToString(&s))) {
      throw (TraceException("Unable to serialize frame to ostream"));
    }

    /* Write the length before the frame. */
    uint64_t len = s.length();
    if (len == 0) {
      throw (TraceException("Attempt to add zero-length frame to trace"));
    }
    WRITE(len);

    /* Write the frame. */
    traceoff_t old_offset = TELL(ofs);
    if (old_offset == -1) {
      throw (TraceException("Unable to determine the current offset in the trace"));
    }

    if (fwrite(s.c_str(), 1, len, ofs) != len) {
      throw (TraceException("Unable to write frame to trace file"));
    }

    /* Double-check our size. */
    assert ((uint64_t)old_offset + len == (uint64_t)TELL(ofs));
  }