xref: /netbsd/external/gpl3/gdb/dist/sim/ppc/cap.c (revision 48596154)
166e63ce3Schristos /*  This file is part of the program psim.
266e63ce3Schristos 
366e63ce3Schristos     Copyright (C) 1994-1995,1997, Andrew Cagney <cagney@highland.com.au>
466e63ce3Schristos 
566e63ce3Schristos     This program is free software; you can redistribute it and/or modify
666e63ce3Schristos     it under the terms of the GNU General Public License as published by
7*48596154Schristos     the Free Software Foundation; either version 3 of the License, or
866e63ce3Schristos     (at your option) any later version.
966e63ce3Schristos 
1066e63ce3Schristos     This program is distributed in the hope that it will be useful,
1166e63ce3Schristos     but WITHOUT ANY WARRANTY; without even the implied warranty of
1266e63ce3Schristos     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1366e63ce3Schristos     GNU General Public License for more details.
1466e63ce3Schristos 
1566e63ce3Schristos     You should have received a copy of the GNU General Public License
16*48596154Schristos     along with this program; if not, see <http://www.gnu.org/licenses/>.
1766e63ce3Schristos 
1866e63ce3Schristos     */
1966e63ce3Schristos 
2066e63ce3Schristos 
2166e63ce3Schristos #ifndef _CAP_C_
2266e63ce3Schristos #define _CAP_C_
2366e63ce3Schristos 
2466e63ce3Schristos #include "cap.h"
2566e63ce3Schristos 
2666e63ce3Schristos typedef struct _cap_mapping cap_mapping;
2766e63ce3Schristos struct _cap_mapping {
2866e63ce3Schristos   unsigned_cell external;
2966e63ce3Schristos   void *internal;
3066e63ce3Schristos   cap_mapping *next;
3166e63ce3Schristos };
3266e63ce3Schristos 
3366e63ce3Schristos struct _cap {
3466e63ce3Schristos   int nr_mappings;
3566e63ce3Schristos   cap_mapping *mappings;
3666e63ce3Schristos };
3766e63ce3Schristos 
3866e63ce3Schristos INLINE_CAP\
3966e63ce3Schristos (cap *)
cap_create(const char * key)4066e63ce3Schristos cap_create(const char *key)
4166e63ce3Schristos {
4266e63ce3Schristos   return ZALLOC(cap);
4366e63ce3Schristos }
4466e63ce3Schristos 
4566e63ce3Schristos INLINE_CAP\
4666e63ce3Schristos (void)
cap_init(cap * db)4766e63ce3Schristos cap_init(cap *db)
4866e63ce3Schristos {
4966e63ce3Schristos   cap_mapping *current_map = db->mappings;
5066e63ce3Schristos   if (current_map != NULL) {
5166e63ce3Schristos     db->nr_mappings = db->mappings->external;
5266e63ce3Schristos     /* verify that the mappings that were not removed are in sequence
5366e63ce3Schristos        down to nr 1 */
5466e63ce3Schristos     while (current_map->next != NULL) {
5566e63ce3Schristos       if (current_map->external != current_map->next->external + 1)
5666e63ce3Schristos 	error("cap: cap database possibly corrupt");
5766e63ce3Schristos       current_map = current_map->next;
5866e63ce3Schristos     }
5966e63ce3Schristos     ASSERT(current_map->next == NULL);
6066e63ce3Schristos     if (current_map->external != 1)
6166e63ce3Schristos       error("cap: cap database possibly currupt");
6266e63ce3Schristos   }
6366e63ce3Schristos   else {
6466e63ce3Schristos     db->nr_mappings = 0;
6566e63ce3Schristos   }
6666e63ce3Schristos }
6766e63ce3Schristos 
6866e63ce3Schristos INLINE_CAP\
6966e63ce3Schristos (void *)
cap_internal(cap * db,signed_cell external)7066e63ce3Schristos cap_internal(cap *db,
7166e63ce3Schristos 	     signed_cell external)
7266e63ce3Schristos {
7366e63ce3Schristos   cap_mapping *current_map = db->mappings;
7466e63ce3Schristos   while (current_map != NULL) {
7566e63ce3Schristos     if (current_map->external == external)
7666e63ce3Schristos       return current_map->internal;
7766e63ce3Schristos     current_map = current_map->next;
7866e63ce3Schristos   }
7966e63ce3Schristos   return (void*)0;
8066e63ce3Schristos }
8166e63ce3Schristos 
8266e63ce3Schristos INLINE_CAP\
8366e63ce3Schristos (signed_cell)
cap_external(cap * db,void * internal)8466e63ce3Schristos cap_external(cap *db,
8566e63ce3Schristos 	     void *internal)
8666e63ce3Schristos {
8766e63ce3Schristos   cap_mapping *current_map = db->mappings;
8866e63ce3Schristos   while (current_map != NULL) {
8966e63ce3Schristos     if (current_map->internal == internal)
9066e63ce3Schristos       return current_map->external;
9166e63ce3Schristos     current_map = current_map->next;
9266e63ce3Schristos   }
9366e63ce3Schristos   return 0;
9466e63ce3Schristos }
9566e63ce3Schristos 
9666e63ce3Schristos INLINE_CAP\
9766e63ce3Schristos (void)
cap_add(cap * db,void * internal)9866e63ce3Schristos cap_add(cap *db,
9966e63ce3Schristos 	void *internal)
10066e63ce3Schristos {
10166e63ce3Schristos   if (cap_external(db, internal) != 0) {
10266e63ce3Schristos     error("cap: attempting to add an object already in the data base");
10366e63ce3Schristos   }
10466e63ce3Schristos   else {
10566e63ce3Schristos     /* insert at the front making things in decending order */
10666e63ce3Schristos     cap_mapping *new_map = ZALLOC(cap_mapping);
10766e63ce3Schristos     new_map->next = db->mappings;
10866e63ce3Schristos     new_map->internal = internal;
10966e63ce3Schristos     db->nr_mappings += 1;
11066e63ce3Schristos     new_map->external = db->nr_mappings;
11166e63ce3Schristos     db->mappings = new_map;
11266e63ce3Schristos   }
11366e63ce3Schristos }
11466e63ce3Schristos 
11566e63ce3Schristos INLINE_CAP\
11666e63ce3Schristos (void)
cap_remove(cap * db,void * internal)11766e63ce3Schristos cap_remove(cap *db,
11866e63ce3Schristos 	   void *internal)
11966e63ce3Schristos {
12066e63ce3Schristos   cap_mapping **current_map = &db->mappings;
12166e63ce3Schristos   while (*current_map != NULL) {
12266e63ce3Schristos     if ((*current_map)->internal == internal) {
12366e63ce3Schristos       cap_mapping *delete = *current_map;
12466e63ce3Schristos       *current_map = delete->next;
12566e63ce3Schristos       free(delete);
12666e63ce3Schristos       return;
12766e63ce3Schristos     }
12866e63ce3Schristos     current_map = &(*current_map)->next;
12966e63ce3Schristos   }
13066e63ce3Schristos   error("cap: attempt to remove nonexistant internal object");
13166e63ce3Schristos }
13266e63ce3Schristos 
13366e63ce3Schristos #endif
134