static int dg_close (PS ps) { struct ps_dg *pt = (struct ps_dg *) ps -> ps_addr; if (pt == NULL) return OK; if (pt -> ps_input.pio_qb) qb_free (pt -> ps_input.pio_qb); if (pt -> ps_output.pio_qb) qb_free (pt -> ps_output.pio_qb); set_check_fd (pt -> ps_fd, NULLIFP, NULLCP); free ((char *) pt); return OK; }
int uart_deinit() { if (close(uartfd) != 0) { generateError("Could not close /dev/ttyAMA0"); return 0; } qb_free(&queuebuffer); return 1; }
static int dg_prime (PS ps, int waiting) { struct qbuf *qb; struct ps_dg *pt = (struct ps_dg *) ps -> ps_addr; struct ps_inout *pi = &pt -> ps_input; switch (waiting) { case 0: if (pi -> pio_cnt > 0) return OK; break; case 1: default: return (pi -> pio_cnt > 0 ? DONE : OK); case -1: if (pi -> pio_cnt <= 0) return OK; break; } if (pi -> pio_qb != NULL) { qb_free (pi -> pio_qb); pi -> pio_qb = NULL; } pi -> pio_cnt = 0; if (waiting < 0) return ps_seterr (ps, PS_ERR_EXTRA, NOTOK); if ((*pi -> pio_fnx) (pt -> ps_fd, &qb) == NOTOK) return ps_seterr (ps, PS_ERR_IO, NOTOK); if (pi -> pio_qb = qb) pi -> pio_ptr = qb -> qb_data, pi -> pio_cnt = qb -> qb_len; else pi -> pio_ptr = NULL, pi -> pio_cnt = 0; return OK; }
static void free_info(struct PgqTableInfo *info) { struct PgqTriggerInfo *tg, *tmp = info->tg_cache; int i; for (tg = info->tg_cache; tg; ) { tmp = tg->next; if (tg->ignore_list) pfree((void *)tg->ignore_list); if (tg->pkey_list) pfree((void *)tg->pkey_list); for (i = 0; i < EV_NFIELDS; i++) { if (tg->query[i]) qb_free(tg->query[i]); } pfree(tg); tg = tmp; } pfree(info->table_name); pfree(info->pkey_attno); pfree((void *)info->pkey_list); }
static void make_query(struct PgqTriggerEvent *ev, int fld, const char *arg) { struct TriggerData *tg = ev->tgdata; struct PgqTriggerInfo *tgargs; struct QueryBuilder *q; Oid tgoid = tg->tg_trigger->tgoid; const char *pfx = "select "; if (ev->op_type == 'R') elog(ERROR, "Custom expressions do not make sense for truncater trigger"); /* make sure tgargs exists */ if (!ev->tgargs) ev->tgargs = find_trigger_info(ev->info, tgoid, true); tgargs = ev->tgargs; if (tgargs->query[fld]) { /* seems we already have prepared query */ if (tgargs->query[fld]->plan) return; /* query is broken, last prepare failed? */ qb_free(tgargs->query[fld]); tgargs->query[fld] = NULL; } /* allocate query in right context */ q = qb_create(&tg_ops, tbl_cache_ctx); /* attach immediately */ tgargs->query[fld] = q; /* prepare the query */ qb_add_raw(q, pfx, strlen(pfx)); qb_add_parse(q, arg, tg); qb_prepare(q, tg); }
static void clean_info(struct PgqTableInfo *info, bool found) { struct PgqTriggerInfo *tg, *tmp = info->tg_cache; int i; if (!found) goto uninitialized; for (tg = info->tg_cache; tg; ) { tmp = tg->next; if (tg->ignore_list) pfree((void *)tg->ignore_list); if (tg->pkey_list) pfree((void *)tg->pkey_list); for (i = 0; i < EV_NFIELDS; i++) { if (tg->query[i]) qb_free(tg->query[i]); } pfree(tg); tg = tmp; } if (info->table_name) pfree(info->table_name); if (info->pkey_attno) pfree(info->pkey_attno); if (info->pkey_list) pfree((void *)info->pkey_list); uninitialized: info->tg_cache = NULL; info->table_name = NULL; info->pkey_attno = NULL; info->pkey_list = NULL; info->n_pkeys = 0; info->invalid = true; }
int main(int argc, char **argv) { QueueBuffer *qb; char buffer[256]; char bigbuffer[165500]; int i, bytes; memset(bigbuffer, '.', sizeof(bigbuffer)); /* Test 1 Push large amount of data */ printf("\n == Test 1 == \n\n"); qb_initialize(&qb); qb_push(qb, bigbuffer, sizeof(bigbuffer)); for (i = 0; i < 4096; i += 16) qb_push(qb, "Here r 16 char. ", 16); while ((bytes = qb_pop(qb, buffer, 32)) > 0) { buffer[bytes] = '\0'; printf("%08d bytes remaining after pop : \"%s\"\n", qb_getSize(qb), buffer); } qb_free(&qb); /* Test 2 Pop data off while pushing more data on This is more similar to what it will be like in production */ printf("\n == Test 2 == \n\n"); qb_initialize(&qb); qb_push(qb, bigbuffer, 15000); while ((bytes = qb_pop(qb, buffer, 256)) > 128) { qb_push(qb, bigbuffer, 128); } qb_free(&qb); /* Test 3 Free an unused QueueBuffer */ printf("\n == Test 3 == \n\n"); qb_initialize(&qb); qb_free(&qb); /* Test 4 Free the QueueBuffer before popping all data off (checking for possible memory leaks) */ printf("\n == Test 4 == \n\n"); qb_initialize(&qb); qb_push(qb, bigbuffer, sizeof(bigbuffer)); qb_pop(qb, buffer, 64); qb_free(&qb); // Check that qb is NULL after qb_free() if (qb == NULL) printf("qb is correctly now a NULL pointer\n"); else printf("qb is NOT a NULL pointer as it should be\n"); printf("\nDone!\n\n"); return 0; }
void s_samplesort(int fd, off_t size,char *base_name, int t){ struct queue_buf *buff; struct queue_buf **file_buff; int *files, *sizes, *keys; int i,j,cur,r,ret,ret1; char name_file[500]; int k; k = t * min(ceildiv(size,M),ceildiv(M,B)); r = 0; buff = qb_new(M); files = (int *)malloc(sizeof(int)*k); sizes = (int *)malloc(sizeof(int)*k); keys = (int *)malloc(sizeof(int)*(k+1)); file_buff = (struct queue_buf **)malloc(sizeof(struct queue_buf *)*k); select_keys(keys,fd,size,k); for(i = 0; i < k; i++){ file_buff[i] = qb_new(B); sizes[i] = 0; sprintf(name_file, "%s_%d\0", base_name,i); if((files[i] = open(name_file, O_RDWR | O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO))== -1){ printf("%s_%d fail1\n", base_name,i); perror("aca"); exit(1); } results.io_rand++; } j=1; while(1){ if((ret1 = qb_refill(buff,fd))== -1){ printf("%s_%d fail2\n", base_name,i); perror("aca"); exit(1); } results.io_rand++; results.io_acc+=ceildiv(buff->n_elems,B); if (qb_empty(buff)) break; while(!qb_empty(buff)){ cur = qb_dequeue(buff); i = bucket(cur,keys,k+1); qb_enqueue(file_buff[i], cur); if(qb_full(file_buff[i])){ results.io_rand++; results.io_acc+=ceildiv(file_buff[i]->n_elems,B); sizes[i] += file_buff[i]->n_elems; if((ret = qb_flush(file_buff[i],files[i]))== -1){ printf("%s_%d fail3\n", base_name,i); perror("aca"); exit(1); } } } } close(fd); remove(curr_name); /* Pueden quedar restos en los bufs. Hay que vaciar todo */ for (i = 0; i < k; i++) { if (!qb_empty(file_buff[i])) { results.io_rand++; results.io_acc+=ceildiv(file_buff[i]->n_elems,B); sizes[i] += file_buff[i]->n_elems; qb_flush(file_buff[i], files[i]); } close(files[i]); qb_free(file_buff[i]); } free(file_buff); free(keys); for(i = 0; i < k; i++){ sprintf(name_file, "%s_%d\0", base_name,i); if(sizes[i] == 0){ remove(name_file); } else if(sizes[i] <= M){ if((files[i] = open(name_file, O_RDWR | O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO))== -1){ printf("%s_%d fail1\n", base_name,i); perror("aca"); exit(1); } if((ret1 = qb_refill(buff,files[i]))== -1){ printf("%s_%d fail2\n", base_name,i); perror("aca"); exit(1); } results.io_rand++; results.io_acc+=ceildiv(buff->n_elems,B); quicksort(buff); lseek64(files[i],0,SEEK_SET); results.io_rand++; qb_flush(buff,files[i]); results.io_rand++; results.io_acc+=ceildiv(buff->n_elems,B); close(files[i]); remove(name_file); } } qb_free(buff); for (i = 0; i < k; i++) { sprintf(name_file, "%s_%d\0", base_name,i); if (sizes[i] > M) { if((files[i] = open(name_file, O_RDWR | O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO))== -1){ printf("%s_%d fail1\n",base_name, i); perror("aca"); exit(1); } results.io_rand++; curr_name= name_file; s_samplesort(files[i],sizes[i],name_file,t); } } free(sizes); free(files); }