Annotation of libaitio/src/sarray.c, revision 1.1.2.1
1.1.2.1 ! misho 1: #include "global.h"
! 2:
! 3:
! 4: /*
! 5: * io_sarrInit() - Create and initialize dynamic split-order array
! 6: * @numItems = Number of Items
! 7: * @segLen = Length of segment
! 8: * return: NULL error, != NULL allocated memory for array
! 9: */
! 10: inline sarr_t *
! 11: io_sarrInit(int numItems, int segLen)
! 12: {
! 13: sarr_t *arr = NULL;
! 14:
! 15: if (segLen < 1)
! 16: return NULL;
! 17:
! 18: arr = malloc(sizeof(sarr_t));
! 19: if (!arr) {
! 20: LOGERR;
! 21: return NULL;
! 22: }
! 23:
! 24: arr->sarr_num = numItems;
! 25: arr->sarr_seg = segLen;
! 26: arr->sarr_siz = numItems / segLen;
! 27: arr->sarr_data = calloc(arr->sarr_siz, sizeof(sarr_seg_t));
! 28: if (!arr->sarr_data) {
! 29: LOGERR;
! 30: free(arr);
! 31: return NULL;
! 32: } else
! 33: memset(arr->sarr_data, 0, arr->sarr_siz * sizeof(sarr_seg_t));
! 34:
! 35: return arr;
! 36: }
! 37:
! 38: /*
! 39: * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
! 40: * @parr = Array
! 41: * return: none
! 42: */
! 43: inline void
! 44: io_sarrDestroy(sarr_t ** __restrict parr)
! 45: {
! 46: register int i;
! 47:
! 48: assert(parr);
! 49: if (!parr)
! 50: return;
! 51:
! 52: for (i = 0; i < (*parr)->sarr_siz; i++)
! 53: if ((*parr)->sarr_data[i]) {
! 54: free((*parr)->sarr_data[i]);
! 55: (*parr)->sarr_data[i] = NULL;
! 56: }
! 57:
! 58: if ((*parr)->sarr_data)
! 59: free((*parr)->sarr_data);
! 60: free(*parr);
! 61: *parr = NULL;
! 62: }
! 63:
! 64: /*
! 65: * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
! 66: * @arr = Array
! 67: * return: -1 error, >-1 freed segments
! 68: */
! 69: int
! 70: io_sarrVacuum(sarr_t * __restrict arr)
! 71: {
! 72: register int i, j;
! 73: int cx = 0;
! 74: sarr_seg_t seg;
! 75:
! 76: assert(arr);
! 77: if (!arr)
! 78: return -1;
! 79:
! 80: for (i = 0; i < arr->sarr_siz; i++)
! 81: if (arr->sarr_data[i]) {
! 82: for (j = 0, seg = arr->sarr_data[i]; j < arr->sarr_seg; j++)
! 83: if (seg[j])
! 84: break;
! 85: if (j == arr->sarr_seg) {
! 86: free(arr->sarr_data[i]);
! 87: arr->sarr_data[i] = NULL;
! 88: cx++;
! 89: }
! 90: }
! 91:
! 92: return cx;
! 93: }
! 94:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>