-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex_factory.c
186 lines (165 loc) · 5 KB
/
vertex_factory.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* $Id: vertex_factory.c,v 1.5 2001/12/19 22:32:05 acs Exp $
Written by Adam Siepel, Spring 2001
Copyright 2001, Adam Siepel */
/* "Factory" for vertices, allowing more efficient memory allocation
and deallocation. Compile with -DTHREADSAFE for concurrent access
from multiple threads. */
#include <stdlib.h>
#include "vertex_factory.h"
#include <stdio.h>
#define VFWIDTH 1024
void
clean_vf ( VertexFactory * vf, int ngenes, void ( *clear_mem ) ( void * ),
void *arg )
{
int i, j;
int length1;
vf->nalloc = 0;
vf->ngenes = ngenes;
vf->clear_mem = clear_mem;
vf->clear_mem_arg = arg;
length1 = vf->length - 1;
for ( i = 0; i < vf->width; i++ )
{
for ( j = 0; j < length1; j++ )
{
vf->vertices[i][j].perm = &( vf->perms[i][j * ngenes] );
vf->vertices[i][j].next = &( vf->vertices[i][j + 1] );
}
if ( i < vf->width - 1 )
{
vf->vertices[i][j].perm = &( vf->perms[i][j * ngenes] );
vf->vertices[i][j].next = &( vf->vertices[i + 1][0] );
}
else
{
vf->vertices[i][j].perm = &( vf->perms[i][j * ngenes] );
vf->vertices[i][j].next = NULL;
}
}
vf->head = &( vf->vertices[0][0] );
vf->tail = &( vf->vertices[vf->width - 1][length1] );
#ifdef THREADSAFE
pthread_mutex_init ( &vf->mutex, NULL );
#endif
return;
}
/* Last two arguments pertain to a function to be called when capacity
has been reached, before reallocating more memory. Pass NULL for
both if you do not wish to specify such a function */
VertexFactory *
new_vf ( int startsize, int ngenes, void ( *clear_mem ) ( void * ),
void *arg )
{
int i;
VertexFactory *vf;
vf = ( VertexFactory * ) malloc ( sizeof ( VertexFactory ) );
vf->vertices = ( Vertex ** ) malloc ( VFWIDTH * sizeof ( Vertex * ) );
vf->vertices[0] = ( Vertex * ) malloc ( startsize * sizeof ( Vertex ) );
vf->width = 1;
vf->perms = ( int ** ) malloc ( VFWIDTH * sizeof ( int * ) );
vf->perms[0] = ( int * ) malloc ( startsize * ngenes * sizeof ( int ) );
vf->nalloc = 0;
vf->ngenes = ngenes;
vf->uncondensedNumGenes = ngenes;
vf->capacity = startsize;
vf->length = startsize;
vf->clear_mem = clear_mem;
vf->clear_mem_arg = arg;
for ( i = 0; i < startsize - 1; i++ )
{
vf->vertices[0][i].perm = &( vf->perms[0][i * ngenes] );
vf->vertices[0][i].next = &( vf->vertices[0][i + 1] );
}
vf->vertices[0][i].perm = &( vf->perms[0][i * ngenes] );
vf->vertices[0][i].next = NULL; /* to avoid the if block in the
for block */
vf->tail = &( vf->vertices[0][i] );
vf->head = &( vf->vertices[0][0] );
#ifdef THREADSAFE
pthread_mutex_init ( &vf->mutex, NULL );
#endif
return vf;
}
Vertex *
get_vertex ( VertexFactory * vf )
{
int oldcap, i;
Vertex *retval;
int width1;
int length;
#ifdef THREADSAFE
pthread_mutex_lock ( &vf->mutex );
#endif
if ( vf->head == NULL )
{ /*vf->nalloc == vf->capacity) { */
length = vf->length;
width1 = vf->width - 1;
if ( vf->clear_mem != NULL )
{
#ifdef THREADSAFE
pthread_mutex_unlock ( &vf->mutex );
#endif
fprintf ( stderr, "Invoking clear_mem\n" );
vf->clear_mem ( vf->clear_mem_arg );
#ifdef THREADSAFE
pthread_mutex_lock ( &vf->mutex );
#endif
}
oldcap = vf->capacity;
vf->width++;
width1 = vf->width - 1;
vf->vertices[width1] =
( Vertex * ) malloc ( length * sizeof ( Vertex ) );
vf->perms[width1] =
( int * ) malloc ( length * vf->uncondensedNumGenes *
sizeof ( int ) );
vf->capacity += length;
for ( i = 0; i < length - 1; i++ )
{
vf->vertices[width1][i].perm =
&( vf->perms[width1][i * vf->ngenes] );
vf->vertices[width1][i].next = &( vf->vertices[width1][i + 1] );
}
vf->vertices[width1][i].perm = &( vf->perms[width1][i * vf->ngenes] );
vf->vertices[width1][i].next = NULL;
vf->tail = &( vf->vertices[width1][i] );
vf->head = &( vf->vertices[width1][0] );
}
vf->nalloc++;
retval = vf->head;
vf->head = retval->next;
#ifdef THREADSAFE
pthread_mutex_unlock ( &vf->mutex );
#endif
return retval;
}
void
return_vertex ( VertexFactory * vf, Vertex * v )
{
#ifdef THREADSAFE
pthread_mutex_lock ( &vf->mutex );
#endif
/* printf("%d\n", v->memidx); */
vf->nalloc--;
( vf->tail )->next = v;
vf->tail = v;
v->next = NULL;
#ifdef THREADSAFE
pthread_mutex_unlock ( &vf->mutex );
#endif
}
void
vf_free ( VertexFactory * vf )
{
int i;
int width;
width = vf->width;
for ( i = 0; i < width; i++ )
free ( vf->vertices[i] );
for ( i = 0; i < width; i++ )
free ( vf->perms[i] );
free ( vf->vertices );
free ( vf->perms );
free ( vf );
}