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
124
125
126
127
128
129
130
|
package t_array;
use strict;
use vars qw(@ISA);
#require ktemplate;
require t_template;
@ISA=qw(t_template);
my @parms = qw(NAME TYPE);
my %defaults = ( );
my @templatelines = <DATA>;
sub new { # no args
my $self = {};
bless $self;
$self->init(\@parms, \%defaults, \@templatelines);
return $self;
}
__DATA__
/*
* array type, derived from template
*
* parameters:
* NAME: <NAME>
* TYPE: <TYPE>
*
* methods:
* int init() -> nonzero if fail initial allocation
* unsigned long size() -> nonnegative number of values stored
* int grow(newsize) -> negative if fail allocation, memset(,0,) new space
* <TYPE> *getaddr(idx) -> aborts if out of range
* void set(idx, value) -> aborts if out of range
* <TYPE> get(idx) -> value, or aborts if out of range
*/
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <stdint.h>
struct <NAME>__header {
size_t allocated;
<TYPE> *elts;
};
typedef struct <NAME>__header <NAME>;
static inline int
<NAME>_init(<NAME> *arr)
{
arr->elts = calloc(10, sizeof(<TYPE>));
if (arr->elts == NULL)
return ENOMEM;
arr->allocated = 10;
return 0;
}
static inline long
<NAME>_size(<NAME> *arr)
{
return arr->allocated;
}
static inline long
<NAME>_max_size(<NAME> *arr)
{
size_t upper_bound;
upper_bound = SIZE_MAX / sizeof(*arr->elts);
if (upper_bound > LONG_MAX)
upper_bound = LONG_MAX;
return (long) upper_bound;
}
static inline int
<NAME>_grow(<NAME> *arr, unsigned long newcount)
{
size_t oldsize = sizeof(*arr->elts) * arr->allocated;
size_t newsize;
void *ptr;
if (newcount > LONG_MAX)
return -1;
if (newcount < arr->allocated)
return 0;
if (newcount > <NAME>_max_size(arr))
return -1;
newsize = sizeof(*arr->elts) * newcount;
ptr = realloc(arr->elts, newsize);
if (ptr == NULL)
return -1;
memset((char *)ptr + oldsize, 0, newsize - oldsize);
arr->elts = ptr;
arr->allocated = newcount;
return 0;
}
static inline <TYPE> *
<NAME>_getaddr (<NAME> *arr, long idx)
{
if (idx < 0 || idx >= arr->allocated)
abort();
return arr->elts + idx;
}
static inline void
<NAME>_set (<NAME> *arr, long idx, <TYPE> value)
{
<TYPE> *newvalp;
newvalp = <NAME>_getaddr(arr, idx);
*newvalp = value;
}
static inline <TYPE>
<NAME>_get (<NAME> *arr, long idx)
{
return *<NAME>_getaddr(arr, idx);
}
static inline void
<NAME>_destroy (<NAME> *arr)
{
free(arr->elts);
arr->elts = 0;
}
|