Annotation of libaitio/src/sarray.c, revision 1.2
1.2 ! 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 + 1;
! 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:
! 95: /*
! 96: * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
! 97: * @arr = Array
! 98: * @newNumItems = Number of Items
! 99: * return: -1 error, 0 ok
! 100: */
! 101: int
! 102: io_sarrGrow(sarr_t * __restrict arr, int newNumItems)
! 103: {
! 104: sarr_seg_t *data;
! 105: int seg, n = 0;
! 106: register int i;
! 107:
! 108: assert(arr);
! 109: if (!arr)
! 110: return -1;
! 111:
! 112: arr->sarr_num = newNumItems;
! 113: seg = newNumItems / arr->sarr_seg + 1;
! 114: if (arr->sarr_siz == seg)
! 115: return n;
! 116: if (arr->sarr_siz < seg)
! 117: n = seg - arr->sarr_siz;
! 118: else
! 119: for (i = seg; i < arr->sarr_siz; i++)
! 120: if (arr->sarr_data[i])
! 121: free(arr->sarr_data[i]);
! 122:
! 123: arr->sarr_siz = seg;
! 124: data = realloc(arr->sarr_data, arr->sarr_siz * sizeof(sarr_seg_t));
! 125: if (!data) {
! 126: LOGERR;
! 127: return -1;
! 128: } else
! 129: arr->sarr_data = data;
! 130: memset(arr->sarr_data + (arr->sarr_siz - n), 0, n * sizeof(sarr_seg_t));
! 131:
! 132: return 0;
! 133: }
! 134:
! 135: /*
! 136: * io_sarrGet() - Get element from dynamic split-order array
! 137: * @arr = Array
! 138: * @idx = Index (warning 1st element is at position 1)
! 139: * return: NULL not found, !=NULL element
! 140: */
! 141: inline void *
! 142: io_sarrGet(sarr_t * __restrict arr, u_int idx)
! 143: {
! 144: void *ret = NULL;
! 145: sarr_seg_t seg;
! 146:
! 147: assert(arr);
! 148: if (!arr || idx < 1 || arr->sarr_num < idx)
! 149: return ret;
! 150:
! 151: seg = arr->sarr_data[idx / arr->sarr_seg];
! 152: if (seg)
! 153: ret = seg[idx % arr->sarr_seg];
! 154:
! 155: return ret;
! 156: }
! 157:
! 158: /*
! 159: * io_sarrGet2() - Always get element from dynamic split-order array
! 160: * Function automatic grow array. Good use for Hash tables!
! 161: * @arr = Array
! 162: * @idx = Index (warning 1st element is at position 1)
! 163: * return: NULL not found, !=NULL element
! 164: */
! 165: void *
! 166: io_sarrGet2(sarr_t * __restrict arr, u_int idx)
! 167: {
! 168: assert(arr);
! 169: if (!arr || idx < 1)
! 170: return NULL;
! 171: if (arr->sarr_num < idx)
! 172: if (io_sarrGrow(arr, idx))
! 173: return NULL;
! 174: return io_sarrGet(arr, idx);
! 175: }
! 176:
! 177: /*
! 178: * io_sarrSet() - Set element to dynamic split-order array
! 179: * @arr = Array
! 180: * @idx = Index (warning 1st element is at position 1)
! 181: * @data = Value
! 182: * return: NULL error or empty, !=NULL old value in element
! 183: */
! 184: inline void *
! 185: io_sarrSet(sarr_t * __restrict arr, u_int idx, void *data)
! 186: {
! 187: void *ret = NULL;
! 188: sarr_seg_t seg;
! 189: register int pos;
! 190:
! 191: assert(arr);
! 192: if (!arr || idx < 1 || arr->sarr_num < idx)
! 193: return ret;
! 194:
! 195: seg = arr->sarr_data[idx / arr->sarr_seg];
! 196: if (!seg) {
! 197: seg = calloc(arr->sarr_seg, sizeof(void*));
! 198: if (!seg) {
! 199: LOGERR;
! 200: return ret;
! 201: } else
! 202: memset(seg, 0, arr->sarr_seg * sizeof(void*));
! 203: arr->sarr_data[idx / arr->sarr_seg] = seg;
! 204: }
! 205:
! 206: pos = idx % arr->sarr_seg;
! 207: ret = seg[pos];
! 208: seg[pos] = data;
! 209:
! 210: return ret;
! 211: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>