OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [bcache.h] - Diff between revs 105 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 105 Rev 1765
/* Include file cached obstack implementation.
/* Include file cached obstack implementation.
   Written by Fred Fish <fnf@cygnus.com>
   Written by Fred Fish <fnf@cygnus.com>
   Rewritten by Jim Blandy <jimb@cygnus.com>
   Rewritten by Jim Blandy <jimb@cygnus.com>
   Copyright 1999 Free Software Foundation, Inc.
   Copyright 1999 Free Software Foundation, Inc.
 
 
   This file is part of GDB.
   This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */
   Boston, MA 02111-1307, USA.  */
 
 
#ifndef BCACHE_H
#ifndef BCACHE_H
#define BCACHE_H 1
#define BCACHE_H 1
 
 
/* A bcache is a data structure for factoring out duplication in
/* A bcache is a data structure for factoring out duplication in
   read-only structures.  You give the bcache some string of bytes S.
   read-only structures.  You give the bcache some string of bytes S.
   If the bcache already contains a copy of S, it hands you back a
   If the bcache already contains a copy of S, it hands you back a
   pointer to its copy.  Otherwise, it makes a fresh copy of S, and
   pointer to its copy.  Otherwise, it makes a fresh copy of S, and
   hands you back a pointer to that.  In either case, you can throw
   hands you back a pointer to that.  In either case, you can throw
   away your copy of S, and use the bcache's.
   away your copy of S, and use the bcache's.
 
 
   The "strings" in question are arbitrary strings of bytes --- they
   The "strings" in question are arbitrary strings of bytes --- they
   can contain zero bytes.  You pass in the length explicitly when you
   can contain zero bytes.  You pass in the length explicitly when you
   call the bcache function.
   call the bcache function.
 
 
   This means that you can put ordinary C objects in a bcache.
   This means that you can put ordinary C objects in a bcache.
   However, if you do this, remember that structs can contain `holes'
   However, if you do this, remember that structs can contain `holes'
   between members, added for alignment.  These bytes usually contain
   between members, added for alignment.  These bytes usually contain
   garbage.  If you try to bcache two objects which are identical from
   garbage.  If you try to bcache two objects which are identical from
   your code's point of view, but have different garbage values in the
   your code's point of view, but have different garbage values in the
   structure's holes, then the bcache will treat them as separate
   structure's holes, then the bcache will treat them as separate
   strings, and you won't get the nice elimination of duplicates you
   strings, and you won't get the nice elimination of duplicates you
   were hoping for.  So, remember to memset your structures full of
   were hoping for.  So, remember to memset your structures full of
   zeros before bcaching them!
   zeros before bcaching them!
 
 
   You shouldn't modify the strings you get from a bcache, because:
   You shouldn't modify the strings you get from a bcache, because:
 
 
   - You don't necessarily know who you're sharing space with.  If I
   - You don't necessarily know who you're sharing space with.  If I
     stick eight bytes of text in a bcache, and then stick an
     stick eight bytes of text in a bcache, and then stick an
     eight-byte structure in the same bcache, there's no guarantee
     eight-byte structure in the same bcache, there's no guarantee
     those two objects don't actually comprise the same sequence of
     those two objects don't actually comprise the same sequence of
     bytes.  If they happen to, the bcache will use a single byte
     bytes.  If they happen to, the bcache will use a single byte
     string for both of them.  Then, modifying the structure will
     string for both of them.  Then, modifying the structure will
     change the string.  In bizarre ways.
     change the string.  In bizarre ways.
 
 
   - Even if you know for some other reason that all that's okay,
   - Even if you know for some other reason that all that's okay,
     there's another problem.  A bcache stores all its strings in a
     there's another problem.  A bcache stores all its strings in a
     hash table.  If you modify a string's contents, you will probably
     hash table.  If you modify a string's contents, you will probably
     change its hash value.  This means that the modified string is
     change its hash value.  This means that the modified string is
     now in the wrong place in the hash table, and future bcache
     now in the wrong place in the hash table, and future bcache
     probes will never find it.  So by mutating a string, you give up
     probes will never find it.  So by mutating a string, you give up
     any chance of sharing its space with future duplicates.  */
     any chance of sharing its space with future duplicates.  */
 
 
 
 
/* The type used to hold a single bcache string.  The user data is
/* The type used to hold a single bcache string.  The user data is
   stored in d.data.  Since it can be any type, it needs to have the
   stored in d.data.  Since it can be any type, it needs to have the
   same alignment as the most strict alignment of any type on the host
   same alignment as the most strict alignment of any type on the host
   machine.  I don't know of any really correct way to do this in
   machine.  I don't know of any really correct way to do this in
   stock ANSI C, so just do it the same way obstack.h does.
   stock ANSI C, so just do it the same way obstack.h does.
 
 
   It would be nicer to have this stuff hidden away in bcache.c, but
   It would be nicer to have this stuff hidden away in bcache.c, but
   struct objstack contains a struct bcache directly --- not a pointer
   struct objstack contains a struct bcache directly --- not a pointer
   to one --- and then the memory-mapped stuff makes this a real pain.
   to one --- and then the memory-mapped stuff makes this a real pain.
   We don't strictly need to expose struct bstring, but it's better to
   We don't strictly need to expose struct bstring, but it's better to
   have it all in one place.  */
   have it all in one place.  */
 
 
struct bstring {
struct bstring {
  struct bstring *next;
  struct bstring *next;
  size_t length;
  size_t length;
 
 
  union
  union
  {
  {
    char data[1];
    char data[1];
    double dummy;
    double dummy;
  }
  }
  d;
  d;
};
};
 
 
 
 
/* The structure for a bcache itself.
/* The structure for a bcache itself.
   To initialize a bcache, just fill it with zeros.  */
   To initialize a bcache, just fill it with zeros.  */
struct bcache {
struct bcache {
  /* All the bstrings are allocated here.  */
  /* All the bstrings are allocated here.  */
  struct obstack cache;
  struct obstack cache;
 
 
  /* How many hash buckets we're using.  */
  /* How many hash buckets we're using.  */
  unsigned int num_buckets;
  unsigned int num_buckets;
 
 
  /* Hash buckets.  This table is allocated using malloc, so when we
  /* Hash buckets.  This table is allocated using malloc, so when we
     grow the table we can return the old table to the system.  */
     grow the table we can return the old table to the system.  */
  struct bstring **bucket;
  struct bstring **bucket;
 
 
  /* Statistics.  */
  /* Statistics.  */
  unsigned long unique_count;   /* number of unique strings */
  unsigned long unique_count;   /* number of unique strings */
  long total_count;     /* total number of strings cached, including dups */
  long total_count;     /* total number of strings cached, including dups */
  long unique_size;     /* size of unique strings, in bytes */
  long unique_size;     /* size of unique strings, in bytes */
  long total_size;      /* total number of bytes cached, including dups */
  long total_size;      /* total number of bytes cached, including dups */
  long structure_size;  /* total size of bcache, including infrastructure */
  long structure_size;  /* total size of bcache, including infrastructure */
};
};
 
 
 
 
/* Find a copy of the LENGTH bytes at ADDR in BCACHE.  If BCACHE has
/* Find a copy of the LENGTH bytes at ADDR in BCACHE.  If BCACHE has
   never seen those bytes before, add a copy of them to BCACHE.  In
   never seen those bytes before, add a copy of them to BCACHE.  In
   either case, return a pointer to BCACHE's copy of that string.  */
   either case, return a pointer to BCACHE's copy of that string.  */
extern void *bcache (void *addr, int length, struct bcache *bcache);
extern void *bcache (void *addr, int length, struct bcache *bcache);
 
 
/* Free all the storage that BCACHE refers to.  The result is a valid,
/* Free all the storage that BCACHE refers to.  The result is a valid,
   but empty, bcache.  This does not free BCACHE itself, since that
   but empty, bcache.  This does not free BCACHE itself, since that
   might be part of some larger object.  */
   might be part of some larger object.  */
extern void free_bcache (struct bcache *bcache);
extern void free_bcache (struct bcache *bcache);
 
 
/* Print statistics on BCACHE's memory usage and efficacity at
/* Print statistics on BCACHE's memory usage and efficacity at
   eliminating duplication.  TYPE should be a string describing the
   eliminating duplication.  TYPE should be a string describing the
   kind of data BCACHE holds.  Statistics are printed using
   kind of data BCACHE holds.  Statistics are printed using
   `printf_filtered' and its ilk.  */
   `printf_filtered' and its ilk.  */
extern void print_bcache_statistics (struct bcache *bcache, char *type);
extern void print_bcache_statistics (struct bcache *bcache, char *type);
 
 
#endif /* BCACHE_H */
#endif /* BCACHE_H */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.