int main(int argc, char *argv[]) { PointerArray pa = { 0 }; paSet(&pa, 0, (void *) 0x1); make_sure_that(paCount(&pa) == 1); make_sure_that(paGet(&pa, 0) == (void *) 0x1); make_sure_that(paGet(&pa, 1) == NULL); make_sure_that(paGet(&pa, 2) == NULL); paSet(&pa, 2, (void *) 0x3); make_sure_that(paCount(&pa) == 3); make_sure_that(paGet(&pa, 0) == (void *) 0x1); make_sure_that(paGet(&pa, 1) == NULL); make_sure_that(paGet(&pa, 2) == (void *) 0x3); paSet(&pa, 1, (void *) 0x2); make_sure_that(paCount(&pa) == 3); make_sure_that(paGet(&pa, 0) == (void *) 0x1); make_sure_that(paGet(&pa, 1) == (void *) 0x2); make_sure_that(paGet(&pa, 2) == (void *) 0x3); paDrop(&pa, 0); make_sure_that(paCount(&pa) == 3); make_sure_that(paGet(&pa, 0) == NULL); make_sure_that(paGet(&pa, 1) == (void *) 0x2); make_sure_that(paGet(&pa, 2) == (void *) 0x3); paDrop(&pa, 2); make_sure_that(paCount(&pa) == 2); make_sure_that(paGet(&pa, 0) == NULL); make_sure_that(paGet(&pa, 1) == (void *) 0x2); make_sure_that(paGet(&pa, 2) == NULL); paDrop(&pa, 1); make_sure_that(paCount(&pa) == 0); make_sure_that(paGet(&pa, 0) == NULL); make_sure_that(paGet(&pa, 1) == NULL); make_sure_that(paGet(&pa, 2) == NULL); paClear(&pa); }
/* * Drop the subscription on file descriptor <fd>. */ void disDropData(Dispatcher *dis, int fd) { DIS_File *file = paGet(&dis->files, fd); P dbgPrint(stderr, "Dropping fd %d\n", fd); dbgAssert(stderr, file != NULL, "unknown file descriptor: %d\n", fd); paDrop(&dis->files, fd); }
/* * Disconnect from a file descriptor that was returned earlier using nsConnect(). */ void nsDisconnect(NS *ns, int fd) { NS_Connection *conn = paGet(&ns->connections, fd); P dbgPrint(stderr, "Closing fd %d\n", fd); close(fd); disDropData(&ns->dis, fd); paDrop(&ns->connections, fd); bufReset(&conn->incoming); free(conn); }
/* * Set the entry at <index> of pointer array <pa> to <ptr>. */ void paSet(PointerArray *pa, int index, void *ptr) { if (ptr == NULL) { paDrop(pa, index); } else if (index < 0) { dbgAbort(stderr, "index must be >= 0, got %d\n", index); } else if (index >= pa->num_ptrs) { int new_num_ptrs = index + 1; pa->ptr = realloc(pa->ptr, sizeof(void *) * new_num_ptrs); memset(pa->ptr + pa->num_ptrs, 0, sizeof(void *) * (new_num_ptrs - pa->num_ptrs)); pa->num_ptrs = new_num_ptrs; } pa->ptr[index] = ptr; }
/* * Close dispatcher <dis>. This removes all file descriptors and timers, which will * cause disRun() to return. */ void disClose(Dispatcher *dis) { int fd; DIS_Timer *timer; for (fd = 0; fd < paCount(&dis->files); fd++) { DIS_File *file = paGet(&dis->files, fd); if (file != NULL) { paDrop(&dis->files, fd); bufReset(&file->outgoing); free(file); } } while ((timer = listRemoveHead(&dis->timers)) != NULL) { free(timer); } }