From 874d41add3ed75e569ff9a94ad7065a082671409 Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Sun, 26 Nov 2017 01:14:26 -0800 Subject: [PATCH] i965: Use C99 struct initializers in brw_bufmgr.c. This is cleaner than using a non-standard memclear macro (which does a memset to 0) and then initializing fields after the fact. We move the declarations to where we initialized the fields. While we're at it, we move the declaration of 'ret' that goes with the ioctl, eliminating the declaration section altogether. Reviewed-by: Chris Wilson --- src/mesa/drivers/dri/i965/brw_bufmgr.c | 140 +++++++++---------------- 1 file changed, 49 insertions(+), 91 deletions(-) diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.c b/src/mesa/drivers/dri/i965/brw_bufmgr.c index 469895ed6b0..bd46035c37c 100644 --- a/src/mesa/drivers/dri/i965/brw_bufmgr.c +++ b/src/mesa/drivers/dri/i965/brw_bufmgr.c @@ -84,8 +84,6 @@ #define VG_DEFINED(ptr, size) VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size)) #define VG_NOACCESS(ptr, size) VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size)) -#define memclear(s) memset(&s, 0, sizeof(s)) - #define PAGE_SIZE 4096 #define FILE_DEBUG_FLAG DEBUG_BUFMGR @@ -226,13 +224,9 @@ int brw_bo_busy(struct brw_bo *bo) { struct brw_bufmgr *bufmgr = bo->bufmgr; - struct drm_i915_gem_busy busy; - int ret; + struct drm_i915_gem_busy busy = { .handle = bo->gem_handle }; - memclear(busy); - busy.handle = bo->gem_handle; - - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); if (ret == 0) { bo->idle = !busy.busy; return busy.busy; @@ -243,12 +237,12 @@ brw_bo_busy(struct brw_bo *bo) int brw_bo_madvise(struct brw_bo *bo, int state) { - struct drm_i915_gem_madvise madv; + struct drm_i915_gem_madvise madv = { + .handle = bo->gem_handle, + .madv = state, + .retained = 1, + }; - memclear(madv); - madv.handle = bo->gem_handle; - madv.madv = state; - madv.retained = 1; drmIoctl(bo->bufmgr->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv); return madv.retained; @@ -366,8 +360,6 @@ retry: } if (!alloc_from_cache) { - struct drm_i915_gem_create create; - bo = calloc(1, sizeof(*bo)); if (!bo) goto err; @@ -375,8 +367,7 @@ retry: bo->size = bo_size; bo->idle = true; - memclear(create); - create.size = bo_size; + struct drm_i915_gem_create create = { .size = bo_size }; /* All new BOs we get from the kernel are zeroed, so we don't need to * worry about that here. @@ -500,9 +491,6 @@ brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr, const char *name, unsigned int handle) { struct brw_bo *bo; - int ret; - struct drm_gem_open open_arg; - struct drm_i915_gem_get_tiling get_tiling; /* At the moment most applications only have a few named bo. * For instance, in a DRI client only the render buffers passed @@ -517,9 +505,8 @@ brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr, goto out; } - memclear(open_arg); - open_arg.name = handle; - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_OPEN, &open_arg); + struct drm_gem_open open_arg = { .name = handle }; + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_OPEN, &open_arg); if (ret != 0) { DBG("Couldn't reference %s handle 0x%08x: %s\n", name, handle, strerror(errno)); @@ -554,8 +541,7 @@ brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr, _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo); _mesa_hash_table_insert(bufmgr->name_table, &bo->global_name, bo); - memclear(get_tiling); - get_tiling.handle = bo->gem_handle; + struct drm_i915_gem_get_tiling get_tiling = { .handle = bo->gem_handle }; ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling); if (ret != 0) goto err_unref; @@ -579,8 +565,6 @@ static void bo_free(struct brw_bo *bo) { struct brw_bufmgr *bufmgr = bo->bufmgr; - struct drm_gem_close close; - int ret; if (bo->map_cpu) { VG_NOACCESS(bo->map_cpu, bo->size); @@ -608,9 +592,8 @@ bo_free(struct brw_bo *bo) } /* Close this object */ - memclear(close); - close.handle = bo->gem_handle; - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_CLOSE, &close); + struct drm_gem_close close = { .handle = bo->gem_handle }; + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_CLOSE, &close); if (ret != 0) { DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", bo->gem_handle, bo->name, strerror(errno)); @@ -739,14 +722,12 @@ brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags) assert(bo->cache_coherent || !(flags & MAP_WRITE)); if (!bo->map_cpu) { - struct drm_i915_gem_mmap mmap_arg; - void *map; - DBG("brw_bo_map_cpu: %d (%s)\n", bo->gem_handle, bo->name); - memclear(mmap_arg); - mmap_arg.handle = bo->gem_handle; - mmap_arg.size = bo->size; + struct drm_i915_gem_mmap mmap_arg = { + .handle = bo->gem_handle, + .size = bo->size, + }; int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg); if (ret != 0) { ret = -errno; @@ -754,7 +735,7 @@ brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags) __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno)); return NULL; } - map = (void *) (uintptr_t) mmap_arg.addr_ptr; + void *map = (void *) (uintptr_t) mmap_arg.addr_ptr; VG_DEFINED(map, bo->size); if (p_atomic_cmpxchg(&bo->map_cpu, NULL, map)) { @@ -804,15 +785,13 @@ brw_bo_map_wc(struct brw_context *brw, struct brw_bo *bo, unsigned flags) return NULL; if (!bo->map_wc) { - struct drm_i915_gem_mmap mmap_arg; - void *map; - DBG("brw_bo_map_wc: %d (%s)\n", bo->gem_handle, bo->name); - memclear(mmap_arg); - mmap_arg.handle = bo->gem_handle; - mmap_arg.size = bo->size; - mmap_arg.flags = I915_MMAP_WC; + struct drm_i915_gem_mmap mmap_arg = { + .handle = bo->gem_handle, + .size = bo->size, + .flags = I915_MMAP_WC, + }; int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg); if (ret != 0) { ret = -errno; @@ -821,7 +800,7 @@ brw_bo_map_wc(struct brw_context *brw, struct brw_bo *bo, unsigned flags) return NULL; } - map = (void *) (uintptr_t) mmap_arg.addr_ptr; + void *map = (void *) (uintptr_t) mmap_arg.addr_ptr; VG_DEFINED(map, bo->size); if (p_atomic_cmpxchg(&bo->map_wc, NULL, map)) { @@ -870,13 +849,9 @@ brw_bo_map_gtt(struct brw_context *brw, struct brw_bo *bo, unsigned flags) /* Get a mapping of the buffer if we haven't before. */ if (bo->map_gtt == NULL) { - struct drm_i915_gem_mmap_gtt mmap_arg; - void *map; - DBG("bo_map_gtt: mmap %d (%s)\n", bo->gem_handle, bo->name); - memclear(mmap_arg); - mmap_arg.handle = bo->gem_handle; + struct drm_i915_gem_mmap_gtt mmap_arg = { .handle = bo->gem_handle }; /* Get the fake offset back... */ int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg); @@ -887,8 +862,8 @@ brw_bo_map_gtt(struct brw_context *brw, struct brw_bo *bo, unsigned flags) } /* and mmap it. */ - map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, - MAP_SHARED, bufmgr->fd, mmap_arg.offset); + void *map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, + MAP_SHARED, bufmgr->fd, mmap_arg.offset); if (map == MAP_FAILED) { DBG("%s:%d: Error mapping buffer %d (%s): %s .\n", __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno)); @@ -990,15 +965,15 @@ brw_bo_subdata(struct brw_bo *bo, uint64_t offset, uint64_t size, const void *data) { struct brw_bufmgr *bufmgr = bo->bufmgr; - struct drm_i915_gem_pwrite pwrite; - int ret; - memclear(pwrite); - pwrite.handle = bo->gem_handle; - pwrite.offset = offset; - pwrite.size = size; - pwrite.data_ptr = (uint64_t) (uintptr_t) data; - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); + struct drm_i915_gem_pwrite pwrite = { + .handle = bo->gem_handle, + .offset = offset, + .size = size, + .data_ptr = (uint64_t) (uintptr_t) data, + }; + + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); if (ret != 0) { ret = -errno; DBG("%s:%d: Error writing data to buffer %d: " @@ -1050,17 +1025,16 @@ int brw_bo_wait(struct brw_bo *bo, int64_t timeout_ns) { struct brw_bufmgr *bufmgr = bo->bufmgr; - struct drm_i915_gem_wait wait; - int ret; /* If we know it's idle, don't bother with the kernel round trip */ if (bo->idle && !bo->external) return 0; - memclear(wait); - wait.bo_handle = bo->gem_handle; - wait.timeout_ns = timeout_ns; - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_WAIT, &wait); + struct drm_i915_gem_wait wait = { + .bo_handle = bo->gem_handle, + .timeout_ns = timeout_ns, + }; + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_WAIT, &wait); if (ret == -1) return -errno; @@ -1136,13 +1110,11 @@ brw_bo_get_tiling(struct brw_bo *bo, uint32_t *tiling_mode, struct brw_bo * brw_bo_gem_create_from_prime(struct brw_bufmgr *bufmgr, int prime_fd) { - int ret; uint32_t handle; struct brw_bo *bo; - struct drm_i915_gem_get_tiling get_tiling; mtx_lock(&bufmgr->lock); - ret = drmPrimeFDToHandle(bufmgr->fd, prime_fd, &handle); + int ret = drmPrimeFDToHandle(bufmgr->fd, prime_fd, &handle); if (ret) { DBG("create_from_prime: failed to obtain handle from fd: %s\n", strerror(errno)); @@ -1185,8 +1157,7 @@ brw_bo_gem_create_from_prime(struct brw_bufmgr *bufmgr, int prime_fd) bo->reusable = false; bo->external = true; - memclear(get_tiling); - get_tiling.handle = bo->gem_handle; + struct drm_i915_gem_get_tiling get_tiling = { .handle = bo->gem_handle }; if (drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) goto err; @@ -1249,10 +1220,8 @@ brw_bo_flink(struct brw_bo *bo, uint32_t *name) struct brw_bufmgr *bufmgr = bo->bufmgr; if (!bo->global_name) { - struct drm_gem_flink flink; + struct drm_gem_flink flink = { .handle = bo->gem_handle }; - memclear(flink); - flink.handle = bo->gem_handle; if (drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_FLINK, &flink)) return -errno; @@ -1330,11 +1299,8 @@ init_cache_buckets(struct brw_bufmgr *bufmgr) uint32_t brw_create_hw_context(struct brw_bufmgr *bufmgr) { - struct drm_i915_gem_context_create create; - int ret; - - memclear(create); - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create); + struct drm_i915_gem_context_create create = { }; + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create); if (ret != 0) { DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno)); return 0; @@ -1365,7 +1331,7 @@ brw_hw_context_set_priority(struct brw_bufmgr *bufmgr, void brw_destroy_hw_context(struct brw_bufmgr *bufmgr, uint32_t ctx_id) { - struct drm_i915_gem_context_destroy d = {.ctx_id = ctx_id }; + struct drm_i915_gem_context_destroy d = { .ctx_id = ctx_id }; if (ctx_id != 0 && drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &d) != 0) { @@ -1377,13 +1343,8 @@ brw_destroy_hw_context(struct brw_bufmgr *bufmgr, uint32_t ctx_id) int brw_reg_read(struct brw_bufmgr *bufmgr, uint32_t offset, uint64_t *result) { - struct drm_i915_reg_read reg_read; - int ret; - - memclear(reg_read); - reg_read.offset = offset; - - ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_REG_READ, ®_read); + struct drm_i915_reg_read reg_read = { .offset = offset }; + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_REG_READ, ®_read); *result = reg_read.val; return ret; @@ -1392,12 +1353,9 @@ brw_reg_read(struct brw_bufmgr *bufmgr, uint32_t offset, uint64_t *result) static int gem_param(int fd, int name) { - drm_i915_getparam_t gp; int v = -1; /* No param uses (yet) the sign bit, reserve it for errors */ - memset(&gp, 0, sizeof(gp)); - gp.param = name; - gp.value = &v; + struct drm_i915_getparam gp = { .param = name, .value = &v }; if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp)) return -1; -- 2.30.2