panfrost: Move to use ralloc for some allocations

Submitted by Tomeu Vizoso on June 18, 2019, 1:39 p.m.

Details

Message ID 20190618133921.34516-1-tomeu.vizoso@collabora.com
State New
Headers show
Series "panfrost: Move to use ralloc for some allocations" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Tomeu Vizoso June 18, 2019, 1:39 p.m.
We have some serious leaks, so plug some and also move to ralloc to
limit the lifetime of some objects to that of their parent.

Lots more such work to do.

Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
---
 src/gallium/drivers/panfrost/pan_context.c  | 24 +++++++------
 src/gallium/drivers/panfrost/pan_drm.c      |  2 +-
 src/gallium/drivers/panfrost/pan_job.c      |  6 ++--
 src/gallium/drivers/panfrost/pan_resource.c | 39 ++++++++++++---------
 src/gallium/drivers/panfrost/pan_resource.h |  1 +
 src/gallium/drivers/panfrost/pan_screen.c   |  8 +++--
 6 files changed, 44 insertions(+), 36 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/gallium/drivers/panfrost/pan_context.c b/src/gallium/drivers/panfrost/pan_context.c
index 7f5741da48e8..5dcd41c459b8 100644
--- a/src/gallium/drivers/panfrost/pan_context.c
+++ b/src/gallium/drivers/panfrost/pan_context.c
@@ -2006,7 +2006,7 @@  panfrost_set_constant_buffer(
         pbuf->size = sz;
 
         if (pbuf->buffer) {
-                free(pbuf->buffer);
+                ralloc_free(pbuf->buffer);
                 pbuf->buffer = NULL;
         }
 
@@ -2033,7 +2033,7 @@  panfrost_set_constant_buffer(
 
         /* Copy the constant buffer into the driver context for later upload */
 
-        pbuf->buffer = malloc(sz);
+        pbuf->buffer = rzalloc_size(ctx, sz);
         memcpy(pbuf->buffer, cpu + buf->buffer_offset, sz);
 }
 
@@ -2055,7 +2055,7 @@  panfrost_create_sampler_view(
         struct pipe_resource *texture,
         const struct pipe_sampler_view *template)
 {
-        struct panfrost_sampler_view *so = CALLOC_STRUCT(panfrost_sampler_view);
+        struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
         int bytes_per_pixel = util_format_get_blocksize(texture->format);
 
         pipe_reference(NULL, &texture->reference);
@@ -2193,7 +2193,7 @@  panfrost_sampler_view_destroy(
         struct pipe_sampler_view *view)
 {
         pipe_resource_reference(&view->texture, NULL);
-        free(view);
+        ralloc_free(view);
 }
 
 static void
@@ -2286,7 +2286,7 @@  panfrost_create_blend_state(struct pipe_context *pipe,
                             const struct pipe_blend_state *blend)
 {
         struct panfrost_context *ctx = pan_context(pipe);
-        struct panfrost_blend_state *so = CALLOC_STRUCT(panfrost_blend_state);
+        struct panfrost_blend_state *so = rzalloc(ctx, struct panfrost_blend_state);
         so->base = *blend;
 
         /* TODO: The following features are not yet implemented */
@@ -2336,7 +2336,7 @@  panfrost_delete_blend_state(struct pipe_context *pipe,
                 DBG("Deleting blend state leak blend shaders bytecode\n");
         }
 
-        free(blend);
+        ralloc_free(blend);
 }
 
 static void
@@ -2482,6 +2482,8 @@  panfrost_destroy(struct pipe_context *pipe)
         screen->driver->free_slab(screen, &panfrost->shaders);
         screen->driver->free_slab(screen, &panfrost->tiler_heap);
         screen->driver->free_slab(screen, &panfrost->tiler_polygon_list);
+
+        ralloc_free(pipe);
 }
 
 static struct pipe_query *
@@ -2489,7 +2491,7 @@  panfrost_create_query(struct pipe_context *pipe,
 		      unsigned type,
 		      unsigned index)
 {
-        struct panfrost_query *q = CALLOC_STRUCT(panfrost_query);
+        struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
 
         q->type = type;
         q->index = index;
@@ -2500,7 +2502,7 @@  panfrost_create_query(struct pipe_context *pipe,
 static void
 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
 {
-        FREE(q);
+        ralloc_free(q);
 }
 
 static boolean
@@ -2584,7 +2586,7 @@  panfrost_create_stream_output_target(struct pipe_context *pctx,
 {
         struct pipe_stream_output_target *target;
 
-        target = CALLOC_STRUCT(pipe_stream_output_target);
+        target = rzalloc(pctx, struct pipe_stream_output_target);
 
         if (!target)
                 return NULL;
@@ -2604,7 +2606,7 @@  panfrost_stream_output_target_destroy(struct pipe_context *pctx,
                                  struct pipe_stream_output_target *target)
 {
         pipe_resource_reference(&target->buffer, NULL);
-        free(target);
+        ralloc_free(target);
 }
 
 static void
@@ -2647,7 +2649,7 @@  panfrost_setup_hardware(struct panfrost_context *ctx)
 struct pipe_context *
 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
 {
-        struct panfrost_context *ctx = CALLOC_STRUCT(panfrost_context);
+        struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
         struct panfrost_screen *pscreen = pan_screen(screen);
         memset(ctx, 0, sizeof(*ctx));
         struct pipe_context *gallium = (struct pipe_context *) ctx;
diff --git a/src/gallium/drivers/panfrost/pan_drm.c b/src/gallium/drivers/panfrost/pan_drm.c
index e2f4f2437d30..3b2733a562e4 100644
--- a/src/gallium/drivers/panfrost/pan_drm.c
+++ b/src/gallium/drivers/panfrost/pan_drm.c
@@ -130,7 +130,7 @@  panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *m
 static struct panfrost_bo *
 panfrost_drm_import_bo(struct panfrost_screen *screen, struct winsys_handle *whandle)
 {
-	struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
+	struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
 	struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
         struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
 	struct drm_panfrost_mmap_bo mmap_bo = {0,};
diff --git a/src/gallium/drivers/panfrost/pan_job.c b/src/gallium/drivers/panfrost/pan_job.c
index 6e913ac3374d..7ea6bff9e9b5 100644
--- a/src/gallium/drivers/panfrost/pan_job.c
+++ b/src/gallium/drivers/panfrost/pan_job.c
@@ -174,13 +174,11 @@  panfrost_job_hash(const void *key)
 void
 panfrost_job_init(struct panfrost_context *ctx)
 {
-        /* TODO: Don't leak */
-        ctx->jobs = _mesa_hash_table_create(NULL,
+        ctx->jobs = _mesa_hash_table_create(ctx,
                                             panfrost_job_hash,
                                             panfrost_job_compare);
 
-        ctx->write_jobs = _mesa_hash_table_create(NULL,
+        ctx->write_jobs = _mesa_hash_table_create(ctx,
                                             _mesa_hash_pointer,
                                             _mesa_key_pointer_equal);
-
 }
diff --git a/src/gallium/drivers/panfrost/pan_resource.c b/src/gallium/drivers/panfrost/pan_resource.c
index ac83f38b3279..332208d4e2fe 100644
--- a/src/gallium/drivers/panfrost/pan_resource.c
+++ b/src/gallium/drivers/panfrost/pan_resource.c
@@ -58,7 +58,7 @@  panfrost_resource_from_handle(struct pipe_screen *pscreen,
 
         assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
 
-        rsc = CALLOC_STRUCT(panfrost_resource);
+        rsc = rzalloc(pscreen, struct panfrost_resource);
         if (!rsc)
                 return NULL;
 
@@ -138,7 +138,7 @@  panfrost_create_surface(struct pipe_context *pipe,
 {
         struct pipe_surface *ps = NULL;
 
-        ps = CALLOC_STRUCT(pipe_surface);
+        ps = rzalloc(pipe, struct pipe_surface);
 
         if (ps) {
                 pipe_reference_init(&ps->reference, 1);
@@ -173,7 +173,7 @@  panfrost_surface_destroy(struct pipe_context *pipe,
 {
         assert(surf->texture);
         pipe_resource_reference(&surf->texture, NULL);
-        free(surf);
+        ralloc_free(surf);
 }
 
 static void
@@ -265,7 +265,7 @@  panfrost_setup_slices(const struct pipe_resource *tmpl, struct panfrost_bo *bo)
 static struct panfrost_bo *
 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
 {
-	struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
+	struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
         pipe_reference_init(&bo->reference, 1);
 
         /* Based on the usage, figure out what storing will be used. There are
@@ -313,7 +313,7 @@  static struct pipe_resource *
 panfrost_resource_create(struct pipe_screen *screen,
                          const struct pipe_resource *template)
 {
-        struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
+        struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
         struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
 
         so->base = *template;
@@ -369,10 +369,8 @@  panfrost_resource_create(struct pipe_screen *screen,
 }
 
 static void
-panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
+panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
 {
-	struct panfrost_bo *bo = (struct panfrost_bo *)pbo;
-
         if ((bo->layout == PAN_LINEAR || bo->layout == PAN_TILED) &&
                         !bo->imported) {
                 struct panfrost_memory mem = {
@@ -404,6 +402,8 @@  panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
         if (bo->imported) {
                 screen->driver->free_imported_bo(screen, bo);
         }
+
+        ralloc_free(bo);
 }
 
 void
@@ -436,7 +436,7 @@  panfrost_resource_destroy(struct pipe_screen *screen,
                 panfrost_bo_unreference(screen, rsrc->bo);
 
         util_range_destroy(&rsrc->valid_buffer_range);
-	FREE(rsrc);
+	ralloc_free(rsrc);
 }
 
 static void *
@@ -451,7 +451,7 @@  panfrost_transfer_map(struct pipe_context *pctx,
         struct panfrost_resource *rsrc = pan_resource(resource);
         struct panfrost_bo *bo = rsrc->bo;
 
-        struct panfrost_gtransfer *transfer = CALLOC_STRUCT(panfrost_gtransfer);
+        struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
         transfer->base.level = level;
         transfer->base.usage = usage;
         transfer->base.box = *box;
@@ -508,7 +508,7 @@  panfrost_transfer_map(struct pipe_context *pctx,
                 transfer->base.layer_stride = transfer->base.stride * box->height;
 
                 /* TODO: Reads */
-                transfer->map = malloc(transfer->base.layer_stride * box->depth);
+                transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
 
                 return transfer->map;
         } else {
@@ -569,8 +569,6 @@  panfrost_transfer_unmap(struct pipe_context *pctx,
                                 panfrost_tile_texture(screen, prsrc, trans);
                         }
                 }
-
-                free(trans->map);
         }
 
 
@@ -581,8 +579,8 @@  panfrost_transfer_unmap(struct pipe_context *pctx,
         /* Derefence the resource */
         pipe_resource_reference(&transfer->resource, NULL);
 
-        /* Transfer itself is CALLOCed at the moment */
-        free(transfer);
+        /* Transfer itself is RALLOCed at the moment */
+        ralloc_free(transfer);
 }
 
 static void
@@ -603,7 +601,7 @@  static struct pb_slab *
 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
 {
         struct panfrost_screen *screen = (struct panfrost_screen *) priv;
-        struct panfrost_memory *mem = CALLOC_STRUCT(panfrost_memory);
+        struct panfrost_memory *mem = rzalloc(screen, struct panfrost_memory);
 
         size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
 
@@ -613,7 +611,7 @@  panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned gro
         LIST_INITHEAD(&mem->slab.free);
         for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
                 /* Create a slab entry */
-                struct panfrost_memory_entry *entry = CALLOC_STRUCT(panfrost_memory_entry);
+                struct panfrost_memory_entry *entry = rzalloc(mem, struct panfrost_memory_entry);
                 entry->offset = entry_size * i;
 
                 entry->base.slab = &mem->slab;
@@ -644,6 +642,7 @@  panfrost_slab_free(void *priv, struct pb_slab *slab)
         struct panfrost_screen *screen = (struct panfrost_screen *) priv;
 
         screen->driver->free_slab(screen, mem);
+        ralloc_free(mem);
 }
 
 static void
@@ -708,6 +707,12 @@  panfrost_resource_screen_init(struct panfrost_screen *pscreen)
                         panfrost_slab_free);
 }
 
+void
+panfrost_resource_screen_deinit(struct panfrost_screen *pscreen)
+{
+        pb_slabs_deinit(&pscreen->slabs);
+}
+
 void
 panfrost_resource_context_init(struct pipe_context *pctx)
 {
diff --git a/src/gallium/drivers/panfrost/pan_resource.h b/src/gallium/drivers/panfrost/pan_resource.h
index d75b68c2ebce..114e283889b0 100644
--- a/src/gallium/drivers/panfrost/pan_resource.h
+++ b/src/gallium/drivers/panfrost/pan_resource.h
@@ -122,6 +122,7 @@  pan_transfer(struct pipe_transfer *p)
 }
 
 void panfrost_resource_screen_init(struct panfrost_screen *screen);
+void panfrost_resource_screen_deinit(struct panfrost_screen *screen);
 
 void panfrost_resource_context_init(struct pipe_context *pctx);
 
diff --git a/src/gallium/drivers/panfrost/pan_screen.c b/src/gallium/drivers/panfrost/pan_screen.c
index 9cd98cd90510..7964ca64f689 100644
--- a/src/gallium/drivers/panfrost/pan_screen.c
+++ b/src/gallium/drivers/panfrost/pan_screen.c
@@ -518,9 +518,11 @@  panfrost_is_format_supported( struct pipe_screen *screen,
 
 
 static void
-panfrost_destroy_screen( struct pipe_screen *screen )
+panfrost_destroy_screen(struct pipe_screen *pscreen)
 {
-        FREE(screen);
+        struct panfrost_screen *screen = pan_screen(pscreen);
+        panfrost_resource_screen_deinit(screen);
+        ralloc_free(screen);
 }
 
 static void
@@ -569,7 +571,7 @@  panfrost_screen_get_compiler_options(struct pipe_screen *pscreen,
 struct pipe_screen *
 panfrost_create_screen(int fd, struct renderonly *ro)
 {
-        struct panfrost_screen *screen = CALLOC_STRUCT(panfrost_screen);
+        struct panfrost_screen *screen = rzalloc(NULL, struct panfrost_screen);
 
 	pan_debug = debug_get_option_pan_debug();
 

Comments