blob: 2b0fe9930b22cbd3215ad9b5603d6dc61a211ae6 [file] [log] [blame]
sewardj7df15152007-02-25 15:04:40 +00001
2/*--------------------------------------------------------------------*/
3/*--- An expandable array implementation. pub_tool_xarray.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardjb3a1e4b2015-08-21 11:32:26 +000010 Copyright (C) 2007-2015 OpenWorks LLP
sewardj7df15152007-02-25 15:04:40 +000011 info@open-works.co.uk
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_TOOL_XARRAY_H
32#define __PUB_TOOL_XARRAY_H
33
florian535fb1b2013-09-15 13:54:34 +000034#include "pub_tool_basics.h" // Word
35
sewardj7df15152007-02-25 15:04:40 +000036//--------------------------------------------------------------------
37// PURPOSE: Provides a simple but useful structure, which is an array
38// in which elements can be added at the end. The array is expanded
39// as needed by multiplying its size by a constant factor (usually 2).
40// This gives amortised O(1) insertion cost, and, following sorting,
41// the usual O(log N) binary search cost. Arbitrary element sizes
42// are allowed; the comparison function for sort/lookup can be changed
43// at any time, and duplicates (modulo the comparison function) are
44// allowed.
45//--------------------------------------------------------------------
46
47
48/* It's an abstract type. Bwaha. */
sewardj9c606bd2008-09-18 18:12:50 +000049typedef struct _XArray XArray;
sewardj7df15152007-02-25 15:04:40 +000050
florian6bd9dc12012-11-23 16:17:43 +000051typedef Int (*XACmpFn_t)(const void *, const void *);
52
sewardj7df15152007-02-25 15:04:40 +000053/* Create new XArray, using given allocation and free function, and
florian91ed8cc2014-09-15 18:50:17 +000054 for elements of the specified size. alloc_fn must not return NULL (that
55 is, if it returns it must have succeeded.)
56 This function never returns NULL. */
florian54fe2022012-10-27 23:07:42 +000057extern XArray* VG_(newXA) ( void*(*alloc_fn)(const HChar*,SizeT),
58 const HChar* cc,
sewardj7df15152007-02-25 15:04:40 +000059 void(*free_fn)(void*),
60 Word elemSzB );
61
62/* Free all memory associated with an XArray. */
63extern void VG_(deleteXA) ( XArray* );
64
65/* Set the comparison function for this XArray. This clears an
66 internal 'array is sorted' flag, which means you must call sortXA
67 before making further queries with lookupXA. */
florian6bd9dc12012-11-23 16:17:43 +000068extern void VG_(setCmpFnXA) ( XArray*, XACmpFn_t);
sewardj7df15152007-02-25 15:04:40 +000069
sewardj0f631482007-02-27 16:40:53 +000070/* Add an element to an XArray. Element is copied into the XArray.
71 Index at which it was added is returned. Note this will be
72 invalidated if the array is later sortXA'd. */
florian6bd9dc12012-11-23 16:17:43 +000073extern Word VG_(addToXA) ( XArray*, const void* elem );
sewardj7df15152007-02-25 15:04:40 +000074
sewardjb8b79ad2008-03-03 01:35:41 +000075/* Add a sequence of bytes to an XArray of bytes. Asserts if nbytes
76 is negative or the array's element size is not 1. Returns the
77 index at which the first byte was added. */
florian6bd9dc12012-11-23 16:17:43 +000078extern Word VG_(addBytesToXA) ( XArray* xao, const void* bytesV, Word nbytes );
sewardjb8b79ad2008-03-03 01:35:41 +000079
sewardj7df15152007-02-25 15:04:40 +000080/* Sort an XArray using its comparison function, if set; else bomb.
81 Probably not a stable sort w.r.t. equal elements module cmpFn. */
82extern void VG_(sortXA) ( XArray* );
83
84/* Lookup (by binary search) 'key' in the array. Set *first to be the
85 index of the first, and *last to be the index of the last matching
86 value found. If any values are found, return True, else return
sewardjffce8152011-06-24 10:09:41 +000087 False, and don't change *first or *last. first and/or last may be
88 NULL. Bomb if the array is not sorted. */
florian2fa8a5f2014-10-18 20:56:13 +000089extern Bool VG_(lookupXA) ( const XArray*, const void* key,
sewardj7df15152007-02-25 15:04:40 +000090 /*OUT*/Word* first, /*OUT*/Word* last );
91
sewardjdaa5da52009-07-24 08:42:07 +000092/* A version of VG_(lookupXA) in which you can specify your own
93 comparison function. This is unsafe in the sense that if the array
94 is not totally ordered as defined by your comparison function, then
95 this function may loop indefinitely, so it is up to you to ensure
96 that the array is suitably ordered. This is in comparison to
97 VG_(lookupXA), which refuses to do anything (asserts) unless the
98 array has first been sorted using the same comparison function as
99 is being used for the lookup. */
florian2fa8a5f2014-10-18 20:56:13 +0000100extern Bool VG_(lookupXA_UNSAFE) ( const XArray* xao, const void* key,
sewardjdaa5da52009-07-24 08:42:07 +0000101 /*OUT*/Word* first, /*OUT*/Word* last,
florian6bd9dc12012-11-23 16:17:43 +0000102 XACmpFn_t cmpFn );
sewardjdaa5da52009-07-24 08:42:07 +0000103
sewardj7df15152007-02-25 15:04:40 +0000104/* How elements are there in this XArray now? */
florian2fa8a5f2014-10-18 20:56:13 +0000105extern Word VG_(sizeXA) ( const XArray* );
sewardj7df15152007-02-25 15:04:40 +0000106
philipped4dc5fc2015-05-01 16:46:38 +0000107/* If you know how many elements an XArray will have, you can
108 optimise memory usage and number of reallocation needed
109 to insert these elements. The call to VG_(hintSizeXA) must be
110 done just after the call to VG_(newXA), before any element
111 has been inserted. */
112extern void VG_(hintSizeXA) ( XArray*, Word);
113
sewardj7df15152007-02-25 15:04:40 +0000114/* Index into the XArray. Checks bounds and bombs if the index is
115 invalid. What this returns is the address of the specified element
116 in the array, not (of course) the element itself. Note that the
sewardjc5fc8662014-03-20 23:00:09 +0000117 element may get moved by subsequent calls to addToXA / sortXA /
118 insertIndexXA, so you should copy it out immediately and not regard
119 its address as unchanging. Note also that indexXA will of course
120 not return NULL if it succeeds. */
florian2fa8a5f2014-10-18 20:56:13 +0000121extern void* VG_(indexXA) ( const XArray*, Word );
sewardj7df15152007-02-25 15:04:40 +0000122
123/* Drop the last n elements of an XArray. Bombs if there are less
sewardjdaa5da52009-07-24 08:42:07 +0000124 than n elements in the array. This is an O(1) operation. */
sewardj7df15152007-02-25 15:04:40 +0000125extern void VG_(dropTailXA) ( XArray*, Word );
126
sewardjdaa5da52009-07-24 08:42:07 +0000127/* Drop the first n elements of an XArray. Bombs if there are less
128 than n elements in the array. This is an O(N) operation, where N
129 is the number of elements remaining in the XArray. */
130extern void VG_(dropHeadXA) ( XArray*, Word );
131
sewardj291849f2012-04-20 23:58:55 +0000132/* Remove the specified element of an XArray, and slide all elements
133 beyond it back one place. This is an O(N) operation, where N is
134 the number of elements after the specified element, in the
135 array. */
136extern void VG_(removeIndexXA)( XArray*, Word );
137
sewardjc5fc8662014-03-20 23:00:09 +0000138/* Insert an element into an XArray at the given index. The existing
139 element at the index and all above it are slid upwards one slot so
140 as to make space. Element is copied into the XArray. This is an
141 O(N) operation, when N is the number of elements after the
142 specified element, in the array. */
143extern void VG_(insertIndexXA)( XArray*, Word, const void* elem );
144
sewardjb8b79ad2008-03-03 01:35:41 +0000145/* Make a new, completely independent copy of the given XArray, using
146 the existing allocation function to allocate the new space.
florian91ed8cc2014-09-15 18:50:17 +0000147 Space for the clone (and all additions to it) is billed to 'cc' unless
148 that is NULL, in which case the parent's cost-center is used.
149 Ths function never returns NULL. */
florian2fa8a5f2014-10-18 20:56:13 +0000150extern XArray* VG_(cloneXA)( const HChar* cc, const XArray* xa );
sewardj7df15152007-02-25 15:04:40 +0000151
sewardj8f8fa172010-05-05 09:23:41 +0000152/* Get the raw array and size so callers can index it really fast.
153 This is dangerous in the sense that there's no range or
154 anything-else checking. It's also dangerous in that if
155 VG_(addToXA) is used, the contents may be re-located without
156 warning, hence making the contents address returned here
157 invalid. */
158extern void VG_(getContentsXA_UNSAFE)( XArray* sr,
159 /*OUT*/void** ctsP,
160 /*OUT*/Word* usedP );
161
sewardj588adef2009-08-15 22:41:51 +0000162/* Convenience function: printf into an XArray of HChar, adding stuff
163 at the end. This is very convenient for concocting arbitrary
164 length printf output in an XArray. Note that the resulting string
bartb3af9cf2011-10-06 19:08:37 +0000165 is NOT zero-terminated. */
sewardj588adef2009-08-15 22:41:51 +0000166extern void VG_(xaprintf)( XArray* dst, const HChar* format, ... )
167 PRINTF_CHECK(2, 3);
168
sewardj7df15152007-02-25 15:04:40 +0000169#endif // __PUB_TOOL_XARRAY_H
170
171/*--------------------------------------------------------------------*/
172/*--- end pub_tool_xarray.h ---*/
173/*--------------------------------------------------------------------*/