Bug 651892 part 2 - Add a constructor to serializable that takes a buffer instead of an istream. r=tglek
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 23 Jun 2011 04:07:57 +0200
changeset 72396 9bf9fb1544bc2393c6f239eec67b880c3fc42adb
parent 72395 080a8d06788964cf5d8c6467a35061bac2bbee4a
child 72397 8b6ea9fa617328717298269efda72370af83f355
push id159
push usereakhgari@mozilla.com
push dateTue, 16 Aug 2011 17:53:11 +0000
treeherdermozilla-beta@8786e3e49240 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstglek
bugs651892
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 651892 part 2 - Add a constructor to serializable that takes a buffer instead of an istream. r=tglek
build/unix/elfhack/elfxx.h
--- a/build/unix/elfhack/elfxx.h
+++ b/build/unix/elfhack/elfxx.h
@@ -185,16 +185,47 @@ public:
     ElfSection *getSection() { return section; }
 };
 
 template <typename T>
 class serializable: public T::Type32 {
 public:
     serializable() {};
     serializable(const typename T::Type32 &p): T::Type32(p) {};
+
+private:
+    template <typename R>
+    void init(const char *buf, size_t len, char ei_data)
+    {
+        R e;
+        assert(len <= sizeof(e));
+        memcpy(&e, buf, sizeof(e));
+        if (ei_data == ELFDATA2LSB) {
+            T::template swap<little_endian>(e, *this);
+            return;
+        } else if (ei_data == ELFDATA2MSB) {
+            T::template swap<big_endian>(e, *this);
+            return;
+        }
+        throw std::runtime_error("Unsupported ELF data encoding");
+    }
+
+public:
+    serializable(const char *buf, size_t len, char ei_class, char ei_data)
+    {
+        if (ei_class == ELFCLASS32) {
+            init<typename T::Type32>(buf, len, ei_data);
+            return;
+        } else if (ei_class == ELFCLASS64) {
+            init<typename T::Type64>(buf, len, ei_data);
+            return;
+        }
+        throw std::runtime_error("Unsupported ELF class");
+    }
+
     serializable(std::ifstream &file, char ei_class, char ei_data)
     {
         if (ei_class == ELFCLASS32) {
             typename T::Type32 e;
             file.read((char *)&e, sizeof(e));
             if (ei_data == ELFDATA2LSB) {
                 T::template swap<little_endian>(e, *this);
                 return;