gmp-utils: New API to simply use of GMP's integer/rational/float objects
[binutils-gdb.git] / sim / common / hw-alloc.c
blob718dbba8a178c8a14b71bb72446a8d0ede5d236c
1 /* Hardware memory allocator.
2 Copyright (C) 1998-2020 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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.
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.
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/>. */
21 #include "hw-main.h"
22 #include "hw-base.h"
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
29 struct hw_alloc_data
31 void *alloc;
32 struct hw_alloc_data *next;
35 void
36 create_hw_alloc_data (struct hw *me)
38 /* NULL */
41 void
42 delete_hw_alloc_data (struct hw *me)
44 while (me->alloc_of_hw != NULL)
46 hw_free (me, me->alloc_of_hw->alloc);
52 void *
53 hw_zalloc (struct hw *me, unsigned long size)
55 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
56 memory->alloc = zalloc (size);
57 memory->next = me->alloc_of_hw;
58 me->alloc_of_hw = memory;
59 return memory->alloc;
62 void *
63 hw_malloc (struct hw *me, unsigned long size)
65 struct hw_alloc_data *memory = ZALLOC (struct hw_alloc_data);
66 memory->alloc = zalloc (size);
67 memory->next = me->alloc_of_hw;
68 me->alloc_of_hw = memory;
69 return memory->alloc;
72 void
73 hw_free (struct hw *me,
74 void *alloc)
76 struct hw_alloc_data **memory;
77 for (memory = &me->alloc_of_hw;
78 *memory != NULL;
79 memory = &(*memory)->next)
81 if ((*memory)->alloc == alloc)
83 struct hw_alloc_data *die = (*memory);
84 (*memory) = die->next;
85 free (die->alloc);
86 free (die);
87 return;
90 hw_abort (me, "free of memory not belonging to a device");