Initial creation of sourceware repository
[deliverable/binutils-gdb.git] / sim / common / hw-alloc.c
CommitLineData
c906108c
SS
1/* Hardware memory allocator.
2 Copyright (C) 1998 Free Software Foundation, Inc.
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
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
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
17You should have received a copy of the GNU General Public License along
18with this program; if not, write to the Free Software Foundation, Inc.,
1959 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21
22#include "hw-main.h"
23#include "hw-base.h"
24
25
26#ifdef HAVE_STDLIB_H
27#include <stdlib.h>
28#endif
29
30struct hw_alloc_data {
31 void *alloc;
32 int zalloc_p;
33 struct hw_alloc_data *next;
34};
35
36void
37create_hw_alloc_data (struct hw *me)
38{
39 /* NULL */
40}
41
42void
43delete_hw_alloc_data (struct hw *me)
44{
45 if (me->alloc_of_hw != NULL)
46 hw_abort (me, "hw-alloc botch");
47 while (me->alloc_of_hw != NULL)
48 {
49 hw_free (me, me->alloc_of_hw->alloc);
50 }
51}
52
53
54
55void *
56hw_zalloc (struct hw *me, unsigned long size)
57{
58 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
59 memory->alloc = zalloc (size);
60 memory->zalloc_p = 1;
61 memory->next = me->alloc_of_hw;
62 me->alloc_of_hw = memory;
63 return memory->alloc;
64}
65
66void *
67hw_malloc (struct hw *me, unsigned long size)
68{
69 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
70 memory->alloc = zalloc (size);
71 memory->zalloc_p = 0;
72 memory->next = me->alloc_of_hw;
73 me->alloc_of_hw = memory;
74 return memory->alloc;
75}
76
77void
78hw_free (struct hw *me,
79 void *alloc)
80{
81 struct hw_alloc_data **memory;
82 for (memory = &me->alloc_of_hw;
83 *memory != NULL;
84 memory = &(*memory)->next)
85 {
86 if ((*memory)->alloc == alloc)
87 {
88 struct hw_alloc_data *die = (*memory);
89 (*memory) = die->next;
90 if (die->zalloc_p)
91 zfree (die->alloc);
92 else
93 free (die->alloc);
94 zfree (die);
95 return;
96 }
97 }
98 hw_abort (me, "free of memory not belonging to a device");
99}
This page took 0.0261 seconds and 4 git commands to generate.