1/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "intel_resolve_map.h"
25
26#include <assert.h>
27#include <stdlib.h>
28
29/**
30 * \brief Set that the miptree slice at (level, layer) needs a resolve.
31 *
32 * If a map element already exists with the given key, then the value is
33 * changed to the given value of \c need.
34 */
35void
36intel_resolve_map_set(struct intel_resolve_map *head,
37		      uint32_t level,
38		      uint32_t layer,
39		      enum gen6_hiz_op need)
40{
41   struct intel_resolve_map **tail = &head->next;
42   struct intel_resolve_map *prev = head;
43
44   while (*tail) {
45      if ((*tail)->level == level && (*tail)->layer == layer) {
46         (*tail)->need = need;
47	 return;
48      }
49      prev = *tail;
50      tail = &(*tail)->next;
51   }
52
53   *tail = malloc(sizeof(**tail));
54   (*tail)->prev = prev;
55   (*tail)->next = NULL;
56   (*tail)->level = level;
57   (*tail)->layer = layer;
58   (*tail)->need = need;
59}
60
61/**
62 * \brief Get an element from the map.
63 * \return null if element is not contained in map.
64 */
65struct intel_resolve_map*
66intel_resolve_map_get(struct intel_resolve_map *head,
67		      uint32_t level,
68		      uint32_t layer)
69{
70   struct intel_resolve_map *item = head->next;
71
72   while (item) {
73      if (item->level == level && item->layer == layer)
74	 break;
75      else
76	 item = item->next;
77   }
78
79   return item;
80}
81
82/**
83 * \brief Remove and free an element from the map.
84 */
85void
86intel_resolve_map_remove(struct intel_resolve_map *elem)
87{
88   if (elem->prev)
89      elem->prev->next = elem->next;
90   if (elem->next)
91      elem->next->prev = elem->prev;
92   free(elem);
93}
94
95/**
96 * \brief Remove and free all elements of the map.
97 */
98void
99intel_resolve_map_clear(struct intel_resolve_map *head)
100{
101   struct intel_resolve_map *next = head->next;
102   struct intel_resolve_map *trash;
103
104   while (next) {
105      trash = next;
106      next = next->next;
107      free(trash);
108   }
109
110   head->next = NULL;
111}
112