summaryrefslogtreecommitdiffstats
path: root/generic/tclThread.c
blob: d1f26917639641bd86fad634c5d893c319ebba95 (plain)
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
/*
 * tclThread.c --
 *
 *	This file implements Platform independent thread operations. Most of
 *	the real work is done in the platform dependent files.
 *
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * Copyright (c) 2008 by George Peter Staplin
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclInt.h"

/*
 * There are three classes of synchronization objects: mutexes, thread data
 * keys, and condition variables. The following are used to record the memory
 * used for these objects so they can be finalized.
 *
 * These statics are guarded by the mutex in the caller of
 * TclRememberThreadData, e.g., TclpThreadDataKeyInit
 */

typedef struct {
    int num;		/* Number of objects remembered */
    int max;		/* Max size of the array */
    void **list;	/* List of pointers */
} SyncObjRecord;

static SyncObjRecord keyRecord = {0, 0, NULL};
static SyncObjRecord mutexRecord = {0, 0, NULL};
static SyncObjRecord condRecord = {0, 0, NULL};

/*
 * Prototypes of functions used only in this file.
 */

static void		ForgetSyncObject(void *objPtr, SyncObjRecord *recPtr);
static void		RememberSyncObject(void *objPtr,
			    SyncObjRecord *recPtr);

/*
 * Several functions are #defined to nothing in tcl.h if TCL_THREADS is not
 * specified. Here we undo that so the functions are defined in the stubs
 * table.
 */

#ifndef TCL_THREADS
#undef Tcl_MutexLock
#undef Tcl_MutexUnlock
#undef Tcl_MutexFinalize
#undef Tcl_ConditionNotify
#undef Tcl_ConditionWait
#undef Tcl_ConditionFinalize
#endif

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetThreadData --
 *
 *	This function allocates and initializes a chunk of thread local
 *	storage.
 *
 * Results:
 *	A thread-specific pointer to the data structure.
 *
 * Side effects:
 *	Will allocate memory the first time this thread calls for this chunk
 *	of storage.
 *
 *----------------------------------------------------------------------
 */

void *
Tcl_GetThreadData(
    Tcl_ThreadDataKey *keyPtr,	/* Identifier for the data chunk */
    int size)			/* Size of storage block */
{
    void *result;
#ifdef TCL_THREADS
    /*
     * Initialize the key for this thread.
     */

    result = TclThreadStorageKeyGet(keyPtr);

    if (result == NULL) {
	result = ckalloc(size);
	memset(result, 0, (size_t) size);
	TclThreadStorageKeySet(keyPtr, result);
    }
#else /* TCL_THREADS */
    if (*keyPtr == NULL) {
	result = ckalloc(size);
	memset(result, 0, (size_t)size);
	*keyPtr = result;
	RememberSyncObject(keyPtr, &keyRecord);
    } else {
	result = *keyPtr;
    }
#endif /* TCL_THREADS */
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TclThreadDataKeyGet --
 *
 *	This function returns a pointer to a block of thread local storage.
 *
 * Results:
 *	A thread-specific pointer to the data structure, or NULL if the memory
 *	has not been assigned to this key for this thread.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void *
TclThreadDataKeyGet(
    Tcl_ThreadDataKey *keyPtr)	/* Identifier for the data chunk. */

{
#ifdef TCL_THREADS
    return TclThreadStorageKeyGet(keyPtr);
#else /* TCL_THREADS */
    return *keyPtr;
#endif /* TCL_THREADS */
}

/*
 *----------------------------------------------------------------------
 *
 * RememberSyncObject
 *
 *	Keep a list of (mutexes/condition variable/data key) used during
 *	finalization.
 *
 *	Assume master lock is held.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Add to the appropriate list.
 *
 *----------------------------------------------------------------------
 */

static void
RememberSyncObject(
    void *objPtr,		/* Pointer to sync object */
    SyncObjRecord *recPtr)	/* Record of sync objects */
{
    void **newList;
    int i, j;


    /*
     * Reuse any free slot in the list.
     */

    for (i=0 ; i < recPtr->num ; ++i) {
	if (recPtr->list[i] == NULL) {
	    recPtr->list[i] = objPtr;
	    return;
	}
    }

    /*
     * Grow the list of pointers if necessary, copying only non-NULL
     * pointers to the new list.
     */

    if (recPtr->num >= recPtr->max) {
	recPtr->max += 8;
	newList = ckalloc(recPtr->max * sizeof(void *));
	for (i=0,j=0 ; i<recPtr->num ; i++) {
	    if (recPtr->list[i] != NULL) {
		newList[j++] = recPtr->list[i];
	    }
	}
	if (recPtr->list != NULL) {
	    ckfree(recPtr->list);
	}
	recPtr->list = newList;
	recPtr->num = j;
    }

    recPtr->list[recPtr->num] = objPtr;
    recPtr->num++;
}

/*
 *----------------------------------------------------------------------
 *
 * ForgetSyncObject
 *
 *	Remove a single object from the list.
 *	Assume master lock is held.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Remove from the appropriate list.
 *
 *----------------------------------------------------------------------
 */

static void
ForgetSyncObject(
    void *objPtr,		/* Pointer to sync object */
    SyncObjRecord *recPtr)	/* Record of sync objects */
{
    int i;

    for (i=0 ; i<recPtr->num ; i++) {
	if (objPtr == recPtr->list[i]) {
	    recPtr->list[i] = NULL;
	    return;
	}
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclRememberMutex
 *
 *	Keep a list of mutexes used during finalization.
 *	Assume master lock is held.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Add to the mutex list.
 *
 *----------------------------------------------------------------------
 */

void
TclRememberMutex(
    Tcl_Mutex *mutexPtr)
{
    RememberSyncObject(mutexPtr, &mutexRecord);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_MutexFinalize --
 *
 *	Finalize a single mutex and remove it from the list of remembered
 *	objects.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Remove the mutex from the list.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_MutexFinalize(
    Tcl_Mutex *mutexPtr)
{
#ifdef TCL_THREADS
    TclpFinalizeMutex(mutexPtr);
#endif
    TclpMasterLock();
    ForgetSyncObject(mutexPtr, &mutexRecord);
    TclpMasterUnlock();
}

/*
 *----------------------------------------------------------------------
 *
 * TclRememberCondition
 *
 *	Keep a list of condition variables used during finalization.
 *	Assume master lock is held.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Add to the condition variable list.
 *
 *----------------------------------------------------------------------
 */

void
TclRememberCondition(
    Tcl_Condition *condPtr)
{
    RememberSyncObject(condPtr, &condRecord);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ConditionFinalize --
 *
 *	Finalize a single condition variable and remove it from the list of
 *	remembered objects.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Remove the condition variable from the list.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_ConditionFinalize(
    Tcl_Condition *condPtr)
{
#ifdef TCL_THREADS
    TclpFinalizeCondition(condPtr);
#endif
    TclpMasterLock();
    ForgetSyncObject(condPtr, &condRecord);
    TclpMasterUnlock();
}

/*
 *----------------------------------------------------------------------
 *
 * TclFinalizeThreadData --
 *
 *	This function cleans up the thread-local storage. This is called once
 *	for each thread.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Frees up all thread local storage.
 *
 *----------------------------------------------------------------------
 */

void
TclFinalizeThreadData(void)
{
    TclFinalizeThreadDataThread();
}

/*
 *----------------------------------------------------------------------
 *
 * TclFinalizeSynchronization --
 *
 *	This function cleans up all synchronization objects: mutexes,
 *	condition variables, and thread-local storage.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Frees up the memory.
 *
 *----------------------------------------------------------------------
 */

void
TclFinalizeSynchronization(void)
{
    int i;
    void *blockPtr;
    Tcl_ThreadDataKey *keyPtr;
#ifdef TCL_THREADS
    Tcl_Mutex *mutexPtr;
    Tcl_Condition *condPtr;

    TclpMasterLock();
#endif

    /*
     * If we're running unthreaded, the TSD blocks are simply stored inside
     * their thread data keys. Free them here.
     */

    if (keyRecord.list != NULL) {
	for (i=0 ; i<keyRecord.num ; i++) {
	    keyPtr = (Tcl_ThreadDataKey *) keyRecord.list[i];
	    blockPtr = *keyPtr;
	    ckfree(blockPtr);
	}
	ckfree(keyRecord.list);
	keyRecord.list = NULL;
    }
    keyRecord.max = 0;
    keyRecord.num = 0;

#ifdef TCL_THREADS
    /*
     * Call thread storage master cleanup.
     */

    TclFinalizeThreadStorage();

    for (i=0 ; i<mutexRecord.num ; i++) {
	mutexPtr = (Tcl_Mutex *)mutexRecord.list[i];
	if (mutexPtr != NULL) {
	    TclpFinalizeMutex(mutexPtr);
	}
    }
    if (mutexRecord.list != NULL) {
	ckfree(mutexRecord.list);
	mutexRecord.list = NULL;
    }
    mutexRecord.max = 0;
    mutexRecord.num = 0;

    for (i=0 ; i<condRecord.num ; i++) {
	condPtr = (Tcl_Condition *) condRecord.list[i];
	if (condPtr != NULL) {
	    TclpFinalizeCondition(condPtr);
	}
    }
    if (condRecord.list != NULL) {
	ckfree(condRecord.list);
	condRecord.list = NULL;
    }
    condRecord.max = 0;
    condRecord.num = 0;

    TclpMasterUnlock();
#endif /* TCL_THREADS */
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ExitThread --
 *
 *	This function is called to terminate the current thread. This should
 *	be used by extensions that create threads with additional interpreters
 *	in them.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	All thread exit handlers are invoked, then the thread dies.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_ExitThread(
    int status)
{
    Tcl_FinalizeThread();
#ifdef TCL_THREADS
    TclpThreadExit(status);
#endif
}

#ifndef TCL_THREADS

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ConditionWait, et al. --
 *
 *	These noop functions are provided so the stub table does not have to
 *	be conditionalized for threads. The real implementations of these
 *	functions live in the platform specific files.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

#undef Tcl_ConditionWait
void
Tcl_ConditionWait(
    Tcl_Condition *condPtr,	/* Really (pthread_cond_t **) */
    Tcl_Mutex *mutexPtr,	/* Really (pthread_mutex_t **) */
    const Tcl_Time *timePtr) /* Timeout on waiting period */
{
}

#undef Tcl_ConditionNotify
void
Tcl_ConditionNotify(
    Tcl_Condition *condPtr)
{
}

#undef Tcl_MutexLock
void
Tcl_MutexLock(
    Tcl_Mutex *mutexPtr)
{
}

#undef Tcl_MutexUnlock
void
Tcl_MutexUnlock(
    Tcl_Mutex *mutexPtr)
{
}
#endif /* !TCL_THREADS */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */
ef='#n2360'>2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group.                                               *
* Copyright by the Board of Trustees of the University of Illinois.         *
* All rights reserved.                                                      *
*                                                                           *
* This file is part of HDF5.  The full HDF5 copyright notice, including     *
* terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdlib.h>
#include <string.h>

#include "H5LTprivate.h"
#include "H5TBprivate.h"


/*-------------------------------------------------------------------------
*
* internal functions
*
*-------------------------------------------------------------------------
*/

static hbool_t H5TB_find_field(const char *field,
                           const char *field_list);

static herr_t H5TB_attach_attributes(const char *table_title,
                                     hid_t loc_id,
                                     const char *dset_name,
                                     hsize_t nfields,
                                     hid_t tid);

static hid_t H5TB_create_type(hid_t loc_id,
                              const char *dset_name,
                              size_t type_size,
                              const size_t *field_offset,
                              const size_t *field_sizes,
                              hid_t ftype_id);

/*-------------------------------------------------------------------------
*
* Create functions
*
*-------------------------------------------------------------------------
*/

/*-------------------------------------------------------------------------
* Function: H5TBmake_table
*
* Purpose: Make a table
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*             Quincey Koziol
*
* Date: January 17, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBmake_table(const char *table_title,
                      hid_t loc_id,
                      const char *dset_name,
                      hsize_t nfields,
                      hsize_t nrecords,
                      size_t type_size,
                      const char *field_names[],
                      const size_t *field_offset,
                      const hid_t *field_types,
                      hsize_t chunk_size,
                      void *fill_data,
                      int compress,
                      const void *buf)
{
    hid_t   did = H5I_BADID;
    hid_t   sid = H5I_BADID;
    hid_t   mem_type_id = H5I_BADID;
    hid_t   plist_id = H5I_BADID;
    hid_t   attr_id = H5I_BADID;
    hsize_t dims[1];
    hsize_t dims_chunk[1];
    hsize_t maxdims[1] = {H5S_UNLIMITED};
    char    attr_name[255];
    char    *member_name = NULL;
    unsigned char *tmp_buf = NULL;
    hsize_t i;
    herr_t  ret_val = -1;

    /* check the arguments */
    if (table_title == NULL) {
      goto out;
    }
    if (dset_name == NULL) {
      goto out;
    }
    if (field_names == NULL) {
      goto out;
    }
    
    dims[0]       = nrecords;
    dims_chunk[0] = chunk_size;

    /* create the memory data type. */
    if((mem_type_id = H5Tcreate(H5T_COMPOUND, type_size)) < 0)
        goto out;

    /* insert fields. */
    for(i = 0; i < nfields; i++)
        if(H5Tinsert(mem_type_id, field_names[i], field_offset[i], field_types[i] ) < 0)
            goto out;

    /* create a simple data space with unlimited size */
    if((sid = H5Screate_simple(1, dims, maxdims)) < 0)
        goto out;

    /* modify dataset creation properties, i.e. enable chunking  */
    if((plist_id = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        goto out;
    if(H5Pset_chunk(plist_id, 1, dims_chunk) < 0)
        goto out;

    /* set the fill value using a struct as the data type. */
    if(fill_data)
        if(H5Pset_fill_value(plist_id, mem_type_id, fill_data) < 0)
            goto out;

    /*
    dataset creation property list is modified to use
    GZIP compression with the compression effort set to 6.
    */
    if(compress)
        if(H5Pset_deflate(plist_id, 6) < 0)
            goto out;

    /* create the dataset. */
    if((did = H5Dcreate2(loc_id, dset_name, mem_type_id, sid, H5P_DEFAULT, plist_id, H5P_DEFAULT)) < 0)
        goto out;

    /* only write if there is something to write */
    if(buf)
        if(H5Dwrite(did, mem_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
            goto out;

    /* terminate access to the data space. */
    if(H5Sclose(sid) < 0)
        goto out;
    sid = H5I_BADID;

    /* end access to the dataset */
    if(H5Dclose(did) < 0)
        goto out;
    did = H5I_BADID;

    /* end access to the property list */
    if(H5Pclose(plist_id) < 0)
        goto out;
    plist_id = H5I_BADID;

    /*-------------------------------------------------------------------------
    * set the conforming table attributes
    *-------------------------------------------------------------------------
    */

    /* attach the CLASS attribute */
    if(H5LTset_attribute_string(loc_id, dset_name, "CLASS", TABLE_CLASS) < 0)
        goto out;

    /* attach the VERSION attribute */
    if(H5LTset_attribute_string(loc_id, dset_name, "VERSION", TABLE_VERSION) < 0)
        goto out;

    /* attach the TITLE attribute */
    if(H5LTset_attribute_string(loc_id, dset_name, "TITLE", table_title) < 0)
        goto out;

    /* attach the FIELD_ name attribute */
    for(i = 0; i < nfields; i++) {
        /* get the member name */
        if(NULL == (member_name = H5Tget_member_name(mem_type_id, (unsigned)i)))
            goto out;

        HDsnprintf(attr_name, sizeof(attr_name), "FIELD_%d_NAME", (int)i);

        /* attach the attribute */
        if(H5LTset_attribute_string(loc_id, dset_name, attr_name, member_name) < 0)
            goto out;

        H5free_memory(member_name);
        member_name = NULL;
    } /* end for */

    /* attach the FIELD_ fill value attribute */
    if(fill_data) {
        tmp_buf = (unsigned char *)fill_data;

        /* open the dataset. */
        if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
            goto out;

        if((sid = H5Screate(H5S_SCALAR)) < 0)
            goto out;

        for(i = 0; i < nfields; i++) {
            HDsnprintf(attr_name, sizeof(attr_name), "FIELD_%d_FILL", (int)i);

            if((attr_id = H5Acreate2(did, attr_name, field_types[i], sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
                goto out;

            if(H5Awrite(attr_id, field_types[i], tmp_buf + field_offset[i]) < 0)
                goto out;

            if(H5Aclose(attr_id) < 0)
                goto out;
            attr_id = H5I_BADID;
        } /* end for */

        /* terminate access to the data space. */
        if(H5Sclose(sid) < 0)
            goto out;
        sid = H5I_BADID;

        /* end access to the dataset */
        if(H5Dclose(did) < 0)
            goto out;
        did = H5I_BADID;
    } /* end if */

    /* release the datatype. */
    if(H5Tclose(mem_type_id) < 0)
        goto out;
    mem_type_id = H5I_BADID;

    ret_val = 0;

out:
    if(member_name)
        H5free_memory(member_name);
    if(attr_id > 0)
        if(H5Aclose(attr_id) < 0)
            ret_val = -1;
    if(plist_id > 0)
        if(H5Pclose(plist_id) < 0)
            ret_val = -1;
    if(sid > 0)
        if(H5Sclose(sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBmake_table() */

/*-------------------------------------------------------------------------
*
* Write functions
*
*-------------------------------------------------------------------------
*/

/*-------------------------------------------------------------------------
* Function: H5TBappend_records
*
* Purpose: Appends records to a table
*
* Return: Success: 0, Failure: -1
*
* Programmers:
*  Pedro Vicente, pvn@ncsa.uiuc.edu
*  Quincey Koziol
*
* Date: November 19, 2001
*
* Comments: Uses memory offsets
*
*-------------------------------------------------------------------------
*/
herr_t H5TBappend_records(hid_t loc_id,
                          const char *dset_name,
                          hsize_t nrecords,
                          size_t type_size,
                          const size_t *field_offset,
                          const size_t *field_sizes,
                          const void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hsize_t  nrecords_orig;
    hsize_t  nfields;
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* get the original number of records and fields  */
    if(H5TBget_table_info(loc_id, dset_name, &nfields, &nrecords_orig) < 0)
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatypes */
    if((tid = H5Dget_type(did)) < 0)
        goto out;

    if((mem_type_id = H5TB_create_type(loc_id, dset_name, type_size, field_offset, field_sizes, tid)) < 0)
        goto out;

    /* append the records */
    if((H5TB_common_append_records(did, mem_type_id, (size_t)nrecords, nrecords_orig, buf)) < 0)
        goto out;

    ret_val = 0;

out:
    if(tid > 0)
        if(H5Tclose(tid) < 0)
            ret_val = -1;
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBappend_records() */

/*-------------------------------------------------------------------------
* Function: H5TBwrite_records
*
* Purpose: Writes records
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 19, 2001
*
* Comments: Uses memory offsets
*
*-------------------------------------------------------------------------
*/
herr_t H5TBwrite_records(hid_t loc_id,
                         const char *dset_name,
                         hsize_t start,
                         hsize_t nrecords,
                         size_t type_size,
                         const size_t *field_offset,
                         const size_t *field_sizes,
                         const void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    sid = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hsize_t  count[1];
    hsize_t  offset[1];
    hsize_t  mem_size[1];
    hsize_t  dims[1];
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatype */
    if((tid = H5Dget_type(did)) < 0)
        goto out;

    if((mem_type_id = H5TB_create_type(loc_id, dset_name, type_size, field_offset, field_sizes, tid)) < 0)
        goto out;

    /* get the dataspace handle */
    if((sid = H5Dget_space(did)) < 0)
        goto out;

    /* get records */
    if(H5Sget_simple_extent_dims(sid, dims, NULL) < 0)
        goto out;

    if(start + nrecords > dims[0])
        goto out;

    /* define a hyperslab in the dataset of the size of the records */
    offset[0] = start;
    count[0]  = nrecords;
    if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, offset, NULL, count, NULL) < 0)
        goto out;

    /* create a memory dataspace handle */
    mem_size[0] = count[0];
    if((m_sid = H5Screate_simple(1, mem_size, NULL)) < 0)
        goto out;

    if(H5Dwrite(did, mem_type_id, m_sid, sid, H5P_DEFAULT, buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(m_sid > 0)
        if(H5Sclose(m_sid) < 0)
            ret_val = -1;
    if(sid > 0)
        if(H5Sclose(sid) < 0)
            ret_val = -1;
    if(tid > 0)
        if(H5Tclose(tid) < 0)
            ret_val = -1;
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBwrite_records() */

/*-------------------------------------------------------------------------
* Function: H5TBwrite_fields_name
*
* Purpose: Writes fields
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 21, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBwrite_fields_name(hid_t loc_id,
                             const char *dset_name,
                             const char *field_names,
                             hsize_t start,
                             hsize_t nrecords,
                             size_t type_size,
                             const size_t *field_offset,
                             const size_t *field_sizes,
                             const void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    write_type_id = H5I_BADID;
    hid_t    member_type_id = H5I_BADID;
    hid_t    nmtype_id = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hid_t    file_space_id = H5I_BADID;
    hid_t    preserve_id = H5I_BADID;
    hssize_t nfields;
    hssize_t i, j;
    hsize_t  count[1];
    hsize_t  offset[1];
    char     *member_name = NULL;
    size_t   size_native;
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;
    if (field_names == NULL) 
        goto out;

    /* create xfer properties to preserve initialized data */
    if((preserve_id = H5Pcreate(H5P_DATASET_XFER)) < 0)
        goto out;
    if(H5Pset_preserve(preserve_id, 1) < 0)
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatype */
    if((tid = H5Dget_type(did)) < 0)
        goto out;

    /* get the number of fields */
    if((nfields = H5Tget_nmembers(tid)) < 0)
        goto out;

    /* create a write id */
    if((write_type_id = H5Tcreate(H5T_COMPOUND, type_size)) < 0)
        goto out;

    j = 0;

    /* iterate though the members */
    for(i = 0; i < nfields; i++) {
        /* get the member name */
        if(NULL == (member_name = H5Tget_member_name(tid, (unsigned)i)))
            goto out;

        if(H5TB_find_field(member_name, field_names)) {
            /* get the member type */
            if((member_type_id = H5Tget_member_type(tid, (unsigned)i)) < 0)
                goto out;

            /* convert to native type */
            if((nmtype_id = H5Tget_native_type(member_type_id, H5T_DIR_DEFAULT)) < 0)
                goto out;

            if(0 == (size_native = H5Tget_size(nmtype_id)))
                goto out;

            /* adjust, if necessary */
            if(field_sizes[j] != size_native)
                if(H5Tset_size(nmtype_id, field_sizes[j]) < 0)
                    goto out;

            /* the field in the file is found by its name */
            if(field_offset) {
                if(H5Tinsert(write_type_id, member_name, field_offset[j], nmtype_id) < 0)
                    goto out;
            } /* end if */
            /* only one field */
            else {
                if(H5Tinsert(write_type_id, member_name, (size_t)0, nmtype_id) < 0)
                    goto out;
            } /* end else */

            j++;

            /* close */
            if(H5Tclose(member_type_id) < 0)
                goto out;
            member_type_id = H5I_BADID;
            if(H5Tclose(nmtype_id) < 0)
                goto out;
            nmtype_id = H5I_BADID;
        } /* end if */

        H5free_memory(member_name);
        member_name = NULL;
    } /* end for */

    /* get the dataspace handle */
    if((file_space_id = H5Dget_space(did)) < 0)
        goto out;
    if((m_sid = H5Screate_simple(1, &nrecords, NULL)) < 0)
        goto out;

    /* define a hyperslab in the dataset */
    offset[0] = start;
    count[0]  = nrecords;
    if(H5Sselect_hyperslab(file_space_id, H5S_SELECT_SET, offset, NULL, count, NULL) < 0)
        goto out;

    /* write */
    if(H5Dwrite(did, write_type_id, m_sid, file_space_id, preserve_id, buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(member_name)
        H5free_memory(member_name);
    if(preserve_id > 0)
        if(H5Pclose(preserve_id) < 0)
            ret_val = -1;
    if(write_type_id > 0)
        if(H5Tclose(write_type_id) < 0)
            ret_val = -1;
    if(tid > 0)
        if(H5Tclose(tid) < 0)
            ret_val = -1;
    if(file_space_id > 0)
        if(H5Sclose(file_space_id) < 0)
            ret_val = -1;
    if(m_sid > 0)
        if(H5Sclose(m_sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBwrite_fields_name() */

/*-------------------------------------------------------------------------
* Function: H5TBwrite_fields_index
*
* Purpose: Writes fields
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 21, 2001
*
* Comments: Uses memory offsets
*
*-------------------------------------------------------------------------
*/
herr_t H5TBwrite_fields_index(hid_t loc_id,
                              const char *dset_name,
                              hsize_t nfields,
                              const int *field_index,
                              hsize_t start,
                              hsize_t nrecords,
                              size_t type_size,
                              const size_t *field_offset,
                              const size_t *field_sizes,
                              const void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    write_type_id = H5I_BADID;
    hid_t    member_type_id = H5I_BADID;
    hid_t    nmtype_id = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hid_t    file_space_id = H5I_BADID;
    hid_t    preserve_id = H5I_BADID;
    hsize_t  count[1];
    hsize_t  offset[1];
    hsize_t  i;
    size_t   size_native;
    char    *member_name = NULL;
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* create xfer properties to preserve initialized data */
    if((preserve_id = H5Pcreate(H5P_DATASET_XFER)) < 0)
        goto out;
    if(H5Pset_preserve(preserve_id, 1) < 0)
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatype */
    if((tid = H5Dget_type(did)) < 0)
        goto out;

    /* create a write id */
    if((write_type_id = H5Tcreate(H5T_COMPOUND, type_size)) < 0)
        goto out;

    /* iterate tru the members */
    for(i = 0; i < nfields; i++) {
        unsigned  j;

        /* Range check value */
        if(field_index[i] < 0)
            goto out;

        j = (unsigned)field_index[i];

        /* get the member name */
        if(NULL == (member_name = H5Tget_member_name(tid, j)))
            goto out;

        /* get the member type */
        if((member_type_id = H5Tget_member_type(tid, j)) < 0)
            goto out;

        /* convert to native type */
        if((nmtype_id = H5Tget_native_type(member_type_id,H5T_DIR_DEFAULT)) < 0)
            goto out;

        if(0 == (size_native = H5Tget_size(nmtype_id)))
            goto out;

        if(field_sizes[i] != size_native)
            if(H5Tset_size(nmtype_id, field_sizes[i]) < 0)
                goto out;

        /* the field in the file is found by its name */
        if(field_offset) {
            if(H5Tinsert(write_type_id, member_name, field_offset[i], nmtype_id) < 0)
                goto out;
        } /* end if */
        /* only one field */
        else {
            if(H5Tinsert(write_type_id, member_name, (size_t)0, nmtype_id) < 0)
                goto out;
        } /* end else */

        /* close */
        if(H5Tclose(member_type_id) < 0)
            goto out;
        member_type_id = H5I_BADID;
        if(H5Tclose(nmtype_id) < 0)
            goto out;
        nmtype_id = H5I_BADID;

        H5free_memory(member_name);
        member_name = NULL;
    } /* end for */

    /* get the dataspace handles */
    if((file_space_id = H5Dget_space(did)) < 0)
        goto out;
    if((m_sid = H5Screate_simple(1, &nrecords, NULL)) < 0)
        goto out;

    /* define a hyperslab in the dataset */
    offset[0] = start;
    count[0]  = nrecords;
    if(H5Sselect_hyperslab(file_space_id, H5S_SELECT_SET, offset, NULL, count, NULL) < 0)
        goto out;

    /* write */
    if(H5Dwrite(did, write_type_id, m_sid, file_space_id, preserve_id, buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(member_name)
        H5free_memory(member_name);
    if(preserve_id > 0)
        if(H5Pclose(preserve_id) < 0)
            ret_val = -1;
    if(write_type_id > 0)
        if(H5Tclose(write_type_id) < 0)
            ret_val = -1;
    if(member_type_id > 0)
        if(H5Tclose(member_type_id) < 0)
            ret_val = -1;
    if(nmtype_id > 0)
        if(H5Tclose(nmtype_id) < 0)
            ret_val = -1;
    if(tid > 0)
        if(H5Tclose(tid) < 0)
            ret_val = -1;
    if(file_space_id > 0)
        if(H5Sclose(file_space_id) < 0)
            ret_val = -1;
    if(m_sid > 0)
        if(H5Sclose(m_sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBwrite_fields_index() */


/*-------------------------------------------------------------------------
*
* Read functions
*
*-------------------------------------------------------------------------
*/


/*-------------------------------------------------------------------------
* Function: H5TBread_table
*
* Purpose: Reads a table
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 20, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBread_table(hid_t loc_id,
                      const char *dset_name,
                      size_t type_size,
                      const size_t *field_offset,
                      const size_t *field_sizes,
                      void *dst_buf)
{
    hid_t    did = H5I_BADID;
    hid_t    ftype_id = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hid_t    sid = H5I_BADID;
    hsize_t  dims[1];
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the dataspace handle */
    if((sid = H5Dget_space(did)) < 0)
        goto out;

    /* get dimensions */
    if(H5Sget_simple_extent_dims(sid, dims, NULL) < 0)
        goto out;

    /* get the datatypes */
    if((ftype_id = H5Dget_type (did)) < 0)
        goto out;

    if((mem_type_id = H5TB_create_type(loc_id, dset_name, type_size, field_offset, field_sizes, ftype_id)) < 0)
        goto out;

    /* read */
    if(H5Dread(did, mem_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, dst_buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;
    if(ftype_id > 0)
        if(H5Tclose(ftype_id) < 0)
            ret_val = -1;
    if(sid > 0)
        if(H5Sclose(sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBread_table() */

/*-------------------------------------------------------------------------
* Function: H5TBread_records
*
* Purpose: Reads records
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 19, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBread_records(hid_t loc_id,
                        const char *dset_name,
                        hsize_t start,
                        hsize_t nrecords,
                        size_t type_size,
                        const size_t *field_offset,
                        const size_t *field_sizes,
                        void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    ftype_id = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hsize_t  nrecords_orig;
    hsize_t  nfields;
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* get the number of records and fields  */
    if(H5TBget_table_info(loc_id, dset_name, &nfields, &nrecords_orig) < 0)
        goto out;

    /* open the dataset */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatypes */
    if((ftype_id = H5Dget_type(did)) < 0)
        goto out;

    if((mem_type_id = H5TB_create_type(loc_id, dset_name, type_size, field_offset, field_sizes, ftype_id)) < 0)
        goto out;

    /* read the records */
    if((H5TB_common_read_records(did, mem_type_id, start, (size_t)nrecords, nrecords_orig, buf)) < 0)
        goto out;

    ret_val = 0;

out:
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;
    if(ftype_id > 0)
        if(H5Tclose(ftype_id) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBread_records() */

/*-------------------------------------------------------------------------
* Function: H5TBread_fields_name
*
* Purpose: Reads fields
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 19, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBread_fields_name(hid_t loc_id,
                            const char *dset_name,
                            const char *field_names,
                            hsize_t start,
                            hsize_t nrecords,
                            size_t type_size,
                            const size_t *field_offset,
                            const size_t *field_sizes,
                            void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    ftype_id = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hid_t    mtype_id = H5I_BADID;
    hid_t    nmtype_id = H5I_BADID;
    hid_t    sid = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hssize_t nfields;
    hsize_t  count[1];
    hsize_t  offset[1];
    hsize_t  mem_size[1];
    size_t   size_native;
    char    *member_name = NULL;
    hssize_t i, j;
    herr_t   ret_val = -1;


    /* check the arguments */
    if (dset_name == NULL) 
        goto out;
    if (field_names == NULL) 
        goto out;

    /* open the dataset */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatype */
    if((ftype_id = H5Dget_type(did)) < 0)
        goto out;

    /* get the number of fields */
    if((nfields = H5Tget_nmembers(ftype_id)) < 0)
        goto out;

    /* create a memory read id */
    if((mem_type_id = H5Tcreate(H5T_COMPOUND, type_size)) < 0)
        goto out;

    /* iterate through the members */
    for(i = 0, j = 0; i < nfields; i++) {
        /* get the member name */
        if(NULL == (member_name = H5Tget_member_name(ftype_id, (unsigned)i)))
            goto out;

        if(H5TB_find_field(member_name, field_names)) {
            /* get the member type */
            if((mtype_id = H5Tget_member_type(ftype_id, (unsigned)i)) < 0)
                goto out;

            /* convert to native type */
            if((nmtype_id = H5Tget_native_type(mtype_id, H5T_DIR_DEFAULT)) < 0)
                goto out;

            if(0 == (size_native = H5Tget_size(nmtype_id)))
                goto out;

            if(field_sizes[j] != size_native)
                if(H5Tset_size(nmtype_id, field_sizes[j]) < 0)
                    goto out;

            /* the field in the file is found by its name */
            if(field_offset) {
                if(H5Tinsert(mem_type_id, member_name, field_offset[j], nmtype_id) < 0)
                    goto out;
            } /* end if */
            else {
                if(H5Tinsert(mem_type_id, member_name, (size_t)0, nmtype_id) < 0)
                    goto out;
            } /* end else */

            /* close */
            if(H5Tclose(mtype_id) < 0)
                goto out;
            mtype_id = H5I_BADID;
            if(H5Tclose(nmtype_id) < 0)
                goto out;
            nmtype_id = H5I_BADID;
            j++;
        } /* end if */

        H5free_memory(member_name);
        member_name = NULL;
    } /* end for */

    /* check to make sure field was found, no reason to continue if it does not exist */
    if(j == 0)
      goto out;
    
    /* get the dataspace handle */
    if((sid = H5Dget_space(did)) < 0)
        goto out;

    /* define a hyperslab in the dataset */
    offset[0] = start;
    count[0]  = nrecords;
    if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, offset, NULL, count, NULL) < 0)
        goto out;

    /* create a memory dataspace handle */
    mem_size[0] = count[0];
    if((m_sid = H5Screate_simple(1, mem_size, NULL)) < 0)
        goto out;

    /* read */
    if(H5Dread(did, mem_type_id, m_sid, sid, H5P_DEFAULT, buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(member_name)
        H5free_memory(member_name);
    if(mtype_id > 0)
        if(H5Tclose(mtype_id) < 0)
            ret_val = -1;
    if(nmtype_id > 0)
        if(H5Tclose(nmtype_id) < 0)
            ret_val = -1;
    if(mem_type_id > 0)
        if(H5Tclose(mem_type_id) < 0)
            ret_val = -1;
    if(ftype_id > 0)
        if(H5Tclose(ftype_id) < 0)
            ret_val = -1;
    if(m_sid > 0)
        if(H5Sclose(m_sid) < 0)
            ret_val = -1;
    if(sid > 0)
        if(H5Sclose(sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBread_fields_name() */

/*-------------------------------------------------------------------------
* Function: H5TBread_fields_index
*
* Purpose: Reads fields
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 19, 2001
*
* Comments:
*
*-------------------------------------------------------------------------
*/
herr_t H5TBread_fields_index(hid_t loc_id,
                             const char *dset_name,
                             hsize_t nfields,
                             const int *field_index,
                             hsize_t start,
                             hsize_t nrecords,
                             size_t type_size,
                             const size_t *field_offset,
                             const size_t *field_sizes,
                             void *buf)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    read_type_id = H5I_BADID;
    hid_t    member_type_id = H5I_BADID;
    hid_t    nmtype_id = H5I_BADID;
    hid_t    sid = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hsize_t  count[1];
    hsize_t  offset[1];
    hsize_t  mem_size[1];
    hsize_t  i;
    size_t   size_native;
    char    *member_name = NULL;
    herr_t   ret_val = -1;

    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /* get the datatype */
    if((tid = H5Dget_type(did)) < 0)
        goto out;

    /* create a read id */
    if((read_type_id = H5Tcreate(H5T_COMPOUND, type_size)) < 0)
        goto out;

    /* iterate tru the members */
    for(i = 0; i < nfields; i++) {
        unsigned  j;

        /* Range check */
        if(field_index[i] < 0)
            goto out;

        j = (unsigned)field_index[i];

        /* get the member name */
        if(NULL == (member_name = H5Tget_member_name(tid, (unsigned)j)))
            goto out;

        /* get the member type */
        if((member_type_id = H5Tget_member_type(tid, (unsigned)j)) < 0)
            goto out;

        /* convert to native type */
        if((nmtype_id = H5Tget_native_type(member_type_id, H5T_DIR_DEFAULT)) < 0)
            goto out;

        if(0 == (size_native = H5Tget_size(nmtype_id)))
            goto out;

        if(field_sizes[i] != size_native)
            if(H5Tset_size(nmtype_id, field_sizes[i]) < 0)
                goto out;

        /* the field in the file is found by its name */
        if(field_offset) {
            if(H5Tinsert(read_type_id, member_name, field_offset[i], nmtype_id) < 0)
                goto out;
        } /* end if */
        else {
            if(H5Tinsert(read_type_id, member_name, (size_t)0, nmtype_id) < 0)
                goto out;
        } /* end else */

        /* close the member type */
        if(H5Tclose(member_type_id) < 0)
            goto out;
        member_type_id = H5I_BADID;
        if(H5Tclose(nmtype_id) < 0)
            goto out;
        nmtype_id = H5I_BADID;

        H5free_memory(member_name);
        member_name = NULL;
    } /* end for */

    /* get the dataspace handle */
    if((sid = H5Dget_space(did)) < 0)
        goto out;

    /* define a hyperslab in the dataset */
    offset[0] = start;
    count[0]  = nrecords;
    if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, offset, NULL, count, NULL) < 0)
        goto out;

    /* create a memory dataspace handle */
    mem_size[0] = count[0];
    if((m_sid = H5Screate_simple(1, mem_size, NULL)) < 0)
        goto out;

    /* read */
    if(H5Dread( did, read_type_id, m_sid, sid, H5P_DEFAULT, buf) < 0)
        goto out;

    ret_val = 0;

out:
    if(member_name)
        H5free_memory(member_name);
    if(read_type_id > 0)
        if(H5Tclose(read_type_id) < 0)
            ret_val = -1;
    if(member_type_id > 0)
        if(H5Tclose(member_type_id) < 0)
            ret_val = -1;
    if(nmtype_id > 0)
        if(H5Tclose(nmtype_id) < 0)
            ret_val = -1;
    if(tid > 0)
        if(H5Tclose(tid) < 0)
            ret_val = -1;
    if(m_sid > 0)
        if(H5Sclose(m_sid) < 0)
            ret_val = -1;
    if(sid > 0)
        if(H5Sclose(sid) < 0)
            ret_val = -1;
    if(did > 0)
        if(H5Dclose(did) < 0)
            ret_val = -1;

    return ret_val;
} /* end H5TBread_fields_index() */


/*-------------------------------------------------------------------------
*
* Manipulation functions
*
*-------------------------------------------------------------------------
*/

/*-------------------------------------------------------------------------
* Function: H5TBdelete_record
*
* Purpose: Delete records from middle of table ("pulling up" all the records after it)
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: November 26, 2001
*
*-------------------------------------------------------------------------
*/
herr_t H5TBdelete_record(hid_t loc_id,
                         const char *dset_name,
                         hsize_t start,
                         hsize_t nrecords)
{
    hid_t    did = H5I_BADID;
    hid_t    tid = H5I_BADID;
    hid_t    sid = H5I_BADID;
    hid_t    m_sid = H5I_BADID;
    hid_t    mem_type_id = H5I_BADID;
    hsize_t  nfields;
    hsize_t  ntotal_records;
    hsize_t  read_start;
    hsize_t  read_nrecords;
    hsize_t  count[1];
    hsize_t  offset[1];
    hsize_t  mem_size[1];
    hsize_t  dims[1];
    size_t   src_size;
    size_t  *src_offset = NULL;
    size_t  *src_sizes = NULL;
    unsigned char *tmp_buf = NULL;
    herr_t   ret_val = -1;

    
    /* check the arguments */
    if (dset_name == NULL) 
        goto out;

    /*-------------------------------------------------------------------------
    * first we get information about type size and offsets on disk
    *-------------------------------------------------------------------------
    */

    /* get the number of records and fields  */
    if(H5TBget_table_info(loc_id, dset_name, &nfields, &ntotal_records) < 0)
        goto out;

    if(NULL == (src_offset = (size_t *)HDmalloc((size_t)nfields * sizeof(size_t))))
        goto out;
    if(NULL == (src_sizes = (size_t *)HDmalloc((size_t)nfields * sizeof(size_t))))
        goto out;

    /* get field info */
    if(H5TBget_field_info(loc_id, dset_name, NULL, src_sizes, src_offset, &src_size) < 0)
        goto out;

    /* open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        goto out;

    /*-------------------------------------------------------------------------
    * read the records after the deleted one(s)
    *-------------------------------------------------------------------------
    */

    read_start = start + nrecords;
    read_nrecords = ntotal_records - read_start;

    if(read_nrecords) {
        if(NULL == (tmp_buf = (unsigned char *)HDcalloc((size_t)read_nrecords, src_size)))
            goto out;

        /* read the records after the deleted one(s) */
        if(H5TBread_records(loc_id, dset_name, read_start, read_nrecords, src_size, src_offset, src_sizes, tmp_buf) < 0)
            goto out;

        /*-------------------------------------------------------------------------
        * write the records in another position
        *-------------------------------------------------------------------------
        */

        /* get the datatype */
        if((tid = H5Dget_type(did)) < 0)
            goto out;

        /* get the dataspace handle */
        if((sid = H5Dget_space(did)) < 0)
            goto out;

	/* create the memory data type. */