[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:37:48 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 | 19 +++++++++++++++----
 1 file changed, 15 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..b3fba29 100644
--- a/src/mesa/drivers/dri/i965/brw_ir_allocator.h
+++ b/src/mesa/drivers/dri/i965/brw_ir_allocator.h
@@ -40,17 +40,28 @@ namespace brw {
 
       ~simple_allocator()
       {
-         free(offsets);
-         free(sizes);
+         delete[] offsets;
+         delete[] sizes;
       }
 
       unsigned
       allocate(unsigned size)
       {
          if (capacity <= count) {
+            unsigned *tmp_sizes;
+            unsigned *tmp_offsets;
+
             capacity = MAX2(16, capacity * 2);
-            sizes = (unsigned *)realloc(sizes, capacity * sizeof(unsigned));
-            offsets = (unsigned *)realloc(offsets, capacity * sizeof(unsigned));
+
+            tmp_sizes = new unsigned[capacity * sizeof(unsigned)];
+            memcpy(tmp_sizes, sizes, count*sizeof(unsigned));
+            delete[] sizes;
+            sizes = tmp_sizes;
+
+            tmp_offsets = new unsigned[capacity * sizeof(unsigned)];
+            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