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