xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/DbeArray.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1*cb63e24eSchristos /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
24f645668Schristos    Contributed by Oracle.
34f645668Schristos 
44f645668Schristos    This file is part of GNU Binutils.
54f645668Schristos 
64f645668Schristos    This program is free software; you can redistribute it and/or modify
74f645668Schristos    it under the terms of the GNU General Public License as published by
84f645668Schristos    the Free Software Foundation; either version 3, or (at your option)
94f645668Schristos    any later version.
104f645668Schristos 
114f645668Schristos    This program is distributed in the hope that it will be useful,
124f645668Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
134f645668Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144f645668Schristos    GNU General Public License for more details.
154f645668Schristos 
164f645668Schristos    You should have received a copy of the GNU General Public License
174f645668Schristos    along with this program; if not, write to the Free Software
184f645668Schristos    Foundation, 51 Franklin Street - Fifth Floor, Boston,
194f645668Schristos    MA 02110-1301, USA.  */
204f645668Schristos 
214f645668Schristos #ifndef _DbeArray_H
224f645668Schristos #define _DbeArray_H
234f645668Schristos 
244f645668Schristos template <typename ITEM> class DbeArray
254f645668Schristos {
264f645668Schristos public:
274f645668Schristos 
DbeArray(long sz)284f645668Schristos   DbeArray (long sz)
294f645668Schristos   {
304f645668Schristos     count = 0;
314f645668Schristos     limit = sz;
324f645668Schristos     data = new ITEM[limit];
334f645668Schristos   };
344f645668Schristos 
354f645668Schristos   virtual
~DbeArray()364f645668Schristos   ~DbeArray ()
374f645668Schristos   {
384f645668Schristos     delete[] data;
394f645668Schristos   }
404f645668Schristos 
414f645668Schristos   int
append(const ITEM & item)424f645668Schristos   append (const ITEM &item)
434f645668Schristos   {
444f645668Schristos     int n = allocate (1);
454f645668Schristos     ITEM *p = get (n);
464f645668Schristos     *p = item;
474f645668Schristos     return n;
484f645668Schristos   };
494f645668Schristos 
504f645668Schristos   ITEM *
get(long index)514f645668Schristos   get (long index)
524f645668Schristos   {
534f645668Schristos     return (index < count && index >= 0) ? data + index : (ITEM *) NULL;
544f645668Schristos   };
554f645668Schristos 
564f645668Schristos   int
allocate(int cnt)574f645668Schristos   allocate (int cnt)
584f645668Schristos   {
594f645668Schristos     count += cnt;
604f645668Schristos     resize (count);
614f645668Schristos     return count - cnt;
624f645668Schristos   };
634f645668Schristos 
644f645668Schristos   int
size()654f645668Schristos   size ()
664f645668Schristos   {
674f645668Schristos     return (int) count;
684f645668Schristos   };
694f645668Schristos 
704f645668Schristos   void
reset()714f645668Schristos   reset ()
724f645668Schristos   {
734f645668Schristos     count = 0;
744f645668Schristos   };
754f645668Schristos 
764f645668Schristos private:
774f645668Schristos 
784f645668Schristos   void
resize(long cnt)794f645668Schristos   resize (long cnt)
804f645668Schristos   {
814f645668Schristos     if (limit <= cnt)
824f645668Schristos       {
834f645668Schristos 	limit *= 2;
844f645668Schristos 	if (limit < cnt)
854f645668Schristos 	  limit = cnt + 1;
864f645668Schristos 	ITEM *d = new ITEM[limit];
874f645668Schristos 	if (count > 0)
884f645668Schristos 	  memcpy (d, data, sizeof (ITEM) * count);
894f645668Schristos 	delete[] data;
904f645668Schristos 	data = d;
914f645668Schristos       }
924f645668Schristos   };
934f645668Schristos 
944f645668Schristos   ITEM *data;   // Pointer to data vector
954f645668Schristos   long count;   // Number of items
964f645668Schristos   long limit;   // Array length
974f645668Schristos };
984f645668Schristos 
994f645668Schristos #endif /* _DbeArray_H */
100