Split out hw-event code. Clean up interface. Update all users.
[deliverable/binutils-gdb.git] / sim / common / hw-device.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include "sim-main.h"
23
24 #include "hw-device.h"
25 #include "hw-properties.h"
26
27 #if HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30
31 \f
32 /* Address methods */
33
34 const hw_unit *
35 hw_unit_address (struct hw *me)
36 {
37 return &me->unit_address_of_hw;
38 }
39
40
41 \f
42 /* IOCTL: */
43
44 int
45 hw_ioctl (struct hw *me,
46 hw_ioctl_request request,
47 ...)
48 {
49 int status;
50 va_list ap;
51 va_start(ap, request);
52 status = me->to_ioctl (me, request, ap);
53 va_end(ap);
54 return status;
55 }
56
57 /* Mechanism for associating allocated memory regions to a device.
58 When a device is deleted any remaining memory regions are also
59 reclaimed.
60
61 FIXME: Perhaphs this can be generalized, perhaphs it should not
62 be. */
63
64 struct hw_alloc_data {
65 void *alloc;
66 int zalloc_p;
67 struct hw_alloc_data *next;
68 };
69
70 void *
71 hw_zalloc (struct hw *me, unsigned long size)
72 {
73 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
74 memory->alloc = zalloc (size);
75 memory->zalloc_p = 1;
76 memory->next = me->alloc_of_hw;
77 me->alloc_of_hw = memory;
78 return memory->alloc;
79 }
80
81 void *
82 hw_malloc (struct hw *me, unsigned long size)
83 {
84 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
85 memory->alloc = zalloc (size);
86 memory->zalloc_p = 0;
87 memory->next = me->alloc_of_hw;
88 me->alloc_of_hw = memory;
89 return memory->alloc;
90 }
91
92 void
93 hw_free (struct hw *me,
94 void *alloc)
95 {
96 struct hw_alloc_data **memory;
97 for (memory = &me->alloc_of_hw;
98 *memory != NULL;
99 memory = &(*memory)->next)
100 {
101 if ((*memory)->alloc == alloc)
102 {
103 struct hw_alloc_data *die = (*memory);
104 (*memory) = die->next;
105 if (die->zalloc_p)
106 zfree (die->alloc);
107 else
108 free (die->alloc);
109 zfree (die);
110 return;
111 }
112 }
113 hw_abort (me, "free of memory not belonging to a device");
114 }
115
116 void
117 hw_free_all (struct hw *me)
118 {
119 while (me->alloc_of_hw != NULL)
120 {
121 hw_free (me, me->alloc_of_hw->alloc);
122 }
123 }
124
125 char *
126 hw_strdup (struct hw *me, const char *str)
127 {
128 if (str != NULL)
129 {
130 char *dup = hw_zalloc (me, strlen (str) + 1);
131 strcpy (dup, str);
132 return dup;
133 }
134 else
135 {
136 return NULL;
137 }
138 }
This page took 0.033476 seconds and 4 git commands to generate.