[Mesa-dev] [PATCH 08/32] i965/fs: Fix stack allocation of fs_inst and stop stealing src array provided on construction.

Francisco Jerez currojerez at riseup.net
Fri Feb 6 06:42:48 PST 2015


Using 'ralloc*(this, ...)' is wrong if the object has automatic
storage or was allocated through any other means.  Use normal dynamic
memory instead.
---
 src/mesa/drivers/dri/i965/brw_fs.cpp  | 68 +++++++++++++++++------------------
 src/mesa/drivers/dri/i965/brw_ir_fs.h |  8 +++--
 2 files changed, 39 insertions(+), 37 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 6d44fa7..0a82fb7 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -54,13 +54,16 @@ extern "C" {
 
 void
 fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
-              fs_reg *src, int sources)
+              const fs_reg *src, unsigned sources)
 {
    memset(this, 0, sizeof(*this));
 
+   this->src = new fs_reg[MAX2(sources, 3)];
+   for (unsigned i = 0; i < sources; i++)
+      this->src[i] = src[i];
+
    this->opcode = opcode;
    this->dst = dst;
-   this->src = src;
    this->sources = sources;
    this->exec_size = exec_size;
 
@@ -75,7 +78,7 @@ fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
       if (dst.file == GRF) {
          this->exec_size = dst.width;
       } else {
-         for (int i = 0; i < sources; ++i) {
+         for (unsigned i = 0; i < sources; ++i) {
             if (src[i].file != GRF && src[i].file != ATTR)
                continue;
 
@@ -90,7 +93,7 @@ fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
    }
    assert(this->exec_size != 0);
 
-   for (int i = 0; i < sources; ++i) {
+   for (unsigned i = 0; i < sources; ++i) {
       switch (this->src[i].file) {
       case BAD_FILE:
          this->src[i].effective_width = 8;
@@ -140,82 +143,68 @@ fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
 
 fs_inst::fs_inst()
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   init(BRW_OPCODE_NOP, 8, dst, src, 0);
+   init(BRW_OPCODE_NOP, 8, dst, NULL, 0);
 }
 
 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   init(opcode, exec_size, reg_undef, src, 0);
+   init(opcode, exec_size, reg_undef, NULL, 0);
 }
 
 fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   init(opcode, 0, dst, src, 0);
+   init(opcode, 0, dst, NULL, 0);
 }
 
 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
                  const fs_reg &src0)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
+   const fs_reg src[1] = { src0 };
    init(opcode, exec_size, dst, src, 1);
 }
 
 fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst, const fs_reg &src0)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
+   const fs_reg src[1] = { src0 };
    init(opcode, 0, dst, src, 1);
 }
 
 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
                  const fs_reg &src0, const fs_reg &src1)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
-   src[1] = src1;
+   const fs_reg src[2] = { src0, src1 };
    init(opcode, exec_size, dst, src, 2);
 }
 
 fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst, const fs_reg &src0,
                  const fs_reg &src1)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
-   src[1] = src1;
+   const fs_reg src[2] = { src0, src1 };
    init(opcode, 0, dst, src, 2);
 }
 
 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
                  const fs_reg &src0, const fs_reg &src1, const fs_reg &src2)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
-   src[1] = src1;
-   src[2] = src2;
+   const fs_reg src[3] = { src0, src1, src2 };
    init(opcode, exec_size, dst, src, 3);
 }
 
 fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst, const fs_reg &src0,
                  const fs_reg &src1, const fs_reg &src2)
 {
-   fs_reg *src = ralloc_array(this, fs_reg, 3);
-   src[0] = src0;
-   src[1] = src1;
-   src[2] = src2;
+   const fs_reg src[3] = { src0, src1, src2 };
    init(opcode, 0, dst, src, 3);
 }
 
-fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst, fs_reg src[], int sources)
+fs_inst::fs_inst(enum opcode opcode, const fs_reg &dst,
+                 const fs_reg src[], unsigned sources)
 {
    init(opcode, 0, dst, src, sources);
 }
 
 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
-                 fs_reg src[], int sources)
+                 const fs_reg src[], unsigned sources)
 {
    init(opcode, exec_width, dst, src, sources);
 }
@@ -224,17 +213,28 @@ fs_inst::fs_inst(const fs_inst &that)
 {
    memcpy(this, &that, sizeof(that));
 
-   this->src = ralloc_array(this, fs_reg, that.sources);
+   this->src = new fs_reg[MAX2(that.sources, 3)];
 
-   for (int i = 0; i < that.sources; i++)
+   for (unsigned i = 0; i < that.sources; i++)
       this->src[i] = that.src[i];
 }
 
+fs_inst::~fs_inst()
+{
+   delete[] this->src;
+}
+
 void
 fs_inst::resize_sources(uint8_t num_sources)
 {
    if (this->sources != num_sources) {
-      this->src = reralloc(this, this->src, fs_reg, num_sources);
+      fs_reg *src = new fs_reg[MAX2(num_sources, 3)];
+
+      for (unsigned i = 0; i < MIN2(this->sources, num_sources); ++i)
+         src[i] = this->src[i];
+
+      delete[] this->src;
+      this->src = src;
       this->sources = num_sources;
    }
 }
@@ -374,7 +374,7 @@ fs_visitor::LOAD_PAYLOAD(const fs_reg &dst, fs_reg *src, int sources)
        * dealing with whole registers.  If this ever changes, we can deal
        * with it later.
        */
-      int size = src[i].effective_width * type_sz(src[i].type);
+      int size = inst->src[i].effective_width * type_sz(src[i].type);
       assert(size % 32 == 0);
       inst->regs_written += (size + 31) / 32;
    }
diff --git a/src/mesa/drivers/dri/i965/brw_ir_fs.h b/src/mesa/drivers/dri/i965/brw_ir_fs.h
index 0c7dcaf..9ef1261 100644
--- a/src/mesa/drivers/dri/i965/brw_ir_fs.h
+++ b/src/mesa/drivers/dri/i965/brw_ir_fs.h
@@ -191,7 +191,7 @@ class fs_inst : public backend_instruction {
    fs_inst &operator=(const fs_inst &);
 
    void init(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
-             fs_reg *src, int sources);
+             const fs_reg *src, unsigned sources);
 
 public:
    DECLARE_RALLOC_CXX_OPERATORS(fs_inst)
@@ -210,10 +210,12 @@ public:
            const fs_reg &src0, const fs_reg &src1, const fs_reg &src2);
    fs_inst(enum opcode opcode, const fs_reg &dst, const fs_reg &src0,
            const fs_reg &src1, const fs_reg &src2);
-   fs_inst(enum opcode opcode, const fs_reg &dst, fs_reg src[], int sources);
+   fs_inst(enum opcode opcode, const fs_reg &dst, const fs_reg src[],
+           unsigned sources);
    fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
-           fs_reg src[], int sources);
+           const fs_reg src[], unsigned sources);
    fs_inst(const fs_inst &that);
+   ~fs_inst();
 
    void resize_sources(uint8_t num_sources);
 
-- 
2.1.3



More information about the mesa-dev mailing list