/
array.h
123 lines (102 loc) · 3.76 KB
/
array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* array.h -- array utilities */
/**
* \file array.h
*
* Utilities for operating on arrays.
*/
#ifndef UTILS_ARRAY_H
#define UTILS_ARRAY_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
/* ----------------------------------------------------------------------- */
/**
* Signifies an array of unknown length, e.g. when used as the size of an
* array which is the final member of a struct.
*/
#define UNKNOWN 1
/* ----------------------------------------------------------------------- */
/* Delete the specified element. */
void array_delete_element(void *array,
size_t elemsize,
int nelems,
int doomed);
/* Delete the specified elements. */
/* last_doomed is inclusive. */
void array_delete_elements(void *array,
size_t elemsize,
int nelems,
int first_doomed,
int last_doomed);
/* ----------------------------------------------------------------------- */
/* Take the contents of an array which used to have elements 'oldwidth' bytes
* wide and adjust them so they are 'newwidth' bytes wide. Set new bytes to
* 'wipe_value'. */
void array_stretch1(unsigned char *base,
int nelems,
size_t oldwidth,
size_t newwidth,
int wipe_value);
void array_stretch2(unsigned char *base,
int nelems,
size_t oldwidth,
size_t newwidth,
int wipe_value);
/* Take the contents of an array which used to have elements 'oldwidth' bytes
* wide and adjust them so they are 'newwidth' bytes wide. */
void array_squeeze1(unsigned char *base,
int nelems,
size_t oldwidth,
size_t newwidth);
void array_squeeze2(unsigned char *base,
int nelems,
size_t oldwidth,
size_t newwidth);
/* Temporary defines until the above functions are renamed. */
#define array_stretch array_stretch2
#define array_squeeze array_squeeze2
/* ----------------------------------------------------------------------- */
/**
* Grow a dynamically allocated array as required.
*
* Presently the growth strategy is doubling.
*
* 'block' can be NULL to perform an initial alloc.
* Start with used == allocated == 0.
*
* \param block Pointer to pointer to block. Updated on success.
* \param elemsize Element size in bytes.
* \param used Number of currently occupied elements.
* \param allocated Pointer to number of allocated elements. Updated on exit.
* \param need Number of unoccupied elements we need.
* \param minimum Minimum number of elements to allocate.
*
* \return 0 - ok, 1 - out of memory
*/
int array_grow(void **block,
size_t elemsize,
int used,
int *allocated,
int need,
int minimum);
/**
* Shrink a dynamically allocated array to have no free entries.
*
* \param block Pointer to pointer to block. Updated on exit.
* \param elemsize Element size.
* \param used Number of currently occupied elements.
* \param allocated Pointer to number of allocated elements. Updated on exit.
*
* \return 0 - ok, 1 - out of memory
*/
int array_shrink(void **block,
size_t elemsize,
int used,
int *allocated);
/* ----------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
#endif /* UTILS_ARRAY_H */