OSDN Git Service

freedreno: add handle and name tracking
[android-x86/external-libdrm.git] / freedreno / freedreno_device.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35
36 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37 static void * dev_table;
38
39 static struct fd_device * fd_device_new_impl(int fd)
40 {
41         struct fd_device *dev = calloc(1, sizeof(*dev));
42         if (!dev)
43                 return NULL;
44         atomic_set(&dev->refcnt, 1);
45         dev->fd = fd;
46         dev->handle_table = drmHashCreate();
47         dev->name_table = drmHashCreate();
48         return dev;
49 }
50
51 /* use inode for key into dev_table, rather than fd, to avoid getting
52  * confused by multiple-opens:
53  */
54 static int devkey(int fd)
55 {
56         struct stat s;
57         if (fstat(fd, &s)) {
58                 ERROR_MSG("stat failed: %s", strerror(errno));
59                 return -1;
60         }
61         return s.st_ino;
62 }
63
64 struct fd_device * fd_device_new(int fd)
65 {
66         struct fd_device *dev = NULL;
67         int key = devkey(fd);
68
69         pthread_mutex_lock(&table_lock);
70
71         if (!dev_table)
72                 dev_table = drmHashCreate();
73
74         if (drmHashLookup(dev_table, key, (void **)&dev)) {
75                 dev = fd_device_new_impl(fd);
76                 drmHashInsert(dev_table, key, dev);
77         } else {
78                 dev = fd_device_ref(dev);
79         }
80
81         pthread_mutex_unlock(&table_lock);
82
83         return dev;
84 }
85
86 struct fd_device * fd_device_ref(struct fd_device *dev)
87 {
88         atomic_inc(&dev->refcnt);
89         return dev;
90 }
91
92 void fd_device_del(struct fd_device *dev)
93 {
94         if (!atomic_dec_and_test(&dev->refcnt))
95                 return;
96         pthread_mutex_lock(&table_lock);
97         drmHashDestroy(dev->handle_table);
98         drmHashDestroy(dev->name_table);
99         drmHashDelete(dev_table, devkey(dev->fd));
100         pthread_mutex_unlock(&table_lock);
101         free(dev);
102 }