Blame amdgpu/handle_table.c

Packit Service 103f6b
/*
Packit Service 103f6b
 * Copyright 2018 Advanced Micro Devices, Inc.
Packit Service 103f6b
 *
Packit Service 103f6b
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 103f6b
 * copy of this software and associated documentation files (the "Software"),
Packit Service 103f6b
 * to deal in the Software without restriction, including without limitation
Packit Service 103f6b
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 103f6b
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 103f6b
 * Software is furnished to do so, subject to the following conditions:
Packit Service 103f6b
 *
Packit Service 103f6b
 * The above copyright notice and this permission notice shall be included in
Packit Service 103f6b
 * all copies or substantial portions of the Software.
Packit Service 103f6b
 *
Packit Service 103f6b
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 103f6b
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 103f6b
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit Service 103f6b
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit Service 103f6b
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit Service 103f6b
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Packit Service 103f6b
 * OTHER DEALINGS IN THE SOFTWARE.
Packit Service 103f6b
 *
Packit Service 103f6b
 */
Packit Service 103f6b
Packit Service 103f6b
#include <stdlib.h>
Packit Service 103f6b
#include <string.h>
Packit Service 103f6b
#include <errno.h>
Packit Service 103f6b
#include <unistd.h>
Packit Service 103f6b
#include "handle_table.h"
Packit Service 103f6b
#include "util_math.h"
Packit Service 103f6b
Packit Service 103f6b
drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
Packit Service 103f6b
				    void *value)
Packit Service 103f6b
{
Packit Service 103f6b
	if (key >= table->max_key) {
Packit Service 103f6b
		uint32_t alignment = sysconf(_SC_PAGESIZE) / sizeof(void*);
Packit Service 103f6b
		uint32_t max_key = ALIGN(key + 1, alignment);
Packit Service 103f6b
		void **values;
Packit Service 103f6b
Packit Service 103f6b
		values = realloc(table->values, max_key * sizeof(void *));
Packit Service 103f6b
		if (!values)
Packit Service 103f6b
			return -ENOMEM;
Packit Service 103f6b
Packit Service 103f6b
		memset(values + table->max_key, 0, (max_key - table->max_key) *
Packit Service 103f6b
		       sizeof(void *));
Packit Service 103f6b
Packit Service 103f6b
		table->max_key = max_key;
Packit Service 103f6b
		table->values = values;
Packit Service 103f6b
	}
Packit Service 103f6b
	table->values[key] = value;
Packit Service 103f6b
	return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_private void handle_table_remove(struct handle_table *table, uint32_t key)
Packit Service 103f6b
{
Packit Service 103f6b
	if (key < table->max_key)
Packit Service 103f6b
		table->values[key] = NULL;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_private void *handle_table_lookup(struct handle_table *table, uint32_t key)
Packit Service 103f6b
{
Packit Service 103f6b
	if (key < table->max_key)
Packit Service 103f6b
		return table->values[key];
Packit Service 103f6b
	else
Packit Service 103f6b
		return NULL;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_private void handle_table_fini(struct handle_table *table)
Packit Service 103f6b
{
Packit Service 103f6b
	free(table->values);
Packit Service 103f6b
	table->max_key = 0;
Packit Service 103f6b
	table->values = NULL;
Packit Service 103f6b
}