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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1996 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include "pkglib.h"
/*
* This is the module responsible for allocating and maintaining lists that
* require allocation of memory. For certain lists, large chunks are
* allocated once to contain a large number of entries in each chunk (bl_*
* for block list). The other approach involves the augmentation of linked
* lists, each entry of which is alloc'd individually.
*/
#define ERR_CS_ALLOC "ERROR: Cannot allocate control structure for %s array."
#define ERR_MEM_ALLOC "ERROR: Cannot allocate memory for %s array."
#define MAX_ARRAYS 50
#define ARRAY_END(x) (bl_cs_array[x]->cur_segment->avail_ptr)
#define REC_SIZE(x) (bl_cs_array[x]->struct_size)
#define EOSEG(x) (bl_cs_array[x]->cur_segment->eoseg_ptr)
#define GET_AVAIL(x) (ARRAY_END(x) + REC_SIZE(x))
struct alloc_seg {
char *seg_ptr; /* ptr to the allocated block */
char *avail_ptr; /* ptr to the next available list element */
char *eoseg_ptr; /* last byte in the segment */
int full; /* segment has no available space */
struct alloc_seg *next; /* next record */
};
struct blk_list_cs {
int struct_size; /* size of a single list element */
int count_per_block; /* number of list elements per block */
int block_size; /* just to save time - alloc size */
int data_handle; /* list_handle for pointer array */
struct alloc_seg *alloc_segs; /* memory pool */
struct alloc_seg *cur_segment; /* the current allocated segment */
int total_elem; /* total elements stored */
int contiguous; /* use realloc to grow */
char *desc; /* description of the list */
};
static struct blk_list_cs *bl_cs_array[MAX_ARRAYS];
static int next_array_elem;
/* Support functions */
static int
invalid_handle(int list_handle)
{
if (list_handle < 0 || list_handle >= next_array_elem)
return (1);
return (0);
}
static int
invalid_record(int list_handle, int recno)
{
if (invalid_handle(list_handle))
return (1);
if (recno < 0 || recno > bl_cs_array[list_handle]->total_elem)
return (1);
return (0);
}
static void
free_list(int list_handle)
{
struct blk_list_cs *bl_ptr;
struct alloc_seg *segstr_ptr, *nextstr_ptr;
/* Make sure this wasn't free'd earlier */
if (bl_cs_array[list_handle] == NULL)
return;
bl_ptr = bl_cs_array[list_handle];
/* First free the alloc_seg list. */
segstr_ptr = bl_ptr->alloc_segs;
if (segstr_ptr) {
do {
nextstr_ptr = segstr_ptr->next;
/* Free the memory block. */
free((void *)segstr_ptr->seg_ptr);
/* Free the control structure. */
free((void *)segstr_ptr);
segstr_ptr = nextstr_ptr;
} while (segstr_ptr);
}
/* Free the block control structure. */
free((void *)bl_ptr->desc);
free((void *)bl_ptr);
bl_cs_array[list_handle] = NULL;
}
/* Allocate another alloc_seg structure. */
static int
alloc_next_seg(struct blk_list_cs *bl_ptr)
{
struct alloc_seg *new_alloc_cs;
if (bl_ptr->contiguous) {
int offset_to_avail, seg_size, new_size;
struct alloc_seg *alloc_segment;
if (bl_ptr->alloc_segs) {
alloc_segment = bl_ptr->alloc_segs;
offset_to_avail = (alloc_segment->avail_ptr -
alloc_segment->seg_ptr);
seg_size = (alloc_segment->eoseg_ptr -
alloc_segment->seg_ptr);
new_size = (seg_size + bl_ptr->block_size);
} else {
if ((bl_ptr->alloc_segs =
(struct alloc_seg *)calloc(1,
sizeof (struct alloc_seg))) == NULL) {
logerr(gettext(ERR_CS_ALLOC), (bl_ptr->desc ?
bl_ptr->desc : "an unknown"));
return (0);
}
alloc_segment = bl_ptr->alloc_segs;
offset_to_avail = 0;
seg_size = 0;
new_size = bl_ptr->block_size;
}
bl_ptr->cur_segment = alloc_segment;
if ((alloc_segment->seg_ptr =
(char *)realloc((void *)alloc_segment->seg_ptr,
(unsigned)new_size)) == NULL) {
logerr(gettext(ERR_MEM_ALLOC), (bl_ptr->desc ?
bl_ptr->desc : "an unknown"));
return (0);
}
alloc_segment->next = NULL;
/* reset the status */
alloc_segment->full = 0;
/* readjust the original pointers */
alloc_segment->avail_ptr = alloc_segment->seg_ptr +
offset_to_avail;
alloc_segment->eoseg_ptr = alloc_segment->seg_ptr + new_size;
(void) memset(alloc_segment->avail_ptr, '\000',
bl_ptr->block_size);
} else {
/* Allocate the control structure and link it into the list. */
if ((new_alloc_cs = (struct alloc_seg *)malloc(
sizeof (struct alloc_seg))) == NULL) {
logerr(gettext(ERR_CS_ALLOC), (bl_ptr->desc ?
bl_ptr->desc : "an unknown"));
return (0);
}
if (bl_ptr->alloc_segs == NULL) {
/*
* If this is the first allocation, then initialize
* the head pointer and set cur_segment to this first
* block of memory.
*/
bl_ptr->alloc_segs = new_alloc_cs;
} else {
/*
* Otherwise, point the current cur_segment to the
* next one and then point to the new one.
*/
bl_ptr->cur_segment->next = new_alloc_cs;
}
new_alloc_cs->next = NULL;
bl_ptr->cur_segment = new_alloc_cs;
new_alloc_cs->full = 0;
/* Now allocate the block of memory that this controls. */
if ((new_alloc_cs->seg_ptr = calloc(bl_ptr->count_per_block,
bl_ptr->struct_size)) == NULL) {
logerr(gettext(ERR_MEM_ALLOC), (bl_ptr->desc ?
bl_ptr->desc : "an unknown"));
return (0);
}
new_alloc_cs->avail_ptr = new_alloc_cs->seg_ptr;
new_alloc_cs->eoseg_ptr = (new_alloc_cs->seg_ptr +
bl_ptr->block_size);
}
return (1);
}
/*
* These first functions (beginning with bl_*) manage simple block lists. The
* pointers returned, may get lost if they aren't assigned to an array or
* something. While individual records can be obtained by record number, the
* process isn't very efficient. Look to the array management section
* (ar_*)for an easily administrable list.
*/
/*
* Create a block list. Allocate memory for a block list structure and
* initialize that structure. This doesn't actually allocate memory for the
* list yet, just the controlling data structure. Returns -1 on failure and a
* valid block list handle otherwise.
*
* NOTE: At the time of writing, it was not seen as important to recover block
* pointers made available with a bl_free() (two of these at most in
* pkginstall). If this became important later, we could trade efficiency for
* speed by ignoring next_array_elem and actually scanning through the array
* for a NULL pointer and then return that.
*/
int
bl_create(int count_per_block, int struct_size, char *desc)
{
struct blk_list_cs *bl_ptr;
int retval;
if ((bl_cs_array[next_array_elem] =
(struct blk_list_cs *)calloc(1, sizeof (struct blk_list_cs))) ==
NULL) {
logerr(gettext(ERR_CS_ALLOC), (desc ? desc : "an unknown"));
return (-1);
}
bl_ptr = bl_cs_array[next_array_elem];
retval = next_array_elem++;
bl_ptr->data_handle = -1;
bl_ptr->struct_size = struct_size;
bl_ptr->count_per_block = count_per_block;
bl_ptr->block_size = (count_per_block * struct_size);
bl_ptr->desc = strdup((desc ? desc : "unknown"));
return (retval);
}
/*
* Get the next available entry in the list. This will allocate memory as
* required based on the initialization values in bl_create(). Returns a
* pointer to the allocated memory segment or NULL if operation was not
* possible.
*/
char *
bl_next_avail(int list_handle)
{
struct blk_list_cs *bl_ptr;
char *retval;
if (invalid_handle(list_handle))
return (NULL);
bl_ptr = bl_cs_array[list_handle];
/*
* Allocate more memory if none is allocated yet or our last access
* filled the allotted segment.
*/
if (bl_ptr->cur_segment == NULL || bl_ptr->cur_segment->full)
if (!alloc_next_seg(bl_ptr))
return (NULL);
/* Get the correct pointer. */
retval = bl_ptr->cur_segment->avail_ptr;
/* Advance it and mark if full. */
bl_ptr->cur_segment->avail_ptr += bl_ptr->struct_size;
bl_ptr->total_elem++;
if (bl_ptr->cur_segment->avail_ptr >= bl_ptr->cur_segment->eoseg_ptr)
bl_ptr->cur_segment->full = 1;
return (retval);
}
char *
bl_get_record(int list_handle, int recno)
{
struct blk_list_cs *bl_ptr;
struct alloc_seg *cur_as_ptr;
int cur_rec = 0;
if (invalid_record(list_handle, recno))
return (NULL);
bl_ptr = bl_cs_array[list_handle];
cur_as_ptr = bl_ptr->alloc_segs;
while (recno > (cur_rec + bl_ptr->count_per_block)) {
cur_as_ptr = cur_as_ptr->next;
if (cur_as_ptr == NULL)
return (NULL);
cur_rec += bl_ptr->count_per_block;
}
/*
* Now cur_as_ptr points to the allocated segment bearing the
* intended record and all we do now is move down that by the
* remaining record lengths.
*/
return ((char *)cur_as_ptr + ((recno - cur_rec) * bl_ptr->struct_size));
}
void
bl_free(int list_handle)
{
int cur_handle;
if (list_handle == -1) {
for (cur_handle = 0; cur_handle < next_array_elem;
cur_handle++) {
free_list(cur_handle);
}
} else {
if (invalid_handle(list_handle))
return;
free_list(list_handle);
}
}
/*
* These are the array management functions. They insert into (and can return
* a pointer to) a contiguous list of pointers to stuff. This keeps
* everything together in a very handy package and is very similar in
* appearance to the arrays created by the old AT&T code. The method for
* presenting the interface is entirely different, however.
*/
/*
* This constructs, maintains and returns pointers into a growable array of
* pointers to structures of the form
* struct something *array[n]
* The last element in the array is always NULL.
*/
int
ar_create(int count_per_block, int struct_size, char *desc)
{
int data_handle, retval;
char ar_desc[60];
struct blk_list_cs *array_ptr;
if ((data_handle = bl_create(count_per_block, struct_size, desc)) == -1)
return (-1);
sprintf(ar_desc, "%s pointer", desc);
if ((retval = bl_create(count_per_block, sizeof (char *),
ar_desc)) == -1)
return (-1);
array_ptr = bl_cs_array[retval];
array_ptr->contiguous = 1;
array_ptr->data_handle = data_handle;
return (retval);
}
/* Return a pointer to the first element in the array. */
char **
ar_get_head(int list_handle)
{
if (invalid_handle(list_handle) ||
bl_cs_array[list_handle]->alloc_segs == NULL)
return (NULL);
return ((char **)bl_cs_array[list_handle]->alloc_segs->seg_ptr);
}
/*
* Free up the entry in the array indicated by index, but hold onto it for
* future use.
*/
int
ar_delete(int list_handle, int index)
{
char **array;
char *deleted_rec;
int i;
struct blk_list_cs *list_ptr, *data_ptr;
if ((array = ar_get_head(list_handle)) == NULL)
return (0);
if (invalid_record(list_handle, index))
return (0);
/* Get the pointer to the array control structure. */
list_ptr = bl_cs_array[list_handle];
if (!(list_ptr->contiguous))
return (0); /* This isn't an array. */
data_ptr = bl_cs_array[list_ptr->data_handle];
/*
* Since this looks just like an array. Record the pointer being
* deleted for insertion into the avail list at the end and move all
* elements below it up one.
*/
deleted_rec = array[index];
for (i = index; array[i] != NULL; i++)
array[i] = array[i+1];
/*
* Now insert the deleted entry into the avails list after the NULL
* and adjust the avail_ptr to point to the NULL again.
*/
array[i] = deleted_rec;
list_ptr->alloc_segs->avail_ptr -= list_ptr->struct_size;
/* Adjust other entries in the control structure. */
list_ptr->alloc_segs->full = 0;
list_ptr->total_elem -= 1;
/* Clear the deleted data area. */
(void) memset(deleted_rec, '\000', data_ptr->struct_size);
return (1);
}
/*
* Return a new pointer to a structure pointer. Find an available element in
* the array and point it at an available element in the data pool
* constructed of block lists. Allocate new memory as necessary.
*/
char **
ar_next_avail(int list_handle)
{
struct blk_list_cs *array_ptr;
char *data_area, **pointer_area;
if (invalid_handle(list_handle) ||
!(bl_cs_array[list_handle]->contiguous) ||
invalid_handle(bl_cs_array[list_handle]->data_handle))
return (NULL);
array_ptr = bl_cs_array[list_handle];
/*
* First see if an avail has already been allocated (it will be right
* after the NULL termination of the array if it exists). Return
* that, if found.
*/
if ((bl_cs_array[list_handle]->cur_segment != NULL) &&
(ARRAY_END(list_handle) + REC_SIZE(list_handle) <
EOSEG(list_handle)) &&
(*(pointer_area = (char **) GET_AVAIL(list_handle)) != NULL)) {
/* We can reclaim a previous deletion. */
data_area = *pointer_area;
*(char **)(ARRAY_END(list_handle)) = data_area; /* reactivate */
*pointer_area-- = NULL; /* new end */
array_ptr->cur_segment->avail_ptr += array_ptr->struct_size;
array_ptr->total_elem++;
} else {
/*
* Get the data area first. This is the record we're pointing
* to from the array.
*/
data_area = bl_next_avail(array_ptr->data_handle);
/* Now get the next pointer from the pointer array. */
pointer_area = (char **) bl_next_avail(list_handle);
*pointer_area = data_area;
/*
* The array must be NULL terminated. So, if the block list
* structure is full, we have to grow it without resetting
* the avail pointer. NOTE: This will only work for a
* contiguous list!
*/
if (bl_cs_array[list_handle]->alloc_segs->full) {
char **old_list_pointer, **new_list_pointer;
/*
* First grab the old numbers in case realloc() moves
* everything.
*/
old_list_pointer = ar_get_head(list_handle);
/*
* Now allocate additional contiguous memory, moving
* the original block if necessary.
*/
if (!alloc_next_seg(array_ptr))
return (NULL);
/*
* Now determine if everything moved and readjust the
* pointer_area if required.
*/
new_list_pointer = ar_get_head(list_handle);
if (old_list_pointer != new_list_pointer) {
pointer_area += (new_list_pointer -
old_list_pointer);
}
}
}
return (pointer_area);
}
/*
* Relinquish the array back to the memory pool. Note that there is no method
* provided to free *all* arrays.
*/
void
ar_free(int list_handle)
{
if (invalid_handle(list_handle))
return;
bl_free(bl_cs_array[list_handle]->data_handle);
bl_free(list_handle);
}
|