[Mesa-dev] [PATCH] i965: Use new/delete instead of realloc() in brw_ir_allocator.h

Juha-Pekka Heikkila juhapekka.heikkila at gmail.com
Wed Feb 11 06:56:56 PST 2015


There is no error path available thus instead of giving
realloc possibility to fail use new which will never
return null pointer and throws bad_alloc on failure.

Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila at gmail.com>
---
 src/mesa/drivers/dri/i965/brw_ir_allocator.h | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_ir_allocator.h b/src/mesa/drivers/dri/i965/brw_ir_allocator.h
index b1237ed..5330bff 100644
--- a/src/mesa/drivers/dri/i965/brw_ir_allocator.h
+++ b/src/mesa/drivers/dri/i965/brw_ir_allocator.h
@@ -40,8 +40,8 @@ namespace brw {
 
       ~simple_allocator()
       {
-         free(offsets);
-         free(sizes);
+         delete[] offsets;
+         delete[] sizes;
       }
 
       unsigned
@@ -49,8 +49,16 @@ namespace brw {
       {
          if (capacity <= count) {
             capacity = MAX2(16, capacity * 2);
-            sizes = (unsigned *)realloc(sizes, capacity * sizeof(unsigned));
-            offsets = (unsigned *)realloc(offsets, capacity * sizeof(unsigned));
+
+            unsigned *tmp_sizes = new unsigned[capacity];
+            memcpy(tmp_sizes, sizes, count * sizeof(unsigned));
+            delete[] sizes;
+            sizes = tmp_sizes;
+
+            unsigned *tmp_offsets = new unsigned[capacity];
+            memcpy(tmp_offsets, offsets, count * sizeof(unsigned));
+            delete[] offsets;
+            offsets = tmp_offsets;
          }
 
          sizes[count] = size;
-- 
1.8.5.1



More information about the mesa-dev mailing list