handle_table.c revision 7cdc0497
17cdc0497Smrg/*
27cdc0497Smrg * Copyright 2018 Advanced Micro Devices, Inc.
37cdc0497Smrg *
47cdc0497Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57cdc0497Smrg * copy of this software and associated documentation files (the "Software"),
67cdc0497Smrg * to deal in the Software without restriction, including without limitation
77cdc0497Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87cdc0497Smrg * and/or sell copies of the Software, and to permit persons to whom the
97cdc0497Smrg * Software is furnished to do so, subject to the following conditions:
107cdc0497Smrg *
117cdc0497Smrg * The above copyright notice and this permission notice shall be included in
127cdc0497Smrg * all copies or substantial portions of the Software.
137cdc0497Smrg *
147cdc0497Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157cdc0497Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
167cdc0497Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
177cdc0497Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
187cdc0497Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
197cdc0497Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
207cdc0497Smrg * OTHER DEALINGS IN THE SOFTWARE.
217cdc0497Smrg *
227cdc0497Smrg */
237cdc0497Smrg
247cdc0497Smrg#include <stdlib.h>
257cdc0497Smrg#include <string.h>
267cdc0497Smrg#include <errno.h>
277cdc0497Smrg#include <unistd.h>
287cdc0497Smrg#include "handle_table.h"
297cdc0497Smrg#include "util_math.h"
307cdc0497Smrg
317cdc0497Smrgdrm_private int handle_table_insert(struct handle_table *table, uint32_t key,
327cdc0497Smrg				    void *value)
337cdc0497Smrg{
347cdc0497Smrg	if (key >= table->max_key) {
357cdc0497Smrg		uint32_t alignment = sysconf(_SC_PAGESIZE) / sizeof(void*);
367cdc0497Smrg		uint32_t max_key = ALIGN(key + 1, alignment);
377cdc0497Smrg		void **values;
387cdc0497Smrg
397cdc0497Smrg		values = realloc(table->values, max_key * sizeof(void *));
407cdc0497Smrg		if (!values)
417cdc0497Smrg			return -ENOMEM;
427cdc0497Smrg
437cdc0497Smrg		memset(values + table->max_key, 0, (max_key - table->max_key) *
447cdc0497Smrg		       sizeof(void *));
457cdc0497Smrg
467cdc0497Smrg		table->max_key = max_key;
477cdc0497Smrg		table->values = values;
487cdc0497Smrg	}
497cdc0497Smrg	table->values[key] = value;
507cdc0497Smrg	return 0;
517cdc0497Smrg}
527cdc0497Smrg
537cdc0497Smrgdrm_private void handle_table_remove(struct handle_table *table, uint32_t key)
547cdc0497Smrg{
557cdc0497Smrg	if (key < table->max_key)
567cdc0497Smrg		table->values[key] = NULL;
577cdc0497Smrg}
587cdc0497Smrg
597cdc0497Smrgdrm_private void *handle_table_lookup(struct handle_table *table, uint32_t key)
607cdc0497Smrg{
617cdc0497Smrg	if (key < table->max_key)
627cdc0497Smrg		return table->values[key];
637cdc0497Smrg	else
647cdc0497Smrg		return NULL;
657cdc0497Smrg}
667cdc0497Smrg
677cdc0497Smrgdrm_private void handle_table_fini(struct handle_table *table)
687cdc0497Smrg{
697cdc0497Smrg	free(table->values);
707cdc0497Smrg	table->max_key = 0;
717cdc0497Smrg	table->values = NULL;
727cdc0497Smrg}
73