]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/hw-alloc.c
sim: clean up C11 header includes
[thirdparty/binutils-gdb.git] / sim / common / hw-alloc.c
1 /* Hardware memory allocator.
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "hw-main.h"
22 #include "hw-base.h"
23
24 #include <stdlib.h>
25
26 struct hw_alloc_data
27 {
28 void *alloc;
29 struct hw_alloc_data *next;
30 };
31
32 void
33 create_hw_alloc_data (struct hw *me)
34 {
35 /* NULL */
36 }
37
38 void
39 delete_hw_alloc_data (struct hw *me)
40 {
41 while (me->alloc_of_hw != NULL)
42 {
43 hw_free (me, me->alloc_of_hw->alloc);
44 }
45 }
46
47
48
49 void *
50 hw_zalloc (struct hw *me, unsigned long size)
51 {
52 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
53 memory->alloc = zalloc (size);
54 memory->next = me->alloc_of_hw;
55 me->alloc_of_hw = memory;
56 return memory->alloc;
57 }
58
59 void *
60 hw_malloc (struct hw *me, unsigned long size)
61 {
62 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
63 memory->alloc = zalloc (size);
64 memory->next = me->alloc_of_hw;
65 me->alloc_of_hw = memory;
66 return memory->alloc;
67 }
68
69 void
70 hw_free (struct hw *me,
71 void *alloc)
72 {
73 struct hw_alloc_data **memory;
74 for (memory = &me->alloc_of_hw;
75 *memory != NULL;
76 memory = &(*memory)->next)
77 {
78 if ((*memory)->alloc == alloc)
79 {
80 struct hw_alloc_data *die = (*memory);
81 (*memory) = die->next;
82 free (die->alloc);
83 free (die);
84 return;
85 }
86 }
87 hw_abort (me, "free of memory not belonging to a device");
88 }