Skip to content

Commit a79968e

Browse files
committed
Added some documentation about drvector and the new
zero_alloc feature.
1 parent 949da73 commit a79968e

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

ext/drcontainers/drcontainers.dox

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,98 @@ flexible usage. See hashtable_init_ex() and related functions.
6161

6262
\section sec_drcontainers_vector DrVector
6363

64-
The DrVector is a simple resizable array.
64+
The \p drvector DynamoRIO Extension provides a simple resizable array (a vector).
65+
66+
\subsection sec_drvector_init Initialization
67+
68+
Initialize a vector with drvector_init():
69+
\code
70+
drvector_t vec;
71+
drvector_init(&vec, 10, true, free_callback);
72+
\endcode
73+
74+
The \p initial_capacity can be 0, in which case the internal array is lazily allocated
75+
upon the first insertion (using an internal default initial size).
76+
77+
\subsection sec_drvector_ops Operations
78+
79+
Entries can be added to the end of the vector using drvector_append():
80+
\code
81+
drvector_append(&vec, data);
82+
\endcode
83+
84+
Entries can be retrieved or set by index:
85+
\code
86+
void *data = drvector_get_entry(&vec, 5);
87+
drvector_set_entry(&vec, 3, new_data);
88+
\endcode
89+
90+
drvector_get_entry() returns \p NULL if the index is out of bounds.
91+
92+
For an unsynchronized vector, the caller is free to directly access the \p array
93+
field of the \p drvector_t structure for better performance.
94+
95+
drvector_set_entry() will automatically resize the vector if the index is beyond
96+
the current capacity. It also updates the number of entries to be \p idx + 1 if the
97+
index is greater than or equal to the current number of entries.
98+
99+
\subsection sec_drcontainers_vector_synch Synchronization
100+
101+
The vector can be initialized to automatically synchronize each operation by
102+
passing \p true for the \p synch parameter to drvector_init().
103+
104+
Even when \p synch is \p false, the vector's lock is initialized and can be
105+
manually acquired and released:
106+
\code
107+
drvector_lock(&vec);
108+
/* perform multiple operations or access vec->array directly */
109+
drvector_unlock(&vec);
110+
\endcode
111+
112+
\subsection sec_drcontainers_vector_mem Memory Management
113+
114+
A callback for freeing each data item can be provided to drvector_init().
115+
This callback is called for each entry when drvector_delete() or drvector_clear()
116+
is called.
117+
118+
The #drvector_t.zero_alloc field can be set to \p true to ensure that any new memory
119+
allocated during resizing is zeroed. When \p zero_alloc is \p true, drvector_clear()
120+
also zeros the entire array after calling the free callback.
121+
122+
To completely free the vector's storage:
123+
\code
124+
drvector_delete(&vec);
125+
\endcode
126+
127+
To clear the vector without freeing its internal storage:
128+
\code
129+
drvector_clear(&vec);
130+
\endcode
131+
132+
\subsection sec_drvector_example Example
133+
134+
\code
135+
void free_data(void *ptr) {
136+
dr_global_free(ptr, sizeof(int));
137+
}
138+
139+
drvector_t vec;
140+
drvector_init(&vec, 0, false, free_data);
141+
vec.zero_alloc = true;
142+
143+
int *data = dr_global_alloc(sizeof(int));
144+
*data = 42;
145+
drvector_append(&vec, data);
146+
147+
/* Accessing by index */
148+
int *val = (int *)drvector_get_entry(&vec, 0);
149+
150+
/* Direct access (unsynchronized) */
151+
if (vec.entries > 0)
152+
val = (int *)vec.array[0];
153+
154+
drvector_delete(&vec);
155+
\endcode
65156

66157
\section sec_drcontainers_table DrTable
67158

0 commit comments

Comments
 (0)